jsonapi icon indicating copy to clipboard operation
jsonapi copied to clipboard

API not idiomatic

Open emersion opened this issue 9 years ago • 5 comments

Get* functions are not idiomatic in Go.

emersion avatar Sep 09 '16 16:09 emersion

What about something like https://golang.org/pkg/net/http/#Header?

smotes avatar Sep 09 '16 16:09 smotes

See https://golang.org/doc/effective_go.html#Getters

(For http.Header the Get function returns the field given as param so it's okay)

emersion avatar Sep 09 '16 16:09 emersion

Ok, so then that would imply a change to the read adapters' signatures. Something along the lines of:

type identityReadAdapter interface {
    ID() (string, error)
    Type() (string, error)
}

type attributesReadAdapter interface {
    Attributes() (map[string]interface{}, error)
}

...etc

It doesn't seem like implementing any of those methods would conflict with other packages. The only real issue I have here is that they aren't true getters since they return a value/error pair, so is it even appropriate to consider them under this idiom?

Also I'd think to leave the jsonapi.Links and jsonapi.Relationships signatures unchanged since they work in a similar way as http.Header.

smotes avatar Sep 09 '16 21:09 smotes

Why do these functions return an error in the first place?

emersion avatar Sep 10 '16 08:09 emersion

Because errors can occur while converting data to/from the resource. For a concrete example, consider that the specification states that a resource id member must be of type string. However, it is not uncommon to use an integer to identify an object on the backend. In order to avoid ignoring errors in cases like this (for instance, calling something like strconv.ParseInt), all read adapter implementations should return a value/error pair and all write adapter implementations should return an error.

smotes avatar Sep 12 '16 19:09 smotes