Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom types #104

Open
wants to merge 29 commits into
base: feature/embeded-structs
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
53197ad
working version
skimata Feb 4, 2017
9045ea9
fix text
skimata Feb 6, 2017
7bb11b8
combine test files
skimata Feb 6, 2017
87fcc79
move private funcs to bottom
skimata Feb 6, 2017
1b5f1b4
ErrInvalidType should ignore interfaces
skimata Feb 21, 2017
06cdde6
replace MarshalOnePayload w/ MarshalPayload; fix bug w/ node merge()
skimata Jul 18, 2017
01c2432
minor tweaks; address a couple comments
skimata Jul 18, 2017
ad5f5cd
decompose unmarshalNode() to smaller funcs; unmarshal should go from …
skimata Jul 18, 2017
ab94c5a
deep copy the node when passing relation/sideloaded notes to unmarshal()
skimata Jul 19, 2017
7d26540
add some comments and do some additional cleanup
skimata Jul 19, 2017
f79a192
add test uses annotationIgnore
skimata Jul 19, 2017
deeffb7
implement support for struct fields that implement json.Marshaler/Unm…
skimata Jul 20, 2017
c66d1da
add additional test that compares marshal/unmarshal behavior w/ stand…
skimata Jul 20, 2017
218abd9
add support for pointer embedded structs
skimata Jul 21, 2017
7f51be8
add support for slice of json.Marshaler/Unmarshaler; add UnixMilli type
skimata Jul 25, 2017
fc52cdf
add support for maps of json.Marshaler/Unmarshaler
skimata Jul 25, 2017
405fe10
additional tests for marshal/unmarshal behavior of custom types
skimata Jul 25, 2017
3aaeac8
add float/exponential notation support for UnixMilli
skimata Jul 26, 2017
ddc78e5
fix TestEmbededStructs_nonNilStructPtr; bug on loop w/ (multiple) emb…
skimata Jul 28, 2017
01f918e
fix TestMarshal_duplicatePrimaryAnnotationFromEmbeddedStructs; fix or…
skimata Jul 28, 2017
110f01b
Merge branch 'feature/embeded-structs-fix-tests' into custom-types
skimata Jul 28, 2017
d874c21
make ISO8601Datetime and UnixMilli private
skimata Jul 28, 2017
75748b1
clean up comment
skimata Jul 28, 2017
1a042c5
simplify and refactor handleAttributeUnmarshal()
skimata Jul 28, 2017
db8ca4e
cleanup comment
skimata Jul 28, 2017
bf72c7a
improve check on json.Unmarshaler implementations
skimata Jul 31, 2017
e55ed48
cleanup handlePrimaryUnmarshal()
skimata Jul 31, 2017
37d04ea
rename fieldType to structField
skimata Jul 31, 2017
a6437e5
remove 'ErrInvalidType' response; allow all json lib supported types …
skimata Aug 16, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
cleanup handlePrimaryUnmarshal()
skimata committed Jul 31, 2017
commit e55ed48dcc7e547b887425a545f0b686d5c73555
69 changes: 19 additions & 50 deletions request.go
Original file line number Diff line number Diff line change
@@ -274,65 +274,34 @@ func handlePrimaryUnmarshal(data *Node, args []string, fieldType reflect.StructF
kind = fieldType.Type.Kind()
}

var idValue reflect.Value
switch kind {
default:
// only handle strings and numerics
return ErrBadJSONAPIID
case reflect.String:
assign(fieldValue, reflect.ValueOf(data.ID))
case
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:

// Handle String case
if kind == reflect.String {
// ID will have to be transmitted as a string per the JSON API spec
idValue = reflect.ValueOf(data.ID)
} else {
// Value was not a string... only other supported type was a numeric,
// which would have been sent as a float value.
floatValue, err := strconv.ParseFloat(data.ID, 64)
fv, err := strconv.ParseFloat(data.ID, 64)
if err != nil {
// Could not convert the value in the "id" attr to a float
return ErrBadJSONAPIID
}

// Convert the numeric float to one of the supported ID numeric types
// (int[8,16,32,64] or uint[8,16,32,64])
switch kind {
case reflect.Int:
n := int(floatValue)
idValue = reflect.ValueOf(&n)
case reflect.Int8:
n := int8(floatValue)
idValue = reflect.ValueOf(&n)
case reflect.Int16:
n := int16(floatValue)
idValue = reflect.ValueOf(&n)
case reflect.Int32:
n := int32(floatValue)
idValue = reflect.ValueOf(&n)
case reflect.Int64:
n := int64(floatValue)
idValue = reflect.ValueOf(&n)
case reflect.Uint:
n := uint(floatValue)
idValue = reflect.ValueOf(&n)
case reflect.Uint8:
n := uint8(floatValue)
idValue = reflect.ValueOf(&n)
case reflect.Uint16:
n := uint16(floatValue)
idValue = reflect.ValueOf(&n)
case reflect.Uint32:
n := uint32(floatValue)
idValue = reflect.ValueOf(&n)
case reflect.Uint64:
n := uint64(floatValue)
idValue = reflect.ValueOf(&n)
default:
// We had a JSON float (numeric), but our field was not one of the
// allowed numeric types
return ErrBadJSONAPIID
b, err := json.Marshal(fv)
if err != nil {
return err
}

v := fieldValue.Addr().Interface()
if err := json.Unmarshal(b, v); err != nil {
return err
}
}

// set value and clear ID to denote it's already been processed
assign(fieldValue, idValue)
// clear ID to denote it's already been processed
data.ID = ""

return nil
}