MCPcopy Index your code
hub / github.com/wavetermdev/waveterm / MapToStruct

Function MapToStruct

tsunami/util/marshal.go:12–46  ·  view source on GitHub ↗
(in map[string]any, out any)

Source from the content-addressed store, hash-verified

10)
11
12func MapToStruct(in map[string]any, out any) error {
13 // Check that out is a pointer
14 outValue := reflect.ValueOf(out)
15 if outValue.Kind() != reflect.Ptr {
16 return fmt.Errorf("out parameter must be a pointer, got %v", outValue.Kind())
17 }
18
19 // Get the struct it points to
20 elem := outValue.Elem()
21 if elem.Kind() != reflect.Struct {
22 return fmt.Errorf("out parameter must be a pointer to struct, got pointer to %v", elem.Kind())
23 }
24
25 // Get type information
26 typ := elem.Type()
27
28 // For each field in the struct
29 for i := 0; i < typ.NumField(); i++ {
30 field := typ.Field(i)
31
32 // Skip unexported fields
33 if !field.IsExported() {
34 continue
35 }
36
37 name := getJSONName(field)
38 if value, ok := in[name]; ok {
39 if err := setValue(elem.Field(i), value); err != nil {
40 return fmt.Errorf("error setting field %s: %w", name, err)
41 }
42 }
43 }
44
45 return nil
46}
47
48func StructToMap(in any) (map[string]any, error) {
49 // 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