diff --git a/opts.go b/opts.go index 2508b42..7dae85e 100644 --- a/opts.go +++ b/opts.go @@ -54,6 +54,8 @@ type options struct { parsed valueCache activeFields *fieldSet + + ignoreCommas bool } type valueCache map[string]spliceValue @@ -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`. diff --git a/reify_test.go b/reify_test.go index cd12c6a..9d85f09 100644 --- a/reify_test.go +++ b/reify_test.go @@ -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(), diff --git a/types.go b/types.go index b0b8e92..949f910 100644 --- a/types.go +++ b/types.go @@ -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