Skip to content

Commit

Permalink
clean up the code a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
guregu committed Dec 8, 2023
1 parent e8575d2 commit b6a30d9
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 171 deletions.
6 changes: 5 additions & 1 deletion debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ import (
"text/tabwriter"
)

// TODO: delete me
// debugging tools for dynamo developers
// disabled by default, use `go build -tags debug` to enable
// warning: not covered by API stability guarantees

// DumpType dumps a description of x's typedef to stdout.
func DumpType(x any) {
plan, err := typedefOf(reflect.TypeOf(x))
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion decodefunc.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func decodeMap(decodeKey func(reflect.Value, string) error) func(plan *typedef,
for name, av := range item {
vp := new(V)
decodeKey(kp, name)
decodeAttr(av, vp) // TODO fix order
decodeAttr(av, vp) // TODO: make argument order consistent
out[*kp] = *vp
}
*/
Expand Down
2 changes: 1 addition & 1 deletion encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func marshalSliceNoOmit(values []interface{}) ([]*dynamodb.AttributeValue, error
return avs, nil
}

func encodeItem(fields []fieldMeta, rv reflect.Value) (Item, error) {
func encodeItem(fields []structField, rv reflect.Value) (Item, error) {
item := make(Item, len(fields))
for _, field := range fields {
fv := dig(rv, field.index)
Expand Down
50 changes: 43 additions & 7 deletions encodefunc.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,22 @@ func encodeType(rt reflect.Type, flags encodeFlags) (encodeFunc, error) {
return nil, fmt.Errorf("dynamo marshal: unsupported type %s", rt.String())
}

func encodePtr(rt reflect.Type, flags encodeFlags) (encodeFunc, error) {
elem, err := encodeType(rt.Elem(), flags)
if err != nil {
return nil, err
}
return func(rv reflect.Value, flags encodeFlags) (*dynamodb.AttributeValue, error) {
if rv.IsNil() {
if flags&flagNull != 0 {
return nullAV, nil
}
return nil, nil
}
return elem(rv.Elem(), flags)
}, nil
}

func encode2[T any](fn func(T, encodeFlags) (*dynamodb.AttributeValue, error)) func(rv reflect.Value, flags encodeFlags) (*dynamodb.AttributeValue, error) {
target := reflect.TypeOf((*T)(nil)).Elem()
interfacing := target.Kind() == reflect.Interface
Expand Down Expand Up @@ -133,7 +149,7 @@ func encodeString(rv reflect.Value, flags encodeFlags) (*dynamodb.AttributeValue
s := rv.String()
if len(s) == 0 {
if flags&flagAllowEmpty != 0 {
return &dynamodb.AttributeValue{S: &s}, nil
return emptyS, nil
}
if flags&flagNull != 0 {
return nullAV, nil
Expand All @@ -158,20 +174,40 @@ var encodeTextMarshaler = encode2[encoding.TextMarshaler](func(x encoding.TextMa
return &dynamodb.AttributeValue{S: &str}, nil
})

func encodePtr(rt reflect.Type, flags encodeFlags) (encodeFunc, error) {
elem, err := encodeType(rt.Elem(), flags)
if err != nil {
return nil, err
func encodeBytes(rt reflect.Type, flags encodeFlags) encodeFunc {
if rt.Kind() == reflect.Array {
size := rt.Len()
return func(rv reflect.Value, flags encodeFlags) (*dynamodb.AttributeValue, error) {
if rv.IsZero() {
switch {
case flags&flagNull != 0:
return nullAV, nil
case flags&flagAllowEmpty != 0:
return emptyB, nil
}
return nil, nil
}
data := make([]byte, size)
reflect.Copy(reflect.ValueOf(data), rv)
return &dynamodb.AttributeValue{B: data}, nil
}
}

return func(rv reflect.Value, flags encodeFlags) (*dynamodb.AttributeValue, error) {
if rv.IsNil() {
if flags&flagNull != 0 {
return nullAV, nil
}
return nil, nil
}
return elem(rv.Elem(), flags)
}, nil
if rv.Len() == 0 {
if flags&flagAllowEmpty != 0 {
return emptyB, nil
}
return nil, nil
}
return &dynamodb.AttributeValue{B: rv.Bytes()}, nil
}
}

func encodeStruct(rt reflect.Type) (encodeFunc, error) {
Expand Down
Loading

0 comments on commit b6a30d9

Please sign in to comment.