Debian Codegraph @

model/value_literal.go

raw symbols SCIP
1 package model
2
3 import (
4 "reflect"
5 "slices"
6 )
7
8 func newPtr() reflect.Value {
9 return reflect.New(reflect.TypeFor[any]())
10 }
11
12 // NewNullValue creates a new Value with a nil value.
13 func NewNullValue() *Value {
14 return NewValue(newPtr())
15 }
16
17 // IsNull returns true if the value is null.
18 func (v *Value) IsNull() bool {
19 return v.UnpackKinds(reflect.Pointer, reflect.Interface).isNull()
20 }
21
22 func (v *Value) isNull() bool {
23 // This logic can be cleaned up.
24 unpacked, err := v.UnpackUntilKinds(reflect.Chan, reflect.Func, reflect.Map, reflect.Pointer, reflect.UnsafePointer, reflect.Interface, reflect.Slice)
25 if err != nil {
26 return false
27 }
28 return unpacked.value.IsNil()
29 }
30
31 // NewStringValue creates a new Value with a string value.
32 func NewStringValue(x string) *Value {
33 res := newPtr()
34 res.Elem().Set(reflect.ValueOf(x))
35 return NewValue(res)
36 }
37
38 // IsString returns true if the value is a string.
39 func (v *Value) IsString() bool {
40 return v.UnpackKinds(reflect.Pointer, reflect.Interface).isString()
41 }
42
43 func (v *Value) isString() bool {
44 return v.value.Kind() == reflect.String
45 }
46
47 // StringValue returns the string value of the Value.
48 func (v *Value) StringValue() (string, error) {
49 unpacked := v.UnpackKinds(reflect.Pointer, reflect.Interface)
50 if !unpacked.isString() {
51 return "", ErrUnexpectedType{
52 Expected: TypeString,
53 Actual: v.Type(),
54 }
55 }
56 return unpacked.value.String(), nil
57 }
58
59 // StringLen returns the length of the string.
60 func (v *Value) StringLen() (int, error) {
61 val, err := v.StringValue()
62 if err != nil {
63 return 0, err
64 }
65 return len(val), nil
66 }
67
68 // StringIndexRange returns a new string containing the values between the start and end indexes.
69 // Comparable to go's string[start:end].
70 func (v *Value) StringIndexRange(start, end int) (*Value, error) {
71 strVal, err := v.StringValue()
72 if err != nil {
73 return nil, err
74 }
75
76 inBytes := []rune(strVal)
77 l := len(inBytes)
78
79 if start < 0 {
80 start = l + start
81 }
82 if end < 0 {
83 end = l + end
84 }
85
86 resBytes := make([]rune, 0)
87
88 if start > end {
89 for i := start; i >= end; i-- {
90 resBytes = append(resBytes, inBytes[i])
91 }
92 } else {
93 for i := start; i <= end; i++ {
94 resBytes = append(resBytes, inBytes[i])
95 }
96 }
97
98 res := string(resBytes)
99
100 return NewStringValue(res), nil
101 }
102
103 // NewIntValue creates a new Value with an int value.
104 func NewIntValue(x int64) *Value {
105 res := newPtr()
106 res.Elem().Set(reflect.ValueOf(x))
107 return NewValue(res)
108 }
109
110 // IsInt returns true if the value is an int.
111 func (v *Value) IsInt() bool {
112 return v.UnpackKinds(reflect.Pointer, reflect.Interface).isInt()
113 }
114
115 func (v *Value) isInt() bool {
116 return slices.Contains([]reflect.Kind{reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64}, v.value.Kind())
117 }
118
119 // IntValue returns the int value of the Value.
120 func (v *Value) IntValue() (int64, error) {
121 unpacked := v.UnpackKinds(reflect.Pointer, reflect.Interface)
122 if !unpacked.isInt() {
123 return 0, ErrUnexpectedType{
124 Expected: TypeInt,
125 Actual: v.Type(),
126 }
127 }
128 return unpacked.value.Int(), nil
129 }
130
131 // NewFloatValue creates a new Value with a float value.
132 func NewFloatValue(x float64) *Value {
133 res := newPtr()
134 res.Elem().Set(reflect.ValueOf(x))
135 return NewValue(res)
136 }
137
138 // IsFloat returns true if the value is a float.
139 func (v *Value) IsFloat() bool {
140 return v.UnpackKinds(reflect.Pointer, reflect.Interface).isFloat()
141 }
142
143 func (v *Value) isFloat() bool {
144 return slices.Contains([]reflect.Kind{reflect.Float32, reflect.Float64}, v.value.Kind())
145 }
146
147 // FloatValue returns the float value of the Value.
148 func (v *Value) FloatValue() (float64, error) {
149 unpacked := v.UnpackKinds(reflect.Pointer, reflect.Interface)
150 if !unpacked.IsFloat() {
151 return 0, ErrUnexpectedType{
152 Expected: TypeFloat,
153 Actual: v.Type(),
154 }
155 }
156 return unpacked.value.Float(), nil
157 }
158
159 // NewBoolValue creates a new Value with a bool value.
160 func NewBoolValue(x bool) *Value {
161 res := newPtr()
162 res.Elem().Set(reflect.ValueOf(x))
163 return NewValue(res)
164 }
165
166 // IsBool returns true if the value is a bool.
167 func (v *Value) IsBool() bool {
168 return v.UnpackKinds(reflect.Pointer, reflect.Interface).isBool()
169 }
170
171 func (v *Value) isBool() bool {
172 return v.value.Kind() == reflect.Bool
173 }
174
175 // BoolValue returns the bool value of the Value.
176 func (v *Value) BoolValue() (bool, error) {
177 unpacked := v.UnpackKinds(reflect.Pointer, reflect.Interface)
178 if !unpacked.IsBool() {
179 return false, ErrUnexpectedType{
180 Expected: TypeBool,
181 Actual: v.Type(),
182 }
183 }
184 return unpacked.value.Bool(), nil
185 }