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

[feat] add an option for queryStr separator #129

Merged
merged 2 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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 string, querySeparator rune) ([]Query, error) {
var queries []Query
for _, q := range strings.Split(src, ".") {
for _, q := range strings.Split(src, string([]rune{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 rune
}

// 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(r rune) Option {
return func(opt *option) {
opt.querySeparator = r
}
}
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
Loading