You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

51 regels
1.2 KiB

  1. // Copyright 2015 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package gensupport
  5. import (
  6. "net/url"
  7. "google.golang.org/api/googleapi"
  8. )
  9. // URLParams is a simplified replacement for url.Values
  10. // that safely builds up URL parameters for encoding.
  11. type URLParams map[string][]string
  12. // Get returns the first value for the given key, or "".
  13. func (u URLParams) Get(key string) string {
  14. vs := u[key]
  15. if len(vs) == 0 {
  16. return ""
  17. }
  18. return vs[0]
  19. }
  20. // Set sets the key to value.
  21. // It replaces any existing values.
  22. func (u URLParams) Set(key, value string) {
  23. u[key] = []string{value}
  24. }
  25. // SetMulti sets the key to an array of values.
  26. // It replaces any existing values.
  27. // Note that values must not be modified after calling SetMulti
  28. // so the caller is responsible for making a copy if necessary.
  29. func (u URLParams) SetMulti(key string, values []string) {
  30. u[key] = values
  31. }
  32. // Encode encodes the values into ``URL encoded'' form
  33. // ("bar=baz&foo=quux") sorted by key.
  34. func (u URLParams) Encode() string {
  35. return url.Values(u).Encode()
  36. }
  37. func SetOptions(u URLParams, opts ...googleapi.CallOption) {
  38. for _, o := range opts {
  39. u.Set(o.Get())
  40. }
  41. }