| 251 | } |
| 252 | |
| 253 | func unset(branch interface{}, path []interface{}) (interface{}, error) { |
| 254 | switch branch := branch.(type) { |
| 255 | case TreeBranch: |
| 256 | for i, item := range branch { |
| 257 | if item.Key == path[0] { |
| 258 | if len(path) == 1 { |
| 259 | branch = slices.Delete(branch, i, i+1) |
| 260 | } else { |
| 261 | v, err := unset(item.Value, path[1:]) |
| 262 | if err != nil { |
| 263 | return nil, err |
| 264 | } |
| 265 | branch[i].Value = v |
| 266 | } |
| 267 | return branch, nil |
| 268 | } |
| 269 | } |
| 270 | return nil, &SopsKeyNotFound{Msg: "Key not found: %s", Key: path[0]} |
| 271 | case []interface{}: |
| 272 | position := path[0].(int) |
| 273 | if position >= len(branch) { |
| 274 | return nil, &SopsKeyNotFound{Msg: "Index %d out of bounds", Key: path[0]} |
| 275 | } |
| 276 | if len(path) == 1 { |
| 277 | branch = slices.Delete(branch, position, position+1) |
| 278 | } else { |
| 279 | v, err := unset(branch[position], path[1:]) |
| 280 | if err != nil { |
| 281 | return nil, err |
| 282 | } |
| 283 | branch[position] = v |
| 284 | } |
| 285 | return branch, nil |
| 286 | default: |
| 287 | return nil, fmt.Errorf("Unsupported type: %T for item '%s'", branch, path[0]) |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | // Unset removes a value on a given tree from the specified path |
| 292 | func (branch TreeBranch) Unset(path []interface{}) (TreeBranch, error) { |