1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
|
package dynamodb
import (
"encoding/base64"
"encoding/json"
"fmt"
"math"
"reflect"
"sort"
"strconv"
"strings"
"sync"
"unicode"
)
func MarshalAttributes(m interface{}) ([]Attribute, error) {
v := reflect.ValueOf(m).Elem()
builder := &attributeBuilder{}
builder.buffer = []Attribute{}
for _, f := range cachedTypeFields(v.Type()) { // loop on each field
fv := fieldByIndex(v, f.index)
if !fv.IsValid() || isEmptyValueToOmit(fv) {
continue
}
err := builder.reflectToDynamoDBAttribute(f.name, fv)
if err != nil {
return builder.buffer, err
}
}
return builder.buffer, nil
}
func UnmarshalAttributes(attributesRef *map[string]*Attribute, m interface{}) error {
rv := reflect.ValueOf(m)
if rv.Kind() != reflect.Ptr || rv.IsNil() {
return fmt.Errorf("InvalidUnmarshalError reflect.ValueOf(v): %#v, m interface{}: %#v", rv, reflect.TypeOf(m))
}
v := reflect.ValueOf(m).Elem()
attributes := *attributesRef
for _, f := range cachedTypeFields(v.Type()) { // loop on each field
fv := fieldByIndex(v, f.index)
correlatedAttribute := attributes[f.name]
if correlatedAttribute == nil {
continue
}
err := unmarshallAttribute(correlatedAttribute, fv)
if err != nil {
return err
}
}
return nil
}
type attributeBuilder struct {
buffer []Attribute
}
func (builder *attributeBuilder) Push(attribute *Attribute) {
builder.buffer = append(builder.buffer, *attribute)
}
func unmarshallAttribute(a *Attribute, v reflect.Value) error {
switch v.Kind() {
case reflect.Bool:
n, err := strconv.ParseInt(a.Value, 10, 64)
if err != nil {
return fmt.Errorf("UnmarshalTypeError (bool) %#v: %#v", a.Value, err)
}
v.SetBool(n != 0)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
n, err := strconv.ParseInt(a.Value, 10, 64)
if err != nil || v.OverflowInt(n) {
return fmt.Errorf("UnmarshalTypeError (number) %#v: %#v", a.Value, err)
}
v.SetInt(n)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
n, err := strconv.ParseUint(a.Value, 10, 64)
if err != nil || v.OverflowUint(n) {
return fmt.Errorf("UnmarshalTypeError (number) %#v: %#v", a.Value, err)
}
v.SetUint(n)
case reflect.Float32, reflect.Float64:
n, err := strconv.ParseFloat(a.Value, v.Type().Bits())
if err != nil || v.OverflowFloat(n) {
return fmt.Errorf("UnmarshalTypeError (number) %#v: %#v", a.Value, err)
}
v.SetFloat(n)
case reflect.String:
v.SetString(a.Value)
case reflect.Slice:
if v.Type().Elem().Kind() == reflect.Uint8 { // byte arrays are a special case
b := make([]byte, base64.StdEncoding.DecodedLen(len(a.Value)))
n, err := base64.StdEncoding.Decode(b, []byte(a.Value))
if err != nil {
return fmt.Errorf("UnmarshalTypeError (byte) %#v: %#v", a.Value, err)
}
v.Set(reflect.ValueOf(b[0:n]))
break
}
if a.SetType() { // Special NS and SS types should be correctly handled
nativeSetCreated := false
switch v.Type().Elem().Kind() {
case reflect.Bool:
nativeSetCreated = true
arry := reflect.MakeSlice(v.Type(), len(a.SetValues), len(a.SetValues))
for i, aval := range a.SetValues {
n, err := strconv.ParseInt(aval, 10, 64)
if err != nil {
return fmt.Errorf("UnmarshalSetTypeError (bool) %#v: %#v", aval, err)
}
arry.Index(i).SetBool(n != 0)
}
v.Set(arry)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
nativeSetCreated = true
arry := reflect.MakeSlice(v.Type(), len(a.SetValues), len(a.SetValues))
for i, aval := range a.SetValues {
n, err := strconv.ParseInt(aval, 10, 64)
if err != nil || arry.Index(i).OverflowInt(n) {
return fmt.Errorf("UnmarshalSetTypeError (number) %#v: %#v", aval, err)
}
arry.Index(i).SetInt(n)
}
v.Set(arry)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
nativeSetCreated = true
arry := reflect.MakeSlice(v.Type(), len(a.SetValues), len(a.SetValues))
for i, aval := range a.SetValues {
n, err := strconv.ParseUint(aval, 10, 64)
if err != nil || arry.Index(i).OverflowUint(n) {
return fmt.Errorf("UnmarshalSetTypeError (number) %#v: %#v", aval, err)
}
arry.Index(i).SetUint(n)
}
v.Set(arry)
case reflect.Float32, reflect.Float64:
nativeSetCreated = true
arry := reflect.MakeSlice(v.Type(), len(a.SetValues), len(a.SetValues))
for i, aval := range a.SetValues {
n, err := strconv.ParseFloat(aval, arry.Index(i).Type().Bits())
if err != nil || arry.Index(i).OverflowFloat(n) {
return fmt.Errorf("UnmarshalSetTypeError (number) %#v: %#v", aval, err)
}
arry.Index(i).SetFloat(n)
}
v.Set(arry)
case reflect.String:
nativeSetCreated = true
arry := reflect.MakeSlice(v.Type(), len(a.SetValues), len(a.SetValues))
for i, aval := range a.SetValues {
arry.Index(i).SetString(aval)
}
v.Set(arry)
}
if nativeSetCreated {
break
}
}
// Slices can be marshalled as nil, but otherwise are handled
// as arrays.
fallthrough
case reflect.Array, reflect.Struct, reflect.Map, reflect.Interface, reflect.Ptr:
unmarshalled := reflect.New(v.Type())
err := json.Unmarshal([]byte(a.Value), unmarshalled.Interface())
if err != nil {
return err
}
v.Set(unmarshalled.Elem())
default:
return fmt.Errorf("UnsupportedTypeError %#v", v.Type())
}
return nil
}
// reflectValueQuoted writes the value in v to the output.
// If quoted is true, the serialization is wrapped in a JSON string.
func (e *attributeBuilder) reflectToDynamoDBAttribute(name string, v reflect.Value) error {
if !v.IsValid() {
return nil
} // don't build
switch v.Kind() {
case reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr, reflect.Float32, reflect.Float64:
rv, err := numericReflectedValueString(v)
if err != nil {
return err
}
e.Push(NewNumericAttribute(name, rv))
case reflect.String:
e.Push(NewStringAttribute(name, v.String()))
case reflect.Slice:
if v.IsNil() {
break
}
if v.Type().Elem().Kind() == reflect.Uint8 {
// Byte slices are treated as errors
s := v.Bytes()
dst := make([]byte, base64.StdEncoding.EncodedLen(len(s)))
base64.StdEncoding.Encode(dst, s)
e.Push(NewStringAttribute(name, string(dst)))
break
}
// Special NS and SS types should be correctly handled
nativeSetCreated := false
switch v.Type().Elem().Kind() {
case reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr, reflect.Float32, reflect.Float64:
nativeSetCreated = true
arrystrings := make([]string, v.Len())
for i, _ := range arrystrings {
var err error
arrystrings[i], err = numericReflectedValueString(v.Index(i))
if err != nil {
return err
}
}
e.Push(NewNumericSetAttribute(name, arrystrings))
case reflect.String: // simple copy will suffice
nativeSetCreated = true
arrystrings := make([]string, v.Len())
for i, _ := range arrystrings {
arrystrings[i] = v.Index(i).String()
}
e.Push(NewStringSetAttribute(name, arrystrings))
}
if nativeSetCreated {
break
}
// Slices can be marshalled as nil, but otherwise are handled
// as arrays.
fallthrough
case reflect.Array, reflect.Struct, reflect.Map, reflect.Interface, reflect.Ptr:
jsonVersion, err := json.Marshal(v.Interface())
if err != nil {
return err
}
escapedJson := `"` + string(jsonVersion) + `"` // strconv.Quote not required because the entire string is escaped from json Marshall
e.Push(NewStringAttribute(name, escapedJson[1:len(escapedJson)-1]))
default:
return fmt.Errorf("UnsupportedTypeError %#v", v.Type())
}
return nil
}
func numericReflectedValueString(v reflect.Value) (string, error) {
switch v.Kind() {
case reflect.Bool:
x := v.Bool()
if x {
return "1", nil
} else {
return "0", nil
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return strconv.FormatInt(v.Int(), 10), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return strconv.FormatUint(v.Uint(), 10), nil
case reflect.Float32, reflect.Float64:
f := v.Float()
if math.IsInf(f, 0) || math.IsNaN(f) {
return "", fmt.Errorf("UnsupportedValueError %#v (formatted float: %s)", v, strconv.FormatFloat(f, 'g', -1, v.Type().Bits()))
}
return strconv.FormatFloat(f, 'g', -1, v.Type().Bits()), nil
}
return "", fmt.Errorf("UnsupportedNumericValueError %#v", v.Type())
}
// In DynamoDB we should omit empty value in some type
// See http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_PutItem.html
func isEmptyValueToOmit(v reflect.Value) bool {
switch v.Kind() {
case reflect.Array, reflect.Map, reflect.Slice, reflect.String, reflect.Interface, reflect.Ptr:
// should omit if empty value
return isEmptyValue(v)
}
// otherwise should not omit
return false
}
// ---------------- Below are copied handy functions from http://golang.org/src/pkg/encoding/json/encode.go --------------------------------
func isEmptyValue(v reflect.Value) bool {
switch v.Kind() {
case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
return v.Len() == 0
case reflect.Bool:
return !v.Bool()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return v.Int() == 0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return v.Uint() == 0
case reflect.Float32, reflect.Float64:
return v.Float() == 0
case reflect.Interface, reflect.Ptr:
return v.IsNil()
}
return false
}
func fieldByIndex(v reflect.Value, index []int) reflect.Value {
for _, i := range index {
if v.Kind() == reflect.Ptr {
if v.IsNil() {
return reflect.Value{}
}
v = v.Elem()
}
v = v.Field(i)
}
return v
}
// A field represents a single field found in a struct.
type field struct {
name string
tag bool
index []int
typ reflect.Type
omitEmpty bool
quoted bool
}
// byName sorts field by name, breaking ties with depth,
// then breaking ties with "name came from json tag", then
// breaking ties with index sequence.
type byName []field
func (x byName) Len() int { return len(x) }
func (x byName) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
func (x byName) Less(i, j int) bool {
if x[i].name != x[j].name {
return x[i].name < x[j].name
}
if len(x[i].index) != len(x[j].index) {
return len(x[i].index) < len(x[j].index)
}
if x[i].tag != x[j].tag {
return x[i].tag
}
return byIndex(x).Less(i, j)
}
// byIndex sorts field by index sequence.
type byIndex []field
func (x byIndex) Len() int { return len(x) }
func (x byIndex) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
func (x byIndex) Less(i, j int) bool {
for k, xik := range x[i].index {
if k >= len(x[j].index) {
return false
}
if xik != x[j].index[k] {
return xik < x[j].index[k]
}
}
return len(x[i].index) < len(x[j].index)
}
func isValidTag(s string) bool {
if s == "" {
return false
}
for _, c := range s {
switch {
case strings.ContainsRune("!#$%&()*+-./:<=>?@[]^_{|}~ ", c):
// Backslash and quote chars are reserved, but
// otherwise any punctuation chars are allowed
// in a tag name.
default:
if !unicode.IsLetter(c) && !unicode.IsDigit(c) {
return false
}
}
}
return true
}
// tagOptions is the string following a comma in a struct field's "json"
// tag, or the empty string. It does not include the leading comma.
type tagOptions string
// Contains returns whether checks that a comma-separated list of options
// contains a particular substr flag. substr must be surrounded by a
// string boundary or commas.
func (o tagOptions) Contains(optionName string) bool {
if len(o) == 0 {
return false
}
s := string(o)
for s != "" {
var next string
i := strings.Index(s, ",")
if i >= 0 {
s, next = s[:i], s[i+1:]
}
if s == optionName {
return true
}
s = next
}
return false
}
// parseTag splits a struct field's json tag into its name and
// comma-separated options.
func parseTag(tag string) (string, tagOptions) {
if idx := strings.Index(tag, ","); idx != -1 {
return tag[:idx], tagOptions(tag[idx+1:])
}
return tag, tagOptions("")
}
// typeFields returns a list of fields that JSON should recognize for the given type.
// The algorithm is breadth-first search over the set of structs to include - the top struct
// and then any reachable anonymous structs.
func typeFields(t reflect.Type) []field {
// Anonymous fields to explore at the current level and the next.
current := []field{}
next := []field{{typ: t}}
// Count of queued names for current level and the next.
count := map[reflect.Type]int{}
nextCount := map[reflect.Type]int{}
// Types already visited at an earlier level.
visited := map[reflect.Type]bool{}
// Fields found.
var fields []field
for len(next) > 0 {
current, next = next, current[:0]
count, nextCount = nextCount, map[reflect.Type]int{}
for _, f := range current {
if visited[f.typ] {
continue
}
visited[f.typ] = true
// Scan f.typ for fields to include.
for i := 0; i < f.typ.NumField(); i++ {
sf := f.typ.Field(i)
if sf.PkgPath != "" { // unexported
continue
}
tag := sf.Tag.Get("json")
if tag == "-" {
continue
}
name, opts := parseTag(tag)
if !isValidTag(name) {
name = ""
}
index := make([]int, len(f.index)+1)
copy(index, f.index)
index[len(f.index)] = i
ft := sf.Type
if ft.Name() == "" && ft.Kind() == reflect.Ptr {
// Follow pointer.
ft = ft.Elem()
}
// Record found field and index sequence.
if name != "" || !sf.Anonymous || ft.Kind() != reflect.Struct {
tagged := name != ""
if name == "" {
name = sf.Name
}
fields = append(fields, field{name, tagged, index, ft,
opts.Contains("omitempty"), opts.Contains("string")})
if count[f.typ] > 1 {
// If there were multiple instances, add a second,
// so that the annihilation code will see a duplicate.
// It only cares about the distinction between 1 or 2,
// so don't bother generating any more copies.
fields = append(fields, fields[len(fields)-1])
}
continue
}
// Record new anonymous struct to explore in next round.
nextCount[ft]++
if nextCount[ft] == 1 {
next = append(next, field{name: ft.Name(), index: index, typ: ft})
}
}
}
}
sort.Sort(byName(fields))
// Delete all fields that are hidden by the Go rules for embedded fields,
// except that fields with JSON tags are promoted.
// The fields are sorted in primary order of name, secondary order
// of field index length. Loop over names; for each name, delete
// hidden fields by choosing the one dominant field that survives.
out := fields[:0]
for advance, i := 0, 0; i < len(fields); i += advance {
// One iteration per name.
// Find the sequence of fields with the name of this first field.
fi := fields[i]
name := fi.name
for advance = 1; i+advance < len(fields); advance++ {
fj := fields[i+advance]
if fj.name != name {
break
}
}
if advance == 1 { // Only one field with this name
out = append(out, fi)
continue
}
dominant, ok := dominantField(fields[i : i+advance])
if ok {
out = append(out, dominant)
}
}
fields = out
sort.Sort(byIndex(fields))
return fields
}
// dominantField looks through the fields, all of which are known to
// have the same name, to find the single field that dominates the
// others using Go's embedding rules, modified by the presence of
// JSON tags. If there are multiple top-level fields, the boolean
// will be false: This condition is an error in Go and we skip all
// the fields.
func dominantField(fields []field) (field, bool) {
// The fields are sorted in increasing index-length order. The winner
// must therefore be one with the shortest index length. Drop all
// longer entries, which is easy: just truncate the slice.
length := len(fields[0].index)
tagged := -1 // Index of first tagged field.
for i, f := range fields {
if len(f.index) > length {
fields = fields[:i]
break
}
if f.tag {
if tagged >= 0 {
// Multiple tagged fields at the same level: conflict.
// Return no field.
return field{}, false
}
tagged = i
}
}
if tagged >= 0 {
return fields[tagged], true
}
// All remaining fields have the same length. If there's more than one,
// we have a conflict (two fields named "X" at the same level) and we
// return no field.
if len(fields) > 1 {
return field{}, false
}
return fields[0], true
}
var fieldCache struct {
sync.RWMutex
m map[reflect.Type][]field
}
// cachedTypeFields is like typeFields but uses a cache to avoid repeated work.
func cachedTypeFields(t reflect.Type) []field {
fieldCache.RLock()
f := fieldCache.m[t]
fieldCache.RUnlock()
if f != nil {
return f
}
// Compute fields without lock.
// Might duplicate effort but won't hold other computations back.
f = typeFields(t)
if f == nil {
f = []field{}
}
fieldCache.Lock()
if fieldCache.m == nil {
fieldCache.m = map[reflect.Type][]field{}
}
fieldCache.m[t] = f
fieldCache.Unlock()
return f
}
|