param_filler.go 3.9 KB

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