Skip to content

Commit

Permalink
[hcledit/read] return error if fallback occurred
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-ph committed Apr 24, 2024
1 parent 9104984 commit f62f2dc
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 48 deletions.
4 changes: 2 additions & 2 deletions cmd/hcledit/internal/command/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ func runRead(opts *ReadOptions, args []string) (string, error) {
return "", fmt.Errorf("failed to read file: %s", err)
}

readOpts:= []hcledit.Option{}
readOpts := []hcledit.Option{}

Check failure on line 53 in cmd/hcledit/internal/command/read.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] cmd/hcledit/internal/command/read.go#L53

File is not `gofmt`-ed with `-s` (gofmt)
Raw output
cmd/hcledit/internal/command/read.go:53: File is not `gofmt`-ed with `-s` (gofmt)
	readOpts:= []hcledit.Option{}
if opts.Fallback {
readOpts = append(readOpts, hcledit.WithReadFallbackToRawString())
}
results, err := editor.Read(query, readOpts...)
if err != nil {
if err != nil && !opts.Fallback {
return "", fmt.Errorf("failed to read file: %s", err)
}

Expand Down
15 changes: 11 additions & 4 deletions hcledit.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,10 @@ func (h *HCLEditor) Read(queryStr string, opts ...Option) (map[string]interface{
if err != nil {
return nil, err
}


fallback := opt.readFallbackToRawString
results := make(map[string]cty.Value)
hdlr, err := handler.NewReadHandler(results, opt.readFallbackToRawString)
hdlr, err := handler.NewReadHandler(results, fallback)
if err != nil {
return nil, err
}
Expand All @@ -99,11 +100,17 @@ func (h *HCLEditor) Read(queryStr string, opts ...Option) (map[string]interface{
Mode: walker.Read,
}

if err := w.Walk(h.writeFile.Body(), queries, 0, []string{}); err != nil {
err = w.Walk(h.writeFile.Body(), queries, 0, []string{})
if err != nil && !fallback {
return nil, err
}

return convert(results)
ret, convertErr := convert(results)
if convertErr != nil {
return ret, convertErr
}

return ret, err
}

// Update replaces attributes and blocks which matched with its key
Expand Down
93 changes: 55 additions & 38 deletions hcledit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,17 +205,19 @@ object1 = {

func TestRead(t *testing.T) {
cases := map[string]struct {
input string
query string
options []hcledit.Option
want map[string]interface{}
input string
query string
options []hcledit.Option
expectErr bool
want map[string]interface{}
}{
"Attribute": {
input: `
attribute = "R"
`,
query: "attribute",
options: make([]hcledit.Option, 0),
query: "attribute",
options: make([]hcledit.Option, 0),
expectErr: false,
want: map[string]interface{}{
"attribute": "R",
},
Expand All @@ -228,9 +230,10 @@ block "label1" "label2" {
attribute = "str"
}
`,
options: make([]hcledit.Option, 0),
query: "block",
want: map[string]interface{}{},
options: make([]hcledit.Option, 0),
expectErr: false,
query: "block",
want: map[string]interface{}{},
},

"AttributeInBlock1": {
Expand All @@ -239,8 +242,9 @@ block "label1" "label2" {
attribute = "R"
}
`,
options: make([]hcledit.Option, 0),
query: "block.label1.label2.attribute",
options: make([]hcledit.Option, 0),
expectErr: false,
query: "block.label1.label2.attribute",
want: map[string]interface{}{
"block.label1.label2.attribute": "R",
},
Expand All @@ -254,8 +258,9 @@ block1 "label1" "label2" {
}
}
`,
options: make([]hcledit.Option, 0),
query: "block1.label1.label2.block2.label3.label4.attribute",
options: make([]hcledit.Option, 0),
expectErr: false,
query: "block1.label1.label2.block2.label3.label4.attribute",
want: map[string]interface{}{
"block1.label1.label2.block2.label3.label4.attribute": "R",
},
Expand All @@ -272,8 +277,9 @@ block "label" "label2" {
}
`,
options: make([]hcledit.Option, 0),
query: "block.label.*.attribute",
options: make([]hcledit.Option, 0),
expectErr: false,
query: "block.label.*.attribute",
want: map[string]interface{}{
"block.label.label1.attribute": "R",
"block.label.label2.attribute": "R",
Expand All @@ -286,8 +292,9 @@ object = {
attribute = "R"
}
`,
options: make([]hcledit.Option, 0),
query: "object.attribute",
options: make([]hcledit.Option, 0),
expectErr: false,
query: "object.attribute",
want: map[string]interface{}{
"object.attribute": "R",
},
Expand All @@ -300,8 +307,9 @@ object1 = {
}
}
`,
options: make([]hcledit.Option, 0),
query: "object1.object2.attribute",
options: make([]hcledit.Option, 0),
expectErr: false,
query: "object1.object2.attribute",
want: map[string]interface{}{
"object1.object2.attribute": "R",
},
Expand All @@ -311,8 +319,9 @@ object1 = {
input: `
attribute = 1
`,
options: make([]hcledit.Option, 0),
query: "attribute",
options: make([]hcledit.Option, 0),
expectErr: false,
query: "attribute",
want: map[string]interface{}{
"attribute": 1,
},
Expand All @@ -322,8 +331,9 @@ attribute = 1
input: `
attribute = "str"
`,
options: make([]hcledit.Option, 0),
query: "attribute",
options: make([]hcledit.Option, 0),
expectErr: false,
query: "attribute",
want: map[string]interface{}{
"attribute": "str",
},
Expand All @@ -333,8 +343,9 @@ attribute = "str"
input: `
attribute = true
`,
options: make([]hcledit.Option, 0),
query: "attribute",
options: make([]hcledit.Option, 0),
expectErr: false,
query: "attribute",
want: map[string]interface{}{
"attribute": true,
},
Expand All @@ -344,8 +355,9 @@ attribute = true
input: `
attribute = false
`,
options: make([]hcledit.Option, 0),
query: "attribute",
options: make([]hcledit.Option, 0),
expectErr: false,
query: "attribute",
want: map[string]interface{}{
"attribute": false,
},
Expand All @@ -355,8 +367,9 @@ attribute = false
input: `
attribute = ["str1", "str2", "str3"]
`,
options: make([]hcledit.Option, 0),
query: "attribute",
options: make([]hcledit.Option, 0),
expectErr: false,
query: "attribute",
want: map[string]interface{}{
"attribute": []string{"str1", "str2", "str3"},
},
Expand All @@ -366,8 +379,9 @@ attribute = ["str1", "str2", "str3"]
input: `
attribute = [1, 2, 3]
`,
options: make([]hcledit.Option, 0),
query: "attribute",
options: make([]hcledit.Option, 0),
expectErr: false,
query: "attribute",
want: map[string]interface{}{
"attribute": []int{1, 2, 3},
},
Expand All @@ -377,8 +391,9 @@ attribute = [1, 2, 3]
input: `
attribute = [true, false, true]
`,
options: make([]hcledit.Option, 0),
query: "attribute",
options: make([]hcledit.Option, 0),
expectErr: false,
query: "attribute",
want: map[string]interface{}{
"attribute": []bool{true, false, true},
},
Expand All @@ -388,8 +403,9 @@ attribute = [true, false, true]
input: `
attribute = local.var
`,
options: []hcledit.Option{hcledit.WithReadFallbackToRawString()},
query: "attribute",
options: []hcledit.Option{hcledit.WithReadFallbackToRawString()},
expectErr: true,
query: "attribute",
want: map[string]interface{}{
"attribute": "local.var",
},
Expand All @@ -399,8 +415,9 @@ attribute = local.var
input: `
attribute = "some-${local.var}"
`,
options: []hcledit.Option{hcledit.WithReadFallbackToRawString()},
query: "attribute",
options: []hcledit.Option{hcledit.WithReadFallbackToRawString()},
expectErr: true,
query: "attribute",
want: map[string]interface{}{
"attribute": `"some-${local.var}"`,
},
Expand All @@ -416,7 +433,7 @@ attribute = "some-${local.var}"
}

got, err := editor.Read(tc.query, tc.options...)
if err != nil {
if !tc.expectErr && err != nil {
t.Fatal(err)
}

Expand Down
9 changes: 5 additions & 4 deletions internal/handler/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ func NewReadHandler(results map[string]cty.Value, fallbackToRawString bool) (Han

func (h *readHandler) HandleBody(body *hclwrite.Body, name string, keyTrail []string) error {
buf := body.GetAttribute(name).BuildTokens(nil).Bytes()
value, err := parse(buf, name, h.fallbackToRawString)
if err != nil {
fallback := h.fallbackToRawString
value, err := parse(buf, name, fallback)
if err != nil && !fallback {
return err
}
h.results[strings.Join(keyTrail, ".")] = value
return nil
return err
}

func (h *readHandler) HandleObject(object *ast.Object, name string, keyTrail []string) error {
Expand Down Expand Up @@ -59,7 +60,7 @@ func parse(buf []byte, name string, fallback bool) (cty.Value, error) {

// 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 cty.StringVal(string(expr.Range().SliceBytes(buf))), diags
}
return v, nil
}
3 changes: 3 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ func WithNewLine() Option {
}
}

// This provides a fallback to return the raw string of the value if we could
// not parse it. If this option is provided to HCLEditor.Read(), the error
// return value will signal fallback occurred.
func WithReadFallbackToRawString() Option {
return func(opt *option) {
opt.readFallbackToRawString = true
Expand Down

0 comments on commit f62f2dc

Please sign in to comment.