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.
 
 
 

203 lines
4.9 KiB

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