converter.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. package dynamodbattribute
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "reflect"
  7. "runtime"
  8. "strconv"
  9. "github.com/aws/aws-sdk-go/aws/awserr"
  10. "github.com/aws/aws-sdk-go/service/dynamodb"
  11. )
  12. // ConvertToMap accepts a map[string]interface{} or struct and converts it to a
  13. // map[string]*dynamodb.AttributeValue.
  14. //
  15. // If in contains any structs, it is first JSON encoded/decoded it to convert it
  16. // to a map[string]interface{}, so `json` struct tags are respected.
  17. //
  18. // Deprecated: Use MarshalMap instead
  19. func ConvertToMap(in interface{}) (item map[string]*dynamodb.AttributeValue, err error) {
  20. defer func() {
  21. if r := recover(); r != nil {
  22. if e, ok := r.(runtime.Error); ok {
  23. err = e
  24. } else if s, ok := r.(string); ok {
  25. err = fmt.Errorf(s)
  26. } else {
  27. err = r.(error)
  28. }
  29. item = nil
  30. }
  31. }()
  32. if in == nil {
  33. return nil, awserr.New("SerializationError",
  34. "in must be a map[string]interface{} or struct, got <nil>", nil)
  35. }
  36. v := reflect.ValueOf(in)
  37. if v.Kind() != reflect.Struct && !(v.Kind() == reflect.Map && v.Type().Key().Kind() == reflect.String) {
  38. return nil, awserr.New("SerializationError",
  39. fmt.Sprintf("in must be a map[string]interface{} or struct, got %s",
  40. v.Type().String()),
  41. nil)
  42. }
  43. if isTyped(reflect.TypeOf(in)) {
  44. var out map[string]interface{}
  45. in = convertToUntyped(in, out)
  46. }
  47. item = make(map[string]*dynamodb.AttributeValue)
  48. for k, v := range in.(map[string]interface{}) {
  49. item[k] = convertTo(v)
  50. }
  51. return item, nil
  52. }
  53. // ConvertFromMap accepts a map[string]*dynamodb.AttributeValue and converts it to a
  54. // map[string]interface{} or struct.
  55. //
  56. // If v points to a struct, the result is first converted it to a
  57. // map[string]interface{}, then JSON encoded/decoded it to convert to a struct,
  58. // so `json` struct tags are respected.
  59. //
  60. // Deprecated: Use UnmarshalMap instead
  61. func ConvertFromMap(item map[string]*dynamodb.AttributeValue, v interface{}) (err error) {
  62. defer func() {
  63. if r := recover(); r != nil {
  64. if e, ok := r.(runtime.Error); ok {
  65. err = e
  66. } else if s, ok := r.(string); ok {
  67. err = fmt.Errorf(s)
  68. } else {
  69. err = r.(error)
  70. }
  71. item = nil
  72. }
  73. }()
  74. rv := reflect.ValueOf(v)
  75. if rv.Kind() != reflect.Ptr || rv.IsNil() {
  76. return awserr.New("SerializationError",
  77. fmt.Sprintf("v must be a non-nil pointer to a map[string]interface{} or struct, got %s",
  78. rv.Type()),
  79. nil)
  80. }
  81. if rv.Elem().Kind() != reflect.Struct && !(rv.Elem().Kind() == reflect.Map && rv.Elem().Type().Key().Kind() == reflect.String) {
  82. return awserr.New("SerializationError",
  83. fmt.Sprintf("v must be a non-nil pointer to a map[string]interface{} or struct, got %s",
  84. rv.Type()),
  85. nil)
  86. }
  87. m := make(map[string]interface{})
  88. for k, v := range item {
  89. m[k] = convertFrom(v)
  90. }
  91. if isTyped(reflect.TypeOf(v)) {
  92. err = convertToTyped(m, v)
  93. } else {
  94. rv.Elem().Set(reflect.ValueOf(m))
  95. }
  96. return err
  97. }
  98. // ConvertToList accepts an array or slice and converts it to a
  99. // []*dynamodb.AttributeValue.
  100. //
  101. // Converting []byte fields to dynamodb.AttributeValue are only currently supported
  102. // if the input is a map[string]interface{} type. []byte within typed structs are not
  103. // converted correctly and are converted into base64 strings. This is a known bug,
  104. // and will be fixed in a later release.
  105. //
  106. // If in contains any structs, it is first JSON encoded/decoded it to convert it
  107. // to a []interface{}, so `json` struct tags are respected.
  108. //
  109. // Deprecated: Use MarshalList instead
  110. func ConvertToList(in interface{}) (item []*dynamodb.AttributeValue, err error) {
  111. defer func() {
  112. if r := recover(); r != nil {
  113. if e, ok := r.(runtime.Error); ok {
  114. err = e
  115. } else if s, ok := r.(string); ok {
  116. err = fmt.Errorf(s)
  117. } else {
  118. err = r.(error)
  119. }
  120. item = nil
  121. }
  122. }()
  123. if in == nil {
  124. return nil, awserr.New("SerializationError",
  125. "in must be an array or slice, got <nil>",
  126. nil)
  127. }
  128. v := reflect.ValueOf(in)
  129. if v.Kind() != reflect.Array && v.Kind() != reflect.Slice {
  130. return nil, awserr.New("SerializationError",
  131. fmt.Sprintf("in must be an array or slice, got %s",
  132. v.Type().String()),
  133. nil)
  134. }
  135. if isTyped(reflect.TypeOf(in)) {
  136. var out []interface{}
  137. in = convertToUntyped(in, out)
  138. }
  139. item = make([]*dynamodb.AttributeValue, 0, len(in.([]interface{})))
  140. for _, v := range in.([]interface{}) {
  141. item = append(item, convertTo(v))
  142. }
  143. return item, nil
  144. }
  145. // ConvertFromList accepts a []*dynamodb.AttributeValue and converts it to an array or
  146. // slice.
  147. //
  148. // If v contains any structs, the result is first converted it to a
  149. // []interface{}, then JSON encoded/decoded it to convert to a typed array or
  150. // slice, so `json` struct tags are respected.
  151. //
  152. // Deprecated: Use UnmarshalList instead
  153. func ConvertFromList(item []*dynamodb.AttributeValue, v interface{}) (err error) {
  154. defer func() {
  155. if r := recover(); r != nil {
  156. if e, ok := r.(runtime.Error); ok {
  157. err = e
  158. } else if s, ok := r.(string); ok {
  159. err = fmt.Errorf(s)
  160. } else {
  161. err = r.(error)
  162. }
  163. item = nil
  164. }
  165. }()
  166. rv := reflect.ValueOf(v)
  167. if rv.Kind() != reflect.Ptr || rv.IsNil() {
  168. return awserr.New("SerializationError",
  169. fmt.Sprintf("v must be a non-nil pointer to an array or slice, got %s",
  170. rv.Type()),
  171. nil)
  172. }
  173. if rv.Elem().Kind() != reflect.Array && rv.Elem().Kind() != reflect.Slice {
  174. return awserr.New("SerializationError",
  175. fmt.Sprintf("v must be a non-nil pointer to an array or slice, got %s",
  176. rv.Type()),
  177. nil)
  178. }
  179. l := make([]interface{}, 0, len(item))
  180. for _, v := range item {
  181. l = append(l, convertFrom(v))
  182. }
  183. if isTyped(reflect.TypeOf(v)) {
  184. err = convertToTyped(l, v)
  185. } else {
  186. rv.Elem().Set(reflect.ValueOf(l))
  187. }
  188. return err
  189. }
  190. // ConvertTo accepts any interface{} and converts it to a *dynamodb.AttributeValue.
  191. //
  192. // If in contains any structs, it is first JSON encoded/decoded it to convert it
  193. // to a interface{}, so `json` struct tags are respected.
  194. //
  195. // Deprecated: Use Marshal instead
  196. func ConvertTo(in interface{}) (item *dynamodb.AttributeValue, err error) {
  197. defer func() {
  198. if r := recover(); r != nil {
  199. if e, ok := r.(runtime.Error); ok {
  200. err = e
  201. } else if s, ok := r.(string); ok {
  202. err = fmt.Errorf(s)
  203. } else {
  204. err = r.(error)
  205. }
  206. item = nil
  207. }
  208. }()
  209. if in != nil && isTyped(reflect.TypeOf(in)) {
  210. var out interface{}
  211. in = convertToUntyped(in, out)
  212. }
  213. item = convertTo(in)
  214. return item, nil
  215. }
  216. // ConvertFrom accepts a *dynamodb.AttributeValue and converts it to any interface{}.
  217. //
  218. // If v contains any structs, the result is first converted it to a interface{},
  219. // then JSON encoded/decoded it to convert to a struct, so `json` struct tags
  220. // are respected.
  221. //
  222. // Deprecated: Use Unmarshal instead
  223. func ConvertFrom(item *dynamodb.AttributeValue, v interface{}) (err error) {
  224. defer func() {
  225. if r := recover(); r != nil {
  226. if e, ok := r.(runtime.Error); ok {
  227. err = e
  228. } else if s, ok := r.(string); ok {
  229. err = fmt.Errorf(s)
  230. } else {
  231. err = r.(error)
  232. }
  233. item = nil
  234. }
  235. }()
  236. rv := reflect.ValueOf(v)
  237. if rv.Kind() != reflect.Ptr || rv.IsNil() {
  238. return awserr.New("SerializationError",
  239. fmt.Sprintf("v must be a non-nil pointer to an interface{} or struct, got %s",
  240. rv.Type()),
  241. nil)
  242. }
  243. if rv.Elem().Kind() != reflect.Interface && rv.Elem().Kind() != reflect.Struct {
  244. return awserr.New("SerializationError",
  245. fmt.Sprintf("v must be a non-nil pointer to an interface{} or struct, got %s",
  246. rv.Type()),
  247. nil)
  248. }
  249. res := convertFrom(item)
  250. if isTyped(reflect.TypeOf(v)) {
  251. err = convertToTyped(res, v)
  252. } else if res != nil {
  253. rv.Elem().Set(reflect.ValueOf(res))
  254. }
  255. return err
  256. }
  257. func isTyped(v reflect.Type) bool {
  258. switch v.Kind() {
  259. case reflect.Struct:
  260. return true
  261. case reflect.Array, reflect.Slice:
  262. if isTyped(v.Elem()) {
  263. return true
  264. }
  265. case reflect.Map:
  266. if isTyped(v.Key()) {
  267. return true
  268. }
  269. if isTyped(v.Elem()) {
  270. return true
  271. }
  272. case reflect.Ptr:
  273. return isTyped(v.Elem())
  274. }
  275. return false
  276. }
  277. func convertToUntyped(in, out interface{}) interface{} {
  278. b, err := json.Marshal(in)
  279. if err != nil {
  280. panic(err)
  281. }
  282. decoder := json.NewDecoder(bytes.NewReader(b))
  283. decoder.UseNumber()
  284. err = decoder.Decode(&out)
  285. if err != nil {
  286. panic(err)
  287. }
  288. return out
  289. }
  290. func convertToTyped(in, out interface{}) error {
  291. b, err := json.Marshal(in)
  292. if err != nil {
  293. return err
  294. }
  295. decoder := json.NewDecoder(bytes.NewReader(b))
  296. return decoder.Decode(&out)
  297. }
  298. func convertTo(in interface{}) *dynamodb.AttributeValue {
  299. a := &dynamodb.AttributeValue{}
  300. if in == nil {
  301. a.NULL = new(bool)
  302. *a.NULL = true
  303. return a
  304. }
  305. if m, ok := in.(map[string]interface{}); ok {
  306. a.M = make(map[string]*dynamodb.AttributeValue)
  307. for k, v := range m {
  308. a.M[k] = convertTo(v)
  309. }
  310. return a
  311. }
  312. v := reflect.ValueOf(in)
  313. switch v.Kind() {
  314. case reflect.Bool:
  315. a.BOOL = new(bool)
  316. *a.BOOL = v.Bool()
  317. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  318. a.N = new(string)
  319. *a.N = strconv.FormatInt(v.Int(), 10)
  320. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  321. a.N = new(string)
  322. *a.N = strconv.FormatUint(v.Uint(), 10)
  323. case reflect.Float32, reflect.Float64:
  324. a.N = new(string)
  325. *a.N = strconv.FormatFloat(v.Float(), 'f', -1, 64)
  326. case reflect.String:
  327. if n, ok := in.(json.Number); ok {
  328. a.N = new(string)
  329. *a.N = n.String()
  330. } else {
  331. a.S = new(string)
  332. *a.S = v.String()
  333. }
  334. case reflect.Slice:
  335. switch v.Type() {
  336. case reflect.TypeOf(([]byte)(nil)):
  337. a.B = v.Bytes()
  338. default:
  339. a.L = make([]*dynamodb.AttributeValue, v.Len())
  340. for i := 0; i < v.Len(); i++ {
  341. a.L[i] = convertTo(v.Index(i).Interface())
  342. }
  343. }
  344. default:
  345. panic(fmt.Sprintf("the type %s is not supported", v.Type().String()))
  346. }
  347. return a
  348. }
  349. func convertFrom(a *dynamodb.AttributeValue) interface{} {
  350. if a.S != nil {
  351. return *a.S
  352. }
  353. if a.N != nil {
  354. // Number is tricky b/c we don't know which numeric type to use. Here we
  355. // simply try the different types from most to least restrictive.
  356. if n, err := strconv.ParseInt(*a.N, 10, 64); err == nil {
  357. return int(n)
  358. }
  359. if n, err := strconv.ParseUint(*a.N, 10, 64); err == nil {
  360. return uint(n)
  361. }
  362. n, err := strconv.ParseFloat(*a.N, 64)
  363. if err != nil {
  364. panic(err)
  365. }
  366. return n
  367. }
  368. if a.BOOL != nil {
  369. return *a.BOOL
  370. }
  371. if a.NULL != nil {
  372. return nil
  373. }
  374. if a.M != nil {
  375. m := make(map[string]interface{})
  376. for k, v := range a.M {
  377. m[k] = convertFrom(v)
  378. }
  379. return m
  380. }
  381. if a.L != nil {
  382. l := make([]interface{}, len(a.L))
  383. for index, v := range a.L {
  384. l[index] = convertFrom(v)
  385. }
  386. return l
  387. }
  388. if a.B != nil {
  389. return a.B
  390. }
  391. panic(fmt.Sprintf("%#v is not a supported dynamodb.AttributeValue", a))
  392. }