| 1 | package model |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "reflect" |
| 7 | |
| 8 | "github.com/tomwright/dasel/v3/model/orderedmap" |
| 9 | ) |
| 10 | |
| 11 | // NewMapValue creates a new map value. |
| 12 | func NewMapValue() *Value { |
| 13 | return NewValue(orderedmap.NewMap()) |
| 14 | } |
| 15 | |
| 16 | // IsMap returns true if the value is a map. |
| 17 | func (v *Value) IsMap() bool { |
| 18 | return v.isStandardMap() || v.isDencodingMap() |
| 19 | } |
| 20 | |
| 21 | func (v *Value) isStandardMap() bool { |
| 22 | return v.UnpackKinds(reflect.Interface, reflect.Pointer).Kind() == reflect.Map |
| 23 | } |
| 24 | |
| 25 | func (v *Value) isDencodingMap() bool { |
| 26 | return v.UnpackKinds(reflect.Interface, reflect.Pointer).value.Type() == reflect.TypeFor[orderedmap.Map]() |
| 27 | } |
| 28 | |
| 29 | func (v *Value) dencodingMapValue() (*orderedmap.Map, error) { |
| 30 | if v.isDencodingMap() { |
| 31 | m, err := v.UnpackUntilType(reflect.TypeFor[*orderedmap.Map]()) |
| 32 | if err != nil { |
| 33 | return nil, fmt.Errorf("error getting map: %w", err) |
| 34 | } |
| 35 | return m.value.Interface().(*orderedmap.Map), nil |
| 36 | } |
| 37 | return nil, fmt.Errorf("value is not a dencoding map") |
| 38 | } |
| 39 | |
| 40 | // SetMapKey sets the value at the specified key in the map. |
| 41 | func (v *Value) SetMapKey(key string, value *Value) error { |
| 42 | switch { |
| 43 | case v.isDencodingMap(): |
| 44 | m, err := v.dencodingMapValue() |
| 45 | if err != nil { |
| 46 | return fmt.Errorf("error getting map: %w", err) |
| 47 | } |
| 48 | m.Set(key, value) |
| 49 | return nil |
| 50 | case v.isStandardMap(): |
| 51 | unpacked, err := v.UnpackUntilKind(reflect.Map) |
| 52 | if err != nil { |
| 53 | return fmt.Errorf("error unpacking value: %w", err) |
| 54 | } |
| 55 | unpacked.value.SetMapIndex(reflect.ValueOf(key), value.value) |
| 56 | return nil |
| 57 | default: |
| 58 | return fmt.Errorf("value is not a map") |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | func (v *Value) MapCopy() (*Value, error) { |
| 63 | res := NewMapValue() |
| 64 | kvs, err := v.MapKeyValues() |
| 65 | if err != nil { |
| 66 | return nil, fmt.Errorf("error getting map key values: %w", err) |
| 67 | } |
| 68 | for _, kv := range kvs { |
| 69 | if err := res.SetMapKey(kv.Key, kv.Value); err != nil { |
| 70 | return nil, fmt.Errorf("error setting map key: %w", err) |
| 71 | } |
| 72 | } |
| 73 | return res, nil |
| 74 | } |
| 75 | |
| 76 | func (v *Value) MapKeyExists(key string) (bool, error) { |
| 77 | _, err := v.GetMapKey(key) |
| 78 | if err != nil && !errors.As(err, &MapKeyNotFound{}) { |
| 79 | return false, err |
| 80 | } |
| 81 | return err == nil, nil |
| 82 | } |
| 83 | |
| 84 | // GetMapKey returns the value at the specified key in the map. |
| 85 | func (v *Value) GetMapKey(key string) (*Value, error) { |
| 86 | switch { |
| 87 | case v.isDencodingMap(): |
| 88 | m, err := v.dencodingMapValue() |
| 89 | if err != nil { |
| 90 | return nil, fmt.Errorf("error getting map: %w", err) |
| 91 | } |
| 92 | val, ok := m.Get(key) |
| 93 | if !ok { |
| 94 | return nil, MapKeyNotFound{Key: key} |
| 95 | } |
| 96 | if modelValue, isValue := val.(*Value); isValue { |
| 97 | modelValue.setFn = func(newValue *Value) error { |
| 98 | m.Set(key, newValue) |
| 99 | return nil |
| 100 | } |
| 101 | return modelValue, nil |
| 102 | } |
| 103 | res := NewValue(val) |
| 104 | res.setFn = func(newValue *Value) error { |
| 105 | m.Set(key, newValue) |
| 106 | return nil |
| 107 | } |
| 108 | return res, nil |
| 109 | case v.isStandardMap(): |
| 110 | unpacked, err := v.UnpackUntilKind(reflect.Map) |
| 111 | if err != nil { |
| 112 | return nil, fmt.Errorf("error unpacking value: %w", err) |
| 113 | } |
| 114 | i := unpacked.value.MapIndex(reflect.ValueOf(key)) |
| 115 | if !i.IsValid() { |
| 116 | return nil, MapKeyNotFound{Key: key} |
| 117 | } |
| 118 | res := NewValue(i) |
| 119 | res.setFn = func(newValue *Value) error { |
| 120 | mapRv, err := v.UnpackUntilKind(reflect.Map) |
| 121 | if err != nil { |
| 122 | return fmt.Errorf("error unpacking value: %w", err) |
| 123 | } |
| 124 | mapRv.value.SetMapIndex(reflect.ValueOf(key), newValue.value) |
| 125 | return nil |
| 126 | } |
| 127 | return res, nil |
| 128 | default: |
| 129 | return nil, ErrUnexpectedType{ |
| 130 | Expected: TypeMap, |
| 131 | Actual: v.Type(), |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | // DeleteMapKey deletes the key from the map. |
| 137 | func (v *Value) DeleteMapKey(key string) error { |
| 138 | switch { |
| 139 | case v.isDencodingMap(): |
| 140 | m, err := v.dencodingMapValue() |
| 141 | if err != nil { |
| 142 | return fmt.Errorf("error getting map: %w", err) |
| 143 | } |
| 144 | m.Delete(key) |
| 145 | return nil |
| 146 | case v.isStandardMap(): |
| 147 | unpacked, err := v.UnpackUntilKind(reflect.Map) |
| 148 | if err != nil { |
| 149 | return fmt.Errorf("error unpacking value: %w", err) |
| 150 | } |
| 151 | unpacked.value.SetMapIndex(reflect.ValueOf(key), reflect.Value{}) |
| 152 | return nil |
| 153 | default: |
| 154 | return ErrUnexpectedType{ |
| 155 | Expected: TypeMap, |
| 156 | Actual: v.Type(), |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | // MapKeys returns a list of keys in the map. |
| 162 | func (v *Value) MapKeys() ([]string, error) { |
| 163 | switch { |
| 164 | case v.isDencodingMap(): |
| 165 | m, err := v.dencodingMapValue() |
| 166 | if err != nil { |
| 167 | return nil, fmt.Errorf("error getting map: %w", err) |
| 168 | } |
| 169 | return m.Keys(), nil |
| 170 | case v.isStandardMap(): |
| 171 | unpacked, err := v.UnpackUntilKind(reflect.Map) |
| 172 | if err != nil { |
| 173 | return nil, fmt.Errorf("error unpacking value: %w", err) |
| 174 | } |
| 175 | keys := unpacked.value.MapKeys() |
| 176 | strKeys := make([]string, len(keys)) |
| 177 | for i, k := range keys { |
| 178 | strKeys[i] = k.String() |
| 179 | } |
| 180 | return strKeys, nil |
| 181 | default: |
| 182 | return nil, ErrUnexpectedType{ |
| 183 | Expected: TypeMap, |
| 184 | Actual: v.Type(), |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | // RangeMap iterates over each key in the map and calls the provided function with the key and value. |
| 190 | func (v *Value) RangeMap(f func(string, *Value) error) error { |
| 191 | keys, err := v.MapKeys() |
| 192 | if err != nil { |
| 193 | return fmt.Errorf("error getting map keys: %w", err) |
| 194 | } |
| 195 | |
| 196 | for _, k := range keys { |
| 197 | va, err := v.GetMapKey(k) |
| 198 | if err != nil { |
| 199 | return fmt.Errorf("error getting map key: %w", err) |
| 200 | } |
| 201 | if err := f(k, va); err != nil { |
| 202 | return err |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | return nil |
| 207 | } |
| 208 | |
| 209 | // MapKeyValues returns a list of key value pairs in the map. |
| 210 | func (v *Value) MapKeyValues() ([]KeyValue, error) { |
| 211 | keys, err := v.MapKeys() |
| 212 | if err != nil { |
| 213 | return nil, fmt.Errorf("error getting map keys: %w", err) |
| 214 | } |
| 215 | |
| 216 | kvs := make([]KeyValue, len(keys)) |
| 217 | |
| 218 | for i, k := range keys { |
| 219 | va, err := v.GetMapKey(k) |
| 220 | if err != nil { |
| 221 | return nil, fmt.Errorf("error getting map key: %w", err) |
| 222 | } |
| 223 | kvs[i] = KeyValue{ |
| 224 | Key: k, |
| 225 | Value: va, |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | return kvs, nil |
| 230 | } |
| 231 | |
| 232 | // MapLen returns the length of the slice. |
| 233 | func (v *Value) MapLen() (int, error) { |
| 234 | keys, err := v.MapKeys() |
| 235 | if err != nil { |
| 236 | return 0, err |
| 237 | } |
| 238 | return len(keys), nil |
| 239 | } |