@@ -15,11 +15,115 @@ import (
15
15
"github.com/stretchr/testify/require"
16
16
17
17
"github.com/grafana/alloy/internal/runtime/equality"
18
+ "github.com/grafana/alloy/syntax"
18
19
"github.com/grafana/alloy/syntax/parser"
19
20
"github.com/grafana/alloy/syntax/token/builder"
20
21
"github.com/grafana/alloy/syntax/vm"
21
22
)
22
23
24
+ func TestUsingTargetCapsule (t * testing.T ) {
25
+ type testCase struct {
26
+ name string
27
+ inputTarget map [string ]string
28
+ expression string
29
+ decodeInto interface {}
30
+ decodedString string
31
+ expectedEvalError string
32
+ }
33
+
34
+ testCases := []testCase {
35
+ {
36
+ name : "target to map of string -> string" ,
37
+ inputTarget : map [string ]string {"a1a" : "beachfront avenue" , "ice" : "ice" },
38
+ expression : "t" ,
39
+ decodeInto : map [string ]string {},
40
+ decodedString : `{"a1a"="beachfront avenue", "ice"="ice"}` ,
41
+ },
42
+ {
43
+ name : "target to map of string -> any" ,
44
+ inputTarget : map [string ]string {"a1a" : "beachfront avenue" , "ice" : "ice" },
45
+ expression : "t" ,
46
+ decodeInto : map [string ]any {},
47
+ decodedString : `{"a1a"="beachfront avenue", "ice"="ice"}` ,
48
+ },
49
+ {
50
+ name : "target to map of any -> any" ,
51
+ inputTarget : map [string ]string {"a1a" : "beachfront avenue" , "ice" : "ice" },
52
+ expression : "t" ,
53
+ decodeInto : map [any ]any {},
54
+ decodedString : `{"a1a"="beachfront avenue", "ice"="ice"}` ,
55
+ },
56
+ {
57
+ name : "target to map of string -> syntax.Value" ,
58
+ inputTarget : map [string ]string {"a1a" : "beachfront avenue" },
59
+ expression : "t" ,
60
+ decodeInto : map [string ]syntax.Value {},
61
+ decodedString : `{"a1a"="beachfront avenue"}` ,
62
+ },
63
+ {
64
+ name : "target indexing a string value" ,
65
+ inputTarget : map [string ]string {"a1a" : "beachfront avenue" , "hip" : "hop" },
66
+ expression : `t["hip"]` ,
67
+ decodeInto : "" ,
68
+ decodedString : `hop` ,
69
+ },
70
+ {
71
+ name : "target indexing a non-existing string value" ,
72
+ inputTarget : map [string ]string {"a1a" : "beachfront avenue" , "hip" : "hop" },
73
+ expression : `t["boom"]` ,
74
+ decodeInto : "" ,
75
+ decodedString : "<nil>" ,
76
+ },
77
+ {
78
+ name : "target indexing a value like an object field" ,
79
+ inputTarget : map [string ]string {"a1a" : "beachfront avenue" , "hip" : "hop" },
80
+ expression : `t.boom` ,
81
+ decodeInto : "" ,
82
+ expectedEvalError : `field "boom" does not exist` ,
83
+ },
84
+ }
85
+ for _ , tc := range testCases {
86
+ t .Run (tc .name , func (t * testing.T ) {
87
+ target := NewTargetFromMap (tc .inputTarget )
88
+ scope := vm .NewScope (map [string ]interface {}{"t" : target })
89
+ expr , err := parser .ParseExpression (tc .expression )
90
+ require .NoError (t , err )
91
+ eval := vm .New (expr )
92
+ evalError := eval .Evaluate (scope , & tc .decodeInto )
93
+ if tc .expectedEvalError != "" {
94
+ require .ErrorContains (t , evalError , tc .expectedEvalError )
95
+ } else {
96
+ require .NoError (t , evalError )
97
+ }
98
+ require .Equal (t , tc .decodedString , fmt .Sprintf ("%v" , tc .decodeInto ))
99
+ })
100
+ }
101
+ }
102
+
103
+ func TestNestedIndexing (t * testing.T ) {
104
+ targets := []Target {
105
+ NewTargetFromMap (map [string ]string {"foo" : "bar" , "boom" : "bap" }),
106
+ NewTargetFromMap (map [string ]string {"hip" : "hop" , "dont" : "stop" }),
107
+ }
108
+ scope := vm .NewScope (map [string ]interface {}{"targets" : targets })
109
+
110
+ expr , err := parser .ParseExpression (`targets[1]["dont"]` )
111
+ require .NoError (t , err )
112
+ eval := vm .New (expr )
113
+ actual := ""
114
+ err = eval .Evaluate (scope , & actual )
115
+ require .NoError (t , err )
116
+ require .Equal (t , "stop" , actual )
117
+
118
+ expr , err = parser .ParseExpression (`targets[0].boom` )
119
+ require .NoError (t , err )
120
+ eval = vm .New (expr )
121
+ actual = ""
122
+ err = eval .Evaluate (scope , & actual )
123
+ require .NoError (t , err )
124
+ require .Equal (t , "bap" , actual )
125
+ }
126
+
23
127
func TestDecodeMap (t * testing.T ) {
24
128
type testCase struct {
25
129
name string
0 commit comments