File tree Expand file tree Collapse file tree 2 files changed +48
-0
lines changed Expand file tree Collapse file tree 2 files changed +48
-0
lines changed Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ package gomplate
2
2
3
3
import (
4
4
"reflect"
5
+ "regexp"
5
6
6
7
"github.com/flanksource/gomplate/v3/funcs"
7
8
"github.com/flanksource/gomplate/v3/kubernetes"
@@ -38,6 +39,10 @@ func GetCelEnv(environment map[string]any) []cel.EnvOption {
38
39
//
39
40
// Reference: https://github.com/google/cel-spec/blob/master/doc/langdef.md
40
41
var celKeywords = map [string ]struct {}{
42
+ "true" : {},
43
+ "false" : {},
44
+ "null" : {},
45
+ "in" : {},
41
46
"as" : {},
42
47
"break" : {},
43
48
"const" : {},
@@ -57,8 +62,18 @@ var celKeywords = map[string]struct{}{
57
62
"while" : {},
58
63
}
59
64
65
+ var celIdentifierRegexp = regexp .MustCompile (`^[A-Za-z_][A-Za-z0-9_]*$` )
66
+
60
67
// IsCelKeyword returns true if the given key is a reserved word in Cel
61
68
func IsCelKeyword (key string ) bool {
62
69
_ , ok := celKeywords [key ]
63
70
return ok
64
71
}
72
+
73
+ func IsValidCELIdentifier (s string ) bool {
74
+ if len (s ) == 0 {
75
+ return false
76
+ }
77
+
78
+ return ! IsCelKeyword (s ) && celIdentifierRegexp .MatchString (s )
79
+ }
Original file line number Diff line number Diff line change
1
+ package gomplate
2
+
3
+ import (
4
+ "fmt"
5
+ "testing"
6
+
7
+ "github.com/onsi/gomega"
8
+ )
9
+
10
+ func TestIsValidCELIdentifier (t * testing.T ) {
11
+ testCases := []struct {
12
+ identifier string
13
+ valid bool
14
+ }{
15
+ {"variable" , true },
16
+ {"_var123" , true },
17
+ {"someVariable" , true },
18
+
19
+ {"123var" , false },
20
+ {"var-name" , false },
21
+ {"" , false },
22
+ {"var$" , false },
23
+ {"if" , false },
24
+ {"Σ_variable" , false },
25
+ }
26
+
27
+ g := gomega .NewWithT (t )
28
+
29
+ for i , tc := range testCases {
30
+ result := IsValidCELIdentifier (tc .identifier )
31
+ g .Expect (result ).To (gomega .Equal (tc .valid ), fmt .Sprintf ("%d %s" , i + 1 , tc .identifier ))
32
+ }
33
+ }
You can’t perform that action at this time.
0 commit comments