Skip to content

Commit

Permalink
Add flag to ignore commas when parsing. (#192)
Browse files Browse the repository at this point in the history
* Add flag to ignore commas when parsing.

The new flag stops the parser from building arrays automatically when
top level elements are separated with commas. This flag converts parsing
into an actual no-op, passing through the input unmodified. This mode is
necessary when parsing values that may legitimately contain special
characters, like secrets.

* Update changelog.
* Update copyright year.
* Move changelog to a release ahead of tagging.
  • Loading branch information
cmacknz authored May 11, 2022
1 parent fc880ab commit afabcdb
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 37 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Fixed

## [0.8.5]

### Added
- Added new parse.Config flag to disable using commas to imply arrays. #192

### Changed
- The parse.NoopConfig disables using commas to imply arrays by default. #192

## [0.8.4]

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion NOTICE.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright 2016-2021 Elasticsearch BV
Copyright 2016-2022 Elasticsearch BV

This product includes software developed by The Apache Software
Foundation (http://www.apache.org/).
Expand Down
20 changes: 17 additions & 3 deletions parse/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,17 @@ import (

// Config allows enabling and disabling parser features.
type Config struct {
Array bool
Object bool
// Enables parsing arrays from values enclosed in [].
Array bool
// Enables parsing objects enclosed in {}.
Object bool
// Enables parsing double quoted strings, where values are escaped.
StringDQuote bool
// Enables parsing single quotes strings, where no values are escaped.
StringSQuote bool
// Enables ignoring commas as a shortcut for building arrays: a,b parses to [a,b].
// The comma array syntax is enabled by default for backwards compatibility.
IgnoreCommas bool
}

// DefaultConfig is the default config with all parser features enabled.
Expand All @@ -55,6 +62,7 @@ var NoopConfig = Config{
Object: false,
StringDQuote: false,
StringSQuote: false,
IgnoreCommas: true,
}

type flagParser struct {
Expand Down Expand Up @@ -125,7 +133,13 @@ func (p *flagParser) parse() (interface{}, error) {
var values []interface{}

for {
v, err := p.parseValue(toplevelStopSet)
// Enable building arrays when commas separate top level elements by default.
stopSet := toplevelStopSet
if p.cfg.IgnoreCommas {
stopSet = ""
}

v, err := p.parseValue(stopSet)
if err != nil {
return nil, err
}
Expand Down
42 changes: 9 additions & 33 deletions parse/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -722,60 +722,36 @@ func TestFlagValueParsingWithNoop(t *testing.T) {
{`[]`, `[]`},
{
`a,b,c`,
[]interface{}{"a", "b", "c"},
`a,b,c`,
},
{
`C:\Windows\Path1,C:\Windows\Path2`,
[]interface{}{
`C:\Windows\Path1`,
`C:\Windows\Path2`,
},
`C:\Windows\Path1,C:\Windows\Path2`,
},
{
`[array, 1, true, "abc"]`,
[]interface{}{"[array", uint64(1), true, `"abc"]`},
`[array, 1, true, "abc"]`,
},
{
`[test, [1,2,3], on]`,
[]interface{}{
"[test",
"[1",
uint64(2),
"3]",
"on]",
},
`[test, [1,2,3], on]`,
},
{
`[host1:1234, host2:1234]`,
[]interface{}{
"[host1:1234",
"host2:1234]",
},
`[host1:1234, host2:1234]`,
},

// test dictionaries:
{`{}`, `{}`},
{`{'key1': true,
"key2": 1,
key 3: ['test', "test2", off],
nested key: {"a" : 2}}`,
[]interface{}{
`{'key1': true`,
`"key2": 1`,
`key 3: ['test'`,
`"test2"`,
`off]`,
`nested key: {"a" : 2}}`,
},
{
`{'key1': true, "key2": 1, key 3: ['test', "test2", off], nested key: {"a" : 2}}`,
`{'key1': true, "key2": 1, key 3: ['test', "test2", off], nested key: {"a" : 2}}`,
},

// array of top-level dictionaries
{
`{key: 1},{key: 2}`,
[]interface{}{
"{key: 1}",
"{key: 2}",
},
`{key: 1},{key: 2}`,
},
}

Expand Down

0 comments on commit afabcdb

Please sign in to comment.