本文整理汇总了Golang中github.com/juju/schema.Int函数的典型用法代码示例。如果您正苦于以下问题:Golang Int函数的具体用法?Golang Int怎么用?Golang Int使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Int函数的19个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
}
开发者ID:bac,项目名称:juju,代码行数:25,代码来源:ports.go
示例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
}
开发者ID:bac,项目名称:juju,代码行数:50,代码来源:cloudimagemetadata.go
示例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
}
开发者ID:bac,项目名称:juju,代码行数:7,代码来源:serialization.go
示例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
}
开发者ID:bac,项目名称:juju,代码行数:35,代码来源:linklayerdevice.go
示例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
}
开发者ID:AlexisBruemmer,项目名称:juju,代码行数:29,代码来源:relation.go
示例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
}
开发者ID:bac,项目名称:juju,代码行数:31,代码来源:machine.go
示例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\)`)
}
开发者ID:howbazaar,项目名称:schema,代码行数:27,代码来源:schema_test.go
示例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
}
开发者ID:bac,项目名称:juju,代码行数:9,代码来源:serialization.go
示例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\)`)
}
开发者ID:howbazaar,项目名称:schema,代码行数:18,代码来源:schema_test.go
示例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"\)`)
}
开发者ID:bac,项目名称:juju,代码行数:44,代码来源:config_test.go
示例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
}
开发者ID:AlexisBruemmer,项目名称:juju,代码行数:39,代码来源:relation.go
示例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")
}
开发者ID:howbazaar,项目名称:schema,代码行数:23,代码来源:schema_test.go
示例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")
}
开发者ID:cmars,项目名称:oo,代码行数:36,代码来源:form.go
示例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
}
开发者ID:bac,项目名称:juju,代码行数:36,代码来源:subnet.go
示例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)
开发者ID:juju,项目名称:charm,代码行数:31,代码来源:config.go
示例16: importModelV1
func importModelV1(source map[string]interface{}) (*model, error) {
fields := schema.Fields{
"owner": schema.String(),
"config": schema.StringMap(schema.Any()),
"latest-tools": schema.String(),
"blocks": schema.StringMap(schema.String()),
"users": schema.StringMap(schema.Any()),
"machines": schema.StringMap(schema.Any()),
"services": schema.StringMap(schema.Any()),
"relations": schema.StringMap(schema.Any()),
"sequences": schema.StringMap(schema.Int()),
}
// Some values don't have to be there.
defaults := schema.Defaults{
"latest-tools": schema.Omit,
"blocks": schema.Omit,
}
addAnnotationSchema(fields, defaults)
addConstraintsSchema(fields, defaults)
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "model 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 := &model{
Version: 1,
Owner_: valid["owner"].(string),
Config_: valid["config"].(map[string]interface{}),
Sequences_: make(map[string]int),
Blocks_: convertToStringMap(valid["blocks"]),
}
result.importAnnotations(valid)
sequences := valid["sequences"].(map[string]interface{})
for key, value := range sequences {
result.SetSequence(key, int(value.(int64)))
}
if constraintsMap, ok := valid["constraints"]; ok {
constraints, err := importConstraints(constraintsMap.(map[string]interface{}))
if err != nil {
return nil, errors.Trace(err)
}
result.Constraints_ = constraints
}
if availableTools, ok := valid["latest-tools"]; ok {
num, err := version.Parse(availableTools.(string))
if err != nil {
return nil, errors.Trace(err)
}
result.LatestToolsVersion_ = num
}
userMap := valid["users"].(map[string]interface{})
users, err := importUsers(userMap)
if err != nil {
return nil, errors.Annotate(err, "users")
}
result.setUsers(users)
machineMap := valid["machines"].(map[string]interface{})
machines, err := importMachines(machineMap)
if err != nil {
return nil, errors.Annotate(err, "machines")
}
result.setMachines(machines)
serviceMap := valid["services"].(map[string]interface{})
services, err := importServices(serviceMap)
if err != nil {
return nil, errors.Annotate(err, "services")
}
result.setServices(services)
relationMap := valid["relations"].(map[string]interface{})
relations, err := importRelations(relationMap)
if err != nil {
return nil, errors.Annotate(err, "relations")
}
result.setRelations(relations)
return result, nil
}
开发者ID:AlexisBruemmer,项目名称:juju,代码行数:88,代码来源:model.go
示例17: importModelV1
func importModelV1(source map[string]interface{}) (*model, error) {
fields := schema.Fields{
"owner": schema.String(),
"cloud": schema.String(),
"cloud-region": schema.String(),
"config": schema.StringMap(schema.Any()),
"latest-tools": schema.String(),
"blocks": schema.StringMap(schema.String()),
"users": schema.StringMap(schema.Any()),
"machines": schema.StringMap(schema.Any()),
"applications": schema.StringMap(schema.Any()),
"relations": schema.StringMap(schema.Any()),
"ssh-host-keys": schema.StringMap(schema.Any()),
"cloud-image-metadata": schema.StringMap(schema.Any()),
"actions": schema.StringMap(schema.Any()),
"ip-addresses": schema.StringMap(schema.Any()),
"spaces": schema.StringMap(schema.Any()),
"subnets": schema.StringMap(schema.Any()),
"link-layer-devices": schema.StringMap(schema.Any()),
"volumes": schema.StringMap(schema.Any()),
"filesystems": schema.StringMap(schema.Any()),
"storages": schema.StringMap(schema.Any()),
"storage-pools": schema.StringMap(schema.Any()),
"sequences": schema.StringMap(schema.Int()),
}
// Some values don't have to be there.
defaults := schema.Defaults{
"latest-tools": schema.Omit,
"blocks": schema.Omit,
"cloud-region": schema.Omit,
}
addAnnotationSchema(fields, defaults)
addConstraintsSchema(fields, defaults)
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "model 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 := &model{
Version: 1,
Owner_: valid["owner"].(string),
Config_: valid["config"].(map[string]interface{}),
Sequences_: make(map[string]int),
Blocks_: convertToStringMap(valid["blocks"]),
Cloud_: valid["cloud"].(string),
}
result.importAnnotations(valid)
sequences := valid["sequences"].(map[string]interface{})
for key, value := range sequences {
result.SetSequence(key, int(value.(int64)))
}
if constraintsMap, ok := valid["constraints"]; ok {
constraints, err := importConstraints(constraintsMap.(map[string]interface{}))
if err != nil {
return nil, errors.Trace(err)
}
result.Constraints_ = constraints
}
if availableTools, ok := valid["latest-tools"]; ok {
num, err := version.Parse(availableTools.(string))
if err != nil {
return nil, errors.Trace(err)
}
result.LatestToolsVersion_ = num
}
if region, ok := valid["cloud-region"]; ok {
result.CloudRegion_ = region.(string)
}
if credential, ok := valid["cloud-credential"]; ok {
result.CloudCredential_ = credential.(string)
}
userMap := valid["users"].(map[string]interface{})
users, err := importUsers(userMap)
if err != nil {
return nil, errors.Annotate(err, "users")
}
result.setUsers(users)
machineMap := valid["machines"].(map[string]interface{})
machines, err := importMachines(machineMap)
if err != nil {
return nil, errors.Annotate(err, "machines")
}
result.setMachines(machines)
applicationMap := valid["applications"].(map[string]interface{})
applications, err := importApplications(applicationMap)
if err != nil {
return nil, errors.Annotate(err, "applications")
}
//.........这里部分代码省略.........
开发者ID:bac,项目名称:juju,代码行数:101,代码来源:model.go
示例18:
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))),
开发者ID:ktsakalozos,项目名称:charm,代码行数:30,代码来源:meta.go
示例19: importServiceV1
func importServiceV1(source map[string]interface{}) (*service, error) {
fields := schema.Fields{
"name": schema.String(),
"series": schema.String(),
"subordinate": schema.Bool(),
"charm-url": schema.String(),
"cs-channel": schema.String(),
"charm-mod-version": schema.Int(),
"force-charm": schema.Bool(),
"exposed": schema.Bool(),
"min-units": schema.Int(),
"status": schema.StringMap(schema.Any()),
"settings": schema.StringMap(schema.Any()),
"settings-refcount": schema.Int(),
"leader": schema.String(),
"leadership-settings": schema.StringMap(schema.Any()),
"metrics-creds": schema.String(),
"units": schema.StringMap(schema.Any()),
}
defaults := schema.Defaults{
"subordinate": false,
"force-charm": false,
"exposed": false,
"min-units": int64(0),
"leader": "",
"metrics-creds": "",
}
addAnnotationSchema(fields, defaults)
addConstraintsSchema(fields, defaults)
addStatusHistorySchema(fields)
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "service 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 := &service{
Name_: valid["name"].(string),
Series_: valid["series"].(string),
Subordinate_: valid["subordinate"].(bool),
CharmURL_: valid["charm-url"].(string),
Channel_: valid["cs-channel"].(string),
CharmModifiedVersion_: int(valid["charm-mod-version"].(int64)),
ForceCharm_: valid["force-charm"].(bool),
Exposed_: valid["exposed"].(bool),
MinUnits_: int(valid["min-units"].(int64)),
Settings_: valid["settings"].(map[string]interface{}),
SettingsRefCount_: int(valid["settings-refcount"].(int64)),
Leader_: valid["leader"].(string),
LeadershipSettings_: valid["leadership-settings"].(map[string]interface{}),
StatusHistory_: newStatusHistory(),
}
result.importAnnotations(valid)
if err := result.importStatusHistory(valid); err != nil {
return nil, errors.Trace(err)
}
if constraintsMap, ok := valid["constraints"]; ok {
constraints, err := importConstraints(constraintsMap.(map[string]interface{}))
if err != nil {
return nil, errors.Trace(err)
}
result.Constraints_ = constraints
}
encodedCreds := valid["metrics-creds"].(string)
// The model stores the creds encoded, but we want to make sure that
// we are storing something that can be decoded.
if _, err := base64.StdEncoding.DecodeString(encodedCreds); err != nil {
return nil, errors.Annotate(err, "metrics credentials not valid")
}
result.MetricsCredentials_ = encodedCreds
status, err := importStatus(valid["status"].(map[string]interface{}))
if err != nil {
return nil, errors.Trace(err)
}
result.Status_ = status
units, err := importUnits(valid["units"].(map[string]interface{}))
if err != nil {
return nil, errors.Trace(err)
}
result.setUnits(units)
return result, nil
}
开发者ID:AlexisBruemmer,项目名称:juju,代码行数:91,代码来源:service.go
注:本文中的github.com/juju/schema.Int函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论