Skip to content

Simplification of Unmarshal API & Access to Meta and Links #95

Open
@nstratos

Description

@nstratos

With the recent simplification of the Marshal API (cf83b97) I think it might be a good time to consider something similar for the Unmarshal case.

While doing so, it would be even better to provide a solution for accessing the Meta and Links of the JSON API document (relevant issues #82, #68 and #64).

Right now there are two core functions that handle Unmarshaling:

UnmarshalPayload(in io.Reader, model interface{}) error

UnmarshalManyPayload(in io.Reader, t reflect.Type) ([]interface{}, error)

As long as the model is a pointer to a struct or a pointer to a slice, these can be simplified to one function with the same exact signature as UnmarshalPayload(in io.Reader, model interface{}) error. I have already done a proof of concept (nstratos/go-kitsu@8b8706c) for a package I am writing and it seems to be working fine.

The name could be kept the same for compatibility while emphasizing in the docs the new functionality of providing a pointer to slice to fill with data (instead of using UnmarshalManyPayload) or it could be made to new a function that covers both cases. For this discussion I am going to name this new function UnmarshalAll but it could be anything.

A separate but related issue (because of the function signature) is giving access to Meta and Links. The simplest way would be to return something extra besides the error. Perhaps something like this:

type Extras struct {
    Links *Links
    Meta *Meta
}

// Expecting v to be pointer to struct or pointer to slice.
func UnmarshalAll(r io.Reader, v interface{}) (Extras, error)

Thus with this function, the Unmarshal API gets simplified and access to Meta and LInks is provided as well.

Thoughts?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions