Пример #1
0
// Configuration returns the configuration payload
// of the passed event or an empty configuration.
func Configuration(event cells.Event) configuration.Configuration {
	payload, ok := event.Payload().Get(ConfigurationPayload)
	if !ok {
		logger.Warningf("event does not contain configuration payload")
		config, _ := configuration.ReadString("{config}")
		return config
	}
	config, ok := payload.(configuration.Configuration)
	if !ok {
		logger.Warningf("configuration payload has illegal type")
		config, _ := configuration.ReadString("{config}")
		return config
	}
	return config
}
Пример #2
0
// TestGetDefault tests the retrieval of values with defaults.
func TestGetDefault(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)

	source := "{config {a Hello}}"
	config, err := configuration.ReadString(source)
	assert.Nil(err)

	vs := config.At("a").GetDefault("default")
	assert.Equal(vs, "Hello")

	vs = config.At("foo").GetDefault("default")
	assert.Equal(vs, "default")
	vb := config.At("foo").GetBoolDefault(true)
	assert.True(vb)
	vi := config.At("foo").GetIntDefault(42)
	assert.Equal(vi, 42)
	vf := config.At("foo").GetFloat64Default(47.11)
	assert.Equal(vf, 47.11)
	now := time.Now()
	vt := config.At("foo").GetTimeDefault(now)
	assert.Equal(vt, now)
	dur := 5 * time.Second
	vd := config.At("foo").GetDurationDefault(dur)
	assert.Equal(vd, dur)
}
Пример #3
0
// TestApply tests the applying of values.
func TestApply(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)

	source := "{config {a Hello}{sub {a World}}}"
	config, err := configuration.ReadString(source)
	assert.Nil(err)

	applied, err := config.Apply(map[string]string{
		"sub/a": "Tester",
		"b":     "42",
	})
	assert.Nil(err)
	vs, err := applied.At("sub", "a").Get()
	assert.Nil(err)
	assert.Equal(vs, "Tester")
	vi, err := applied.At("b").GetInt()
	assert.Nil(err)
	assert.Equal(vi, 42)

}
Пример #4
0
// TestList tests the listing of configuration keys.
func TestList(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)

	source := `{config {a 1}{b 2}{c 3}{sub {a 4.1}{b 4.2}}}`
	config, err := configuration.ReadString(source)
	assert.Nil(err)

	keys, err := config.At().List()
	assert.Nil(err)
	assert.Length(keys, 4)
	assert.Equal(keys, []string{"a", "b", "c", "sub"})

	keys, err = config.At("sub").List()
	assert.Nil(err)
	assert.Length(keys, 2)
	assert.Equal(keys, []string{"a", "b"})

	keys, err = config.At("sub", "a").List()
	assert.Nil(err)
	assert.Length(keys, 0)

	_, err = config.At("x").List()
	assert.ErrorMatch(err, `.* invalid configuration path "/config/x"`)
}