Store writes the values of the fields of the target struct to the accessor. Nil slice and map fields are skipped and do not overwrite existing accessor values.
(target interface{}, accessor Accessor, options StoreOptions)
| 44 | // Store writes the values of the fields of the target struct to the accessor. |
| 45 | // Nil slice and map fields are skipped and do not overwrite existing accessor values. |
| 46 | func Store(target interface{}, accessor Accessor, options StoreOptions) error { |
| 47 | return forEachField(target, func(tag *fieldTag, field reflect.Value) error { |
| 48 | if !tag.matchesStore(options) { |
| 49 | return nil |
| 50 | } |
| 51 | |
| 52 | var accessorValue interface{} |
| 53 | |
| 54 | if tag.isPtr { |
| 55 | if field.IsNil() { |
| 56 | return nil |
| 57 | } |
| 58 | |
| 59 | accessorValue = field.Elem().Interface() |
| 60 | } else { |
| 61 | if field.Kind() == reflect.Slice || field.Kind() == reflect.Map { |
| 62 | if field.IsNil() { |
| 63 | return nil |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | accessorValue = field.Interface() |
| 68 | } |
| 69 | |
| 70 | err := accessor.Set(FieldSet{ |
| 71 | Path: tag.path, Value: accessorValue, Type: tag.fType, |
| 72 | CreateKey: tag.createKey, |
| 73 | }) |
| 74 | |
| 75 | if err != nil { |
| 76 | return err |
| 77 | } |
| 78 | |
| 79 | return nil |
| 80 | }, |
| 81 | ) |
| 82 | } |
| 83 | |
| 84 | func forEachField(target interface{}, fn func(tag *fieldTag, field reflect.Value) error) error { |
| 85 | value := reflect.Indirect( |