(in map[string]any, out any)
| 55 | } |
| 56 | |
| 57 | func 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 | |
| 93 | func StructToMap(in any) (map[string]any, error) { |
| 94 | // Get value and handle pointer |
no test coverage detected