Skip to content

Commit

Permalink
Add exported IgnoreCommas option to fix parser issues (#197)
Browse files Browse the repository at this point in the history
* attempt to fix weird parse issues

* notice

* add extra check to types
  • Loading branch information
fearful-symmetry authored Feb 15, 2024
1 parent db1ecc8 commit 1f9133c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
8 changes: 8 additions & 0 deletions opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ type options struct {
parsed valueCache

activeFields *fieldSet

ignoreCommas bool
}

type valueCache map[string]spliceValue
Expand Down Expand Up @@ -82,6 +84,12 @@ func StructTag(tag string) Option {
}
}

var IgnoreCommas = doIgnoreCommas

func doIgnoreCommas(o *options) {
o.ignoreCommas = true
}

// ValidatorTag option sets the struct tag name used to set validators
// on struct fields in `Unpack`.
// The default struct tag in `validate`.
Expand Down
38 changes: 38 additions & 0 deletions reify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,46 @@ import (

"github.com/elastic/go-ucfg/parse"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

var exampleInputWithExpVarAndComma = map[string]interface{}{
"condition": "startsWith('${host.name}','motmot')",
"data_stream": map[string]interface{}{
"dataset": "docker.container_logs",
},
}

func TestValueVarExpIgnoreCommas(t *testing.T) {
extendedCfg, err := NewFrom(exampleInputWithExpVarAndComma, VarExp)
require.NoError(t, err)

var test map[string]interface{}
err = extendedCfg.Unpack(&test, ResolveNOOP, IgnoreCommas) // IgnoreCommmas is what makes this not fail
require.NoError(t, err)
require.Equal(t, "startsWith('${host.name}','motmot')", test["condition"])
}

func TestValueUnpackVarExpFail(t *testing.T) {
extendedCfg, err := NewFrom(exampleInputWithExpVarAndComma, VarExp)
require.NoError(t, err)

var test map[string]interface{}
err = extendedCfg.Unpack(&test, ResolveNOOP) // will fail without IgnoreCommas
require.Error(t, err)
}

func TestValueUnpackNoExpVar(t *testing.T) {
extendedCfg, err := NewFrom(exampleInputWithExpVarAndComma) // will not fail, since no VarExp
require.NoError(t, err)

var test map[string]interface{}
err = extendedCfg.Unpack(&test, ResolveNOOP)
require.NoError(t, err)

require.Equal(t, "startsWith('${host.name}','motmot')", test["condition"])
}

func TestUnpackPrimitiveValues(t *testing.T) {
tests := []interface{}{
New(),
Expand Down
5 changes: 5 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,11 @@ func parseValue(p *cfgPrimitive, opts *options, str string, parseCfg parse.Confi
return nil, raiseNoParse(p.ctx, p.meta())
}

// only set IgnoreCommas if the default has been changed.
if opts.ignoreCommas {
parseCfg.IgnoreCommas = opts.ignoreCommas
}

ifc, err := parse.ValueWithConfig(str, parseCfg)
if err != nil {
return nil, err
Expand Down

0 comments on commit 1f9133c

Please sign in to comment.