2023-04-11 22:20:37 +03:00
|
|
|
package reflect // import "go.unistack.org/micro/v4/util/reflect"
|
2020-12-19 23:22:05 +03:00
|
|
|
|
2020-12-20 00:15:29 +03:00
|
|
|
import (
|
2021-01-19 01:40:34 +03:00
|
|
|
"errors"
|
2021-02-05 18:27:16 +03:00
|
|
|
"fmt"
|
2020-12-20 00:15:29 +03:00
|
|
|
"reflect"
|
2021-02-05 18:27:16 +03:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2021-02-06 18:13:43 +03:00
|
|
|
"unicode"
|
2020-12-20 00:15:29 +03:00
|
|
|
)
|
2020-12-19 23:22:05 +03:00
|
|
|
|
2021-01-19 01:40:34 +03:00
|
|
|
var (
|
2022-01-10 16:47:37 +03:00
|
|
|
// ErrInvalidStruct happens when passed not struct and not struct pointer
|
2021-01-19 01:40:34 +03:00
|
|
|
ErrInvalidStruct = errors.New("invalid struct specified")
|
2022-01-10 16:47:37 +03:00
|
|
|
// ErrInvalidValue happens when passed invalid value for field
|
|
|
|
ErrInvalidValue = errors.New("invalid value specified")
|
|
|
|
// ErrNotFound happens when struct field not found
|
|
|
|
ErrNotFound = errors.New("struct field not found")
|
2021-02-14 11:28:50 +03:00
|
|
|
)
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
// Option func signature
|
2021-04-10 01:22:40 +03:00
|
|
|
type Option func(*Options)
|
2021-02-06 18:13:43 +03:00
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
// Options for merge
|
2021-04-10 01:22:40 +03:00
|
|
|
type Options struct {
|
2022-01-10 16:47:37 +03:00
|
|
|
// Tags specifies tags to lookup
|
|
|
|
Tags []string
|
|
|
|
// SliceAppend controls slice appending
|
2021-04-10 01:22:40 +03:00
|
|
|
SliceAppend bool
|
2021-02-06 18:13:43 +03:00
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
// Tags sets the merge tags for lookup
|
2021-04-10 01:22:40 +03:00
|
|
|
func Tags(t []string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Tags = t
|
2020-12-19 23:22:05 +03:00
|
|
|
}
|
2021-03-21 16:17:50 +03:00
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
// SliceAppend sets the option
|
2021-04-10 01:22:40 +03:00
|
|
|
func SliceAppend(b bool) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.SliceAppend = b
|
2020-12-20 00:15:29 +03:00
|
|
|
}
|
|
|
|
}
|
2021-01-20 00:47:13 +03:00
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
// Merge merges map[string]interface{} to destination struct
|
2021-04-10 01:22:40 +03:00
|
|
|
func Merge(dst interface{}, mp map[string]interface{}, opts ...Option) error {
|
|
|
|
var err error
|
|
|
|
var sval reflect.Value
|
|
|
|
var fname string
|
2021-01-21 14:05:50 +03:00
|
|
|
|
2021-04-10 01:22:40 +03:00
|
|
|
options := Options{}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
2021-01-21 14:05:50 +03:00
|
|
|
}
|
|
|
|
|
2021-04-10 01:22:40 +03:00
|
|
|
dviface := reflect.ValueOf(dst)
|
|
|
|
if dviface.Kind() == reflect.Ptr {
|
|
|
|
dviface = dviface.Elem()
|
2021-01-21 14:05:50 +03:00
|
|
|
}
|
2021-04-01 00:30:15 +03:00
|
|
|
|
2021-04-10 01:22:40 +03:00
|
|
|
if dviface.Kind() != reflect.Struct {
|
|
|
|
return ErrInvalidStruct
|
2021-04-01 00:30:15 +03:00
|
|
|
}
|
|
|
|
|
2021-04-10 01:22:40 +03:00
|
|
|
dtype := dviface.Type()
|
|
|
|
for idx := 0; idx < dtype.NumField(); idx++ {
|
|
|
|
dfld := dtype.Field(idx)
|
|
|
|
dval := dviface.Field(idx)
|
|
|
|
if !dval.CanSet() || len(dfld.PkgPath) != 0 || !dval.IsValid() {
|
2021-04-01 00:30:15 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-04-10 01:22:40 +03:00
|
|
|
fname = ""
|
|
|
|
for _, tname := range options.Tags {
|
|
|
|
tvalue, ok := dfld.Tag.Lookup(tname)
|
2021-04-01 00:30:15 +03:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-04-10 01:22:40 +03:00
|
|
|
tpart := strings.Split(tvalue, ",")
|
|
|
|
switch tname {
|
|
|
|
case "protobuf":
|
2021-04-19 11:34:28 +03:00
|
|
|
for _, p := range tpart {
|
|
|
|
if idx := strings.Index(p, "name="); idx > 0 {
|
|
|
|
fname = p[idx:]
|
|
|
|
}
|
|
|
|
}
|
2021-04-01 00:30:15 +03:00
|
|
|
default:
|
2021-04-10 01:22:40 +03:00
|
|
|
fname = tpart[0]
|
2021-04-01 00:30:15 +03:00
|
|
|
}
|
|
|
|
|
2021-04-10 01:22:40 +03:00
|
|
|
if fname != "" {
|
|
|
|
break
|
2021-04-01 00:30:15 +03:00
|
|
|
}
|
|
|
|
}
|
2021-02-06 18:13:43 +03:00
|
|
|
|
2021-04-10 01:22:40 +03:00
|
|
|
if fname == "" {
|
|
|
|
fname = FieldName(dfld.Name)
|
2021-02-06 18:13:43 +03:00
|
|
|
}
|
|
|
|
|
2021-04-10 01:22:40 +03:00
|
|
|
val, ok := mp[fname]
|
|
|
|
if !ok {
|
2021-11-24 00:57:59 +03:00
|
|
|
val, ok = mp[dfld.Name]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2021-02-05 18:27:16 +03:00
|
|
|
}
|
2021-02-06 18:13:43 +03:00
|
|
|
|
2021-04-10 01:22:40 +03:00
|
|
|
sval = reflect.ValueOf(val)
|
2021-02-05 18:27:16 +03:00
|
|
|
|
2021-04-10 01:22:40 +03:00
|
|
|
switch getKind(dval) {
|
2021-02-05 18:27:16 +03:00
|
|
|
case reflect.Bool:
|
2021-04-10 01:22:40 +03:00
|
|
|
err = mergeBool(dval, sval)
|
2021-02-05 18:27:16 +03:00
|
|
|
case reflect.String:
|
2021-04-10 01:22:40 +03:00
|
|
|
err = mergeString(dval, sval)
|
2021-02-05 18:27:16 +03:00
|
|
|
case reflect.Int:
|
2021-04-10 01:22:40 +03:00
|
|
|
err = mergeInt(dval, sval)
|
2021-02-05 18:27:16 +03:00
|
|
|
case reflect.Uint:
|
2021-04-10 01:22:40 +03:00
|
|
|
err = mergeUint(dval, sval)
|
2021-02-06 18:13:43 +03:00
|
|
|
case reflect.Float32:
|
2021-04-10 01:22:40 +03:00
|
|
|
err = mergeFloat(dval, sval)
|
|
|
|
case reflect.Struct:
|
|
|
|
mp, ok := sval.Interface().(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return ErrInvalidValue
|
|
|
|
}
|
|
|
|
err = Merge(dval.Interface(), mp, opts...)
|
2021-02-06 18:13:43 +03:00
|
|
|
case reflect.Ptr:
|
2021-04-10 01:22:40 +03:00
|
|
|
mp, ok := sval.Interface().(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return ErrInvalidValue
|
|
|
|
}
|
|
|
|
if dval.IsNil() {
|
|
|
|
dval.Set(reflect.New(dval.Type().Elem()))
|
|
|
|
if dval.Elem().Type().Kind() == reflect.Struct {
|
|
|
|
for i := 0; i < dval.Elem().NumField(); i++ {
|
|
|
|
field := dval.Elem().Field(i)
|
|
|
|
if field.Type().Kind() == reflect.Ptr && field.IsNil() && dval.Elem().Type().Field(i).Anonymous {
|
2021-02-06 18:13:43 +03:00
|
|
|
field.Set(reflect.New(field.Type().Elem()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-10 01:22:40 +03:00
|
|
|
err = Merge(dval.Interface(), mp, opts...)
|
|
|
|
case reflect.Slice:
|
|
|
|
if !options.SliceAppend && dval.IsNil() {
|
|
|
|
dval.Set(reflect.MakeSlice(dval.Type(), sval.Len(), sval.Len()))
|
|
|
|
} else if options.SliceAppend && dval.IsNil() {
|
|
|
|
dval.Set(reflect.MakeSlice(dval.Type(), 0, 0))
|
2021-02-06 18:13:43 +03:00
|
|
|
}
|
2021-02-05 18:27:16 +03:00
|
|
|
|
2021-04-10 01:22:40 +03:00
|
|
|
for idx := 0; idx < sval.Len(); idx++ {
|
|
|
|
var idval reflect.Value
|
|
|
|
if !options.SliceAppend {
|
|
|
|
idval = dval.Index(idx)
|
|
|
|
} else {
|
|
|
|
idval = reflect.Indirect(reflect.New(dval.Type().Elem()))
|
2021-02-06 18:13:43 +03:00
|
|
|
}
|
2021-04-10 01:22:40 +03:00
|
|
|
if getKind(idval) == reflect.Ptr && idval.IsNil() {
|
|
|
|
idval.Set(reflect.New(idval.Type().Elem()))
|
|
|
|
if idval.Elem().Type().Kind() == reflect.Struct {
|
|
|
|
for i := 0; i < idval.Elem().NumField(); i++ {
|
|
|
|
field := idval.Elem().Field(i)
|
|
|
|
if field.Type().Kind() == reflect.Ptr && field.IsNil() && idval.Elem().Type().Field(i).Anonymous {
|
|
|
|
field.Set(reflect.New(field.Type().Elem()))
|
|
|
|
}
|
|
|
|
}
|
2021-02-06 18:13:43 +03:00
|
|
|
}
|
2021-04-10 01:22:40 +03:00
|
|
|
}
|
|
|
|
switch getKind(idval) {
|
|
|
|
case reflect.Bool:
|
|
|
|
err = mergeBool(idval, sval.Index(idx))
|
|
|
|
case reflect.String:
|
|
|
|
err = mergeString(idval, sval.Index(idx))
|
|
|
|
case reflect.Int:
|
|
|
|
err = mergeInt(idval, sval.Index(idx))
|
|
|
|
case reflect.Uint:
|
|
|
|
err = mergeUint(idval, sval.Index(idx))
|
|
|
|
case reflect.Float32:
|
|
|
|
err = mergeFloat(idval, sval.Index(idx))
|
|
|
|
case reflect.Struct:
|
|
|
|
imp, ok := sval.Index(idx).Interface().(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return ErrInvalidValue
|
2021-02-06 18:13:43 +03:00
|
|
|
}
|
2021-04-10 01:22:40 +03:00
|
|
|
err = Merge(idval.Interface(), imp, opts...)
|
|
|
|
case reflect.Ptr:
|
|
|
|
nsval := sval.Index(idx)
|
|
|
|
if getKind(sval.Index(idx)) == reflect.Interface {
|
|
|
|
nsval = reflect.ValueOf(nsval.Interface())
|
2021-02-06 18:13:43 +03:00
|
|
|
}
|
2021-04-10 01:22:40 +03:00
|
|
|
switch reflect.Indirect(idval).Type().String() {
|
|
|
|
case "wrapperspb.BoolValue":
|
|
|
|
if eva := reflect.Indirect(idval).FieldByName("Value"); eva.IsValid() {
|
|
|
|
err = mergeBool(eva, nsval)
|
|
|
|
}
|
|
|
|
case "wrapperspb.BytesValue":
|
|
|
|
if eva := reflect.Indirect(idval).FieldByName("Value"); eva.IsValid() {
|
|
|
|
err = mergeUint(eva, nsval)
|
|
|
|
}
|
|
|
|
case "wrapperspb.DoubleValue", "wrapperspb.FloatValue":
|
|
|
|
if eva := reflect.Indirect(idval).FieldByName("Value"); eva.IsValid() {
|
|
|
|
err = mergeFloat(eva, nsval)
|
|
|
|
}
|
|
|
|
case "wrapperspb.Int32Value", "wrapperspb.Int64Value":
|
|
|
|
if eva := reflect.Indirect(idval).FieldByName("Value"); eva.IsValid() {
|
|
|
|
err = mergeInt(eva, nsval)
|
|
|
|
}
|
|
|
|
case "wrapperspb.StringValue":
|
|
|
|
if eva := reflect.Indirect(idval).FieldByName("Value"); eva.IsValid() {
|
|
|
|
err = mergeString(eva, nsval)
|
|
|
|
}
|
|
|
|
case "wrapperspb.UInt32Value", "wrapperspb.UInt64Value":
|
|
|
|
if eva := reflect.Indirect(idval).FieldByName("Value"); eva.IsValid() {
|
|
|
|
err = mergeUint(eva, nsval)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
imp, ok := nsval.Interface().(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return ErrInvalidValue
|
|
|
|
}
|
|
|
|
if idval.IsNil() {
|
|
|
|
idval.Set(reflect.New(idval.Type().Elem()))
|
|
|
|
if idval.Elem().Type().Kind() == reflect.Struct {
|
|
|
|
for i := 0; i < idval.Elem().NumField(); i++ {
|
|
|
|
field := idval.Elem().Field(i)
|
|
|
|
if field.Type().Kind() == reflect.Ptr && field.IsNil() && idval.Elem().Type().Field(i).Anonymous {
|
|
|
|
field.Set(reflect.New(field.Type().Elem()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = Merge(idval.Interface(), imp, opts...)
|
2021-02-06 18:13:43 +03:00
|
|
|
}
|
|
|
|
}
|
2021-04-10 01:22:40 +03:00
|
|
|
if options.SliceAppend {
|
|
|
|
dval.Set(reflect.Append(dval, idval))
|
|
|
|
}
|
2021-02-06 18:13:43 +03:00
|
|
|
}
|
2021-04-10 01:22:40 +03:00
|
|
|
/*
|
|
|
|
case reflect.Interface:
|
|
|
|
err = d.decodeBasic(name, input, outVal)
|
|
|
|
case reflect.Map:
|
|
|
|
err = merge(dval, sval)
|
|
|
|
case reflect.Array:
|
|
|
|
err = mergeArray(dval, sval)
|
|
|
|
*/
|
|
|
|
default:
|
|
|
|
err = ErrInvalidValue
|
2021-02-06 18:13:43 +03:00
|
|
|
}
|
2021-04-10 01:22:40 +03:00
|
|
|
|
2021-04-19 11:34:28 +03:00
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("%v key %v invalid val %v", err, fname, sval.Interface())
|
|
|
|
}
|
|
|
|
|
2021-04-10 01:22:40 +03:00
|
|
|
}
|
|
|
|
|
2021-04-19 11:34:28 +03:00
|
|
|
return nil
|
2021-02-06 18:13:43 +03:00
|
|
|
}
|
|
|
|
|
2021-04-10 01:22:40 +03:00
|
|
|
func mergeBool(dval reflect.Value, sval reflect.Value) error {
|
|
|
|
switch getKind(sval) {
|
2021-02-05 18:27:16 +03:00
|
|
|
case reflect.Int:
|
2021-04-10 01:22:40 +03:00
|
|
|
switch sval.Int() {
|
|
|
|
case 1:
|
|
|
|
dval.SetBool(true)
|
|
|
|
case 0:
|
|
|
|
dval.SetBool(false)
|
|
|
|
default:
|
|
|
|
return ErrInvalidValue
|
2021-02-05 18:27:16 +03:00
|
|
|
}
|
|
|
|
case reflect.Uint:
|
2021-04-10 01:22:40 +03:00
|
|
|
switch sval.Uint() {
|
|
|
|
case 1:
|
|
|
|
dval.SetBool(true)
|
|
|
|
case 0:
|
|
|
|
dval.SetBool(false)
|
|
|
|
default:
|
|
|
|
return ErrInvalidValue
|
2021-02-05 18:27:16 +03:00
|
|
|
}
|
2021-02-06 18:13:43 +03:00
|
|
|
case reflect.Float32:
|
2021-04-10 01:22:40 +03:00
|
|
|
switch sval.Float() {
|
|
|
|
case 1:
|
|
|
|
dval.SetBool(true)
|
|
|
|
case 0:
|
|
|
|
dval.SetBool(false)
|
|
|
|
default:
|
|
|
|
return ErrInvalidValue
|
2021-02-05 18:27:16 +03:00
|
|
|
}
|
2021-04-10 01:22:40 +03:00
|
|
|
case reflect.Bool:
|
|
|
|
dval.SetBool(sval.Bool())
|
2021-02-05 18:27:16 +03:00
|
|
|
case reflect.String:
|
2021-04-10 01:22:40 +03:00
|
|
|
switch sval.String() {
|
|
|
|
case "t", "T", "true", "TRUE", "True", "1", "yes":
|
|
|
|
dval.SetBool(true)
|
|
|
|
case "f", "F", "false", "FALSE", "False", "0", "no":
|
|
|
|
dval.SetBool(false)
|
|
|
|
default:
|
|
|
|
return ErrInvalidValue
|
2021-02-05 18:27:16 +03:00
|
|
|
}
|
2021-04-19 01:19:37 +03:00
|
|
|
case reflect.Interface:
|
|
|
|
return mergeBool(dval, reflect.ValueOf(fmt.Sprintf("%v", sval.Interface())))
|
2021-02-05 18:27:16 +03:00
|
|
|
default:
|
2021-04-10 01:22:40 +03:00
|
|
|
return ErrInvalidValue
|
2021-02-05 18:27:16 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-10 01:22:40 +03:00
|
|
|
func mergeString(dval reflect.Value, sval reflect.Value) error {
|
|
|
|
switch getKind(sval) {
|
2021-02-05 18:27:16 +03:00
|
|
|
case reflect.Int:
|
2021-04-10 01:22:40 +03:00
|
|
|
dval.SetString(strconv.FormatInt(sval.Int(), sval.Type().Bits()))
|
2021-02-05 18:27:16 +03:00
|
|
|
case reflect.Uint:
|
2021-04-10 01:22:40 +03:00
|
|
|
dval.SetString(strconv.FormatUint(sval.Uint(), sval.Type().Bits()))
|
2021-02-06 18:13:43 +03:00
|
|
|
case reflect.Float32:
|
2021-04-10 01:22:40 +03:00
|
|
|
dval.SetString(strconv.FormatFloat(sval.Float(), 'f', -1, sval.Type().Bits()))
|
|
|
|
case reflect.Bool:
|
|
|
|
switch sval.Bool() {
|
|
|
|
case true:
|
|
|
|
dval.SetString(strconv.FormatBool(true))
|
|
|
|
case false:
|
|
|
|
dval.SetString(strconv.FormatBool(false))
|
|
|
|
}
|
2021-02-05 18:27:16 +03:00
|
|
|
case reflect.String:
|
2021-04-10 01:22:40 +03:00
|
|
|
dval.SetString(sval.String())
|
2021-04-19 01:19:37 +03:00
|
|
|
case reflect.Interface:
|
|
|
|
return mergeString(dval, reflect.ValueOf(fmt.Sprintf("%v", sval.Interface())))
|
2021-02-05 18:27:16 +03:00
|
|
|
default:
|
2021-04-10 01:22:40 +03:00
|
|
|
return ErrInvalidValue
|
2021-02-05 18:27:16 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-10 01:22:40 +03:00
|
|
|
func mergeInt(dval reflect.Value, sval reflect.Value) error {
|
|
|
|
switch getKind(sval) {
|
2021-02-05 18:27:16 +03:00
|
|
|
case reflect.Int:
|
2021-04-10 01:22:40 +03:00
|
|
|
dval.SetInt(sval.Int())
|
2021-02-05 18:27:16 +03:00
|
|
|
case reflect.Uint:
|
2021-04-10 01:22:40 +03:00
|
|
|
dval.SetInt(int64(sval.Uint()))
|
2021-02-06 18:13:43 +03:00
|
|
|
case reflect.Float32:
|
2021-04-10 01:22:40 +03:00
|
|
|
dval.SetInt(int64(sval.Float()))
|
|
|
|
case reflect.Bool:
|
|
|
|
switch sval.Bool() {
|
|
|
|
case true:
|
|
|
|
dval.SetInt(1)
|
|
|
|
case false:
|
|
|
|
dval.SetInt(0)
|
|
|
|
}
|
2021-02-05 18:27:16 +03:00
|
|
|
case reflect.String:
|
2021-04-10 01:22:40 +03:00
|
|
|
l, err := strconv.ParseInt(sval.String(), 0, dval.Type().Bits())
|
2021-02-14 11:28:50 +03:00
|
|
|
if err != nil {
|
2021-02-05 18:27:16 +03:00
|
|
|
return err
|
|
|
|
}
|
2021-04-10 01:22:40 +03:00
|
|
|
dval.SetInt(l)
|
2021-04-19 01:19:37 +03:00
|
|
|
case reflect.Interface:
|
|
|
|
return mergeInt(dval, reflect.ValueOf(fmt.Sprintf("%v", sval.Interface())))
|
2021-02-05 18:27:16 +03:00
|
|
|
default:
|
2021-04-10 01:22:40 +03:00
|
|
|
return ErrInvalidValue
|
2021-02-05 18:27:16 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-10 01:22:40 +03:00
|
|
|
func mergeUint(dval reflect.Value, sval reflect.Value) error {
|
|
|
|
switch getKind(sval) {
|
2021-02-05 18:27:16 +03:00
|
|
|
case reflect.Int:
|
2021-04-10 01:22:40 +03:00
|
|
|
dval.SetUint(uint64(sval.Int()))
|
2021-02-05 18:27:16 +03:00
|
|
|
case reflect.Uint:
|
2021-04-10 01:22:40 +03:00
|
|
|
dval.SetUint(sval.Uint())
|
2021-02-06 18:13:43 +03:00
|
|
|
case reflect.Float32:
|
2021-04-10 01:22:40 +03:00
|
|
|
dval.SetUint(uint64(sval.Float()))
|
|
|
|
case reflect.Bool:
|
|
|
|
switch sval.Bool() {
|
|
|
|
case true:
|
|
|
|
dval.SetUint(1)
|
|
|
|
case false:
|
|
|
|
dval.SetUint(0)
|
|
|
|
}
|
2021-02-05 18:27:16 +03:00
|
|
|
case reflect.String:
|
2021-04-10 01:22:40 +03:00
|
|
|
l, err := strconv.ParseUint(sval.String(), 0, dval.Type().Bits())
|
2021-02-14 11:28:50 +03:00
|
|
|
if err != nil {
|
2021-02-05 18:27:16 +03:00
|
|
|
return err
|
|
|
|
}
|
2021-04-10 01:22:40 +03:00
|
|
|
dval.SetUint(l)
|
2021-04-19 01:19:37 +03:00
|
|
|
case reflect.Interface:
|
|
|
|
return mergeUint(dval, reflect.ValueOf(fmt.Sprintf("%v", sval.Interface())))
|
2021-02-05 18:27:16 +03:00
|
|
|
default:
|
2021-04-10 01:22:40 +03:00
|
|
|
return ErrInvalidValue
|
2021-02-05 18:27:16 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-10 01:22:40 +03:00
|
|
|
func mergeFloat(dval reflect.Value, sval reflect.Value) error {
|
|
|
|
switch getKind(sval) {
|
2021-02-05 18:27:16 +03:00
|
|
|
case reflect.Int:
|
2021-04-10 01:22:40 +03:00
|
|
|
dval.SetFloat(float64(sval.Int()))
|
2021-02-05 18:27:16 +03:00
|
|
|
case reflect.Uint:
|
2021-04-10 01:22:40 +03:00
|
|
|
dval.SetFloat(float64(sval.Uint()))
|
2021-02-06 18:13:43 +03:00
|
|
|
case reflect.Float32:
|
2021-04-10 01:22:40 +03:00
|
|
|
dval.SetFloat(sval.Float())
|
|
|
|
case reflect.Bool:
|
|
|
|
switch sval.Bool() {
|
|
|
|
case true:
|
|
|
|
dval.SetFloat(1)
|
|
|
|
case false:
|
|
|
|
dval.SetFloat(0)
|
|
|
|
}
|
2021-02-05 18:27:16 +03:00
|
|
|
case reflect.String:
|
2021-04-10 01:22:40 +03:00
|
|
|
l, err := strconv.ParseFloat(sval.String(), dval.Type().Bits())
|
2021-02-14 11:28:50 +03:00
|
|
|
if err != nil {
|
2021-02-05 18:27:16 +03:00
|
|
|
return err
|
|
|
|
}
|
2021-04-10 01:22:40 +03:00
|
|
|
dval.SetFloat(l)
|
2021-04-19 01:19:37 +03:00
|
|
|
case reflect.Interface:
|
|
|
|
return mergeFloat(dval, reflect.ValueOf(fmt.Sprintf("%v", sval.Interface())))
|
2021-02-05 18:27:16 +03:00
|
|
|
default:
|
2021-04-10 01:22:40 +03:00
|
|
|
return ErrInvalidValue
|
2021-02-05 18:27:16 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getKind(val reflect.Value) reflect.Kind {
|
|
|
|
kind := val.Kind()
|
|
|
|
switch {
|
|
|
|
case kind >= reflect.Int && kind <= reflect.Int64:
|
|
|
|
return reflect.Int
|
|
|
|
case kind >= reflect.Uint && kind <= reflect.Uint64:
|
|
|
|
return reflect.Uint
|
|
|
|
case kind >= reflect.Float32 && kind <= reflect.Float64:
|
2021-02-06 18:13:43 +03:00
|
|
|
return reflect.Float32
|
2021-02-05 18:27:16 +03:00
|
|
|
}
|
|
|
|
return kind
|
|
|
|
}
|
2021-02-06 18:13:43 +03:00
|
|
|
|
2021-04-10 01:22:40 +03:00
|
|
|
// IsZero returns true if struct is zero (not have any defined values)
|
|
|
|
func IsZero(src interface{}) bool {
|
|
|
|
v := reflect.ValueOf(src)
|
|
|
|
return IsEmpty(v)
|
2021-02-06 18:13:43 +03:00
|
|
|
}
|
|
|
|
|
2021-04-10 01:22:40 +03:00
|
|
|
// Zero creates new zero interface
|
|
|
|
func Zero(src interface{}) (interface{}, error) {
|
|
|
|
sv := reflect.ValueOf(src)
|
2021-02-06 18:13:43 +03:00
|
|
|
|
2021-04-10 01:22:40 +03:00
|
|
|
if sv.Kind() == reflect.Ptr {
|
|
|
|
sv = sv.Elem()
|
2021-02-06 18:13:43 +03:00
|
|
|
}
|
|
|
|
|
2021-04-10 01:22:40 +03:00
|
|
|
if sv.Kind() == reflect.Invalid {
|
|
|
|
return nil, ErrInvalidStruct
|
2021-02-06 18:13:43 +03:00
|
|
|
}
|
|
|
|
|
2021-04-10 01:22:40 +03:00
|
|
|
dst := reflect.New(sv.Type())
|
2021-02-06 18:13:43 +03:00
|
|
|
|
2021-04-10 01:22:40 +03:00
|
|
|
return dst.Interface(), nil
|
2021-02-06 18:13:43 +03:00
|
|
|
}
|
|
|
|
|
2021-04-10 01:22:40 +03:00
|
|
|
// IsEmpty returns true if value empty
|
|
|
|
func IsEmpty(v reflect.Value) bool {
|
|
|
|
switch getKind(v) {
|
|
|
|
case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
|
|
|
|
return v.Len() == 0
|
|
|
|
case reflect.Bool:
|
|
|
|
return !v.Bool()
|
|
|
|
case reflect.Int:
|
|
|
|
return v.Int() == 0
|
|
|
|
case reflect.Uint:
|
|
|
|
return v.Uint() == 0
|
|
|
|
case reflect.Float32:
|
|
|
|
return v.Float() == 0
|
|
|
|
case reflect.Interface, reflect.Ptr:
|
|
|
|
if v.IsNil() {
|
|
|
|
return true
|
2021-02-06 18:13:43 +03:00
|
|
|
}
|
2021-04-10 01:22:40 +03:00
|
|
|
return IsEmpty(v.Elem())
|
|
|
|
case reflect.Func:
|
|
|
|
return v.IsNil()
|
|
|
|
case reflect.Invalid:
|
|
|
|
return true
|
|
|
|
case reflect.Struct:
|
|
|
|
var ok bool
|
|
|
|
for i := 0; i < v.NumField(); i++ {
|
|
|
|
ok = IsEmpty(v.FieldByIndex([]int{i}))
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
2021-02-06 18:13:43 +03:00
|
|
|
}
|
2021-04-10 01:22:40 +03:00
|
|
|
default:
|
|
|
|
return false
|
2021-02-06 18:13:43 +03:00
|
|
|
}
|
2021-04-10 01:22:40 +03:00
|
|
|
return true
|
2021-02-06 18:13:43 +03:00
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
// FieldName returns map field name that can be looked up in struct field
|
2021-04-10 01:22:40 +03:00
|
|
|
func FieldName(name string) string {
|
|
|
|
newstr := make([]rune, 0, len(name))
|
|
|
|
for idx, chr := range name {
|
|
|
|
if idx == 0 {
|
|
|
|
newstr = append(newstr, unicode.ToLower(chr))
|
|
|
|
continue
|
|
|
|
} else if unicode.IsUpper(chr) {
|
|
|
|
newstr = append(newstr, '_')
|
|
|
|
newstr = append(newstr, unicode.ToLower(chr))
|
|
|
|
continue
|
2021-02-06 18:13:43 +03:00
|
|
|
}
|
2021-04-10 01:22:40 +03:00
|
|
|
newstr = append(newstr, chr)
|
2021-02-06 18:13:43 +03:00
|
|
|
}
|
2021-04-01 00:30:15 +03:00
|
|
|
|
2021-04-10 01:22:40 +03:00
|
|
|
return string(newstr)
|
2021-04-01 00:30:15 +03:00
|
|
|
}
|