diff --git a/hcledit.go b/hcledit.go index b048a20..c6ab425 100644 --- a/hcledit.go +++ b/hcledit.go @@ -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 } @@ -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 } @@ -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) } @@ -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 } @@ -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 } diff --git a/hcledit_test.go b/hcledit_test.go index 5ea9631..1f8588e 100644 --- a/hcledit_test.go +++ b/hcledit_test.go @@ -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: ` `, diff --git a/internal/query/query.go b/internal/query/query.go index d8db363..b1736b8 100644 --- a/internal/query/query.go +++ b/internal/query/query.go @@ -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 { diff --git a/option.go b/option.go index 878df6e..6af2b3b 100644 --- a/option.go +++ b/option.go @@ -5,6 +5,7 @@ type option struct { afterKey string beforeNewline bool readFallbackToRawString bool + querySeparator string } // Option configures specific behavior for specific HCLEditor operations. @@ -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 + } +} diff --git a/option_test.go b/option_test.go index 1a8a6fe..b16b1cd 100644 --- a/option_test.go +++ b/option_test.go @@ -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" +} `, }, }