github.com/timstclair/heapster@v0.20.0-alpha1/Godeps/_workspace/src/google.golang.org/api/googleapi/types.go (about) 1 // Copyright 2013 Google Inc. 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 googleapi 6 7 import ( 8 "encoding/json" 9 "strconv" 10 ) 11 12 // Int64s is a slice of int64s that marshal as quoted strings in JSON. 13 type Int64s []int64 14 15 func (q *Int64s) UnmarshalJSON(raw []byte) error { 16 *q = (*q)[:0] 17 var ss []string 18 if err := json.Unmarshal(raw, &ss); err != nil { 19 return err 20 } 21 for _, s := range ss { 22 v, err := strconv.ParseInt(s, 10, 64) 23 if err != nil { 24 return err 25 } 26 *q = append(*q, int64(v)) 27 } 28 return nil 29 } 30 31 // Int32s is a slice of int32s that marshal as quoted strings in JSON. 32 type Int32s []int32 33 34 func (q *Int32s) UnmarshalJSON(raw []byte) error { 35 *q = (*q)[:0] 36 var ss []string 37 if err := json.Unmarshal(raw, &ss); err != nil { 38 return err 39 } 40 for _, s := range ss { 41 v, err := strconv.ParseInt(s, 10, 32) 42 if err != nil { 43 return err 44 } 45 *q = append(*q, int32(v)) 46 } 47 return nil 48 } 49 50 // Uint64s is a slice of uint64s that marshal as quoted strings in JSON. 51 type Uint64s []uint64 52 53 func (q *Uint64s) UnmarshalJSON(raw []byte) error { 54 *q = (*q)[:0] 55 var ss []string 56 if err := json.Unmarshal(raw, &ss); err != nil { 57 return err 58 } 59 for _, s := range ss { 60 v, err := strconv.ParseUint(s, 10, 64) 61 if err != nil { 62 return err 63 } 64 *q = append(*q, uint64(v)) 65 } 66 return nil 67 } 68 69 // Uint32s is a slice of uint32s that marshal as quoted strings in JSON. 70 type Uint32s []uint32 71 72 func (q *Uint32s) UnmarshalJSON(raw []byte) error { 73 *q = (*q)[:0] 74 var ss []string 75 if err := json.Unmarshal(raw, &ss); err != nil { 76 return err 77 } 78 for _, s := range ss { 79 v, err := strconv.ParseUint(s, 10, 32) 80 if err != nil { 81 return err 82 } 83 *q = append(*q, uint32(v)) 84 } 85 return nil 86 } 87 88 // Float64s is a slice of float64s that marshal as quoted strings in JSON. 89 type Float64s []float64 90 91 func (q *Float64s) UnmarshalJSON(raw []byte) error { 92 *q = (*q)[:0] 93 var ss []string 94 if err := json.Unmarshal(raw, &ss); err != nil { 95 return err 96 } 97 for _, s := range ss { 98 v, err := strconv.ParseFloat(s, 64) 99 if err != nil { 100 return err 101 } 102 *q = append(*q, float64(v)) 103 } 104 return nil 105 } 106 107 func quotedList(n int, fn func(dst []byte, i int) []byte) ([]byte, error) { 108 dst := make([]byte, 0, 2+n*10) // somewhat arbitrary 109 dst = append(dst, '[') 110 for i := 0; i < n; i++ { 111 if i > 0 { 112 dst = append(dst, ',') 113 } 114 dst = append(dst, '"') 115 dst = fn(dst, i) 116 dst = append(dst, '"') 117 } 118 dst = append(dst, ']') 119 return dst, nil 120 } 121 122 func (s Int64s) MarshalJSON() ([]byte, error) { 123 return quotedList(len(s), func(dst []byte, i int) []byte { 124 return strconv.AppendInt(dst, s[i], 10) 125 }) 126 } 127 128 func (s Int32s) MarshalJSON() ([]byte, error) { 129 return quotedList(len(s), func(dst []byte, i int) []byte { 130 return strconv.AppendInt(dst, int64(s[i]), 10) 131 }) 132 } 133 134 func (s Uint64s) MarshalJSON() ([]byte, error) { 135 return quotedList(len(s), func(dst []byte, i int) []byte { 136 return strconv.AppendUint(dst, s[i], 10) 137 }) 138 } 139 140 func (s Uint32s) MarshalJSON() ([]byte, error) { 141 return quotedList(len(s), func(dst []byte, i int) []byte { 142 return strconv.AppendUint(dst, uint64(s[i]), 10) 143 }) 144 } 145 146 func (s Float64s) MarshalJSON() ([]byte, error) { 147 return quotedList(len(s), func(dst []byte, i int) []byte { 148 return strconv.AppendFloat(dst, s[i], 'g', -1, 64) 149 }) 150 } 151 152 /* 153 * Helper routines for simplifying the creation of optional fields of basic type. 154 */ 155 156 // Bool is a helper routine that allocates a new bool value 157 // to store v and returns a pointer to it. 158 func Bool(v bool) *bool { return &v } 159 160 // Int32 is a helper routine that allocates a new int32 value 161 // to store v and returns a pointer to it. 162 func Int32(v int32) *int32 { return &v } 163 164 // Int64 is a helper routine that allocates a new int64 value 165 // to store v and returns a pointer to it. 166 func Int64(v int64) *int64 { return &v } 167 168 // Float64 is a helper routine that allocates a new float64 value 169 // to store v and returns a pointer to it. 170 func Float64(v float64) *float64 { return &v } 171 172 // Uint32 is a helper routine that allocates a new uint32 value 173 // to store v and returns a pointer to it. 174 func Uint32(v uint32) *uint32 { return &v } 175 176 // Uint64 is a helper routine that allocates a new uint64 value 177 // to store v and returns a pointer to it. 178 func Uint64(v uint64) *uint64 { return &v } 179 180 // String is a helper routine that allocates a new string value 181 // to store v and returns a pointer to it. 182 func String(v string) *string { return &v }