示例#1
0
文件: results.go 项目: hdansou/rack
// Extract interprets any serverResult as a Server, if possible.
func (r serverResult) Extract() (*Server, error) {
	if r.Err != nil {
		return nil, r.Err
	}

	var response struct {
		Server Server `mapstructure:"server"`
	}

	config := &mapstructure.DecoderConfig{
		DecodeHook: toMapFromString,
		Result:     &response,
	}
	decoder, err := mapstructure.NewDecoder(config)
	if err != nil {
		return nil, err
	}

	err = decoder.Decode(r.Body)
	if err != nil {
		return nil, err
	}

	return &response.Server, nil
}
示例#2
0
文件: results.go 项目: hdansou/rack
// ExtractServers interprets the results of a single page from a List() call, producing a slice of Server entities.
func ExtractServers(page pagination.Page) ([]Server, error) {
	casted := page.(ServerPage).Body

	var response struct {
		Servers []Server `mapstructure:"servers"`
	}

	config := &mapstructure.DecoderConfig{
		DecodeHook: toMapFromString,
		Result:     &response,
	}
	decoder, err := mapstructure.NewDecoder(config)
	if err != nil {
		return nil, err
	}

	err = decoder.Decode(casted)

	return response.Servers, err
}
示例#3
0
文件: results.go 项目: hdansou/rack
// Extract provides access to the individual Flavor returned by the Get function.
func (gr GetResult) Extract() (*Flavor, error) {
	if gr.Err != nil {
		return nil, gr.Err
	}

	var result struct {
		Flavor Flavor `mapstructure:"flavor"`
	}

	cfg := &mapstructure.DecoderConfig{
		DecodeHook: defaulter,
		Result:     &result,
	}
	decoder, err := mapstructure.NewDecoder(cfg)
	if err != nil {
		return nil, err
	}
	err = decoder.Decode(gr.Body)
	return &result.Flavor, err
}
示例#4
0
文件: results.go 项目: hdansou/rack
// DecodeHeader is a function that decodes a header (usually of type map[string]interface{}) to
// another type (usually a struct). This function is used by the objectstorage package to give
// users access to response headers without having to query a map. A DecodeHookFunction is used,
// because OpenStack-based clients return header values as arrays (Go slices).
func DecodeHeader(from, to interface{}) error {
	config := &mapstructure.DecoderConfig{
		DecodeHook: func(from, to reflect.Kind, data interface{}) (interface{}, error) {
			if from == reflect.Slice {
				return data.([]string)[0], nil
			}
			return data, nil
		},
		Result:           to,
		WeaklyTypedInput: true,
	}
	decoder, err := mapstructure.NewDecoder(config)
	if err != nil {
		return err
	}
	if err := decoder.Decode(from); err != nil {
		return err
	}
	return nil
}
示例#5
0
文件: results.go 项目: hdansou/rack
// ExtractFlavors provides access to the list of flavors in a page acquired from the List operation.
func ExtractFlavors(page pagination.Page) ([]Flavor, error) {
	casted := page.(FlavorPage).Body
	var container struct {
		Flavors []Flavor `mapstructure:"flavors"`
	}

	cfg := &mapstructure.DecoderConfig{
		DecodeHook: defaulter,
		Result:     &container,
	}
	decoder, err := mapstructure.NewDecoder(cfg)
	if err != nil {
		return container.Flavors, err
	}
	err = decoder.Decode(casted)
	if err != nil {
		return container.Flavors, err
	}

	return container.Flavors, nil
}