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] return error if fallback occurred #107

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
3 changes: 1 addition & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
name: Lint

on:
pull_request_target:
Copy link
Member Author

Choose a reason for hiding this comment

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

This causes the lint to run against main rather than the branch HEAD. This means the previous behavior let unlinted code make it to main, but prevents CI from passing on the next PR.

types: [opened, reopened, synchronize]
pull_request:

jobs:
lint:
Expand Down
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 @@
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
13 changes: 10 additions & 3 deletions hcledit.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ func (h *HCLEditor) Read(queryStr string, opts ...Option) (map[string]interface{
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
ryan-ph marked this conversation as resolved.
Show resolved Hide resolved
}

// 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
2 changes: 1 addition & 1 deletion internal/handler/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
body.AppendUnstructuredTokens(
beforeTokens(
fmt.Sprintf("// %s", strings.TrimSpace(strings.TrimPrefix(h.comment, "//"))),
true,
true,

Check failure on line 33 in internal/handler/block.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] internal/handler/block.go#L33

File is not `gofmt`-ed with `-s` (gofmt)
Raw output
internal/handler/block.go:33: File is not `gofmt`-ed with `-s` (gofmt)
				true,	
),
)
}
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
Loading