Skip to content

Commit

Permalink
Merge pull request #34 from mercari/dylan/withnewline
Browse files Browse the repository at this point in the history
add WithNewLine option
  • Loading branch information
tcnksm authored Feb 17, 2021
2 parents 8a293d8 + 7e79b0c commit 564e5d0
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 32 deletions.
6 changes: 4 additions & 2 deletions hcledit.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (h *hclEditImpl) Create(queryStr string, value interface{}, opts ...Option)
return err
}

hdlr, err := handler.New(value, opt.comment, opt.afterKey)
hdlr, err := handler.New(value, opt.comment, opt.afterKey, opt.beforeNewline)
if err != nil {
return err
}
Expand Down Expand Up @@ -133,14 +133,16 @@ func (h *hclEditImpl) Update(queryStr string, value interface{}, opts ...Option)
// the position of target attribute is also changed...
if opt.comment != "" {
return fmt.Errorf("WithComment is not supported for Update")
} else if opt.beforeNewline {
return fmt.Errorf("WithNewLine is not supported for Update")
}

queries, err := query.Build(queryStr)
if err != nil {
return err
}

hdlr, err := handler.New(value, opt.comment, opt.afterKey)
hdlr, err := handler.New(value, opt.comment, opt.afterKey, opt.beforeNewline)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/ast/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ func (o *Object) GetObjectAttribute(name string) *ObjectAtrribute {
return nil
}

func (o *Object) SetObjectAttributeRaw(name string, exprTokens, commentTokens hclwrite.Tokens) *ObjectAtrribute {
func (o *Object) SetObjectAttributeRaw(name string, exprTokens, beforeTokens hclwrite.Tokens) *ObjectAtrribute {
objAttr := o.GetObjectAttribute(name)
if objAttr != nil {
objAttr.exprTokens = exprTokens
} else {
objAttr = newObjectAttribute(name, exprTokens, commentTokens)
objAttr = newObjectAttribute(name, exprTokens, beforeTokens)
o.objectAtrributes = append(o.objectAtrributes, objAttr)
}
return objAttr
Expand Down
6 changes: 1 addition & 5 deletions internal/ast/object_attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ func (oa *ObjectAtrribute) BuildTokens() hclwrite.Tokens {
return tokens
}

func newObjectAttribute(name string, exprTokens, commentTokens hclwrite.Tokens) *ObjectAtrribute {
beforeTokens := hclwrite.Tokens{}
if len(commentTokens) != 0 {
beforeTokens = append(beforeTokens, commentTokens...)
}
func newObjectAttribute(name string, exprTokens, beforeTokens hclwrite.Tokens) *ObjectAtrribute {
beforeTokens = append(beforeTokens, hclwrite.Tokens{
{
Type: hclsyntax.TokenIdent,
Expand Down
16 changes: 8 additions & 8 deletions internal/handler/cty.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ import (
)

type ctyValueHandler struct {
exprTokens hclwrite.Tokens
commentTokens hclwrite.Tokens
exprTokens hclwrite.Tokens
beforeTokens hclwrite.Tokens

afterKey string
}

func newCtyValueHandler(value cty.Value, comment, afterKey string) (Handler, error) {
func newCtyValueHandler(value cty.Value, comment, afterKey string, beforeNewline bool) (Handler, error) {
return &ctyValueHandler{
exprTokens: hclwrite.NewExpressionLiteral(value).BuildTokens(nil),
commentTokens: commentTokens(comment),
exprTokens: hclwrite.NewExpressionLiteral(value).BuildTokens(nil),
beforeTokens: beforeTokens(comment, beforeNewline),

afterKey: afterKey,
}, nil
}

func (h *ctyValueHandler) HandleObject(object *ast.Object, name string, _ []string) error {
object.SetObjectAttributeRaw(name, h.exprTokens, h.commentTokens)
object.SetObjectAttributeRaw(name, h.exprTokens, h.beforeTokens)
if h.afterKey != "" {
object.UpdateObjectAttributeOrder(name, h.afterKey)
}
Expand All @@ -34,8 +34,8 @@ func (h *ctyValueHandler) HandleObject(object *ast.Object, name string, _ []stri
func (h *ctyValueHandler) HandleBody(body *hclwrite.Body, name string, _ []string) error {
body.SetAttributeRaw(name, h.exprTokens)

if len(h.commentTokens) > 0 {
tokens := body.GetAttribute(name).BuildTokens(h.commentTokens)
if len(h.beforeTokens) > 0 {
tokens := body.GetAttribute(name).BuildTokens(h.beforeTokens)
body.RemoveAttribute(name)
body.AppendUnstructuredTokens(tokens)
}
Expand Down
26 changes: 16 additions & 10 deletions internal/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Handler interface {
HandleObject(object *ast.Object, name string, keyTrail []string) error
}

func New(input interface{}, comment, afterKey string) (Handler, error) {
func New(input interface{}, comment, afterKey string, beforeNewline bool) (Handler, error) {
switch v := input.(type) {
case *BlockVal:
return newBlockHandler(v.Labels)
Expand All @@ -33,22 +33,28 @@ func New(input interface{}, comment, afterKey string) (Handler, error) {
return nil, fmt.Errorf("failed to convert cty value from go value: %s", err)
}

return newCtyValueHandler(ctyVal, comment, afterKey)
return newCtyValueHandler(ctyVal, comment, afterKey, beforeNewline)
}

func commentTokens(comment string) hclwrite.Tokens {
if comment == "" {
return hclwrite.Tokens{}
func beforeTokens(comment string, beforeNewline bool) hclwrite.Tokens {
result := hclwrite.Tokens{}
if beforeNewline {
result = append(result, &hclwrite.Token{
Type: hclsyntax.TokenNewline,
Bytes: []byte{'\n'},
})
}

return hclwrite.Tokens{
{
if comment != "" {
result = append(result, &hclwrite.Token{
Type: hclsyntax.TokenComment,
Bytes: []byte(comment),
},
{
})
result = append(result, &hclwrite.Token{
Type: hclsyntax.TokenNewline,
Bytes: []byte{'\n'},
},
})
}

return result
}
3 changes: 1 addition & 2 deletions internal/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ type queryKey struct {
key string
}

type queryWildcard struct {
}
type queryWildcard struct{}

func (c *queryKey) Match(target string) bool {
return target == c.key
Expand Down
12 changes: 10 additions & 2 deletions option.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package hcledit

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

type Option func(*option)
Expand All @@ -20,3 +21,10 @@ func WithAfter(key string) Option {
opt.afterKey = key
}
}

// WithNewLine
func WithNewLine() Option {
return func(opt *option) {
opt.beforeNewline = true
}
}
87 changes: 87 additions & 0 deletions option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,90 @@ block {
})
}
}

func TestWithNewLine(t *testing.T) {
cases := map[string]struct {
input string
exec func(editor hcledit.HCLEditor) error
want string
}{
"CreateAttribute": {
input: `
`,
exec: func(editor hcledit.HCLEditor) error {
return editor.Create("attribute", "str", hcledit.WithNewLine())
},
want: `
attribute = "str"
`,
},

"CreateAttributeInBlock": {
input: `
block "label1" {
}
`,
exec: func(editor hcledit.HCLEditor) error {
return editor.Create("block.label1.attribute", "str", hcledit.WithNewLine())
},
want: `
block "label1" {
attribute = "str"
}
`,
},

"CreateAttributeInObject": {
input: `
object = {
}
`,
exec: func(editor hcledit.HCLEditor) error {
return editor.Create("object.attribute", "str", hcledit.WithNewLine())
},
want: `
object = {
attribute = "str"
}
`,
},
"CreateWithComment": {
input: `
object = {
}
`,
exec: func(editor hcledit.HCLEditor) error {
return editor.Create("object.attribute", "str", hcledit.WithComment("// Comment"), hcledit.WithNewLine())
},
want: `
object = {
// Comment
attribute = "str"
}
`,
},
}

for name, tc := range cases {
tc := tc
t.Run(name, func(t *testing.T) {
editor, err := hcledit.Read(strings.NewReader(tc.input), "")
if err != nil {
t.Fatal(err)
}

if err := tc.exec(editor); err != nil {
t.Fatal(err)
}

got := string(editor.Bytes())
if got != tc.want {
t.Errorf("Update() mismatch:\ngot:%s\nwant:%s\n", got, tc.want)
}
})
}
}
2 changes: 1 addition & 1 deletion write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestOverWriteFile(t *testing.T) {
tempFile := filepath.Join(tempDir, "test.hcl")
if err := ioutil.WriteFile(tempFile, []byte(`
attribute1 = "str1"
`), 0600); err != nil {
`), 0o600); err != nil {
t.Fatal(err)
}

Expand Down

0 comments on commit 564e5d0

Please sign in to comment.