Пример #1
0
func (s *S) TestFloat(c *C) {
	sch := schema.Float()

	out, err := sch.Coerce(float32(1.0), aPath)
	c.Assert(err, IsNil)
	c.Assert(out, Equals, float64(1.0))

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

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

	out, err = sch.Coerce(nil, aPath)
	c.Assert(out, IsNil)
	c.Assert(err, ErrorMatches, "<path>: expected float, got nothing")
}
Пример #2
0
		}
	}
	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,
)
Пример #3
0
	defer option.error(&err, name, value)
	if checker := optionTypeCheckers[option.Type]; checker != nil {
		if value, err = checker.Coerce(value, nil); err != nil {
			return nil, err
		} else if value == "" {
			value = nil
		}
		return value, nil
	}
	panic(fmt.Errorf("option %q has unknown type %q", name, option.Type))
}

var optionTypeCheckers = map[string]schema.Checker{
	"string":  schema.String(),
	"int":     schema.Int(),
	"float":   schema.Float(),
	"boolean": schema.Bool(),
}

// parse returns an appropriately-typed value for the supplied string, or
// returns an error if it cannot be parsed to the correct type. Empty
// string values are returned as nil.
func (option Option) parse(name, str string) (_ interface{}, err error) {
	if str == "" {
		return nil, nil
	}
	defer option.error(&err, name, str)
	switch option.Type {
	case "string":
		return str, nil
	case "int":