本文整理匯總了Golang中github.com/rackspace/rack/internal/github.com/mitchellh/mapstructure.Decode函數的典型用法代碼示例。如果您正苦於以下問題:Golang Decode函數的具體用法?Golang Decode怎麽用?Golang Decode使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Decode函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Extract
// Extract returns a pointer to a Resource object and is called after a
// Get operation.
func (r GetResult) Extract() (*Resource, error) {
if r.Err != nil {
return nil, r.Err
}
var res struct {
Res *Resource `mapstructure:"resource"`
}
if err := mapstructure.Decode(r.Body, &res); err != nil {
return nil, err
}
resource := r.Body.(map[string]interface{})["resource"].(map[string]interface{})
if date, ok := resource["updated_time"]; ok && date != nil {
t, err := time.Parse(time.RFC3339, date.(string))
if err != nil {
return nil, err
}
res.Res.UpdatedTime = t
}
return res.Res, nil
}
示例2: ExtractEvents
// ExtractEvents interprets the results of a single page from a List() call, producing a slice of Event entities.
func ExtractEvents(page pagination.Page) ([]Event, error) {
casted := page.(EventPage).Body
var res struct {
Res []Event `mapstructure:"events"`
}
if err := mapstructure.Decode(casted, &res); err != nil {
return nil, err
}
var events []interface{}
switch casted.(type) {
case map[string]interface{}:
events = casted.(map[string]interface{})["events"].([]interface{})
case map[string][]interface{}:
events = casted.(map[string][]interface{})["events"]
default:
return res.Res, fmt.Errorf("Unknown type: %v", reflect.TypeOf(casted))
}
for i, eventRaw := range events {
event := eventRaw.(map[string]interface{})
if date, ok := event["event_time"]; ok && date != nil {
t, err := time.Parse(time.RFC3339, date.(string))
if err != nil {
return nil, err
}
res.Res[i].Time = t
}
}
return res.Res, nil
}
示例3: ExtractResources
// ExtractResources interprets the results of a single page from a List() call, producing a slice of Resource entities.
func ExtractResources(page pagination.Page) ([]Resource, error) {
casted := page.(ResourcePage).Body
var response struct {
Resources []Resource `mapstructure:"resources"`
}
err := mapstructure.Decode(casted, &response)
var resources []interface{}
switch casted.(type) {
case map[string]interface{}:
resources = casted.(map[string]interface{})["resources"].([]interface{})
case map[string][]interface{}:
resources = casted.(map[string][]interface{})["resources"]
default:
return response.Resources, fmt.Errorf("Unknown type: %v", reflect.TypeOf(casted))
}
for i, resourceRaw := range resources {
resource := resourceRaw.(map[string]interface{})
if date, ok := resource["updated_time"]; ok && date != nil {
t, err := time.Parse(time.RFC3339, date.(string))
if err != nil {
return nil, err
}
response.Resources[i].UpdatedTime = t
}
}
return response.Resources, err
}
示例4: ExtractToken
// ExtractToken interprets a commonResult as a Token.
func (r commonResult) ExtractToken() (*Token, error) {
if r.Err != nil {
return nil, r.Err
}
var response struct {
Token struct {
ExpiresAt string `mapstructure:"expires_at"`
} `mapstructure:"token"`
}
var token Token
// Parse the token itself from the stored headers.
token.ID = r.Header.Get("X-Subject-Token")
err := mapstructure.Decode(r.Body, &response)
if err != nil {
return nil, err
}
// Attempt to parse the timestamp.
token.ExpiresAt, err = time.Parse(gophercloud.RFC3339Milli, response.Token.ExpiresAt)
return &token, err
}
示例5: ExtractToken
// ExtractToken returns the just-created Token from a CreateResult.
func (result CreateResult) ExtractToken() (*Token, error) {
if result.Err != nil {
return nil, result.Err
}
var response struct {
Access struct {
Token struct {
Expires string `mapstructure:"expires"`
ID string `mapstructure:"id"`
Tenant tenants.Tenant `mapstructure:"tenant"`
} `mapstructure:"token"`
} `mapstructure:"access"`
}
err := mapstructure.Decode(result.Body, &response)
if err != nil {
return nil, err
}
expiresTs, err := time.Parse(gophercloud.RFC3339Milli, response.Access.Token.Expires)
if err != nil {
return nil, err
}
return &Token{
ID: response.Access.Token.ID,
ExpiresAt: expiresTs,
Tenant: response.Access.Token.Tenant,
}, nil
}
示例6: ExtractVolumes
// ExtractVolumes extracts and returns Volumes. It is used while iterating over a volumes.List call.
func ExtractVolumes(page pagination.Page) ([]Volume, error) {
var response struct {
Volumes []Volume `json:"volumes"`
}
err := mapstructure.Decode(page.(ListResult).Body, &response)
return response.Volumes, err
}
示例7: ExtractResourceTypes
// ExtractResourceTypes extracts and returns resource types.
func ExtractResourceTypes(page pagination.Page) ([]string, error) {
var response struct {
ResourceTypes []string `mapstructure:"resource_types"`
}
err := mapstructure.Decode(page.(ResourceTypePage).Body, &response)
return response.ResourceTypes, err
}
示例8: ExtractSnapshots
// ExtractSnapshots extracts and returns Snapshots. It is used while iterating over a snapshots.List call.
func ExtractSnapshots(page pagination.Page) ([]Snapshot, error) {
var response struct {
Snapshots []Snapshot `json:"snapshots"`
}
err := mapstructure.Decode(page.(ListResult).Body, &response)
return response.Snapshots, err
}
示例9: ExtractPorts
// ExtractPorts accepts a Page struct, specifically a PortPage struct,
// and extracts the elements into a slice of Port structs. In other words,
// a generic collection is mapped into a relevant slice.
func ExtractPorts(page pagination.Page) ([]Port, error) {
var resp struct {
Ports []Port `mapstructure:"ports" json:"ports"`
}
err := mapstructure.Decode(page.(PortPage).Body, &resp)
return resp.Ports, err
}
示例10: ExtractAPIVersions
// ExtractAPIVersions takes a collection page, extracts all of the elements,
// and returns them a slice of APIVersion structs. It is effectively a cast.
func ExtractAPIVersions(page pagination.Page) ([]APIVersion, error) {
var resp struct {
Versions []APIVersion `mapstructure:"versions"`
}
err := mapstructure.Decode(page.(APIVersionPage).Body, &resp)
return resp.Versions, err
}
示例11: ExtractRules
// ExtractRules accepts a Page struct, specifically a SecGroupRulePage struct,
// and extracts the elements into a slice of SecGroupRule structs. In other words,
// a generic collection is mapped into a relevant slice.
func ExtractRules(page pagination.Page) ([]SecGroupRule, error) {
var resp struct {
SecGroupRules []SecGroupRule `mapstructure:"security_group_rules" json:"security_group_rules"`
}
err := mapstructure.Decode(page.(SecGroupRulePage).Body, &resp)
return resp.SecGroupRules, err
}
示例12: ExtractSubnets
// ExtractSubnets accepts a Page struct, specifically a SubnetPage struct,
// and extracts the elements into a slice of Subnet structs. In other words,
// a generic collection is mapped into a relevant slice.
func ExtractSubnets(page pagination.Page) ([]Subnet, error) {
var resp struct {
Subnets []Subnet `mapstructure:"subnets" json:"subnets"`
}
err := mapstructure.Decode(page.(SubnetPage).Body, &resp)
return resp.Subnets, err
}
示例13: ExtractNetworks
// ExtractNetworks accepts a Page struct, specifically a NetworkPage struct,
// and extracts the elements into a slice of Network structs. In other words,
// a generic collection is mapped into a relevant slice.
func ExtractNetworks(page pagination.Page) ([]Network, error) {
var resp struct {
Networks []Network `mapstructure:"networks" json:"networks"`
}
err := mapstructure.Decode(page.(NetworkPage).Body, &resp)
return resp.Networks, err
}
示例14: ExtractGroups
// ExtractGroups accepts a Page struct, specifically a SecGroupPage struct,
// and extracts the elements into a slice of SecGroup structs. In other words,
// a generic collection is mapped into a relevant slice.
func ExtractGroups(page pagination.Page) ([]SecGroup, error) {
var resp struct {
SecGroups []SecGroup `mapstructure:"security_groups" json:"security_groups"`
}
err := mapstructure.Decode(page.(SecGroupPage).Body, &resp)
return resp.SecGroups, err
}
示例15: ExtractTenants
// ExtractTenants returns a slice of Tenants contained in a single page of results.
func ExtractTenants(page pagination.Page) ([]Tenant, error) {
casted := page.(TenantPage).Body
var response struct {
Tenants []Tenant `mapstructure:"tenants"`
}
err := mapstructure.Decode(casted, &response)
return response.Tenants, err
}