func TestBuildQueryString(t *testing.T) { type testVar string iFalse := false opts := struct { J int `q:"j"` R string `q:"r,required"` C bool `q:"c"` S []string `q:"s"` TS []testVar `q:"ts"` TI []int `q:"ti"` F *bool `q:"f"` }{ J: 2, R: "red", C: true, S: []string{"one", "two", "three"}, TS: []testVar{"a", "b"}, TI: []int{1, 2}, F: &iFalse, } expected := &url.URL{RawQuery: "c=true&f=false&j=2&r=red&s=one&s=two&s=three&ti=1&ti=2&ts=a&ts=b"} actual, err := gophercloud.BuildQueryString(&opts) if err != nil { t.Errorf("Error building query string: %v", err) } th.CheckDeepEquals(t, expected, actual) opts = struct { J int `q:"j"` R string `q:"r,required"` C bool `q:"c"` S []string `q:"s"` TS []testVar `q:"ts"` TI []int `q:"ti"` F *bool `q:"f"` }{ J: 2, C: true, } _, err = gophercloud.BuildQueryString(&opts) if err == nil { t.Errorf("Expected error: 'Required field not set'") } th.CheckDeepEquals(t, expected, actual) _, err = gophercloud.BuildQueryString(map[string]interface{}{"Number": 4}) if err == nil { t.Errorf("Expected error: 'Options type is not a struct'") } }
// ToObjectCreateParams formats a CreateOpts into a query string and map of // headers. func (opts CreateOpts) ToObjectCreateParams() (io.Reader, map[string]string, string, error) { q, err := gophercloud.BuildQueryString(opts) if err != nil { return nil, nil, "", err } h, err := gophercloud.BuildHeaders(opts) if err != nil { return nil, nil, "", err } for k, v := range opts.Metadata { h["X-Object-Meta-"+k] = v } hash := md5.New() buf := bytes.NewBuffer([]byte{}) _, err = io.Copy(io.MultiWriter(hash, buf), opts.Content) if err != nil { return nil, nil, "", err } localChecksum := fmt.Sprintf("%x", hash.Sum(nil)) h["ETag"] = localChecksum return buf, h, q.String(), nil }
// ToRuleListQuery formats a ListOpts into a query string. func (opts ListOpts) ToRuleListQuery() (string, error) { q, err := gophercloud.BuildQueryString(opts) if err != nil { return "", err } return q.String(), nil }
// List returns a Pager which allows you to iterate over a collection of // floating IP resources. It accepts a ListOpts struct, which allows you to // filter and sort the returned collection for greater efficiency. func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager { q, err := gophercloud.BuildQueryString(&opts) if err != nil { return pagination.Pager{Err: err} } u := rootURL(c) + q.String() return pagination.NewPager(c, u, func(r pagination.PageResult) pagination.Page { return FloatingIPPage{pagination.LinkedPageBase{PageResult: r}} }) }
// ToObjectDownloadParams formats a DownloadOpts into a query string and map of // headers. func (opts DownloadOpts) ToObjectDownloadParams() (map[string]string, string, error) { q, err := gophercloud.BuildQueryString(opts) if err != nil { return nil, "", err } h, err := gophercloud.BuildHeaders(opts) if err != nil { return nil, q.String(), err } return h, q.String(), nil }
func TestQueriesAreEscaped(t *testing.T) { type foo struct { Name string `q:"something"` Shape string `q:"else"` } expected := &url.URL{RawQuery: "else=Triangl+e&something=blah%2B%3F%21%21foo"} actual, err := gophercloud.BuildQueryString(foo{Name: "blah+?!!foo", Shape: "Triangl e"}) th.AssertNoErr(t, err) th.AssertDeepEquals(t, expected, actual) }
// List enumerates endpoints in a paginated collection, optionally filtered by ListOpts criteria. func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { u := listURL(client) if opts != nil { q, err := gophercloud.BuildQueryString(opts) if err != nil { return pagination.Pager{Err: err} } u += q.String() } return pagination.NewPager(client, u, func(r pagination.PageResult) pagination.Page { return EndpointPage{pagination.LinkedPageBase{PageResult: r}} }) }
// ToObjectListParams formats a ListOpts into a query string and boolean // representing whether to list complete information for each object. func (opts ListOpts) ToObjectListParams() (bool, string, error) { q, err := gophercloud.BuildQueryString(opts) return opts.Full, q.String(), err }
func (opts ListOpts) ToEndpointListParams() (string, error) { q, err := gophercloud.BuildQueryString(opts) return q.String(), err }
// ToMemberListQuery formats a ListOpts into a query string. func (opts ListMembersOpts) ToMembersListQuery() (string, error) { q, err := gophercloud.BuildQueryString(opts) return q.String(), err }
// ToShareTypeListQuery formats a ListOpts into a query string. func (opts ListOpts) ToShareTypeListQuery() (string, error) { q, err := gophercloud.BuildQueryString(opts) return q.String(), err }
// ToFirewallListQuery formats a ListOpts into a query string. func (opts ListOpts) ToFirewallListQuery() (string, error) { q, err := gophercloud.BuildQueryString(opts) return q.String(), err }
func (opts ListOpts) ToServiceListMap() (string, error) { q, err := gophercloud.BuildQueryString(opts) return q.String(), err }
// ToLoadbalancerListQuery formats a ListOpts into a query string. func (opts ListOpts) ToLoadBalancerListQuery() (string, error) { q, err := gophercloud.BuildQueryString(opts) return q.String(), err }
// ToCDNAssetDeleteParams formats a DeleteOpts into a query string. func (opts DeleteOpts) ToCDNAssetDeleteParams() (string, error) { q, err := gophercloud.BuildQueryString(opts) return q.String(), err }
// ToRolesListAssignmentsQuery formats a ListAssignmentsOpts into a query string. func (opts ListAssignmentsOpts) ToRolesListAssignmentsQuery() (string, error) { q, err := gophercloud.BuildQueryString(opts) return q.String(), err }
// ToObjectGetQuery formats a GetOpts into a query string. func (opts GetOpts) ToObjectGetQuery() (string, error) { q, err := gophercloud.BuildQueryString(opts) return q.String(), err }
// ToObjectDeleteQuery formats a DeleteOpts into a query string. func (opts DeleteOpts) ToObjectDeleteQuery() (string, error) { q, err := gophercloud.BuildQueryString(opts) return q.String(), err }
// ToResourceEventListQuery formats a ListResourceEventsOpts into a query string. func (opts ListResourceEventsOpts) ToResourceEventListQuery() (string, error) { q, err := gophercloud.BuildQueryString(opts) return q.String(), err }
// ToStackEventListQuery formats a ListOpts into a query string. func (opts ListOpts) ToStackEventListQuery() (string, error) { q, err := gophercloud.BuildQueryString(opts) return q.String(), err }