MCPcopy
hub / github.com/wavetermdev/waveterm / MapToStruct

Function MapToStruct

pkg/util/utilfn/marshal.go:57–91  ·  view source on GitHub ↗
(in map[string]any, out any)

Source from the content-addressed store, hash-verified

55}
56
57func MapToStruct(in map[string]any, out any) error {
58 // Check that out is a pointer
59 outValue := reflect.ValueOf(out)
60 if outValue.Kind() != reflect.Ptr {
61 return fmt.Errorf("out parameter must be a pointer, got %v", outValue.Kind())
62 }
63
64 // Get the struct it points to
65 elem := outValue.Elem()
66 if elem.Kind() != reflect.Struct {
67 return fmt.Errorf("out parameter must be a pointer to struct, got pointer to %v", elem.Kind())
68 }
69
70 // Get type information
71 typ := elem.Type()
72
73 // For each field in the struct
74 for i := 0; i < typ.NumField(); i++ {
75 field := typ.Field(i)
76
77 // Skip unexported fields
78 if !field.IsExported() {
79 continue
80 }
81
82 name := getJSONName(field)
83 if value, ok := in[name]; ok {
84 if err := setValue(elem.Field(i), value); err != nil {
85 return fmt.Errorf("error setting field %s: %w", name, err)
86 }
87 }
88 }
89
90 return nil
91}
92
93func StructToMap(in any) (map[string]any, error) {
94 // Get value and handle pointer

Callers 1

callCFuncFunction · 0.92

Calls 3

TypeMethod · 0.80
getJSONNameFunction · 0.70
setValueFunction · 0.70

Tested by

no test coverage detected