| 1 | package model |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "reflect" |
| 6 | ) |
| 7 | |
| 8 | // NewSliceValue returns a new slice value. |
| 9 | func NewSliceValue() *Value { |
| 10 | res := newPtr() |
| 11 | s := reflect.MakeSlice(reflect.SliceOf(reflect.TypeFor[any]()), 0, 0) |
| 12 | ptr := reflect.New(reflect.SliceOf(reflect.TypeFor[any]())) |
| 13 | ptr.Elem().Set(s) |
| 14 | res.Elem().Set(ptr) |
| 15 | return NewValue(res) |
| 16 | } |
| 17 | |
| 18 | // IsSlice returns true if the value is a slice. |
| 19 | func (v *Value) IsSlice() bool { |
| 20 | return v.UnpackKinds(reflect.Interface, reflect.Pointer).isSlice() |
| 21 | } |
| 22 | |
| 23 | func (v *Value) isSlice() bool { |
| 24 | return v.value.Kind() == reflect.Slice |
| 25 | } |
| 26 | |
| 27 | // Append appends a value to the slice. |
| 28 | func (v *Value) Append(val *Value) error { |
| 29 | // Branches behave differently when appending to a slice. |
| 30 | // We expect each item in a branch to be its own value. |
| 31 | if val.IsBranch() { |
| 32 | return val.RangeSlice(func(_ int, item *Value) error { |
| 33 | return v.Append(item) |
| 34 | }) |
| 35 | } |
| 36 | |
| 37 | unpacked := v.UnpackKinds(reflect.Interface, reflect.Pointer) |
| 38 | if !unpacked.isSlice() { |
| 39 | return ErrUnexpectedType{ |
| 40 | Expected: TypeSlice, |
| 41 | Actual: v.Type(), |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | // Wrap the value to preserve metadata and set functions. |
| 46 | valToAppend := reflect.ValueOf(val) |
| 47 | newVal := reflect.Append(unpacked.value, valToAppend) |
| 48 | unpacked.value.Set(newVal) |
| 49 | return nil |
| 50 | } |
| 51 | |
| 52 | // SliceLen returns the length of the slice. |
| 53 | func (v *Value) SliceLen() (int, error) { |
| 54 | unpacked := v.UnpackKinds(reflect.Interface, reflect.Pointer) |
| 55 | if !unpacked.isSlice() { |
| 56 | return 0, ErrUnexpectedType{ |
| 57 | Expected: TypeSlice, |
| 58 | Actual: v.Type(), |
| 59 | } |
| 60 | } |
| 61 | return unpacked.value.Len(), nil |
| 62 | } |
| 63 | |
| 64 | // GetSliceIndex returns the value at the specified index in the slice. |
| 65 | func (v *Value) GetSliceIndex(i int) (*Value, error) { |
| 66 | unpacked := v.UnpackKinds(reflect.Interface, reflect.Pointer) |
| 67 | if !unpacked.isSlice() { |
| 68 | return nil, ErrUnexpectedType{ |
| 69 | Expected: TypeSlice, |
| 70 | Actual: v.Type(), |
| 71 | } |
| 72 | } |
| 73 | if i < 0 || i >= unpacked.value.Len() { |
| 74 | return nil, SliceIndexOutOfRange{Index: i} |
| 75 | } |
| 76 | |
| 77 | item := unpacked.value.Index(i) |
| 78 | if item.Kind() == reflect.Pointer && item.Type() == reflect.TypeFor[*Value]() { |
| 79 | return item.Interface().(*Value), nil |
| 80 | } |
| 81 | if item.Kind() == reflect.Interface && !item.IsNil() { |
| 82 | interfaceVal := item.Interface() |
| 83 | if val, ok := interfaceVal.(*Value); ok { |
| 84 | return val, nil |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | res := NewValue(item) |
| 89 | return res, nil |
| 90 | } |
| 91 | |
| 92 | // SetSliceIndex sets the value at the specified index in the slice. |
| 93 | func (v *Value) SetSliceIndex(i int, val *Value) error { |
| 94 | unpacked := v.UnpackKinds(reflect.Interface, reflect.Pointer) |
| 95 | if !unpacked.isSlice() { |
| 96 | return ErrUnexpectedType{ |
| 97 | Expected: TypeSlice, |
| 98 | Actual: v.Type(), |
| 99 | } |
| 100 | } |
| 101 | if i < 0 || i >= unpacked.value.Len() { |
| 102 | return SliceIndexOutOfRange{Index: i} |
| 103 | } |
| 104 | unpacked.value.Index(i).Set(reflect.ValueOf(val)) |
| 105 | return nil |
| 106 | } |
| 107 | |
| 108 | // RangeSlice iterates over each item in the slice and calls the provided function. |
| 109 | func (v *Value) RangeSlice(f func(int, *Value) error) error { |
| 110 | length, err := v.SliceLen() |
| 111 | if err != nil { |
| 112 | return fmt.Errorf("error getting slice length: %w", err) |
| 113 | } |
| 114 | |
| 115 | for i := 0; i < length; i++ { |
| 116 | va, err := v.GetSliceIndex(i) |
| 117 | if err != nil { |
| 118 | return fmt.Errorf("error getting slice index %d: %w", i, err) |
| 119 | } |
| 120 | if err := f(i, va); err != nil { |
| 121 | return err |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | return nil |
| 126 | } |
| 127 | |
| 128 | // SliceIndexRange returns a new slice containing the values between the start and end indexes. |
| 129 | // Comparable to go's slice[start:end]. |
| 130 | func (v *Value) SliceIndexRange(start, end int) (*Value, error) { |
| 131 | l, err := v.SliceLen() |
| 132 | if err != nil { |
| 133 | return nil, fmt.Errorf("error getting slice length: %w", err) |
| 134 | } |
| 135 | |
| 136 | if start < 0 { |
| 137 | start = l + start |
| 138 | } |
| 139 | if end < 0 { |
| 140 | end = l + end |
| 141 | } |
| 142 | |
| 143 | res := NewSliceValue() |
| 144 | |
| 145 | if start > end { |
| 146 | for i := start; i >= end; i-- { |
| 147 | item, err := v.GetSliceIndex(i) |
| 148 | if err != nil { |
| 149 | return nil, fmt.Errorf("error getting slice index: %w", err) |
| 150 | } |
| 151 | if err := res.Append(item); err != nil { |
| 152 | return nil, fmt.Errorf("error appending value to slice: %w", err) |
| 153 | } |
| 154 | } |
| 155 | } else { |
| 156 | for i := start; i <= end; i++ { |
| 157 | item, err := v.GetSliceIndex(i) |
| 158 | if err != nil { |
| 159 | return nil, fmt.Errorf("error getting slice index: %w", err) |
| 160 | } |
| 161 | if err := res.Append(item); err != nil { |
| 162 | return nil, fmt.Errorf("error appending value to slice: %w", err) |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | return res, nil |
| 168 | } |