Debian Codegraph @

model/value_slice.go

raw symbols SCIP
1 package modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/
2
3 import (
4 "fmtscip-go gomod github.com/golang/go/src go1.25 fmt/"
5 "reflectscip-go gomod github.com/golang/go/src go1.25 reflect/"
6 )
7
8 // NewSliceValue returns a new slice value.
9 func NewSliceValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewSliceValue().() *Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value# {
10 res := newPtrscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/newPtr().()
11 s := reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.MakeSlicescip-go gomod github.com/golang/go/src go1.25 reflect/MakeSlice().(reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.SliceOfscip-go gomod github.com/golang/go/src go1.25 reflect/SliceOf().(reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.TypeForscip-go gomod github.com/golang/go/src go1.25 reflect/TypeFor().[any]()), 0, 0)
12 ptr := reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.Newscip-go gomod github.com/golang/go/src go1.25 reflect/New().(reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.SliceOfscip-go gomod github.com/golang/go/src go1.25 reflect/SliceOf().(reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.TypeForscip-go gomod github.com/golang/go/src go1.25 reflect/TypeFor().[any]()))
13 ptr.Elemscip-go gomod github.com/golang/go/src go1.25 reflect/Value#Elem().().Setscip-go gomod github.com/golang/go/src go1.25 reflect/Value#Set().(s)
14 res.Elemscip-go gomod github.com/golang/go/src go1.25 reflect/Value#Elem().().Setscip-go gomod github.com/golang/go/src go1.25 reflect/Value#Set().(ptr)
15 return NewValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewValue().(res)
16 }
17
18 // IsSlice returns true if the value is a slice.
19 func (v *Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#) IsSlicescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#IsSlice().() bool {
20 return v.UnpackKindsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#UnpackKinds().(reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.Interfacescip-go gomod github.com/golang/go/src go1.25 reflect/Interface., reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.Pointerscip-go gomod github.com/golang/go/src go1.25 reflect/Pointer.).isSlicescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#isSlice().()
21 }
22
23 func (v *Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#) isSlicescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#isSlice().() bool {
24 return v.valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#value..Kindscip-go gomod github.com/golang/go/src go1.25 reflect/Value#Kind().() == reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.Slicescip-go gomod github.com/golang/go/src go1.25 reflect/Slice.
25 }
26
27 // Append appends a value to the slice.
28 func (v *Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#) Appendscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#Append().(val *Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/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.IsBranchscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#IsBranch().() {
32 return val.RangeSlicescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#RangeSlice().(func(_ int, item *Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#) error {
33 return v.Appendscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#Append().(item)
34 })
35 }
36
37 unpacked := v.UnpackKindsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#UnpackKinds().(reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.Interfacescip-go gomod github.com/golang/go/src go1.25 reflect/Interface., reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.Pointerscip-go gomod github.com/golang/go/src go1.25 reflect/Pointer.)
38 if !unpacked.isSlicescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#isSlice().() {
39 return ErrUnexpectedTypescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/ErrUnexpectedType#{
40 Expectedscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/ErrUnexpectedType#Expected.: TypeSlicescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/TypeSlice.,
41 Actualscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/ErrUnexpectedType#Actual.: v.Typescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#Type().(),
42 }
43 }
44
45 // Wrap the value to preserve metadata and set functions.
46 valToAppend := reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.25 reflect/ValueOf().(val)
47 newVal := reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.Appendscip-go gomod github.com/golang/go/src go1.25 reflect/Append().(unpacked.valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#value., valToAppend)
48 unpacked.valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#value..Setscip-go gomod github.com/golang/go/src go1.25 reflect/Value#Set().(newVal)
49 return nil
50 }
51
52 // SliceLen returns the length of the slice.
53 func (v *Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#) SliceLenscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SliceLen().() (int, error) {
54 unpacked := v.UnpackKindsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#UnpackKinds().(reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.Interfacescip-go gomod github.com/golang/go/src go1.25 reflect/Interface., reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.Pointerscip-go gomod github.com/golang/go/src go1.25 reflect/Pointer.)
55 if !unpacked.isSlicescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#isSlice().() {
56 return 0, ErrUnexpectedTypescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/ErrUnexpectedType#{
57 Expectedscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/ErrUnexpectedType#Expected.: TypeSlicescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/TypeSlice.,
58 Actualscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/ErrUnexpectedType#Actual.: v.Typescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#Type().(),
59 }
60 }
61 return unpacked.valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#value..Lenscip-go gomod github.com/golang/go/src go1.25 reflect/Value#Len().(), nil
62 }
63
64 // GetSliceIndex returns the value at the specified index in the slice.
65 func (v *Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#) GetSliceIndexscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#GetSliceIndex().(i int) (*Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#, error) {
66 unpacked := v.UnpackKindsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#UnpackKinds().(reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.Interfacescip-go gomod github.com/golang/go/src go1.25 reflect/Interface., reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.Pointerscip-go gomod github.com/golang/go/src go1.25 reflect/Pointer.)
67 if !unpacked.isSlicescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#isSlice().() {
68 return nil, ErrUnexpectedTypescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/ErrUnexpectedType#{
69 Expectedscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/ErrUnexpectedType#Expected.: TypeSlicescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/TypeSlice.,
70 Actualscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/ErrUnexpectedType#Actual.: v.Typescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#Type().(),
71 }
72 }
73 if i < 0 || i >= unpacked.valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#value..Lenscip-go gomod github.com/golang/go/src go1.25 reflect/Value#Len().() {
74 return nil, SliceIndexOutOfRangescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/SliceIndexOutOfRange#{Indexscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/SliceIndexOutOfRange#Index.: i}
75 }
76
77 item := unpacked.valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#value..Indexscip-go gomod github.com/golang/go/src go1.25 reflect/Value#Index().(i)
78 if item.Kindscip-go gomod github.com/golang/go/src go1.25 reflect/Value#Kind().() == reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.Pointerscip-go gomod github.com/golang/go/src go1.25 reflect/Pointer. && item.Typescip-go gomod github.com/golang/go/src go1.25 reflect/Value#Type().() == reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.TypeForscip-go gomod github.com/golang/go/src go1.25 reflect/TypeFor().[*Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#]() {
79 return item.Interfacescip-go gomod github.com/golang/go/src go1.25 reflect/Value#Interface().().(*Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#), nil
80 }
81 if item.Kindscip-go gomod github.com/golang/go/src go1.25 reflect/Value#Kind().() == reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.Interfacescip-go gomod github.com/golang/go/src go1.25 reflect/Interface. && !item.IsNilscip-go gomod github.com/golang/go/src go1.25 reflect/Value#IsNil().() {
82 interfaceVal := item.Interfacescip-go gomod github.com/golang/go/src go1.25 reflect/Value#Interface().()
83 if val, ok := interfaceVal.(*Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#); ok {
84 return val, nil
85 }
86 }
87
88 res := NewValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewValue().(item)
89 return res, nil
90 }
91
92 // SetSliceIndex sets the value at the specified index in the slice.
93 func (v *Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#) SetSliceIndexscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SetSliceIndex().(i int, val *Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#) error {
94 unpacked := v.UnpackKindsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#UnpackKinds().(reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.Interfacescip-go gomod github.com/golang/go/src go1.25 reflect/Interface., reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.Pointerscip-go gomod github.com/golang/go/src go1.25 reflect/Pointer.)
95 if !unpacked.isSlicescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#isSlice().() {
96 return ErrUnexpectedTypescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/ErrUnexpectedType#{
97 Expectedscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/ErrUnexpectedType#Expected.: TypeSlicescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/TypeSlice.,
98 Actualscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/ErrUnexpectedType#Actual.: v.Typescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#Type().(),
99 }
100 }
101 if i < 0 || i >= unpacked.valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#value..Lenscip-go gomod github.com/golang/go/src go1.25 reflect/Value#Len().() {
102 return SliceIndexOutOfRangescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/SliceIndexOutOfRange#{Indexscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/SliceIndexOutOfRange#Index.: i}
103 }
104 unpacked.valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#value..Indexscip-go gomod github.com/golang/go/src go1.25 reflect/Value#Index().(i).Setscip-go gomod github.com/golang/go/src go1.25 reflect/Value#Set().(reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.25 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 *Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#) RangeSlicescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#RangeSlice().(f func(int, *Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#) error) error {
110 length, err := v.SliceLenscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SliceLen().()
111 if err != nil {
112 return fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("error getting slice length: %w", err)
113 }
114
115 for i := 0; i < length; i++ {
116 va, err := v.GetSliceIndexscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#GetSliceIndex().(i)
117 if err != nil {
118 return fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 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 *Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#) SliceIndexRangescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SliceIndexRange().(start, end int) (*Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#, error) {
131 l, err := v.SliceLenscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SliceLen().()
132 if err != nil {
133 return nil, fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 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 := NewSliceValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewSliceValue().()
144
145 if start > end {
146 for i := start; i >= end; i-- {
147 item, err := v.GetSliceIndexscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#GetSliceIndex().(i)
148 if err != nil {
149 return nil, fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("error getting slice index: %w", err)
150 }
151 if err := res.Appendscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#Append().(item); err != nil {
152 return nil, fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("error appending value to slice: %w", err)
153 }
154 }
155 } else {
156 for i := start; i <= end; i++ {
157 item, err := v.GetSliceIndexscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#GetSliceIndex().(i)
158 if err != nil {
159 return nil, fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("error getting slice index: %w", err)
160 }
161 if err := res.Appendscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#Append().(item); err != nil {
162 return nil, fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("error appending value to slice: %w", err)
163 }
164 }
165 }
166
167 return res, nil
168 }