Skip to content

Commit 29a6255

Browse files
Expression tag value.
1 parent 486066a commit 29a6255

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

_tests/basic-expr.hcl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
String = bar

hclencoder_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ func TestEncoder(t *testing.T) {
3737
},
3838
Output: "basic",
3939
},
40+
{
41+
ID: "basic struct with expression",
42+
Input: struct {
43+
String string `hcl:",expr"`
44+
}{
45+
"bar",
46+
},
47+
Output: "basic-expr",
48+
},
4049
{
4150
ID: "labels changed",
4251
Input: struct {

nodes.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ const (
3232
// a list.
3333
Blocks string = "blocks"
3434

35+
// Expression indicates that this field should not be quoted.
36+
Expression string = "expr"
37+
3538
// UnusedKeysTag is a flag that indicates any unused keys found by the
3639
// decoder are stored in this field of type []string. This has the same
3740
// behavior as the OmitTag and is not encoded.
@@ -61,6 +64,7 @@ type fieldMeta struct {
6164
key bool
6265
squash bool
6366
repeatBlock bool
67+
expression bool
6468
unusedKeys bool
6569
decodedFields bool
6670
omit bool
@@ -83,7 +87,7 @@ func encodeField(in reflect.Value, meta fieldMeta) (node ast.Node, key []*ast.Ob
8387
case reflect.Bool, reflect.Float64, reflect.String,
8488
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
8589
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
86-
return encodePrimitive(in)
90+
return encodePrimitive(in, meta.expression)
8791

8892
case reflect.Slice:
8993
return encodeList(in, meta.repeatBlock)
@@ -101,8 +105,8 @@ func encodeField(in reflect.Value, meta fieldMeta) (node ast.Node, key []*ast.Ob
101105

102106
// encodePrimitive converts a primitive value into an ast.LiteralType. An
103107
// ast.ObjectKey is never returned.
104-
func encodePrimitive(in reflect.Value) (ast.Node, []*ast.ObjectKey, error) {
105-
tkn, err := tokenize(in, false)
108+
func encodePrimitive(in reflect.Value, expr bool) (ast.Node, []*ast.ObjectKey, error) {
109+
tkn, err := tokenize(in, expr)
106110
if err != nil {
107111
return nil, nil, err
108112
}
@@ -328,7 +332,7 @@ func encodeStruct(in reflect.Value) (ast.Node, []*ast.ObjectKey, error) {
328332

329333
// tokenize converts a primitive type into an token.Token. IDENT tokens (unquoted strings)
330334
// can be optionally triggered for any string types.
331-
func tokenize(in reflect.Value, ident bool) (t token.Token, err error) {
335+
func tokenize(in reflect.Value, unquotedString bool) (t token.Token, err error) {
332336
switch in.Kind() {
333337
case reflect.Bool:
334338
return token.Token{
@@ -355,7 +359,7 @@ func tokenize(in reflect.Value, ident bool) (t token.Token, err error) {
355359
}, nil
356360

357361
case reflect.String:
358-
if ident {
362+
if unquotedString {
359363
return token.Token{
360364
Type: token.IDENT,
361365
Text: in.String(),
@@ -397,6 +401,8 @@ func extractFieldMeta(f reflect.StructField) (meta fieldMeta) {
397401
meta.unusedKeys = true
398402
case Blocks:
399403
meta.repeatBlock = true
404+
case Expression:
405+
meta.expression = true
400406
}
401407
}
402408
}

nodes_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,9 @@ func TestEncodePrimitive(t *testing.T) {
120120
},
121121
}
122122

123-
RunAll(tests, encodePrimitive, t)
123+
RunAll(tests, func(value reflect.Value) (ast.Node, []*ast.ObjectKey, error) {
124+
return encodePrimitive(value, false)
125+
}, t)
124126
}
125127

126128
func TestEncodeList(t *testing.T) {

0 commit comments

Comments
 (0)