MCPcopy Index your code
hub / github.com/APIParkLab/APIPark / ToKeyMap

Function ToKeyMap

module/system/dto/input.go:31–60  ·  view source on GitHub ↗
(i interface{})

Source from the content-addressed store, hash-verified

29}
30
31func ToKeyMap(i interface{}) map[string]string {
32 result := make(map[string]string)
33 val := reflect.ValueOf(i)
34 typ := reflect.TypeOf(i)
35 if typ.Kind() == reflect.Ptr {
36 val = val.Elem()
37 typ = typ.Elem()
38 }
39 switch typ.Kind() {
40 case reflect.Struct:
41 {
42 for i := 0; i < typ.NumField(); i++ {
43 f := typ.Field(i)
44 v := val.Field(i)
45 if f.Type.Kind() == reflect.Ptr {
46 if v.IsNil() {
47 continue
48 }
49 v = v.Elem()
50 }
51
52 if f.Tag.Get("key") != "" {
53 result[f.Tag.Get("key")] = v.String()
54 }
55
56 }
57 }
58 }
59 return result
60}

Callers 1

TestMapFunction · 0.85

Calls 2

GetMethod · 0.65
StringMethod · 0.45

Tested by 1

TestMapFunction · 0.68