Debian Codegraph @

model/value.go

raw symbols SCIP
1 package model
2
3 import (
4 "fmt"
5 "reflect"
6 "slices"
7 "strings"
8 )
9
10 type Type string
11
12 func (t Type) String() string {
13 return string(t)
14 }
15
16 const (
17 TypeString Type = "string"
18 TypeInt Type = "int"
19 TypeFloat Type = "float"
20 TypeBool Type = "bool"
21 TypeMap Type = "map"
22 TypeSlice Type = "array"
23 TypeUnknown Type = "unknown"
24 TypeNull Type = "null"
25 )
26
27 // KeyValue represents a key value pair.
28 type KeyValue struct {
29 Key string
30 Value *Value
31 }
32
33 // Values represents a list of values.
34 type Values []*Value
35
36 // ToSliceValue converts a list of values to a slice value.
37 func (v Values) ToSliceValue() (*Value, error) {
38 slice := NewSliceValue()
39 for _, val := range v {
40 if err := slice.Append(val); err != nil {
41 return nil, err
42 }
43 }
44 return slice, nil
45 }
46
47 // Value represents a value.
48 type Value struct {
49 value reflect.Value
50 Metadata map[string]any
51
52 setFn func(*Value) error
53 }
54
55 // String returns the value as a formatted string, along with type info.
56 func (v *Value) String() string {
57 return v.string(0)
58 }
59
60 func indentStr(indent int) string {
61 return strings.Repeat(" ", indent)
62 }
63
64 func (v *Value) string(indent int) string {
65 switch v.Type() {
66 case TypeString:
67 val, err := v.StringValue()
68 if err != nil {
69 panic(err)
70 }
71 return fmt.Sprintf("string{%s}", val)
72 case TypeInt:
73 val, err := v.IntValue()
74 if err != nil {
75 panic(err)
76 }
77 return fmt.Sprintf("int{%d}", val)
78 case TypeFloat:
79 val, err := v.FloatValue()
80 if err != nil {
81 panic(err)
82 }
83 return fmt.Sprintf("float(%g)", val)
84 case TypeBool:
85 val, err := v.BoolValue()
86 if err != nil {
87 panic(err)
88 }
89 return fmt.Sprintf("bool{%t}", val)
90 case TypeMap:
91 res := "{\n"
92 if err := v.RangeMap(func(k string, v *Value) error {
93 res += fmt.Sprintf("%s%s: %s,\n", indentStr(indent+1), k, v.string(indent+1))
94 return nil
95 }); err != nil {
96 panic(err)
97 }
98 return res + indentStr(indent) + "}"
99 case TypeSlice:
100 md := ""
101 if v.IsSpread() {
102 md = "spread, "
103 }
104 if v.IsBranch() {
105 md += "branch, "
106 }
107 res := fmt.Sprintf("array[%s]{\n", strings.TrimSuffix(md, ", "))
108 if err := v.RangeSlice(func(k int, v *Value) error {
109 res += fmt.Sprintf("%s%d: %s,\n", indentStr(indent+1), k, v.string(indent+1))
110 return nil
111 }); err != nil {
112 panic(err)
113 }
114 return res + indentStr(indent) + "}"
115 case TypeNull:
116 return indentStr(indent) + "null"
117 default:
118 return fmt.Sprintf("unknown[%s]", v.Interface())
119 }
120 }
121
122 // NewValue creates a new value.
123 func NewValue(v any) *Value {
124 switch val := v.(type) {
125 case *Value:
126 return NewNestedValue(val)
127 case reflect.Value:
128 return &Value{
129 value: val,
130 Metadata: nil,
131 }
132 case nil:
133 return NewNullValue()
134 default:
135 res := newPtr()
136 if v != nil {
137 res.Elem().Set(reflect.ValueOf(v))
138 }
139 return &Value{
140 value: res,
141 Metadata: nil,
142 }
143 }
144 }
145
146 // NewNestedValue creates a new nested value.
147 func NewNestedValue(v *Value) *Value {
148 return &Value{
149 value: reflect.ValueOf(v),
150 Metadata: nil,
151 }
152 }
153
154 func (v *Value) isDaselValue() bool {
155 cur := v.value
156 for cur.Kind() == reflect.Interface && !cur.IsNil() {
157 cur = cur.Elem()
158 }
159 return cur.Type() == reflect.TypeFor[*Value]()
160 }
161
162 func (v *Value) daselValue() (*Value, error) {
163 if v.isDaselValue() {
164 m, err := v.UnpackUntilType(reflect.TypeFor[*Value]())
165 if err != nil {
166 return nil, fmt.Errorf("error getting dasel value: %w", err)
167 }
168 return m.value.Interface().(*Value), nil
169 }
170 return nil, fmt.Errorf("value is not a dasel value")
171 }
172
173 // Interface returns the value as an interface.
174 func (v *Value) Interface() any {
175 if v.IsNull() {
176 return nil
177 }
178 return v.value.Interface()
179 }
180
181 // Kind returns the reflect kind of the value.
182 func (v *Value) Kind() reflect.Kind {
183 return v.value.Kind()
184 }
185
186 // UnpackKinds unpacks the reflect value until it no longer matches the given kinds.
187 func (v *Value) UnpackKinds(kinds ...reflect.Kind) *Value {
188 val := v
189 for val.isDaselValue() {
190 var err error
191 val, err = val.daselValue()
192 if err != nil {
193 panic(err)
194 }
195 }
196 res := val.value
197 for {
198 if !slices.Contains(kinds, res.Kind()) || res.IsNil() {
199 return NewValue(res)
200 }
201 res = res.Elem()
202 }
203 }
204
205 type ErrCouldNotUnpackToType struct {
206 Type reflect.Type
207 }
208
209 func (e ErrCouldNotUnpackToType) Error() string {
210 return fmt.Sprintf("could not unpack to type: %s", e.Type)
211 }
212
213 // UnpackUntilType unpacks the reflect value until it matches the given type.
214 func (v *Value) UnpackUntilType(t reflect.Type) (*Value, error) {
215 res := v.value
216 var daselValueType = reflect.TypeFor[*Value]()
217 for {
218 if res.Type() == t {
219 return NewValue(res), nil
220 }
221 if t != daselValueType && res.Type() == daselValueType {
222 m, err := v.daselValue()
223 if err != nil {
224 return nil, fmt.Errorf("error getting dasel value: %w", err)
225 }
226 res = m.value
227 continue
228
229 }
230 if res.Kind() == reflect.Interface || res.Kind() == reflect.Pointer && !res.IsNil() {
231 res = res.Elem()
232 continue
233 }
234 return nil, &ErrCouldNotUnpackToType{Type: t}
235 }
236 }
237
238 // UnpackUntilAddressable unpacks the reflect value until it is addressable.
239 func (v *Value) UnpackUntilAddressable() (*Value, error) {
240 res := v.value
241 for {
242 if res.CanAddr() {
243 return NewValue(res), nil
244 }
245 if res.Kind() == reflect.Interface || res.Kind() == reflect.Pointer && !res.IsNil() {
246 res = res.Elem()
247 continue
248 }
249 return nil, fmt.Errorf("could not unpack addressable value")
250 }
251 }
252
253 // UnpackUntilKind unpacks the reflect value until it matches the given kind.
254 func (v *Value) UnpackUntilKind(k reflect.Kind) (*Value, error) {
255 res := v.value
256 for {
257 if res.Kind() == k {
258 return NewValue(res), nil
259 }
260 if res.Kind() == reflect.Interface || res.Kind() == reflect.Pointer && !res.IsNil() {
261 res = res.Elem()
262 continue
263 }
264 return nil, fmt.Errorf("could not unpack to kind: %s", k)
265 }
266 }
267
268 // UnpackUntilKinds unpacks the reflect value until it matches the given kind.
269 func (v *Value) UnpackUntilKinds(kinds ...reflect.Kind) (*Value, error) {
270 res := v.value
271 for {
272 if slices.Contains(kinds, res.Kind()) {
273 return NewValue(res), nil
274 }
275 if res.Kind() == reflect.Interface || res.Kind() == reflect.Pointer && !res.IsNil() {
276 res = res.Elem()
277 continue
278 }
279 return nil, fmt.Errorf("could not unpack to kinds: %v", kinds)
280 }
281 }
282
283 // Type returns the type of the value.
284 func (v *Value) Type() Type {
285 switch {
286 case v.IsString():
287 return TypeString
288 case v.IsInt():
289 return TypeInt
290 case v.IsFloat():
291 return TypeFloat
292 case v.IsBool():
293 return TypeBool
294 case v.IsMap():
295 return TypeMap
296 case v.IsSlice():
297 return TypeSlice
298 case v.IsNull():
299 return TypeNull
300 default:
301 return TypeUnknown
302 }
303 }
304
305 func (v *Value) IsScalar() bool {
306 unpacked := v.UnpackKinds(reflect.Interface, reflect.Pointer)
307 kind := unpacked.Kind()
308
309 switch kind {
310 case reflect.String, reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16,
311 reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16,
312 reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64:
313 return true
314 default:
315 return unpacked.isNull()
316 }
317 }
318
319 // Len returns the length of the value.
320 func (v *Value) Len() (int, error) {
321 var l int
322 var err error
323
324 switch {
325 case v.IsSlice():
326 l, err = v.SliceLen()
327 case v.IsMap():
328 l, err = v.MapLen()
329 case v.IsString():
330 l, err = v.StringLen()
331 default:
332 err = ErrUnexpectedTypes{
333 Expected: []Type{TypeSlice, TypeMap, TypeString},
334 Actual: v.Type(),
335 }
336 }
337
338 if err != nil {
339 return l, err
340 }
341
342 return l, nil
343 }
344
345 func (v *Value) Copy() (*Value, error) {
346 switch v.Type() {
347 case TypeMap:
348 return v.MapCopy()
349 default:
350 return nil, fmt.Errorf("copy not supported for type: %s", v.Type())
351 }
352 }