Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[hcledit/read] fallback to raw values if unparsable #100

Merged
merged 3 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion hcledit.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (h *HCLEditor) Read(queryStr string, opts ...Option) (map[string]interface{
}

results := make(map[string]cty.Value)
hdlr, err := handler.NewReadHandler(results)
hdlr, err := handler.NewReadHandler(results, opt.readFallbackToRawString)
if err != nil {
return nil, err
}
Expand Down
45 changes: 41 additions & 4 deletions hcledit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,17 @@ object1 = {

func TestRead(t *testing.T) {
cases := map[string]struct {
input string
query string
want map[string]interface{}
input string
query string
options []hcledit.Option
want map[string]interface{}
}{
"Attribute": {
input: `
attribute = "R"
`,
query: "attribute",
options: make([]hcledit.Option, 0),
want: map[string]interface{}{
"attribute": "R",
},
Expand All @@ -188,6 +190,7 @@ block "label1" "label2" {
attribute = "str"
}
`,
options: make([]hcledit.Option, 0),
query: "block",
want: map[string]interface{}{},
},
Expand All @@ -198,6 +201,7 @@ block "label1" "label2" {
attribute = "R"
}
`,
options: make([]hcledit.Option, 0),
query: "block.label1.label2.attribute",
want: map[string]interface{}{
"block.label1.label2.attribute": "R",
Expand All @@ -212,6 +216,7 @@ block1 "label1" "label2" {
}
}
`,
options: make([]hcledit.Option, 0),
query: "block1.label1.label2.block2.label3.label4.attribute",
want: map[string]interface{}{
"block1.label1.label2.block2.label3.label4.attribute": "R",
Expand All @@ -229,6 +234,7 @@ block "label" "label2" {
}

`,
options: make([]hcledit.Option, 0),
query: "block.label.*.attribute",
want: map[string]interface{}{
"block.label.label1.attribute": "R",
Expand All @@ -242,6 +248,7 @@ object = {
attribute = "R"
}
`,
options: make([]hcledit.Option, 0),
query: "object.attribute",
want: map[string]interface{}{
"object.attribute": "R",
Expand All @@ -255,6 +262,7 @@ object1 = {
}
}
`,
options: make([]hcledit.Option, 0),
query: "object1.object2.attribute",
want: map[string]interface{}{
"object1.object2.attribute": "R",
Expand All @@ -265,6 +273,7 @@ object1 = {
input: `
attribute = 1
`,
options: make([]hcledit.Option, 0),
query: "attribute",
want: map[string]interface{}{
"attribute": 1,
Expand All @@ -275,6 +284,7 @@ attribute = 1
input: `
attribute = "str"
`,
options: make([]hcledit.Option, 0),
query: "attribute",
want: map[string]interface{}{
"attribute": "str",
Expand All @@ -285,6 +295,7 @@ attribute = "str"
input: `
attribute = true
`,
options: make([]hcledit.Option, 0),
query: "attribute",
want: map[string]interface{}{
"attribute": true,
Expand All @@ -295,6 +306,7 @@ attribute = true
input: `
attribute = false
`,
options: make([]hcledit.Option, 0),
query: "attribute",
want: map[string]interface{}{
"attribute": false,
Expand All @@ -305,6 +317,7 @@ attribute = false
input: `
attribute = ["str1", "str2", "str3"]
`,
options: make([]hcledit.Option, 0),
query: "attribute",
want: map[string]interface{}{
"attribute": []string{"str1", "str2", "str3"},
Expand All @@ -315,6 +328,7 @@ attribute = ["str1", "str2", "str3"]
input: `
attribute = [1, 2, 3]
`,
options: make([]hcledit.Option, 0),
query: "attribute",
want: map[string]interface{}{
"attribute": []int{1, 2, 3},
Expand All @@ -325,11 +339,34 @@ attribute = [1, 2, 3]
input: `
attribute = [true, false, true]
`,
options: make([]hcledit.Option, 0),
query: "attribute",
want: map[string]interface{}{
"attribute": []bool{true, false, true},
},
},

"fallback to absolute variable name": {
input: `
attribute = local.var
`,
options: []hcledit.Option{hcledit.WithReadFallbackToRawString()},
query: "attribute",
want: map[string]interface{}{
"attribute": "local.var",
},
},

"fallback to uninterpolated string": {
input: `
attribute = "some-${local.var}"
`,
options: []hcledit.Option{hcledit.WithReadFallbackToRawString()},
query: "attribute",
want: map[string]interface{}{
"attribute": `"some-${local.var}"`,
},
},
}

for name, tc := range cases {
Expand All @@ -340,7 +377,7 @@ attribute = [true, false, true]
t.Fatal(err)
}

got, err := editor.Read(tc.query)
got, err := editor.Read(tc.query, tc.options...)
if err != nil {
t.Fatal(err)
}
Expand Down
25 changes: 17 additions & 8 deletions internal/handler/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@ import (
)

type readHandler struct {
results map[string]cty.Value
results map[string]cty.Value
fallbackToRawString bool
}

func NewReadHandler(results map[string]cty.Value) (Handler, error) {
func NewReadHandler(results map[string]cty.Value, fallbackToRawString bool) (Handler, error) {
return &readHandler{
results: results,
results: results,
fallbackToRawString: fallbackToRawString,
}, nil
}

func (h *readHandler) HandleBody(body *hclwrite.Body, name string, keyTrail []string) error {
buf := body.GetAttribute(name).BuildTokens(nil).Bytes()
value, err := parse(buf, name)
value, err := parse(buf, name, h.fallbackToRawString)
if err != nil {
return err
}
Expand All @@ -33,24 +35,31 @@ func (h *readHandler) HandleBody(body *hclwrite.Body, name string, keyTrail []st

func (h *readHandler) HandleObject(object *ast.Object, name string, keyTrail []string) error {
buf := object.GetObjectAttribute(name).BuildTokens().Bytes()
value, err := parse(buf, name)
value, err := parse(buf, name, h.fallbackToRawString)
if err != nil {
return err
}
h.results[strings.Join(keyTrail, ".")] = value
return nil
}

func parse(buf []byte, name string) (cty.Value, error) {
func parse(buf []byte, name string, fallback bool) (cty.Value, error) {
file, diags := hclsyntax.ParseConfig(buf, "", hcl.Pos{Line: 1, Column: 1})
if diags.HasErrors() {
return cty.Value{}, diags
}

body := file.Body.(*hclsyntax.Body)
v, diags := body.Attributes[name].Expr.Value(nil)
expr := body.Attributes[name].Expr
v, diags := expr.Value(nil)
if diags.HasErrors() {
return cty.Value{}, diags
if !fallback {
return cty.Value{}, diags
}

// Could not parse the value with a nil EvalContext, so this is likely an
// interpolated string. Instead, attempt to parse the raw string value.
return cty.StringVal(string(expr.Range().SliceBytes(buf))), nil
}
return v, nil
}
13 changes: 10 additions & 3 deletions option.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package hcledit

type option struct {
comment string
afterKey string
beforeNewline bool
comment string
afterKey string
beforeNewline bool
readFallbackToRawString bool
}

// Option configures specific behavior for specific HCLEditor operations.
Expand All @@ -30,3 +31,9 @@ func WithNewLine() Option {
opt.beforeNewline = true
}
}

func WithReadFallbackToRawString() Option {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I generally agree with the code comment left above that we should probably split these into operation specific options, but I think that's out of scope for this PR.

return func(opt *option) {
opt.readFallbackToRawString = true
}
}
Loading