Skip to content
This repository was archived by the owner on Aug 21, 2024. It is now read-only.

Commit f456903

Browse files
authored
Doc updates (#9)
1 parent 662f9e4 commit f456903

File tree

10 files changed

+285
-125
lines changed

10 files changed

+285
-125
lines changed

docs/SUMMARY.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
## Interpolation
1010

11+
* [encoding](interpolation/encoding.md)
1112
* [helpers](interpolation/helpers.md)
13+
* [lists](interpolation/lists.md)
1214
* [maps](interpolation/maps.md)
15+
* [math](interpolation/math.md)
1316
* [strings](interpolation/strings.md)

docs/interpolation/encoding.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Encoding
2+
3+
## base64enc
4+
5+
### Parameters
6+
7+
* *item* - `string`- The value to encode
8+
9+
### Returns
10+
11+
* `string` - The base64 encoded representation of *item*
12+
13+
### Examples
14+
15+
```dart
16+
${base64enc("foo")} // Zm9v
17+
```
18+
19+
---
20+
21+
## base64dec
22+
23+
### Parameters
24+
25+
* *encoding* - `string`- A base64 encoded value
26+
27+
### Returns
28+
29+
* `string` - The decoded representation of *encoding*
30+
31+
### Examples
32+
33+
```dart
34+
${base64dec("Zm9v")} // foo
35+
```
36+
37+
---
38+
39+
## jsonencode
40+
41+
### Parameters
42+
43+
* *item* - `list/map/string`- The value to encode
44+
45+
### Returns
46+
47+
* `string` - The json representation of the value
48+
49+
### Examples
50+
51+
```dart
52+
${jsonencode("foo")} // foo
53+
```
54+
55+
```dart
56+
${jsonencode(list("1", "2"))} // ["1", "2"]
57+
```

docs/interpolation/lists.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Lists
2+
3+
## list
4+
5+
### Parameters
6+
7+
* *value* - `any (variadic)` - The values that make up a list
8+
9+
### Returns
10+
11+
* `list` - The list made up of the arguments provided
12+
13+
### Examples
14+
15+
```dart
16+
${list("1", "2")} // ["1", "2"]
17+
```
18+
19+
---
20+
21+
## concat
22+
23+
### Parameters
24+
25+
* *lists* - `list (variadic)` - The lists to concat
26+
27+
### Returns
28+
29+
* `list` - The resulting list
30+
31+
### Examples
32+
33+
```dart
34+
${concat(list("1", "2"), list("3", "4"))} // ["1", "2", "3", "4"]
35+
```
36+
37+
---
38+
39+
## unique
40+
41+
### Parameters
42+
43+
* *list* - `list` - The lists to extract unique values from
44+
45+
### Returns
46+
47+
* `list` - The resulting list with the unique items
48+
49+
### Examples
50+
51+
```dart
52+
${unique(list("1", "2", "2", "1", "3"))} // ["3"]
53+
```
54+
55+
---
56+
57+
## join
58+
59+
### Parameters
60+
61+
* *sep* - `string` - The separator between each value
62+
* *values* - `list` - The list of values to join
63+
64+
### Returns
65+
66+
* `string` - The values joined by sep
67+
68+
### Examples
69+
70+
```dart
71+
${join(",", ["foo", "bar"])} // "foo,bar"
72+
```

docs/interpolation/math.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Math
2+
3+
## max
4+
5+
### Parameters
6+
7+
* *number* - `float (variadic)`- The values to find the max in
8+
9+
### Returns
10+
11+
* `float` - The largest number found
12+
13+
### Examples
14+
15+
```dart
16+
${max(2.1, 4.7, 4)} // 4.7
17+
```
18+
19+
---
20+
21+
## min
22+
23+
### Parameters
24+
25+
* *number* - `float (variadic)`- The values to find the min in
26+
27+
### Returns
28+
29+
* `float` - The smallest number found
30+
31+
### Examples
32+
33+
```dart
34+
${max(2.1, 4.7, 4)} // 2.1
35+
```

docs/interpolation/strings.md

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,25 +35,6 @@ ${upper("foo")} // FOO
3535

3636
---
3737

38-
## join
39-
40-
### Parameters
41-
42-
* *sep* - `string` - The separator between each value
43-
* *values* - `list` - The list of values to join
44-
45-
### Returns
46-
47-
* `string` - The values joined by sep
48-
49-
### Examples
50-
51-
```dart
52-
${join(",", ["foo", "bar"])} // "foo,bar"
53-
```
54-
55-
---
56-
5738
## split
5839

5940
### Parameters

interpolation/funcs.go

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,39 @@ import (
1010

1111
// CoreFunctions are the custom functions for interpolation
1212
var CoreFunctions = map[string]ast.Function{
13-
"lower": interpolationFuncLower(),
14-
"upper": interpolationFuncUpper(),
15-
"env": interpolationFuncEnv(),
16-
"join": interpolationFuncJoin(),
17-
"has": interpolationFuncHas(),
18-
"map": interpolationFuncMap(),
19-
"keys": interpolationFuncKeys(),
20-
"list": interpolationFuncList(),
21-
"concat": interpolationFuncConcat(),
22-
"replace": interpolationFuncReplace(),
23-
"max": interpolationFuncMax(),
24-
"min": interpolationFuncMin(),
25-
"contains": interpolationFuncContains(),
26-
"split": interpolationFuncSplit(),
27-
"length": interpolationFuncLength(),
13+
// helpers
14+
"env": interpolationFuncEnv(),
15+
"length": interpolationFuncLength(),
16+
17+
// strings
18+
"lower": interpolationFuncLower(),
19+
"upper": interpolationFuncUpper(),
20+
"replace": interpolationFuncReplace(),
21+
"contains": interpolationFuncContains(),
22+
"split": interpolationFuncSplit(),
23+
24+
// maps
25+
"has": interpolationFuncHas(),
26+
"map": interpolationFuncMap(),
27+
"keys": interpolationFuncKeys(),
28+
"merge": interpolationFuncMerge(),
29+
"pick": interpolationFuncPick(),
30+
"omit": interpolationFuncOmit(),
31+
32+
// lists
33+
"join": interpolationFuncJoin(),
34+
"list": interpolationFuncList(),
35+
"concat": interpolationFuncConcat(),
36+
"unique": interpolationFuncUnique(),
37+
38+
// math
39+
"max": interpolationFuncMax(),
40+
"min": interpolationFuncMin(),
41+
42+
// encoding
2843
"jsonencode": interpolationFuncJSONEncode(),
29-
"pick": interpolationFuncPick(),
30-
"omit": interpolationFuncOmit(),
31-
"unique": interpolationFuncUnique(),
44+
"base64enc": interpolationFuncBase64Encode(),
45+
"base64dec": interpolationFuncBase64Decode(),
3246
}
3347

3448
// interpolationFuncEnv will extract a variable out of the env

interpolation/lists.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package interpolation
22

33
import (
44
"fmt"
5+
"strings"
56

67
"reflect"
78

@@ -90,3 +91,26 @@ func interpolationFuncUnique() ast.Function {
9091
},
9192
}
9293
}
94+
95+
// interpolationFuncJoin will join together a list of values with the provided separator
96+
func interpolationFuncJoin() ast.Function {
97+
return ast.Function{
98+
ArgTypes: []ast.Type{ast.TypeString, ast.TypeList},
99+
ReturnType: ast.TypeString,
100+
Callback: func(inputs []interface{}) (interface{}, error) {
101+
var list []string
102+
103+
for _, arg := range inputs[1].([]ast.Variable) {
104+
if arg.Type != ast.TypeString {
105+
return nil, fmt.Errorf(
106+
"only works on string lists, this list contains elements of %s",
107+
arg.Type.Printable(),
108+
)
109+
}
110+
list = append(list, arg.Value.(string))
111+
}
112+
113+
return strings.Join(list, inputs[0].(string)), nil
114+
},
115+
}
116+
}

interpolation/lists_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,65 @@ func TestInterpolationFuncUnique(t *testing.T) {
122122
})
123123
}
124124
}
125+
126+
func TestInterpolationFuncJoin(t *testing.T) {
127+
testCases := []functionTestCase{
128+
{
129+
description: "Joins multiple values",
130+
text: `${join(",", i)}`,
131+
expectation: "Foo,Bar",
132+
vars: map[string]ast.Variable{
133+
"i": ast.Variable{
134+
Type: ast.TypeList,
135+
Value: []ast.Variable{
136+
{
137+
Type: ast.TypeString,
138+
Value: "Foo",
139+
},
140+
{
141+
Type: ast.TypeString,
142+
Value: "Bar",
143+
},
144+
},
145+
},
146+
},
147+
},
148+
{
149+
description: "Bad variable length fails",
150+
text: `${join(",")}`,
151+
expectation: "",
152+
evalError: true,
153+
},
154+
{
155+
description: "Bad parse",
156+
text: `${join(",", [4]}`,
157+
expectation: "",
158+
parseError: true,
159+
},
160+
{
161+
description: "Bad array item",
162+
text: `${join(",", i)}`,
163+
expectation: "Foo,Bar",
164+
vars: map[string]ast.Variable{
165+
"i": ast.Variable{
166+
Type: ast.TypeList,
167+
Value: []ast.Variable{
168+
{
169+
Type: ast.TypeInt,
170+
Value: 4,
171+
},
172+
},
173+
},
174+
},
175+
evalError: true,
176+
},
177+
}
178+
179+
joinTestFunc := testInterpolationFunc(keyFuncs{"join": interpolationFuncJoin})
180+
181+
for _, tc := range testCases {
182+
t.Run(tc.description, func(t *testing.T) {
183+
joinTestFunc(t, tc)
184+
})
185+
}
186+
}

interpolation/strings.go

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package interpolation
22

33
import (
4-
"fmt"
54
"strings"
65

76
"github.com/hashicorp/hil"
@@ -32,29 +31,6 @@ func interpolationFuncUpper() ast.Function {
3231
}
3332
}
3433

35-
// interpolationFuncJoin will join together a list of values with the provided separator
36-
func interpolationFuncJoin() ast.Function {
37-
return ast.Function{
38-
ArgTypes: []ast.Type{ast.TypeString, ast.TypeList},
39-
ReturnType: ast.TypeString,
40-
Callback: func(inputs []interface{}) (interface{}, error) {
41-
var list []string
42-
43-
for _, arg := range inputs[1].([]ast.Variable) {
44-
if arg.Type != ast.TypeString {
45-
return nil, fmt.Errorf(
46-
"only works on string lists, this list contains elements of %s",
47-
arg.Type.Printable(),
48-
)
49-
}
50-
list = append(list, arg.Value.(string))
51-
}
52-
53-
return strings.Join(list, inputs[0].(string)), nil
54-
},
55-
}
56-
}
57-
5834
// interpolationFuncSplit will split a string into substrings separated by a separator string.
5935
func interpolationFuncSplit() ast.Function {
6036
return ast.Function{

0 commit comments

Comments
 (0)