diff --git a/test/portData_test.go b/test/portData_test.go index f92b74c..4557260 100644 --- a/test/portData_test.go +++ b/test/portData_test.go @@ -58,17 +58,16 @@ func TestPortDocToElastic(t *testing.T) { func TestReflect(t *testing.T){ type XX struct{ - a int16 `json:"bb"` - b string `json: "cc"` + A int16 `json:"bb"` + B string `json: "cc" xml:"test"` } - x := utils.ReflectMakeNew(reflect.TypeOf(1212)) - y := utils.ReflectMakeNew(reflect.TypeOf(XX{})) - - log.Print(x) - log.Print(y) - remap := utils.ReflectTagMap(reflect.TypeOf(XX{})) - log.Print(remap) - t.Log(x) + unmar := map[string] interface{}{ + "bb" : 2, + "cc": "hello", + } + xx := XX{} + utils.UnmarshalJson(&xx,unmar) + log.Print(xx) } func TestQueryDoc(t *testing.T){ diff --git a/utils/reflect.go b/utils/reflect.go index 1b0f11d..46d17ea 100644 --- a/utils/reflect.go +++ b/utils/reflect.go @@ -1,6 +1,11 @@ package utils -import "reflect" +import ( + "log" + "reflect" + "strings" +) + func ReflectMakeNew(t reflect.Type) interface{} { retptr := reflect.New(t) @@ -11,11 +16,142 @@ func ReflectMakeNew(t reflect.Type) interface{} { type TagMap map[string] string func ReflectTagMap(t reflect.Type) map[string] TagMap{ - ret := map[string] TagMap{} + log.Print(t.Name()) + ret := make(map[string] TagMap) num := t.NumField() for i := 0;i < num;i++{ - s := t.Field(i).Tag - ret[string(s)] = t.Field(i).Name + sub := strings.Split(string(t.Field(i).Tag),"\"") + for k,v := range(sub){ + if len(v) > 0{ + if k%2 == 0{ + v = strings.Trim(v," ") + } + if v[len(v) - 1] == ':'{ + if _,ok := ret[v[:len(v) - 1]];ok{ + if ((k + 1) < len(sub) - 1) && ((sub[k + 1][len(sub[k + 1]) - 1] ) == ':'){ + continue; + }else{ + ret[v[:len(v) - 1]][sub[k + 1]] = string(t.Field(i).Name) + } + }else{ + ret[v[:len(v) - 1]] = make(TagMap) + log.Print() + if ((k + 1) < len(sub) - 1) && ((sub[k + 1][len(sub[k + 1]) - 1] ) == ':'){ + continue; + }else{ + ret[v[:len(v) - 1]][sub[k + 1]] = string(t.Field(i).Name) + } + } + }else{ + continue + } + } + + } } return ret -} \ No newline at end of file +} + +func SameKind(typ1 reflect.Kind,typ2 reflect.Kind) bool{ + switch typ1{ + case reflect.Int: + if(typ2 == reflect.Int ||typ2 == reflect.Int16 ||typ2 == reflect.Int32 ||typ2 == reflect.Int64||typ2 == reflect.Int8){ + return true + } + if(typ2 == reflect.Uint ||typ2 == reflect.Uint16 ||typ2 == reflect.Uint32 ||typ2 == reflect.Uint64||typ2 == reflect.Uint8){ + return true + } + case reflect.Int8: + if(typ2 == reflect.Int ||typ2 == reflect.Int16 ||typ2 == reflect.Int32 ||typ2 == reflect.Int64||typ2 == reflect.Int8){ + return true + } + if(typ2 == reflect.Uint ||typ2 == reflect.Uint16 ||typ2 == reflect.Uint32 ||typ2 == reflect.Uint64||typ2 == reflect.Uint8){ + return true + } + case reflect.Int16: + if(typ2 == reflect.Int ||typ2 == reflect.Int16 ||typ2 == reflect.Int32 ||typ2 == reflect.Int64||typ2 == reflect.Int8){ + return true + } + if(typ2 == reflect.Uint ||typ2 == reflect.Uint16 ||typ2 == reflect.Uint32 ||typ2 == reflect.Uint64||typ2 == reflect.Uint8){ + return true + } + case reflect.Int32: + if(typ2 == reflect.Int ||typ2 == reflect.Int16 ||typ2 == reflect.Int32 ||typ2 == reflect.Int64||typ2 == reflect.Int8){ + return true + } + if(typ2 == reflect.Uint ||typ2 == reflect.Uint16 ||typ2 == reflect.Uint32 ||typ2 == reflect.Uint64||typ2 == reflect.Uint8){ + return true + } + case reflect.Uint: + if(typ2 == reflect.Int ||typ2 == reflect.Int16 ||typ2 == reflect.Int32 ||typ2 == reflect.Int64||typ2 == reflect.Int8){ + return true + } + if(typ2 == reflect.Uint ||typ2 == reflect.Uint16 ||typ2 == reflect.Uint32 ||typ2 == reflect.Uint64||typ2 == reflect.Uint8){ + return true + } + case reflect.Uint8: + if(typ2 == reflect.Int ||typ2 == reflect.Int16 ||typ2 == reflect.Int32 ||typ2 == reflect.Int64||typ2 == reflect.Int8){ + return true + } + if(typ2 == reflect.Uint ||typ2 == reflect.Uint16 ||typ2 == reflect.Uint32 ||typ2 == reflect.Uint64||typ2 == reflect.Uint8){ + return true + } + case reflect.Uint16: + if(typ2 == reflect.Int ||typ2 == reflect.Int16 ||typ2 == reflect.Int32 ||typ2 == reflect.Int64||typ2 == reflect.Int8){ + return true + } + if(typ2 == reflect.Uint ||typ2 == reflect.Uint16 ||typ2 == reflect.Uint32 ||typ2 == reflect.Uint64||typ2 == reflect.Uint8){ + return true + } + case reflect.Uint32: + if(typ2 == reflect.Int ||typ2 == reflect.Int16 ||typ2 == reflect.Int32 ||typ2 == reflect.Int64||typ2 == reflect.Int8){ + return true + } + if(typ2 == reflect.Uint ||typ2 == reflect.Uint16 ||typ2 == reflect.Uint32 ||typ2 == reflect.Uint64||typ2 == reflect.Uint8){ + return true + } + case reflect.Uint64: + if(typ2 == reflect.Int ||typ2 == reflect.Int16 ||typ2 == reflect.Int32 ||typ2 == reflect.Int64||typ2 == reflect.Int8){ + return true + } + if(typ2 == reflect.Uint ||typ2 == reflect.Uint16 ||typ2 == reflect.Uint32 ||typ2 == reflect.Uint64||typ2 == reflect.Uint8){ + return true + } + case reflect.Float32: + if(typ2 == reflect.Float32 ||typ2 == reflect.Float64){ + return true + } + case reflect.Float64: + if(typ2 == reflect.Float32 ||typ2 == reflect.Float64){ + return true + } + default: + return (typ1 == typ2) + } + return false +} + +func UnmarshalJson(value interface{}, maps map[string]interface{}){ + valueof := reflect.ValueOf(value) + if !valueof.Elem().CanAddr(){ + log.Print("should be addr") + return + } + remap := ReflectTagMap(valueof.Elem().Type()) + _,ok := remap["json"] + if !ok{ + return + } + for k,v := range(maps){ + log.Print(k) + if filedName,ok := remap["json"][k];ok{ + log.Print(valueof.Elem().FieldByName(filedName).Kind(),reflect.TypeOf(v).Kind().String()) + if SameKind(valueof.Elem().FieldByName(filedName).Kind(),reflect.TypeOf(v).Kind()){ + if(valueof.Elem().FieldByName(filedName).CanSet()){ + valueof.Elem().FieldByName(filedName).Set(reflect.ValueOf(v).Convert(valueof.Elem().FieldByName(filedName).Type())) + } + } + } + } +} +