Skip to content

Commit

Permalink
[feat] add an option for queryStr separator
Browse files Browse the repository at this point in the history
  • Loading branch information
ryotafuwa-dev committed Jul 22, 2024
1 parent 2234f28 commit 0ff4033
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 10 deletions.
24 changes: 16 additions & 8 deletions hcledit.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@ func RawVal(rawString string) *handler.RawVal {
func (h *HCLEditor) Create(queryStr string, value interface{}, opts ...Option) error {
defer h.reload()

opt := &option{}
opt := &option{
querySeparator: ".",
}
for _, optFunc := range opts {
optFunc(opt)
}

queries, err := query.Build(queryStr)
queries, err := query.Build(queryStr, opt.querySeparator)
if err != nil {
return err
}
Expand All @@ -78,12 +80,14 @@ func (h *HCLEditor) Create(queryStr string, value interface{}, opts ...Option) e
func (h *HCLEditor) Read(queryStr string, opts ...Option) (map[string]interface{}, error) {
defer h.reload()

opt := &option{}
opt := &option{
querySeparator: ".",
}
for _, optFunc := range opts {
optFunc(opt)
}

queries, err := query.Build(queryStr)
queries, err := query.Build(queryStr, opt.querySeparator)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -122,7 +126,9 @@ func (h *HCLEditor) Read(queryStr string, opts ...Option) (map[string]interface{
func (h *HCLEditor) Update(queryStr string, value interface{}, opts ...Option) error {
defer h.reload()

opt := &option{}
opt := &option{
querySeparator: ".",
}
for _, optFunc := range opts {
optFunc(opt)
}
Expand All @@ -139,7 +145,7 @@ func (h *HCLEditor) Update(queryStr string, value interface{}, opts ...Option) e
return fmt.Errorf("WithNewLine is not supported for Update")
}

queries, err := query.Build(queryStr)
queries, err := query.Build(queryStr, opt.querySeparator)
if err != nil {
return err
}
Expand All @@ -163,12 +169,14 @@ func (h *HCLEditor) Update(queryStr string, value interface{}, opts ...Option) e
func (h *HCLEditor) Delete(queryStr string, opts ...Option) error {
defer h.reload()

opt := &option{}
opt := &option{
querySeparator: ".",
}
for _, optFunc := range opts {
optFunc(opt)
}

queries, err := query.Build(queryStr)
queries, err := query.Build(queryStr, opt.querySeparator)
if err != nil {
return err
}
Expand Down
17 changes: 17 additions & 0 deletions hcledit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,23 @@ object = {
`,
},

"AttributeWithCommaInObject": {
input: `
object = {
attribute2 = "C"
}
`,
query: "object|attribute3.value",
opts: []hcledit.Option{hcledit.WithQuerySeparator("|")},
value: "D",
want: `
object = {
attribute2 = "C"
attribute3.value = "D"
}
`,
},

"Block": {
input: `
`,
Expand Down
4 changes: 2 additions & 2 deletions internal/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ func (c *queryWildcard) Key() string {
return "*"
}

func Build(src string) ([]Query, error) {
func Build(src, querySeparator string) ([]Query, error) {
var queries []Query
for _, q := range strings.Split(src, ".") {
for _, q := range strings.Split(src, querySeparator) {
if q == "*" {
queries = append(queries, &queryWildcard{})
} else {
Expand Down
8 changes: 8 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type option struct {
afterKey string
beforeNewline bool
readFallbackToRawString bool
querySeparator string
}

// Option configures specific behavior for specific HCLEditor operations.
Expand Down Expand Up @@ -40,3 +41,10 @@ func WithReadFallbackToRawString() Option {
opt.readFallbackToRawString = true
}
}

// This sets a separator for queryStr in HCLEditor funcs. The default separator is ".".
func WithQuerySeparator(chr string) Option {
return func(opt *option) {
opt.querySeparator = chr
}
}
15 changes: 15 additions & 0 deletions option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,21 @@ object = {
// Comment
attribute = "str"
}
`,
},

"CreateWithQuerySeparator": {
input: `
object = {
}
`,
exec: func(editor *hcledit.HCLEditor) error {
return editor.Create("object/attribute", "str", hcledit.WithQuerySeparator("/"))
},
want: `
object = {
attribute = "str"
}
`,
},
}
Expand Down

0 comments on commit 0ff4033

Please sign in to comment.