当前位置: 首页>>代码示例>>Golang>>正文


Golang mapstructure.Decode函数代码示例

本文整理汇总了Golang中github.com/jrperritt/rack/internal/github.com/mitchellh/mapstructure.Decode函数的典型用法代码示例。如果您正苦于以下问题:Golang Decode函数的具体用法?Golang Decode怎么用?Golang Decode使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了Decode函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: 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
}
开发者ID:hdansou,项目名称:rack,代码行数:32,代码来源:results.go

示例2: 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
}
开发者ID:hdansou,项目名称:rack,代码行数:27,代码来源:results.go

示例3: 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
}
开发者ID:hdansou,项目名称:rack,代码行数:9,代码来源:results.go

示例4: 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
}
开发者ID:hdansou,项目名称:rack,代码行数:9,代码来源:results.go

示例5: 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
}
开发者ID:hdansou,项目名称:rack,代码行数:12,代码来源:results.go

示例6: ExtractImages

// ExtractImages converts a page of List results into a slice of usable Image structs.
func ExtractImages(page pagination.Page) ([]Image, error) {
	casted := page.(ImagePage).Body
	var results struct {
		Images []Image `mapstructure:"images"`
	}

	err := mapstructure.Decode(casted, &results)
	return results.Images, err
}
开发者ID:hdansou,项目名称:rack,代码行数:10,代码来源:results.go

示例7: 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
}
开发者ID:hdansou,项目名称:rack,代码行数:12,代码来源:results.go

示例8: 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
}
开发者ID:hdansou,项目名称:rack,代码行数:10,代码来源:results.go

示例9: 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
}
开发者ID:hdansou,项目名称:rack,代码行数:12,代码来源:results.go

示例10: 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
}
开发者ID:hdansou,项目名称:rack,代码行数:12,代码来源:results.go

示例11: ExtractInfo

// ExtractInfo is a function that takes a page of objects and returns their full information.
func ExtractInfo(page pagination.Page) ([]Object, error) {
	untyped := page.(ObjectPage).Body.([]interface{})
	results := make([]Object, len(untyped))
	for index, each := range untyped {
		object := each.(map[string]interface{})
		err := mapstructure.Decode(object, &results[index])
		if err != nil {
			return results, err
		}
	}
	return results, nil
}
开发者ID:hdansou,项目名称:rack,代码行数:13,代码来源:results.go

示例12: Extract

// Extract interprets any MetadatumResult as a Metadatum, if possible.
func (r MetadatumResult) Extract() (map[string]string, error) {
	if r.Err != nil {
		return nil, r.Err
	}

	var response struct {
		Metadatum map[string]string `mapstructure:"meta"`
	}

	err := mapstructure.Decode(r.Body, &response)
	return response.Metadatum, err
}
开发者ID:hdansou,项目名称:rack,代码行数:13,代码来源:results.go

示例13: Extract

// Extract is a method that attempts to interpret any VolumeAttachment resource
// response as a VolumeAttachment struct.
func (r VolumeAttachmentResult) Extract() (*VolumeAttachment, error) {
	if r.Err != nil {
		return nil, r.Err
	}

	var res struct {
		VolumeAttachment *VolumeAttachment `json:"volumeAttachment" mapstructure:"volumeAttachment"`
	}

	err := mapstructure.Decode(r.Body, &res)
	return res.VolumeAttachment, err
}
开发者ID:harshalhshah,项目名称:rack,代码行数:14,代码来源:results.go

示例14: Extract

// Extract is a method that attempts to interpret any KeyPair resource response as a KeyPair struct.
func (r keyPairResult) Extract() (*KeyPair, error) {
	if r.Err != nil {
		return nil, r.Err
	}

	var res struct {
		KeyPair *KeyPair `json:"keypair" mapstructure:"keypair"`
	}

	err := mapstructure.Decode(r.Body, &res)
	return res.KeyPair, err
}
开发者ID:hdansou,项目名称:rack,代码行数:13,代码来源:results.go

示例15: ExtractLinkedInts

func ExtractLinkedInts(page Page) ([]int, error) {
	var response struct {
		Ints []int `mapstructure:"ints"`
	}

	err := mapstructure.Decode(page.(LinkedPageResult).Body, &response)
	if err != nil {
		return nil, err
	}

	return response.Ints, nil
}
开发者ID:hdansou,项目名称:rack,代码行数:12,代码来源:linked_test.go


注:本文中的github.com/jrperritt/rack/internal/github.com/mitchellh/mapstructure.Decode函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。