param_filler.go 3.7 KB

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