本文整理汇总了Golang中github.com/juju/schema.FieldMap函数的典型用法代码示例。如果您正苦于以下问题:Golang FieldMap函数的具体用法?Golang FieldMap怎么用?Golang FieldMap使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了FieldMap函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: parseAllocateConstraintsResponse
func parseAllocateConstraintsResponse(source interface{}, machine *machine) (ConstraintMatches, error) {
var empty ConstraintMatches
matchFields := schema.Fields{
"storage": schema.StringMap(schema.ForceInt()),
"interfaces": schema.StringMap(schema.ForceInt()),
}
matchDefaults := schema.Defaults{
"storage": schema.Omit,
"interfaces": schema.Omit,
}
fields := schema.Fields{
"constraints_by_type": schema.FieldMap(matchFields, matchDefaults),
}
checker := schema.FieldMap(fields, nil) // no defaults
coerced, err := checker.Coerce(source, nil)
if err != nil {
return empty, WrapWithDeserializationError(err, "allocation constraints response schema check failed")
}
valid := coerced.(map[string]interface{})
constraintsMap := valid["constraints_by_type"].(map[string]interface{})
result := ConstraintMatches{
Interfaces: make(map[string]Interface),
Storage: make(map[string]BlockDevice),
}
if interfaceMatches, found := constraintsMap["interfaces"]; found {
for label, value := range interfaceMatches.(map[string]interface{}) {
id := value.(int)
iface := machine.Interface(id)
if iface == nil {
return empty, NewDeserializationError("constraint match interface %q: %d does not match an interface for the machine", label, id)
}
result.Interfaces[label] = iface
}
}
if storageMatches, found := constraintsMap["storage"]; found {
for label, value := range storageMatches.(map[string]interface{}) {
id := value.(int)
blockDevice := machine.PhysicalBlockDevice(id)
if blockDevice == nil {
return empty, NewDeserializationError("constraint match storage %q: %d does not match a physical block device for the machine", label, id)
}
result.Storage[label] = blockDevice
}
}
return result, nil
}
示例2: 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
}
示例3: importStorageV1
func importStorageV1(source map[string]interface{}) (*storage, error) {
fields := schema.Fields{
"id": schema.String(),
"kind": schema.String(),
"owner": schema.String(),
"name": schema.String(),
"attachments": schema.List(schema.String()),
}
// Normally a list would have defaults, but in this case storage
// should always have at least one attachment.
checker := schema.FieldMap(fields, nil) // no defaults
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "storage 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 := &storage{
ID_: valid["id"].(string),
Kind_: valid["kind"].(string),
Owner_: valid["owner"].(string),
Name_: valid["name"].(string),
Attachments_: convertToStringSlice(valid["attachments"]),
}
return result, nil
}
示例4: importPayloadV1
func importPayloadV1(source map[string]interface{}) (*payload, error) {
fields := schema.Fields{
"name": schema.String(),
"type": schema.String(),
"raw-id": schema.String(),
"state": schema.String(),
"labels": schema.List(schema.String()),
}
// Some values don't have to be there.
defaults := schema.Defaults{
"labels": schema.Omit,
}
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "payload v1 schema check failed")
}
valid := coerced.(map[string]interface{})
return &payload{
Name_: valid["name"].(string),
Type_: valid["type"].(string),
RawID_: valid["raw-id"].(string),
State_: valid["state"].(string),
Labels_: convertToStringSlice(valid["labels"]),
}, nil
}
示例5: 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
}
示例6: importSpaceV1
func importSpaceV1(source map[string]interface{}) (*space, error) {
fields := schema.Fields{
"name": schema.String(),
"public": schema.Bool(),
"provider-id": 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, "space 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 &space{
Name_: valid["name"].(string),
Public_: valid["public"].(bool),
ProviderID_: valid["provider-id"].(string),
}, nil
}
示例7: importAddressV1
func importAddressV1(source map[string]interface{}) (*address, error) {
fields := schema.Fields{
"value": schema.String(),
"type": schema.String(),
"scope": schema.String(),
"origin": schema.String(),
}
// Some values don't have to be there.
defaults := schema.Defaults{
"scope": "",
"origin": "",
}
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "address 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 &address{
Version: 1,
Value_: valid["value"].(string),
Type_: valid["type"].(string),
Scope_: valid["scope"].(string),
Origin_: valid["origin"].(string),
}, nil
}
示例8: 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
}
示例9: importFilesystemAttachmentV1
func importFilesystemAttachmentV1(source map[string]interface{}) (*filesystemAttachment, error) {
fields := schema.Fields{
"machine-id": schema.String(),
"provisioned": schema.Bool(),
"read-only": schema.Bool(),
"mount-point": schema.String(),
}
defaults := schema.Defaults{
"mount-point": "",
}
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "filesystemAttachment 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 := &filesystemAttachment{
MachineID_: valid["machine-id"].(string),
Provisioned_: valid["provisioned"].(bool),
ReadOnly_: valid["read-only"].(bool),
MountPoint_: valid["mount-point"].(string),
}
return result, nil
}
示例10: space_2_0
func space_2_0(source map[string]interface{}) (*space, error) {
fields := schema.Fields{
"resource_uri": schema.String(),
"id": schema.ForceInt(),
"name": schema.String(),
"subnets": schema.List(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, "space 2.0 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.
subnets, err := readSubnetList(valid["subnets"].([]interface{}), subnet_2_0)
if err != nil {
return nil, errors.Trace(err)
}
result := &space{
resourceURI: valid["resource_uri"].(string),
id: valid["id"].(int),
name: valid["name"].(string),
subnets: subnets,
}
return result, nil
}
示例11: importStatusV1
func importStatusV1(source map[string]interface{}) (StatusPoint_, error) {
fields := schema.Fields{
"value": schema.String(),
"message": schema.String(),
"data": schema.StringMap(schema.Any()),
"updated": schema.Time(),
}
// Some values don't have to be there.
defaults := schema.Defaults{
"message": "",
"data": schema.Omit,
}
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return StatusPoint_{}, errors.Annotatef(err, "status 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.
var data map[string]interface{}
if sourceData, set := valid["data"]; set {
data = sourceData.(map[string]interface{})
}
return StatusPoint_{
Value_: valid["value"].(string),
Message_: valid["message"].(string),
Data_: data,
Updated_: valid["updated"].(time.Time),
}, nil
}
示例12: bootResource_2_0
func bootResource_2_0(source map[string]interface{}) (*bootResource, error) {
fields := schema.Fields{
"resource_uri": schema.String(),
"id": schema.ForceInt(),
"name": schema.String(),
"type": schema.String(),
"architecture": schema.String(),
"subarches": schema.String(),
"kflavor": schema.String(),
}
checker := schema.FieldMap(fields, nil) // no defaults
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, WrapWithDeserializationError(err, "boot resource 2.0 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 := &bootResource{
resourceURI: valid["resource_uri"].(string),
id: valid["id"].(int),
name: valid["name"].(string),
type_: valid["type"].(string),
architecture: valid["architecture"].(string),
subArches: valid["subarches"].(string),
kernelFlavor: valid["kflavor"].(string),
}
return result, nil
}
示例13: TestFieldMapBasic
func (s *S) TestFieldMapBasic(c *gc.C) {
fields := schema.Fields{
"a": schema.Const("A"),
"b": schema.Const("B"),
"c": schema.Const("C"),
}
defaults := schema.Defaults{
"b": schema.Omit,
"c": "C",
}
s.sch = schema.FieldMap(fields, defaults)
assertFieldMap(c, s.sch)
out, err := s.sch.Coerce(map[string]interface{}{"a": "A", "b": "B", "d": "D"}, aPath)
c.Assert(err, gc.IsNil)
c.Assert(out, gc.DeepEquals, map[string]interface{}{"a": "A", "b": "B", "c": "C"})
out, err = s.sch.Coerce(map[string]interface{}{"a": "A", "d": "D"}, aPath)
c.Assert(err, gc.IsNil)
c.Assert(out, gc.DeepEquals, map[string]interface{}{"a": "A", "c": "C"})
out, err = s.sch.Coerce(123, aPath)
c.Assert(err, gc.ErrorMatches, `<path>: expected map, got int\(123\)`)
c.Assert(out, gc.Equals, nil)
out, err = s.sch.Coerce(map[int]string{}, aPath)
c.Assert(err, gc.ErrorMatches, `<path>: expected map\[string], got map\[int]string\(map\[int]string{}\)`)
c.Assert(out, gc.Equals, nil)
type strKey string
out, err = s.sch.Coerce(map[strKey]string{"a": "A"}, aPath)
c.Assert(err, gc.ErrorMatches, `<path>: expected map\[string], got map\[schema_test\.strKey]string\(map\[schema_test.strKey]string{"a":"A"}\)`)
c.Assert(out, gc.Equals, nil)
}
示例14: importOpenedPortsV1
func importOpenedPortsV1(source map[string]interface{}) (*openedPorts, error) {
fields := schema.Fields{
"subnet-id": schema.String(),
"opened-ports": 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, "opened-ports 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.
ports, err := importPortRanges(valid["opened-ports"].(map[string]interface{}))
if err != nil {
return nil, errors.Trace(err)
}
result := &openedPorts{
SubnetID_: valid["subnet-id"].(string),
}
result.setOpenedPorts(ports)
return result, nil
}
示例15: file_2_0
func file_2_0(source map[string]interface{}) (*file, error) {
fields := schema.Fields{
"resource_uri": schema.String(),
"filename": schema.String(),
"anon_resource_uri": schema.String(),
"content": schema.String(),
}
defaults := schema.Defaults{
"content": "",
}
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, WrapWithDeserializationError(err, "file 2.0 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.
anonURI, err := url.ParseRequestURI(valid["anon_resource_uri"].(string))
if err != nil {
return nil, NewUnexpectedError(err)
}
result := &file{
resourceURI: valid["resource_uri"].(string),
filename: valid["filename"].(string),
anonymousURI: anonURI,
content: valid["content"].(string),
}
return result, nil
}