Home | History | Annotate | Download | only in gensupport
      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 
      5 package gensupport
      6 
      7 import (
      8 	"net/url"
      9 
     10 	"google.golang.org/api/googleapi"
     11 )
     12 
     13 // URLParams is a simplified replacement for url.Values
     14 // that safely builds up URL parameters for encoding.
     15 type URLParams map[string][]string
     16 
     17 // Get returns the first value for the given key, or "".
     18 func (u URLParams) Get(key string) string {
     19 	vs := u[key]
     20 	if len(vs) == 0 {
     21 		return ""
     22 	}
     23 	return vs[0]
     24 }
     25 
     26 // Set sets the key to value.
     27 // It replaces any existing values.
     28 func (u URLParams) Set(key, value string) {
     29 	u[key] = []string{value}
     30 }
     31 
     32 // SetMulti sets the key to an array of values.
     33 // It replaces any existing values.
     34 // Note that values must not be modified after calling SetMulti
     35 // so the caller is responsible for making a copy if necessary.
     36 func (u URLParams) SetMulti(key string, values []string) {
     37 	u[key] = values
     38 }
     39 
     40 // Encode encodes the values into ``URL encoded'' form
     41 // ("bar=baz&foo=quux") sorted by key.
     42 func (u URLParams) Encode() string {
     43 	return url.Values(u).Encode()
     44 }
     45 
     46 func SetOptions(u URLParams, opts ...googleapi.CallOption) {
     47 	for _, o := range opts {
     48 		u.Set(o.Get())
     49 	}
     50 }
     51