本文整理汇总了Golang中github.com/juju/schema.Int函数的典型用法代码示例。如果您正苦于以下问题:Golang Int函数的具体用法?Golang Int怎么用?Golang Int使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Int函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: importPortRangeV1
func importPortRangeV1(source map[string]interface{}) (*portRange, error) {
fields := schema.Fields{
"unit-name": schema.String(),
"from-port": schema.Int(),
"to-port": schema.Int(),
"protocol": schema.String(),
}
checker := schema.FieldMap(fields, nil) // no defaults
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "port-range v1 schema check failed")
}
valid := coerced.(map[string]interface{})
// From here we know that the map returned from the schema coercion
// contains fields of the right type.
return &portRange{
UnitName_: valid["unit-name"].(string),
FromPort_: int(valid["from-port"].(int64)),
ToPort_: int(valid["to-port"].(int64)),
Protocol_: valid["protocol"].(string),
}, nil
}
示例2: importCloudImageMetadataV1
func importCloudImageMetadataV1(source map[string]interface{}) (*cloudimagemetadata, error) {
fields := schema.Fields{
"stream": schema.String(),
"region": schema.String(),
"version": schema.String(),
"series": schema.String(),
"arch": schema.String(),
"virt-type": schema.String(),
"root-storage-type": schema.String(),
"root-storage-size": schema.Uint(),
"date-created": schema.Int(),
"source": schema.String(),
"priority": schema.Int(),
"image-id": schema.String(),
}
// Some values don't have to be there.
defaults := schema.Defaults{
"root-storage-size": schema.Omit,
}
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "cloudimagemetadata v1 schema check failed")
}
valid := coerced.(map[string]interface{})
_, ok := valid["root-storage-size"]
var pointerSize *uint64
if ok {
rootStorageSize := valid["root-storage-size"].(uint64)
pointerSize = &rootStorageSize
}
cloudimagemetadata := &cloudimagemetadata{
Stream_: valid["stream"].(string),
Region_: valid["region"].(string),
Version_: valid["version"].(string),
Series_: valid["series"].(string),
Arch_: valid["arch"].(string),
VirtType_: valid["virt-type"].(string),
RootStorageType_: valid["root-storage-type"].(string),
RootStorageSize_: pointerSize,
DateCreated_: valid["date-created"].(int64),
Source_: valid["source"].(string),
Priority_: int(valid["priority"].(int64)),
ImageId_: valid["image-id"].(string),
}
return cloudimagemetadata, nil
}
示例3: versionedEmbeddedChecker
func versionedEmbeddedChecker(name string) schema.Checker {
fields := schema.Fields{
"version": schema.Int(),
}
fields[name] = schema.StringMap(schema.Any())
return schema.FieldMap(fields, nil) // no defaults
}
示例4: importLinkLayerDeviceV1
func importLinkLayerDeviceV1(source map[string]interface{}) (*linklayerdevice, error) {
fields := schema.Fields{
"provider-id": schema.String(),
"machine-id": schema.String(),
"name": schema.String(),
"mtu": schema.Int(),
"type": schema.String(),
"mac-address": schema.String(),
"is-autostart": schema.Bool(),
"is-up": schema.Bool(),
"parent-name": schema.String(),
}
// Some values don't have to be there.
defaults := schema.Defaults{
"provider-id": "",
}
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "linklayerdevice v1 schema check failed")
}
valid := coerced.(map[string]interface{})
return &linklayerdevice{
ProviderID_: valid["provider-id"].(string),
MachineID_: valid["machine-id"].(string),
Name_: valid["name"].(string),
MTU_: uint(valid["mtu"].(int64)),
Type_: valid["type"].(string),
MACAddress_: valid["mac-address"].(string),
IsAutoStart_: valid["is-autostart"].(bool),
IsUp_: valid["is-up"].(bool),
ParentName_: valid["parent-name"].(string),
}, nil
}
示例5: importRelationV1
func importRelationV1(source map[string]interface{}) (*relation, error) {
fields := schema.Fields{
"id": schema.Int(),
"key": schema.String(),
"endpoints": schema.StringMap(schema.Any()),
}
checker := schema.FieldMap(fields, nil) // no defaults
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "relation v1 schema check failed")
}
valid := coerced.(map[string]interface{})
// From here we know that the map returned from the schema coercion
// contains fields of the right type.
result := &relation{
Id_: int(valid["id"].(int64)),
Key_: valid["key"].(string),
}
endpoints, err := importEndpoints(valid["endpoints"].(map[string]interface{}))
if err != nil {
return nil, errors.Trace(err)
}
result.setEndpoints(endpoints)
return result, nil
}
示例6: importAgentToolsV1
func importAgentToolsV1(source map[string]interface{}) (*agentTools, error) {
fields := schema.Fields{
"tools-version": schema.String(),
"url": schema.String(),
"sha256": schema.String(),
"size": schema.Int(),
}
checker := schema.FieldMap(fields, nil) // no defaults
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "agentTools v1 schema check failed")
}
valid := coerced.(map[string]interface{})
// From here we know that the map returned from the schema coercion
// contains fields of the right type.
verString := valid["tools-version"].(string)
toolsVersion, err := version.ParseBinary(verString)
if err != nil {
return nil, errors.Annotatef(err, "agentTools tools-version")
}
return &agentTools{
Version_: 1,
ToolsVersion_: toolsVersion,
URL_: valid["url"].(string),
SHA256_: valid["sha256"].(string),
Size_: valid["size"].(int64),
}, nil
}
示例7: TestStringMap
func (s *S) TestStringMap(c *gc.C) {
s.sch = schema.StringMap(schema.Int())
out, err := s.sch.Coerce(map[string]interface{}{"a": 1, "b": int8(2)}, aPath)
c.Assert(err, gc.IsNil)
c.Assert(out, gc.DeepEquals, map[string]interface{}{"a": int64(1), "b": int64(2)})
out, err = s.sch.Coerce(42, aPath)
c.Assert(out, gc.IsNil)
c.Assert(err, gc.ErrorMatches, "<path>: expected map, got int\\(42\\)")
out, err = s.sch.Coerce(nil, aPath)
c.Assert(out, gc.IsNil)
c.Assert(err, gc.ErrorMatches, "<path>: expected map, got nothing")
out, err = s.sch.Coerce(map[int]int{1: 1}, aPath)
c.Assert(out, gc.IsNil)
c.Assert(err, gc.ErrorMatches, "<path>: expected string, got int\\(1\\)")
out, err = s.sch.Coerce(map[string]bool{"a": true}, aPath)
c.Assert(out, gc.IsNil)
c.Assert(err, gc.ErrorMatches, `<path>\.a: expected int, got bool\(true\)`)
// First path entry shouldn't have dots in an error message.
out, err = s.sch.Coerce(map[string]bool{"a": true}, nil)
c.Assert(out, gc.IsNil)
c.Assert(err, gc.ErrorMatches, `a: expected int, got bool\(true\)`)
}
示例8: versionedChecker
func versionedChecker(name string) schema.Checker {
fields := schema.Fields{
"version": schema.Int(),
}
if name != "" {
fields[name] = schema.List(schema.StringMap(schema.Any()))
}
return schema.FieldMap(fields, nil) // no defaults
}
示例9: TestList
func (s *S) TestList(c *gc.C) {
s.sch = schema.List(schema.Int())
out, err := s.sch.Coerce([]int8{1, 2}, aPath)
c.Assert(err, gc.IsNil)
c.Assert(out, gc.DeepEquals, []interface{}{int64(1), int64(2)})
out, err = s.sch.Coerce(42, aPath)
c.Assert(out, gc.IsNil)
c.Assert(err, gc.ErrorMatches, "<path>: expected list, got int\\(42\\)")
out, err = s.sch.Coerce(nil, aPath)
c.Assert(out, gc.IsNil)
c.Assert(err, gc.ErrorMatches, "<path>: expected list, got nothing")
out, err = s.sch.Coerce([]interface{}{1, true}, aPath)
c.Assert(out, gc.IsNil)
c.Assert(err, gc.ErrorMatches, `<path>\[1\]: expected int, got bool\(true\)`)
}
示例10: TestValidateUnknownAttrs
func (s *ConfigSuite) TestValidateUnknownAttrs(c *gc.C) {
s.addJujuFiles(c)
cfg, err := config.New(config.UseDefaults, map[string]interface{}{
"name": "myenv",
"type": "other",
"uuid": testing.ModelTag.Id(),
"known": "this",
"unknown": "that",
})
c.Assert(err, jc.ErrorIsNil)
// No fields: all attrs passed through.
attrs, err := cfg.ValidateUnknownAttrs(nil, nil)
c.Assert(err, jc.ErrorIsNil)
c.Assert(attrs, gc.DeepEquals, map[string]interface{}{
"known": "this",
"unknown": "that",
})
// Valid field: that and other attrs passed through.
fields := schema.Fields{"known": schema.String()}
attrs, err = cfg.ValidateUnknownAttrs(fields, nil)
c.Assert(err, jc.ErrorIsNil)
c.Assert(attrs, gc.DeepEquals, map[string]interface{}{
"known": "this",
"unknown": "that",
})
// Default field: inserted.
fields["default"] = schema.String()
defaults := schema.Defaults{"default": "the other"}
attrs, err = cfg.ValidateUnknownAttrs(fields, defaults)
c.Assert(err, jc.ErrorIsNil)
c.Assert(attrs, gc.DeepEquals, map[string]interface{}{
"known": "this",
"unknown": "that",
"default": "the other",
})
// Invalid field: failure.
fields["known"] = schema.Int()
_, err = cfg.ValidateUnknownAttrs(fields, defaults)
c.Assert(err, gc.ErrorMatches, `known: expected int, got string\("this"\)`)
}
示例11: importEndpointV1
func importEndpointV1(source map[string]interface{}) (*endpoint, error) {
fields := schema.Fields{
"service-name": schema.String(),
"name": schema.String(),
"role": schema.String(),
"interface": schema.String(),
"optional": schema.Bool(),
"limit": schema.Int(),
"scope": schema.String(),
"unit-settings": schema.StringMap(schema.StringMap(schema.Any())),
}
checker := schema.FieldMap(fields, nil) // No defaults.
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "endpoint v1 schema check failed")
}
valid := coerced.(map[string]interface{})
// From here we know that the map returned from the schema coercion
// contains fields of the right type.
result := &endpoint{
ServiceName_: valid["service-name"].(string),
Name_: valid["name"].(string),
Role_: valid["role"].(string),
Interface_: valid["interface"].(string),
Optional_: valid["optional"].(bool),
Limit_: int(valid["limit"].(int64)),
Scope_: valid["scope"].(string),
UnitSettings_: make(map[string]map[string]interface{}),
}
for unitname, settings := range valid["unit-settings"].(map[string]interface{}) {
result.UnitSettings_[unitname] = settings.(map[string]interface{})
}
return result, nil
}
示例12: TestInt
func (s *S) TestInt(c *gc.C) {
s.sch = schema.Int()
out, err := s.sch.Coerce(42, aPath)
c.Assert(err, gc.IsNil)
c.Assert(out, gc.Equals, int64(42))
out, err = s.sch.Coerce(int8(42), aPath)
c.Assert(err, gc.IsNil)
c.Assert(out, gc.Equals, int64(42))
out, err = s.sch.Coerce("42", aPath)
c.Assert(err, gc.IsNil)
c.Assert(out, gc.Equals, int64(42))
out, err = s.sch.Coerce(true, aPath)
c.Assert(out, gc.IsNil)
c.Assert(err, gc.ErrorMatches, `<path>: expected int, got bool\(true\)`)
out, err = s.sch.Coerce(nil, aPath)
c.Assert(out, gc.IsNil)
c.Assert(err, gc.ErrorMatches, "<path>: expected int, got nothing")
}
示例13: prompt
func (f *PromptingFiller) prompt(name string, attr environschema.Attr) (interface{}, error) {
prompter := f.Prompter
if prompter == nil {
prompter = DefaultPrompter
}
tries := f.MaxTries
if tries == 0 {
tries = 3
}
for i := 0; i < tries; i++ {
val, err := prompter.Prompt(name, attr)
if err != nil {
return nil, errgo.Notef(err, "cannot get input")
}
switch attr.Type {
case environschema.Tbool:
b, err := schema.Bool().Coerce(val, nil)
if err == nil {
return b, nil
}
case environschema.Tint:
i, err := schema.Int().Coerce(val, nil)
if err == nil {
return i, nil
}
case environschema.Tstring:
i, err := schema.String().Coerce(val, nil)
if err == nil {
return i, nil
}
default:
return nil, errgo.Newf("unsupported attribute type %q", attr.Type)
}
}
return nil, errgo.New("too many invalid inputs")
}
示例14: importSubnetV1
func importSubnetV1(source map[string]interface{}) (*subnet, error) {
fields := schema.Fields{
"cidr": schema.String(),
"provider-id": schema.String(),
"vlan-tag": schema.Int(),
"space-name": schema.String(),
"availability-zone": schema.String(),
"allocatable-ip-high": schema.String(),
"allocatable-ip-low": schema.String(),
}
defaults := schema.Defaults{
"provider-id": "",
"allocatable-ip-high": "",
"allocatable-ip-low": "",
}
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "subnet v1 schema check failed")
}
valid := coerced.(map[string]interface{})
// From here we know that the map returned from the schema coercion
// contains fields of the right type.
return &subnet{
CIDR_: valid["cidr"].(string),
ProviderId_: valid["provider-id"].(string),
VLANTag_: int(valid["vlan-tag"].(int64)),
SpaceName_: valid["space-name"].(string),
AvailabilityZone_: valid["availability-zone"].(string),
AllocatableIPHigh_: valid["allocatable-ip-high"].(string),
AllocatableIPLow_: valid["allocatable-ip-low"].(string),
}, nil
}
示例15: parse
if value == nil {
return nil, nil
}
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
}
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.
func (option Option) parse(name, str string) (_ interface{}, err error) {
defer option.error(&err, name, str)
switch option.Type {
case "string":
return str, nil
case "int":
return strconv.ParseInt(str, 10, 64)
case "float":
return strconv.ParseFloat(str, 64)