convert.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package queryparams
  14. import (
  15. "fmt"
  16. "net/url"
  17. "reflect"
  18. "strings"
  19. )
  20. // Marshaler converts an object to a query parameter string representation
  21. type Marshaler interface {
  22. MarshalQueryParameter() (string, error)
  23. }
  24. // Unmarshaler converts a string representation to an object
  25. type Unmarshaler interface {
  26. UnmarshalQueryParameter(string) error
  27. }
  28. func jsonTag(field reflect.StructField) (string, bool) {
  29. structTag := field.Tag.Get("json")
  30. if len(structTag) == 0 {
  31. return "", false
  32. }
  33. parts := strings.Split(structTag, ",")
  34. tag := parts[0]
  35. if tag == "-" {
  36. tag = ""
  37. }
  38. omitempty := false
  39. parts = parts[1:]
  40. for _, part := range parts {
  41. if part == "omitempty" {
  42. omitempty = true
  43. break
  44. }
  45. }
  46. return tag, omitempty
  47. }
  48. func formatValue(value interface{}) string {
  49. return fmt.Sprintf("%v", value)
  50. }
  51. func isPointerKind(kind reflect.Kind) bool {
  52. return kind == reflect.Ptr
  53. }
  54. func isStructKind(kind reflect.Kind) bool {
  55. return kind == reflect.Struct
  56. }
  57. func isValueKind(kind reflect.Kind) bool {
  58. switch kind {
  59. case reflect.String, reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16,
  60. reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8,
  61. reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Float32,
  62. reflect.Float64, reflect.Complex64, reflect.Complex128:
  63. return true
  64. default:
  65. return false
  66. }
  67. }
  68. func zeroValue(value reflect.Value) bool {
  69. return reflect.DeepEqual(reflect.Zero(value.Type()).Interface(), value.Interface())
  70. }
  71. func customMarshalValue(value reflect.Value) (reflect.Value, bool) {
  72. // Return unless we implement a custom query marshaler
  73. if !value.CanInterface() {
  74. return reflect.Value{}, false
  75. }
  76. marshaler, ok := value.Interface().(Marshaler)
  77. if !ok {
  78. if !isPointerKind(value.Kind()) && value.CanAddr() {
  79. marshaler, ok = value.Addr().Interface().(Marshaler)
  80. if !ok {
  81. return reflect.Value{}, false
  82. }
  83. } else {
  84. return reflect.Value{}, false
  85. }
  86. }
  87. // Don't invoke functions on nil pointers
  88. // If the type implements MarshalQueryParameter, AND the tag is not omitempty, AND the value is a nil pointer, "" seems like a reasonable response
  89. if isPointerKind(value.Kind()) && zeroValue(value) {
  90. return reflect.ValueOf(""), true
  91. }
  92. // Get the custom marshalled value
  93. v, err := marshaler.MarshalQueryParameter()
  94. if err != nil {
  95. return reflect.Value{}, false
  96. }
  97. return reflect.ValueOf(v), true
  98. }
  99. func addParam(values url.Values, tag string, omitempty bool, value reflect.Value) {
  100. if omitempty && zeroValue(value) {
  101. return
  102. }
  103. val := ""
  104. iValue := fmt.Sprintf("%v", value.Interface())
  105. if iValue != "<nil>" {
  106. val = iValue
  107. }
  108. values.Add(tag, val)
  109. }
  110. func addListOfParams(values url.Values, tag string, omitempty bool, list reflect.Value) {
  111. for i := 0; i < list.Len(); i++ {
  112. addParam(values, tag, omitempty, list.Index(i))
  113. }
  114. }
  115. // Convert takes an object and converts it to a url.Values object using JSON tags as
  116. // parameter names. Only top-level simple values, arrays, and slices are serialized.
  117. // Embedded structs, maps, etc. will not be serialized.
  118. func Convert(obj interface{}) (url.Values, error) {
  119. result := url.Values{}
  120. if obj == nil {
  121. return result, nil
  122. }
  123. var sv reflect.Value
  124. switch reflect.TypeOf(obj).Kind() {
  125. case reflect.Ptr, reflect.Interface:
  126. sv = reflect.ValueOf(obj).Elem()
  127. default:
  128. return nil, fmt.Errorf("expecting a pointer or interface")
  129. }
  130. st := sv.Type()
  131. if !isStructKind(st.Kind()) {
  132. return nil, fmt.Errorf("expecting a pointer to a struct")
  133. }
  134. // Check all object fields
  135. convertStruct(result, st, sv)
  136. return result, nil
  137. }
  138. func convertStruct(result url.Values, st reflect.Type, sv reflect.Value) {
  139. for i := 0; i < st.NumField(); i++ {
  140. field := sv.Field(i)
  141. tag, omitempty := jsonTag(st.Field(i))
  142. if len(tag) == 0 {
  143. continue
  144. }
  145. ft := field.Type()
  146. kind := ft.Kind()
  147. if isPointerKind(kind) {
  148. ft = ft.Elem()
  149. kind = ft.Kind()
  150. if !field.IsNil() {
  151. field = reflect.Indirect(field)
  152. // If the field is non-nil, it should be added to params
  153. // and the omitempty should be overwite to false
  154. omitempty = false
  155. }
  156. }
  157. switch {
  158. case isValueKind(kind):
  159. addParam(result, tag, omitempty, field)
  160. case kind == reflect.Array || kind == reflect.Slice:
  161. if isValueKind(ft.Elem().Kind()) {
  162. addListOfParams(result, tag, omitempty, field)
  163. }
  164. case isStructKind(kind) && !(zeroValue(field) && omitempty):
  165. if marshalValue, ok := customMarshalValue(field); ok {
  166. addParam(result, tag, omitempty, marshalValue)
  167. } else {
  168. convertStruct(result, ft, field)
  169. }
  170. }
  171. }
  172. }