update vendor

This commit is contained in:
Qiu Jian
2018-12-03 13:17:14 +08:00
parent 94fcb7a400
commit 9ca04590c3
9 changed files with 291 additions and 141 deletions
Generated
+6 -8
View File
@@ -1310,11 +1310,11 @@
[[projects]]
branch = "master"
digest = "1:54554b3c72f4fcbd3c27c5137f9acd9d153d7e7150d77cd509af50c75fcb41b9"
digest = "1:a2346cf0c965791c281a428a3e9bf522913941028e4c814a774af7f3cc2360b0"
name = "yunion.io/x/jsonutils"
packages = ["."]
pruneopts = "UT"
revision = "7079aada4c7e9a37e4c3b183bddb0ef684e038f2"
revision = "814e036849815935e5957ebd03bd0f4fc992fe83"
[[projects]]
branch = "master"
@@ -1329,7 +1329,7 @@
[[projects]]
branch = "master"
digest = "1:bf987f904fd821c6395fefdab956d070596eb401657fff40420014a3a8e3f45c"
digest = "1:5d9a659bf3c1d341cdf4135eca0009dbfa64a6728b3e0535c06afb6a76189a98"
name = "yunion.io/x/pkg"
packages = [
"gotypes",
@@ -1353,7 +1353,6 @@
"util/secrules",
"util/sets",
"util/stringutils",
"util/sysutils",
"util/timeutils",
"util/trace",
"util/ttlpool",
@@ -1363,7 +1362,7 @@
"utils",
]
pruneopts = "UT"
revision = "7614d751299a6703a05f757a13e6dc900332e31b"
revision = "cecf301871b09e94abaf1ff76d5ae7ac51ce0746"
[[projects]]
branch = "master"
@@ -1375,11 +1374,11 @@
[[projects]]
branch = "master"
digest = "1:f07a1ef9758f56186dd9039a8608bc9d537070a7c04479dbab6aeb42501001b7"
digest = "1:bbaf572e68e5dad4045e1e424af29838882c620df391fa1631dd8dc851c87a95"
name = "yunion.io/x/structarg"
packages = ["."]
pruneopts = "UT"
revision = "e0cc2c73375327d401b100bd8988fa340c2687ee"
revision = "c95bf78846decd8dea379e925410f2895fb20076"
[solve-meta]
analyzer-name = "dep"
@@ -1493,7 +1492,6 @@
"yunion.io/x/pkg/util/secrules",
"yunion.io/x/pkg/util/sets",
"yunion.io/x/pkg/util/stringutils",
"yunion.io/x/pkg/util/sysutils",
"yunion.io/x/pkg/util/timeutils",
"yunion.io/x/pkg/util/trace",
"yunion.io/x/pkg/util/ttlpool",
-9
View File
@@ -1,9 +0,0 @@
(The MIT License)
Copyright (c) 2017 marvin + konsorten GmbH (open-source@konsorten.de)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+205 -51
View File
@@ -12,37 +12,118 @@ import (
"reflect"
"time"
"strings"
"yunion.io/x/log"
"yunion.io/x/pkg/gotypes"
"yunion.io/x/pkg/tristate"
"yunion.io/x/pkg/util/reflectutils"
"yunion.io/x/pkg/util/timeutils"
"yunion.io/x/pkg/utils"
)
func marshalSlice(val reflect.Value) *JSONArray {
func marshalSlice(val reflect.Value, info *jsonMarshalInfo) JSONObject {
if val.Len() == 0 && info != nil && info.omitEmpty {
return JSONNull
}
objs := make([]JSONObject, val.Len())
for i := 0; i < val.Len(); i += 1 {
objs[i] = marshalValue(val.Index(i))
objs[i] = marshalValue(val.Index(i), nil)
}
arr := NewArray(objs...)
if info != nil && info.forceString {
return NewString(arr.String())
} else {
return arr
}
return NewArray(objs...)
}
func marshalMap(val reflect.Value) *JSONDict {
func marshalMap(val reflect.Value, info *jsonMarshalInfo) JSONObject {
keys := val.MapKeys()
if len(keys) == 0 && info != nil && info.omitEmpty {
return JSONNull
}
objPairs := make([]JSONPair, 0)
for i := 0; i < len(keys); i += 1 {
key := keys[i]
val := marshalValue(val.MapIndex(key))
val := marshalValue(val.MapIndex(key), nil)
if val != JSONNull {
objPairs = append(objPairs, JSONPair{key: fmt.Sprintf("%s", key), val: val})
}
}
return NewDict(objPairs...)
dict := NewDict(objPairs...)
if info != nil && info.forceString {
return NewString(dict.String())
} else {
return dict
}
}
func marshalStruct(val reflect.Value) *JSONDict {
func marshalStruct(val reflect.Value, info *jsonMarshalInfo) JSONObject {
objPairs := struct2JSONPairs(val)
return NewDict(objPairs...)
if len(objPairs) == 0 && info != nil && info.omitEmpty {
return JSONNull
}
dict := NewDict(objPairs...)
if info != nil && info.forceString {
return NewString(dict.String())
} else {
return dict
}
}
type jsonMarshalInfo struct {
ignore bool
omitEmpty bool
omitFalse bool
omitZero bool
name string
forceString bool
}
func parseJsonMarshalInfo(fieldTag reflect.StructTag) jsonMarshalInfo {
info := jsonMarshalInfo{}
info.omitEmpty = true
info.omitZero = false
info.omitFalse = false
tags := utils.TagMap(fieldTag)
if val, ok := tags["json"]; ok {
keys := strings.Split(val, ",")
if len(keys) > 0 {
if keys[0] == "-" {
if len(keys) > 1 {
info.name = keys[0]
} else {
info.ignore = true
}
} else {
info.name = keys[0]
}
}
if len(keys) > 1 {
for _, k := range keys[1:] {
switch k {
case "omitempty":
info.omitEmpty = true
case "allowempty":
info.omitEmpty = false
case "omitzero":
info.omitZero = true
case "allowzero":
info.omitZero = false
case "omitfalse":
info.omitFalse = true
case "allowfalse":
info.omitFalse = false
case "string":
info.forceString = true
}
}
}
}
if val, ok := tags["name"]; ok {
info.name = val
}
return info
}
func struct2JSONPairs(val reflect.Value) []JSONPair {
@@ -53,16 +134,44 @@ func struct2JSONPairs(val reflect.Value) []JSONPair {
if !gotypes.IsFieldExportable(fieldType.Name) { // unexportable field, ignore
continue
}
if fieldType.Type.Kind() == reflect.Struct && fieldType.Anonymous { // embbed struct
newPairs := struct2JSONPairs(val.Field(i))
objPairs = append(objPairs, newPairs...)
} else {
key := reflectutils.GetStructFieldName(&fieldType) // utils.CamelSplit(fieldType.Name, "_")
if key == "" {
if fieldType.Anonymous {
nextVal := val.Field(i)
switch fieldType.Type.Kind() {
case reflect.Struct: // embbed struct
nextVal = val.Field(i)
case reflect.Interface: // embbed interface
CHECKINTERFACE:
for {
switch nextVal.Type().Kind() {
case reflect.Interface:
nextVal = nextVal.Elem()
case reflect.Ptr:
nextVal = reflect.Indirect(nextVal)
case reflect.Struct:
break CHECKINTERFACE
default:
log.Warningf("embeded interface point to a non struct data %s", nextVal.Type())
break CHECKINTERFACE
}
}
default:
log.Warningf("unsupport anonymous embeded type %s", fieldType.Type.Name())
continue
}
val := marshalValue(val.Field(i))
if val != JSONNull {
newPairs := struct2JSONPairs(nextVal)
objPairs = append(objPairs, newPairs...)
} else {
jsonInfo := parseJsonMarshalInfo(fieldType.Tag)
if jsonInfo.ignore {
continue
}
key := jsonInfo.name
if len(key) == 0 {
key = utils.CamelSplit(fieldType.Name, "_")
}
val := marshalValue(val.Field(i), &jsonInfo)
if val != nil && val != JSONNull {
objPair := JSONPair{key: key, val: val}
objPairs = append(objPairs, objPair)
}
@@ -71,23 +180,41 @@ func struct2JSONPairs(val reflect.Value) []JSONPair {
return objPairs
}
func marshalInt64(val int64) *JSONInt {
return NewInt(val)
}
func marshalFloat64(val float64) *JSONFloat {
return NewFloat(val)
}
func marshalBoolean(val bool) *JSONBool {
if val {
return JSONTrue
func marshalInt64(val int64, info *jsonMarshalInfo) JSONObject {
if val == 0 && info != nil && info.omitZero {
return JSONNull
} else if info != nil && info.forceString {
return NewString(fmt.Sprintf("%d", val))
} else {
return JSONFalse
return NewInt(val)
}
}
func marshalTristate(val tristate.TriState) JSONObject {
func marshalFloat64(val float64, info *jsonMarshalInfo) JSONObject {
if val == 0.0 && info != nil && info.omitZero {
return JSONNull
} else if info != nil && info.forceString {
return NewString(fmt.Sprintf("%f", val))
} else {
return NewFloat(val)
}
}
func marshalBoolean(val bool, info *jsonMarshalInfo) JSONObject {
if !val && info != nil && info.omitFalse {
return JSONNull
} else if info != nil && info.forceString {
return NewString(fmt.Sprintf("%v", val))
} else {
if val {
return JSONTrue
} else {
return JSONFalse
}
}
}
func marshalTristate(val tristate.TriState, info *jsonMarshalInfo) JSONObject {
if val.IsTrue() {
return JSONTrue
} else if val.IsFalse() {
@@ -97,16 +224,19 @@ func marshalTristate(val tristate.TriState) JSONObject {
}
}
func marshalString(val string) JSONObject {
if len(val) == 0 {
func marshalString(val string, info *jsonMarshalInfo) JSONObject {
if len(val) == 0 && info != nil && info.omitEmpty {
return JSONNull
} else {
return NewString(val)
}
}
func marshalTime(val time.Time) *JSONString {
func marshalTime(val time.Time, info *jsonMarshalInfo) JSONObject {
if val.IsZero() {
if info != nil && info.omitEmpty {
return JSONNull
}
return NewString("")
} else {
return NewString(timeutils.FullIsoTime(val))
@@ -118,10 +248,10 @@ func Marshal(obj interface{}) JSONObject {
return JSONNull
}
objValue := reflect.Indirect(reflect.ValueOf(obj))
return marshalValue(objValue)
return marshalValue(objValue, nil)
}
func marshalValue(objValue reflect.Value) JSONObject {
func marshalValue(objValue reflect.Value, info *jsonMarshalInfo) JSONObject {
switch objValue.Type() {
case JSONDictPtrType, JSONArrayPtrType, JSONBoolPtrType, JSONIntPtrType, JSONFloatPtrType, JSONStringPtrType, JSONObjectType:
if objValue.IsNil() {
@@ -131,81 +261,105 @@ func marshalValue(objValue reflect.Value) JSONObject {
case JSONDictType:
json, ok := objValue.Interface().(JSONDict)
if ok {
return &json
if len(json.data) == 0 && info != nil && info.omitEmpty {
return JSONNull
} else {
return &json
}
} else {
return JSONNull
}
case JSONArrayType:
json, ok := objValue.Interface().(JSONArray)
if ok {
return &json
if len(json.data) == 0 && info != nil && info.omitEmpty {
return JSONNull
} else {
return &json
}
} else {
return JSONNull
}
case JSONBoolType:
json, ok := objValue.Interface().(JSONBool)
if ok {
return &json
if !json.data && info != nil && info.omitEmpty {
return JSONNull
} else {
return &json
}
} else {
return JSONNull
}
case JSONIntType:
json, ok := objValue.Interface().(JSONInt)
if ok {
return &json
if json.data == 0 && info != nil && info.omitEmpty {
return JSONNull
} else {
return &json
}
} else {
return JSONNull
}
case JSONFloatType:
json, ok := objValue.Interface().(JSONFloat)
if ok {
return &json
if json.data == 0.0 && info != nil && info.omitEmpty {
return JSONNull
} else {
return &json
}
} else {
return JSONNull
}
case JSONStringType:
json, ok := objValue.Interface().(JSONString)
if ok {
return &json
if len(json.data) == 0 && info != nil && info.omitEmpty {
return JSONNull
} else {
return &json
}
} else {
return JSONNull
}
case tristate.TriStateType:
tri, ok := objValue.Interface().(tristate.TriState)
if ok {
return marshalTristate(tri)
return marshalTristate(tri, info)
} else {
return JSONNull
}
}
switch objValue.Kind() {
case reflect.Slice, reflect.Array:
return marshalSlice(objValue)
return marshalSlice(objValue, info)
case reflect.Struct:
if objValue.Type() == gotypes.TimeType {
return marshalTime(objValue.Interface().(time.Time))
return marshalTime(objValue.Interface().(time.Time), info)
} else {
return marshalStruct(objValue)
return marshalStruct(objValue, info)
}
case reflect.Map:
return marshalMap(objValue)
return marshalMap(objValue, info)
case reflect.String:
strValue := objValue.Convert(gotypes.StringType)
return marshalString(strValue.Interface().(string))
return marshalString(strValue.Interface().(string), info)
case reflect.Bool:
return marshalBoolean(objValue.Interface().(bool))
return marshalBoolean(objValue.Interface().(bool), info)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
intValue := objValue.Convert(gotypes.Int64Type)
return marshalInt64(intValue.Interface().(int64))
return marshalInt64(intValue.Interface().(int64), info)
case reflect.Float32, reflect.Float64:
floatValue := objValue.Convert(gotypes.Float64Type)
return marshalFloat64(floatValue.Interface().(float64))
return marshalFloat64(floatValue.Interface().(float64), info)
case reflect.Interface, reflect.Ptr:
if objValue.IsNil() {
return JSONNull
}
return marshalValue(objValue.Elem())
return marshalValue(objValue.Elem(), info)
default:
log.Errorf("unsupport object %s %s", objValue.Type(), objValue.Interface())
return JSONNull
+1
View File
@@ -66,5 +66,6 @@ func JSONDeserialize(objType reflect.Type, strVal string) (gotypes.ISerializable
if err != nil {
return nil, err
}
objPtr = gotypes.Transform(objType, objPtr)
return objPtr, nil
}
+33 -6
View File
@@ -14,10 +14,13 @@ type ISerializable interface {
type FuncSerializableAllocator func() ISerializable
type FuncSerializableTransformer func(ISerializable) ISerializable
var (
ISerializableType = reflect.TypeOf((*ISerializable)(nil)).Elem()
serializableAllocators = map[reflect.Type]FuncSerializableAllocator{}
ErrTypeNotSerializable = errors.New("Type not serializable")
ISerializableType = reflect.TypeOf((*ISerializable)(nil)).Elem()
serializableAllocators = map[reflect.Type]FuncSerializableAllocator{}
serializableTransformers = map[reflect.Type][]FuncSerializableTransformer{}
ErrTypeNotSerializable = errors.New("Type not serializable")
)
// RegisterSerializable registers an allocator func for the specified serializable type.
@@ -29,17 +32,41 @@ func RegisterSerializable(valType reflect.Type, alloc FuncSerializableAllocator)
if !IsSerializable(valType) {
panic(valType.String() + " does not implement ISerializable")
}
if _, ok := serializableAllocators[valType]; ok {
panic(valType.String() + " has been registered, might need to register a transformer")
}
serializableAllocators[valType] = alloc
}
func RegisterSerializableTransformer(valType reflect.Type, trans FuncSerializableTransformer) {
if !IsSerializable(valType) {
panic(valType.String() + " does not implement ISerializable")
}
if _, ok := serializableTransformers[valType]; !ok {
serializableTransformers[valType] = make([]FuncSerializableTransformer, 0)
}
serializableTransformers[valType] = append(serializableTransformers[valType], trans)
}
func IsSerializable(valType reflect.Type) bool {
return valType.Implements(ISerializableType)
}
func NewSerializable(objType reflect.Type) (ISerializable, error) {
deserFunc, ok := serializableAllocators[objType]
if ok {
return deserFunc(), nil
if !ok {
return nil, ErrTypeNotSerializable
}
return nil, ErrTypeNotSerializable
retVal := deserFunc()
return retVal, nil
}
func Transform(objType reflect.Type, retVal ISerializable) ISerializable {
transFuncs, ok := serializableTransformers[objType]
if ok {
for i := 0; i < len(transFuncs); i += 1 {
retVal = transFuncs[i](retVal)
}
}
return retVal
}
+13 -1
View File
@@ -3,6 +3,7 @@ package prettytable
import (
"bytes"
"strings"
"unicode"
)
type AlignmentType uint8
@@ -102,6 +103,17 @@ func textLine(buf *bytes.Buffer, columns []ptColumn, widths []int) {
}
}
func runeDisplayWidth(r rune) int {
const puncts = "。,;:()、?《》"
if unicode.Is(unicode.Han, r) {
return 2
}
if strings.ContainsRune(puncts, r) {
return 2
}
return 1
}
// cellDisplayWidth returns display width of the cell when printed as the
// nthCol. prevWidth is the total display width (as return by this same func)
// of previous cells in the same line
@@ -116,7 +128,7 @@ func cellDisplayWidth(cell string, nthCol int, prevWidth int) int {
for _, c := range line {
incr := 0
if c != '\t' {
incr = 1
incr = runeDisplayWidth(c)
} else {
// terminal with have the char TabWidth aligned
incr = TabWidth - (x & (TabWidth - 1))
+5 -14
View File
@@ -127,21 +127,12 @@ func ParseSecurityRule(pattern string) (*SecurityRule, error) {
return nil, ErrInvalidAction
}
} else if status == SEG_IP {
// NOTE regutils.MatchCIDR actually also matches IP address without prefix length
if regutils.MatchCIDR(seg) {
if idx := strings.Index(seg, "/"); idx > -1 {
if _, ipnet, err := net.ParseCIDR(seg); err != nil {
return nil, ErrInvalidNet
} else {
rule.IPNet = ipnet
}
} else if ip := net.ParseIP(seg); ip != nil {
rule.IPNet = &net.IPNet{
IP: ip,
Mask: net.CIDRMask(32, 32),
}
} else {
return nil, ErrInvalidIPAddr
_, rule.IPNet, _ = net.ParseCIDR(seg)
} else if regutils.MatchIPAddr(seg) {
rule.IPNet = &net.IPNet{
IP: net.ParseIP(seg),
Mask: net.CIDRMask(32, 32),
}
} else {
rule.IPNet = &net.IPNet{
+10
View File
@@ -0,0 +1,10 @@
package utils
import (
"io"
"runtime/pprof"
)
func DumpAllGoroutineStack(w io.Writer) {
pprof.Lookup("goroutine").WriteTo(w, 1)
}
+18 -52
View File
@@ -187,56 +187,6 @@ func (this *ArgumentParser) addStructArgument(tp reflect.Type, val reflect.Value
return nil
}
/*func findWord(str []byte, offset int) (string, int) {
var buffer bytes.Buffer
i := skipEmpty(str, offset)
if i >= len(str) {
return "", i
}
var endstr string
quote := false
if str[i] == '"' {
quote = true
endstr = "\""
i++
} else if str[i] == '\'' {
quote = true
endstr = "'"
i++
} else {
endstr = " :,\t\n}]"
}
for i < len(str) {
if quote && str[i] == '\\' {
if i+1 < len(str) {
i++
switch str[i] {
case 'n':
buffer.WriteByte('\n')
case 'r':
buffer.WriteByte('\r')
case 't':
buffer.WriteByte('\t')
default:
buffer.WriteByte(str[i])
}
i++
} else {
break
}
} else if strings.IndexByte(endstr, str[i]) >= 0 { // end
if quote {
i++
}
break
} else {
buffer.WriteByte(str[i])
i++
}
}
return buffer.String(), i
}*/
func (this *ArgumentParser) addArgument(f reflect.StructField, v reflect.Value) error {
tagMap := utils.TagMap(f.Tag)
help := tagMap[TAG_HELP]
@@ -901,6 +851,14 @@ func (this *ArgumentParser) ParseArgs(args []string, ignore_unknown bool) error
return err
}
func isQuotedByChar(str string, quoteChar byte) bool {
return str[0] == quoteChar && str[len(str)-1] == quoteChar
}
func isQuoted(str string) bool {
return isQuotedByChar(str, '"') || isQuotedByChar(str, '\'')
}
func (this *ArgumentParser) parseKeyValue(key, value string) error {
arg := this.findOptionalArgument(key)
if arg != nil {
@@ -914,7 +872,15 @@ func (this *ArgumentParser) parseKeyValue(key, value string) error {
}
}
} else {
return arg.SetValue(value)
if !isQuoted(value) {
value = fmt.Sprintf("\"%s\"", value)
}
values := utils.FindWords([]byte(value), 0)
if len(values) == 1 {
return arg.SetValue(values[0])
} else {
log.Warningf("too many arguments %#v for %s", values, key)
}
}
} else {
log.Warningf("Cannot find argument %s", key)
@@ -964,7 +930,7 @@ func (this *ArgumentParser) ParseFile(filepath string) error {
for scanner.Scan() {
line := scanner.Text()
line = strings.TrimSpace(removeComments(line))
line = removeCharacters(line, `"'`)
// line = removeCharacters(line, `"'`)
if len(line) > 0 {
key, val, e := line2KeyValue(line)
if e == nil {