Skip to content

Commit

Permalink
Expression tag value.
Browse files Browse the repository at this point in the history
  • Loading branch information
goncalo-rodrigues committed Dec 5, 2021
1 parent 486066a commit 29a6255
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
1 change: 1 addition & 0 deletions _tests/basic-expr.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
String = bar
9 changes: 9 additions & 0 deletions hclencoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ func TestEncoder(t *testing.T) {
},
Output: "basic",
},
{
ID: "basic struct with expression",
Input: struct {
String string `hcl:",expr"`
}{
"bar",
},
Output: "basic-expr",
},
{
ID: "labels changed",
Input: struct {
Expand Down
16 changes: 11 additions & 5 deletions nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ const (
// a list.
Blocks string = "blocks"

// Expression indicates that this field should not be quoted.
Expression string = "expr"

// UnusedKeysTag is a flag that indicates any unused keys found by the
// decoder are stored in this field of type []string. This has the same
// behavior as the OmitTag and is not encoded.
Expand Down Expand Up @@ -61,6 +64,7 @@ type fieldMeta struct {
key bool
squash bool
repeatBlock bool
expression bool
unusedKeys bool
decodedFields bool
omit bool
Expand All @@ -83,7 +87,7 @@ func encodeField(in reflect.Value, meta fieldMeta) (node ast.Node, key []*ast.Ob
case reflect.Bool, reflect.Float64, reflect.String,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return encodePrimitive(in)
return encodePrimitive(in, meta.expression)

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

// encodePrimitive converts a primitive value into an ast.LiteralType. An
// ast.ObjectKey is never returned.
func encodePrimitive(in reflect.Value) (ast.Node, []*ast.ObjectKey, error) {
tkn, err := tokenize(in, false)
func encodePrimitive(in reflect.Value, expr bool) (ast.Node, []*ast.ObjectKey, error) {
tkn, err := tokenize(in, expr)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -328,7 +332,7 @@ func encodeStruct(in reflect.Value) (ast.Node, []*ast.ObjectKey, error) {

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

case reflect.String:
if ident {
if unquotedString {
return token.Token{
Type: token.IDENT,
Text: in.String(),
Expand Down Expand Up @@ -397,6 +401,8 @@ func extractFieldMeta(f reflect.StructField) (meta fieldMeta) {
meta.unusedKeys = true
case Blocks:
meta.repeatBlock = true
case Expression:
meta.expression = true
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion nodes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ func TestEncodePrimitive(t *testing.T) {
},
}

RunAll(tests, encodePrimitive, t)
RunAll(tests, func(value reflect.Value) (ast.Node, []*ast.ObjectKey, error) {
return encodePrimitive(value, false)
}, t)
}

func TestEncodeList(t *testing.T) {
Expand Down

0 comments on commit 29a6255

Please sign in to comment.