blog_backend_api/utils/reflect.go

158 lines
4.8 KiB
Go
Raw Normal View History

2021-02-05 17:48:22 +00:00
package utils
2021-02-06 16:45:59 +00:00
import (
"log"
"reflect"
"strings"
)
2021-02-05 17:48:22 +00:00
func ReflectMakeNew(t reflect.Type) interface{} {
retptr := reflect.New(t)
sval := retptr.Elem().Interface()
return sval
}
type TagMap map[string] string
func ReflectTagMap(t reflect.Type) map[string] TagMap{
2021-02-06 16:45:59 +00:00
log.Print(t.Name())
ret := make(map[string] TagMap)
2021-02-05 17:48:22 +00:00
num := t.NumField()
for i := 0;i < num;i++{
2021-02-06 16:45:59 +00:00
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
}
}
}
2021-02-05 17:48:22 +00:00
}
return ret
2021-02-06 16:45:59 +00:00
}
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()))
}
}
}
}
}