// 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 }
// 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 }
// 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 }
// 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 }
// 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 }