diff --git a/.example/other/test.go b/.example/other/test.go index fabb90ff8..67760c9c5 100644 --- a/.example/other/test.go +++ b/.example/other/test.go @@ -13,10 +13,6 @@ type TokenRequest struct { } func main() { - s := "123456" - fmt.Println(s[0:2]) - fmt.Println(s[1:3]) - return // s := ` //{ // "policy": {"name":"john"}, diff --git a/util/gconv/gconv.go b/util/gconv/gconv.go index e9e028b25..f694aeb76 100644 --- a/util/gconv/gconv.go +++ b/util/gconv/gconv.go @@ -379,11 +379,13 @@ func Int64(i interface{}) int64 { default: s := String(value) isMinus := false - if s[0] == '-' { - isMinus = true - s = s[1:] - } else if s[0] == '+' { - s = s[1:] + if len(s) > 0 { + if s[0] == '-' { + isMinus = true + s = s[1:] + } else if s[0] == '+' { + s = s[1:] + } } // Hexadecimal if len(s) > 2 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X') { diff --git a/util/gconv/gconv_map.go b/util/gconv/gconv_map.go index 33163b34e..63a6b47d7 100644 --- a/util/gconv/gconv_map.go +++ b/util/gconv/gconv_map.go @@ -51,11 +51,19 @@ func doMapConvert(value interface{}, recursive bool, tags ...string) map[string] switch r := value.(type) { case string: if len(r) > 0 && r[0] == '{' && r[len(r)-1] == '}' { - json.Unmarshal([]byte(r), &m) + if err := json.Unmarshal([]byte(r), &m); err != nil { + return nil + } + } else { + return nil } case []byte: if len(r) > 0 && r[0] == '{' && r[len(r)-1] == '}' { - json.Unmarshal(r, &m) + if err := json.Unmarshal(r, &m); err != nil { + return nil + } + } else { + return nil } case map[interface{}]interface{}: for k, v := range r { diff --git a/util/gconv/gconv_struct.go b/util/gconv/gconv_struct.go index 47ba66ece..c2ffb1cfe 100644 --- a/util/gconv/gconv_struct.go +++ b/util/gconv/gconv_struct.go @@ -253,7 +253,8 @@ func bindVarToReflectValue(structFieldValue reflect.Value, value interface{}) (e if err := Struct(value, structFieldValue); err != nil { structFieldValue.Set(reflect.ValueOf(value)) } - + // Note that the slice element might be type of struct, + // so it uses Struct function doing the converting internally. case reflect.Slice, reflect.Array: a := reflect.Value{} v := reflect.ValueOf(value)