(in map[string]any, out any)
| 10 | ) |
| 11 | |
| 12 | func 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 | |
| 48 | func StructToMap(in any) (map[string]any, error) { |
| 49 | // Get value and handle pointer |
no test coverage detected