param_filler.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // +build codegen
  2. package api
  3. import (
  4. "encoding/json"
  5. "fmt"
  6. "reflect"
  7. "strings"
  8. "github.com/aws/aws-sdk-go/private/util"
  9. )
  10. // A paramFiller provides string formatting for a shape and its types.
  11. type paramFiller struct {
  12. prefixPackageName bool
  13. }
  14. // typeName returns the type name of a shape.
  15. func (f paramFiller) typeName(shape *Shape) string {
  16. if f.prefixPackageName && shape.Type == "structure" {
  17. return "*" + shape.API.PackageName() + "." + shape.GoTypeElem()
  18. }
  19. return shape.GoType()
  20. }
  21. // ParamsStructFromJSON returns a JSON string representation of a structure.
  22. func ParamsStructFromJSON(value interface{}, shape *Shape, prefixPackageName bool) string {
  23. f := paramFiller{prefixPackageName: prefixPackageName}
  24. return util.GoFmt(f.paramsStructAny(value, shape))
  25. }
  26. // paramsStructAny returns the string representation of any value.
  27. func (f paramFiller) paramsStructAny(value interface{}, shape *Shape) string {
  28. if value == nil {
  29. return ""
  30. }
  31. switch shape.Type {
  32. case "structure":
  33. if value != nil {
  34. vmap := value.(map[string]interface{})
  35. return f.paramsStructStruct(vmap, shape)
  36. }
  37. case "list":
  38. vlist := value.([]interface{})
  39. return f.paramsStructList(vlist, shape)
  40. case "map":
  41. vmap := value.(map[string]interface{})
  42. return f.paramsStructMap(vmap, shape)
  43. case "string", "character":
  44. v := reflect.Indirect(reflect.ValueOf(value))
  45. if v.IsValid() {
  46. return fmt.Sprintf("aws.String(%#v)", v.Interface())
  47. }
  48. case "blob":
  49. v := reflect.Indirect(reflect.ValueOf(value))
  50. if v.IsValid() && shape.Streaming {
  51. return fmt.Sprintf("bytes.NewReader([]byte(%#v))", v.Interface())
  52. } else if v.IsValid() {
  53. return fmt.Sprintf("[]byte(%#v)", v.Interface())
  54. }
  55. case "boolean":
  56. v := reflect.Indirect(reflect.ValueOf(value))
  57. if v.IsValid() {
  58. return fmt.Sprintf("aws.Bool(%#v)", v.Interface())
  59. }
  60. case "integer", "long":
  61. v := reflect.Indirect(reflect.ValueOf(value))
  62. if v.IsValid() {
  63. return fmt.Sprintf("aws.Int64(%v)", v.Interface())
  64. }
  65. case "float", "double":
  66. v := reflect.Indirect(reflect.ValueOf(value))
  67. if v.IsValid() {
  68. return fmt.Sprintf("aws.Float64(%v)", v.Interface())
  69. }
  70. case "timestamp":
  71. v := reflect.Indirect(reflect.ValueOf(value))
  72. if v.IsValid() {
  73. return fmt.Sprintf("aws.Time(time.Unix(%d, 0))", int(v.Float()))
  74. }
  75. case "jsonvalue":
  76. v, err := json.Marshal(value)
  77. if err != nil {
  78. panic("failed to marshal JSONValue, " + err.Error())
  79. }
  80. const tmpl = `func() aws.JSONValue {
  81. var m aws.JSONValue
  82. if err := json.Unmarshal([]byte(%q), &m); err != nil {
  83. panic("failed to unmarshal JSONValue, "+err.Error())
  84. }
  85. return m
  86. }()`
  87. return fmt.Sprintf(tmpl, string(v))
  88. default:
  89. panic("Unhandled type " + shape.Type)
  90. }
  91. return ""
  92. }
  93. // paramsStructStruct returns the string representation of a structure
  94. func (f paramFiller) paramsStructStruct(value map[string]interface{}, shape *Shape) string {
  95. out := "&" + f.typeName(shape)[1:] + "{\n"
  96. for _, n := range shape.MemberNames() {
  97. ref := shape.MemberRefs[n]
  98. name := findParamMember(value, n)
  99. if val := f.paramsStructAny(value[name], ref.Shape); val != "" {
  100. out += fmt.Sprintf("%s: %s,\n", n, val)
  101. }
  102. }
  103. out += "}"
  104. return out
  105. }
  106. // paramsStructMap returns the string representation of a map of values
  107. func (f paramFiller) paramsStructMap(value map[string]interface{}, shape *Shape) string {
  108. out := f.typeName(shape) + "{\n"
  109. keys := util.SortedKeys(value)
  110. for _, k := range keys {
  111. v := value[k]
  112. out += fmt.Sprintf("%q: %s,\n", k, f.paramsStructAny(v, shape.ValueRef.Shape))
  113. }
  114. out += "}"
  115. return out
  116. }
  117. // paramsStructList returns the string representation of slice of values
  118. func (f paramFiller) paramsStructList(value []interface{}, shape *Shape) string {
  119. out := f.typeName(shape) + "{\n"
  120. for _, v := range value {
  121. out += fmt.Sprintf("%s,\n", f.paramsStructAny(v, shape.MemberRef.Shape))
  122. }
  123. out += "}"
  124. return out
  125. }
  126. // findParamMember searches a map for a key ignoring case. Returns the map key if found.
  127. func findParamMember(value map[string]interface{}, key string) string {
  128. for actualKey := range value {
  129. if strings.ToLower(key) == strings.ToLower(actualKey) {
  130. return actualKey
  131. }
  132. }
  133. return ""
  134. }