MCPcopy
hub / github.com/TomWright/dasel / Add

Method Add

model/value_math.go:9–66  ·  view source on GitHub ↗

Add adds two values together.

(other *Value)

Source from the content-addressed store, hash-verified

7
8// Add adds two values together.
9func (v *Value) Add(other *Value) (*Value, error) {
10 if v.IsInt() && other.IsInt() {
11 a, err := v.IntValue()
12 if err != nil {
13 return nil, err
14 }
15 b, err := other.IntValue()
16 if err != nil {
17 return nil, err
18 }
19 return NewValue(a + b), nil
20 }
21 if v.IsFloat() && other.IsFloat() {
22 a, err := v.FloatValue()
23 if err != nil {
24 return nil, err
25 }
26 b, err := other.FloatValue()
27 if err != nil {
28 return nil, err
29 }
30 return NewValue(a + b), nil
31 }
32 if v.IsInt() && other.IsFloat() {
33 a, err := v.IntValue()
34 if err != nil {
35 return nil, err
36 }
37 b, err := other.FloatValue()
38 if err != nil {
39 return nil, err
40 }
41 return NewValue(float64(a) + b), nil
42 }
43 if v.IsFloat() && other.IsInt() {
44 a, err := v.FloatValue()
45 if err != nil {
46 return nil, err
47 }
48 b, err := other.IntValue()
49 if err != nil {
50 return nil, err
51 }
52 return NewValue(a + float64(b)), nil
53 }
54 if v.IsString() && other.IsString() {
55 a, err := v.StringValue()
56 if err != nil {
57 return nil, err
58 }
59 b, err := other.StringValue()
60 if err != nil {
61 return nil, err
62 }
63 return NewValue(a + b), nil
64 }
65 return nil, fmt.Errorf("could not add: %w", ErrIncompatibleTypes{A: v, B: other})
66}

Callers 2

TestValue_AddFunction · 0.80
initFunction · 0.80

Calls 7

IsIntMethod · 0.95
IntValueMethod · 0.95
IsFloatMethod · 0.95
FloatValueMethod · 0.95
IsStringMethod · 0.95
StringValueMethod · 0.95
NewValueFunction · 0.85

Tested by 1

TestValue_AddFunction · 0.64