Skip to content

Commit

Permalink
remove unused error return, enhance docs, style fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonc committed Nov 15, 2023
1 parent 1037764 commit 960294d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
40 changes: 30 additions & 10 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ var (
ErrUnknownFieldNumberType = errors.New("The struct field was not of a known number type")
// ErrInvalidType is returned when the given type is incompatible with the expected type.
ErrInvalidType = errors.New("Invalid type provided") // I wish we used punctuation.
// ErrTypeNotFound is returned when the given type not found on the model.
ErrTypeNotFound = errors.New("no primary type annotation found on model")
)

// ErrUnsupportedPtrType is returned when the Struct field was a pointer but
Expand Down Expand Up @@ -155,7 +157,13 @@ func jsonapiTypeOfModel(structModel reflect.Type) (string, error) {
for i := 0; i < structModel.NumField(); i++ {
fieldType := structModel.Field(i)
args, err := getStructTags(fieldType)
if err != nil || len(args) < 2 {

// A jsonapi tag was found, but it was improperly structured
if err != nil {
return "", err
}

if len(args) < 2 {
continue
}

Expand All @@ -164,7 +172,7 @@ func jsonapiTypeOfModel(structModel reflect.Type) (string, error) {
}
}

return "", errors.New("no primary annotation found on model")
return "", ErrTypeNotFound
}

// structFieldIndex holds a bit of information about a type found at a struct field index
Expand All @@ -175,15 +183,31 @@ type structFieldIndex struct {

// choiceStructMapping reflects on a value that may be a slice
// of choice type structs or a choice type struct. A choice type
// struct is a struct comprising of pointers to other jsonapi models,
// struct is a struct comprised of pointers to other jsonapi models,
// only one of which is populated with a value by the decoder.
//
// The specified type is probed and a map is generated that maps the
// underlying model type (its 'primary' type) to the field number
// within the choice type struct. This data can then be used to correctly
// assign each data relationship node to the correct choice type
// struct field.
func choiceStructMapping(choice reflect.Type) (result map[string]structFieldIndex, err error) {
//
// For example, if the `choice` type was
//
// type OneOfMedia struct {
// Video *Video
// Image *Image
// }
//
// then the resulting map would be
//
// {
// "videos" => {Video, 0}
// "images" => {Image, 1}
// }
//
// where `"videos"` is the value of the `primary` annotation on the `Video` model
func choiceStructMapping(choice reflect.Type) (result map[string]structFieldIndex) {
result = make(map[string]structFieldIndex)

for choice.Kind() != reflect.Struct {
Expand Down Expand Up @@ -213,7 +237,7 @@ func choiceStructMapping(choice reflect.Type) (result map[string]structFieldInde
}
}

return result, nil
return result
}

func getStructTags(field reflect.StructField) ([]string, error) {
Expand Down Expand Up @@ -395,11 +419,7 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node)
// struct type field.
var choiceMapping map[string]structFieldIndex = nil
if annotation == annotationPolyRelation {
choiceMapping, err = choiceStructMapping(fieldValue.Type())
if err != nil {
er = err
break
}
choiceMapping = choiceStructMapping(fieldValue.Type())
}

if isSlice {
Expand Down
5 changes: 1 addition & 4 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -743,10 +743,7 @@ func Test_choiceStructMapping(t *testing.T) {
}

for _, c := range cases {
result, err := choiceStructMapping(c.val)
if err != nil {
t.Fatal(err)
}
result := choiceStructMapping(c.val)
imageField, ok := result["images"]
if !ok || imageField.FieldNum != 0 {
t.Errorf("expected \"images\" to be the first field, but got %d", imageField.FieldNum)
Expand Down

0 comments on commit 960294d

Please sign in to comment.