param_filler.go 3.7 KB

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