Example #1
0
func (s *S) TestOneOf(c *C) {
	sch := schema.OneOf(schema.Const("foo"), schema.Const(42))

	out, err := sch.Coerce("foo", aPath)
	c.Assert(err, IsNil)
	c.Assert(out, Equals, "foo")

	out, err = sch.Coerce(42, aPath)
	c.Assert(err, IsNil)
	c.Assert(out, Equals, 42)

	out, err = sch.Coerce("bar", aPath)
	c.Assert(out, IsNil)
	c.Assert(err, ErrorMatches, `<path>: unexpected value "bar"`)
}
Example #2
0
func (s *S) TestStrictFieldMap(c *C) {
	fields := schema.Fields{
		"a": schema.Const("A"),
		"b": schema.Const("B"),
		"c": schema.Const("C"),
	}
	defaults := schema.Defaults{
		"b": schema.Omit,
		"c": "C",
	}
	sch := schema.StrictFieldMap(fields, defaults)
	assertFieldMap(c, sch)

	out, err := sch.Coerce(map[string]interface{}{"a": "A", "b": "B", "d": "D"}, aPath)
	c.Assert(out, IsNil)
	c.Assert(err, ErrorMatches, `<path>.d: expected nothing, got "D"`)
}
Example #3
0
func (s *S) TestSchemaMap(c *C) {
	fields1 := schema.FieldMap(schema.Fields{
		"type": schema.Const(1),
		"a":    schema.Const(2),
	}, nil)
	fields2 := schema.FieldMap(schema.Fields{
		"type": schema.Const(3),
		"b":    schema.Const(4),
	}, nil)
	sch := schema.FieldMapSet("type", []schema.Checker{fields1, fields2})

	out, err := sch.Coerce(map[string]int{"type": 1, "a": 2}, aPath)
	c.Assert(err, IsNil)
	c.Assert(out, DeepEquals, map[string]interface{}{"type": 1, "a": 2})

	out, err = sch.Coerce(map[string]int{"type": 3, "b": 4}, aPath)
	c.Assert(err, IsNil)
	c.Assert(out, DeepEquals, map[string]interface{}{"type": 3, "b": 4})

	out, err = sch.Coerce(map[string]int{}, aPath)
	c.Assert(out, IsNil)
	c.Assert(err, ErrorMatches, `<path>\.type: expected supported selector, got nothing`)

	out, err = sch.Coerce(map[string]int{"type": 2}, aPath)
	c.Assert(out, IsNil)
	c.Assert(err, ErrorMatches, `<path>\.type: expected supported selector, got 2`)

	out, err = sch.Coerce(map[string]int{"type": 3, "b": 5}, aPath)
	c.Assert(out, IsNil)
	c.Assert(err, ErrorMatches, `<path>\.b: expected 4, got 5`)

	out, err = sch.Coerce(42, aPath)
	c.Assert(out, IsNil)
	c.Assert(err, ErrorMatches, `<path>: expected map, got 42`)

	out, err = sch.Coerce(nil, aPath)
	c.Assert(out, IsNil)
	c.Assert(err, ErrorMatches, `<path>: expected map, got nothing`)

	// First path entry shouldn't have dots in an error message.
	out, err = sch.Coerce(map[string]int{"a": 1}, nil)
	c.Assert(out, IsNil)
	c.Assert(err, ErrorMatches, `type: expected supported selector, got nothing`)
}
Example #4
0
func (s *S) TestFieldMapDefaultInvalid(c *C) {
	fields := schema.Fields{
		"a": schema.Const("A"),
	}
	defaults := schema.Defaults{
		"a": "B",
	}
	sch := schema.FieldMap(fields, defaults)
	_, err := sch.Coerce(map[string]interface{}{}, aPath)
	c.Assert(err, ErrorMatches, `<path>.a: expected "A", got "B"`)
}
Example #5
0
func (s *S) TestFieldMap(c *C) {
	fields := schema.Fields{
		"a": schema.Const("A"),
		"b": schema.Const("B"),
		"c": schema.Const("C"),
	}
	defaults := schema.Defaults{
		"b": schema.Omit,
		"c": "C",
	}
	sch := schema.FieldMap(fields, defaults)
	assertFieldMap(c, sch)

	out, err := sch.Coerce(map[string]interface{}{"a": "A", "b": "B", "d": "D"}, aPath)
	c.Assert(err, IsNil)
	c.Assert(out, DeepEquals, map[string]interface{}{"a": "A", "b": "B", "c": "C"})

	out, err = sch.Coerce(map[string]interface{}{"a": "A", "d": "D"}, aPath)
	c.Assert(err, IsNil)
	c.Assert(out, DeepEquals, map[string]interface{}{"a": "A", "c": "C"})
}
Example #6
0
func (s *S) TestConst(c *C) {
	sch := schema.Const("foo")

	out, err := sch.Coerce("foo", aPath)
	c.Assert(err, IsNil)
	c.Assert(out, Equals, "foo")

	out, err = sch.Coerce(42, aPath)
	c.Assert(out, IsNil)
	c.Assert(err, ErrorMatches, `<path>: expected "foo", got 42`)

	out, err = sch.Coerce(nil, aPath)
	c.Assert(out, IsNil)
	c.Assert(err, ErrorMatches, `<path>: expected "foo", got nothing`)
}
Example #7
0
	v, err = mapC.Coerce(v, path)
	if err != nil {
		return
	}
	m := v.(map[string]interface{})
	if _, ok := m["limit"]; !ok {
		m["limit"] = c.limit
	}
	return ifaceSchema.Coerce(m, path)
}

var ifaceSchema = schema.FieldMap(
	schema.Fields{
		"interface": schema.String(),
		"limit":     schema.OneOf(schema.Const(nil), schema.Int()),
		"scope":     schema.OneOf(schema.Const(string(ScopeGlobal)), schema.Const(string(ScopeContainer))),
		"optional":  schema.Bool(),
	},
	schema.Defaults{
		"scope":    string(ScopeGlobal),
		"optional": false,
	},
)

var charmSchema = schema.FieldMap(
	schema.Fields{
		"name":        schema.String(),
		"summary":     schema.String(),
		"description": schema.String(),
		"peers":       schema.StringMap(ifaceExpander(int64(1))),
Example #8
0
			out[k] = opt.Default
		}
	}
	return out, nil
}

var validTypes = map[string]reflect.Kind{
	"string":  reflect.String,
	"int":     reflect.Int64,
	"boolean": reflect.Bool,
	"float":   reflect.Float64,
}

var optionSchema = schema.FieldMap(
	schema.Fields{
		"type":        schema.OneOf(schema.Const("string"), schema.Const("int"), schema.Const("float"), schema.Const("boolean")),
		"default":     schema.OneOf(schema.String(), schema.Int(), schema.Float(), schema.Bool()),
		"description": schema.String(),
	},
	schema.Defaults{
		"default":     schema.Omit,
		"description": schema.Omit,
	},
)

var configSchema = schema.FieldMap(
	schema.Fields{
		"options": schema.StringMap(optionSchema),
	},
	nil,
)