Documentation
¶
Overview ¶
Example ¶
Example demonstrates marshaling and then unmarshaling (round-trip)
package main import ( "fmt" "github.com/nisimpson/jsonapi" ) // Example structs type User struct { ID string `jsonapi:"primary,users"` Name string `jsonapi:"attr,name"` Email string `jsonapi:"attr,email,omitempty"` } func main() { originalUser := User{ ID: "42", Name: "Alice Smith", Email: "alice@example.com", } // Marshal to JSON:API marshaledData, _ := jsonapi.Marshal(originalUser) // Unmarshal back to struct var roundTripUser User err := jsonapi.Unmarshal(marshaledData, &roundTripUser) if err != nil { fmt.Printf("Error: %v\n", err) } else { fmt.Printf("Round-trip User: ID=%s, Name=%s, Email=%s\n", roundTripUser.ID, roundTripUser.Name, roundTripUser.Email) fmt.Printf("Round-trip successful: %t\n", originalUser.ID == roundTripUser.ID && originalUser.Name == roundTripUser.Name && originalUser.Email == roundTripUser.Email) } }
Output: Round-trip User: ID=42, Name=Alice Smith, Email=alice@example.com Round-trip successful: true
Example (UnmarshalDocument) ¶
Example_unmarshalDocument demonstrates unmarshaling a JSON:API document directly
package main import ( "encoding/json" "fmt" "github.com/nisimpson/jsonapi" ) func main() { jsonData := `{ "data": { "type": "users", "id": "1", "attributes": { "name": "John Doe", "email": "john@example.com" } } }` doc := jsonapi.Document{} err := json.Unmarshal([]byte(jsonData), &doc) if err != nil { fmt.Printf("Error: %v\n", err) } else { resource, ok := doc.Data.One() if ok { fmt.Printf("Document Resource: ID=%s, Type=%s\n", resource.ID, resource.Type) fmt.Printf("Name: %s\n", resource.Attributes["name"]) fmt.Printf("Email: %s\n", resource.Attributes["email"]) } } }
Output: Document Resource: ID=1, Type=users Name: John Doe Email: john@example.com
Index ¶
- Constants
- Variables
- func DocumentLinks(links map[string]Link) func(*MarshalOptions)
- func DocumentMeta(meta map[string]interface{}) func(*MarshalOptions)
- func IncludeRelatedResources() func(*MarshalOptions)
- func Marshal(out interface{}, opts ...func(*MarshalOptions)) ([]byte, error)
- func MarshalWithContext(ctx context.Context, out interface{}, opts ...func(*MarshalOptions)) ([]byte, error)
- func PermitReadOnly(enabled bool) func(*UnmarshalOptions)
- func PopulateFromIncluded() func(*UnmarshalOptions)
- func SparseFieldsets(resourceType string, fields []string) func(*MarshalOptions)
- func StrictMode() func(*UnmarshalOptions)
- func Unmarshal(data []byte, out interface{}, opts ...func(*UnmarshalOptions)) error
- func UnmarshalDocument(ctx context.Context, doc *Document, out interface{}, ...) error
- func UnmarshalWithContext(ctx context.Context, data []byte, out interface{}, ...) error
- func WithMarshaler(fn func(interface{}) ([]byte, error)) func(*MarshalOptions)
- func WithUnmarshaler(fn func([]byte, interface{}) error) func(*UnmarshalOptions)
- type Document
- type Error
- type Link
- type LinksMarshaler
- type LinksUnmarshaler
- type MarshalOptions
- type MetaMarshaler
- type MetaUnmarshaler
- type MultiError
- type PrimaryData
- type Relationship
- type RelationshipLinksMarshaler
- type RelationshipLinksUnmarshaler
- type RelationshipMetaMarshaler
- type RelationshipMetaUnmarshaler
- type Resource
- type ResourceMarshaler
- type ResourceUnmarshaler
- type UnmarshalOptions
Examples ¶
Constants ¶
const ( // StructTagName is the name of the struct tag used for JSON:API field definitions. // Example: // // `jsonapi:"primary,users"` StructTagName = "jsonapi" // TagValuePrimary indicates a field contains the primary resource ID and type. // Format: `jsonapi:"primary,resource-type"` // Example: // // `jsonapi:"primary,users"` TagValuePrimary = "primary" // TagValueAttribute indicates a field should be marshaled as a JSON:API attribute. // Format: `jsonapi:"attr,attribute-name[,omitempty]"` // Example: // // `jsonapi:"attr,name"` or `jsonapi:"attr,email,omitempty"` TagValueAttribute = "attr" // TagValueRelationship indicates a field should be marshaled as a JSON:API relationship. // Format: `jsonapi:"relation,relationship-name[,omitempty]"` // Example: // // `jsonapi:"relation,posts"` or `jsonapi:"relation,profile,omitempty"` TagValueRelationship = "relation" // TagOptionOmitEmpty is a tag option that causes empty/zero values to be omitted // during marshaling. Can be used with both attributes and relationships. // Example: // // `jsonapi:"attr,email,omitempty"` TagOptionOmitEmpty = "omitempty" // TagOptionReadOnly is a tag option that prevents a field from being unmarshaled // unless the [PermitReadOnly] option is used. Read-only fields are still marshaled normally. // Can be used with both attributes and relationships. // Example: // // `jsonapi:"attr,created_at,readonly"` or `jsonapi:"relation,author,readonly"` TagOptionReadOnly = "readonly" // TagValueIgnore causes a field to be ignored during marshaling and unmarshaling. // Format: `jsonapi:"-"` TagValueIgnore = "-" )
Struct tag constants for JSON:API field definitions. These constants define the tag name and tag values used in struct field tags to control JSON:API marshaling and unmarshaling behavior.
Variables ¶
var ( // ErrReadOnly is returned when attempts to [Unmarshal] a [Document] with // readonly attributes occur. ErrReadOnly = errors.New("read-only") )
Functions ¶
func DocumentLinks ¶
func DocumentLinks(links map[string]Link) func(*MarshalOptions)
DocumentLinks returns a function that modifies MarshalOptions to add links to the JSON:API document. The provided links map will be set as the top-level links object in the resulting document. This can be used to add pagination, self-referential, or related resource links to the document.
func DocumentMeta ¶
func DocumentMeta(meta map[string]interface{}) func(*MarshalOptions)
DocumentMeta returns a function that modifies MarshalOptions to add metadata to the JSON:API document. The provided meta map will be set as the top-level meta object in the resulting document. This is useful for adding custom metadata like pagination info or document-level statistics.
func IncludeRelatedResources ¶
func IncludeRelatedResources() func(*MarshalOptions)
IncludeRelatedResources instructs the Marshaler to add any related resources found within the document's primary data to the included array.
func Marshal ¶
func Marshal(out interface{}, opts ...func(*MarshalOptions)) ([]byte, error)
Marshal marshals a Go struct into a JSON:API Resource using the default context.
Example (Custom) ¶
ExampleMarshalCustom demonstrates custom marshaling using the MarshalJSONAPIResource interface
package main import ( "context" "encoding/json" "fmt" "github.com/nisimpson/jsonapi" ) // Custom marshaler example type CustomUser struct { ID string Name string } func (u CustomUser) MarshalJSONAPIResource(ctx context.Context) (jsonapi.Resource, error) { return jsonapi.Resource{ Type: "users", ID: u.ID, Attributes: map[string]interface{}{ "name": u.Name, "custom_field": "custom_value", }, }, nil } func main() { customUser := CustomUser{ ID: "1", Name: "John Doe", } data, _ := jsonapi.Marshal(customUser, jsonapi.WithMarshaler(func(out interface{}) ([]byte, error) { return json.MarshalIndent(out, "", " ") })) fmt.Println(string(data)) }
Output: { "data": { "id": "1", "type": "users", "attributes": { "custom_field": "custom_value", "name": "John Doe" } } }
Example (MultipleResources) ¶
ExampleMarshalMultipleResources demonstrates marshaling multiple resources to JSON:API format
package main import ( "encoding/json" "fmt" "github.com/nisimpson/jsonapi" ) // Example structs type User struct { ID string `jsonapi:"primary,users"` Name string `jsonapi:"attr,name"` Email string `jsonapi:"attr,email,omitempty"` } func main() { users := []User{ {ID: "1", Name: "John Doe", Email: "john@example.com"}, {ID: "2", Name: "Jane Doe", Email: "jane@example.com"}, } data, _ := jsonapi.Marshal(users, jsonapi.WithMarshaler(func(out interface{}) ([]byte, error) { return json.MarshalIndent(out, "", " ") })) fmt.Println(string(data)) }
Output: { "data": [ { "id": "1", "type": "users", "attributes": { "email": "john@example.com", "name": "John Doe" } }, { "id": "2", "type": "users", "attributes": { "email": "jane@example.com", "name": "Jane Doe" } } ] }
Example (Relationships) ¶
ExampleMarshalRelationships demonstrates marshaling relationships with included resources
package main import ( "encoding/json" "fmt" "github.com/nisimpson/jsonapi" ) type Post struct { ID string `jsonapi:"primary,posts"` Title string `jsonapi:"attr,title"` Body string `jsonapi:"attr,body"` } type UserWithPosts struct { ID string `jsonapi:"primary,users"` Name string `jsonapi:"attr,name"` Posts []Post `jsonapi:"relation,posts"` } func main() { userWithPosts := UserWithPosts{ ID: "1", Name: "John Doe", Posts: []Post{ {ID: "1", Title: "First Post", Body: "Content 1"}, {ID: "2", Title: "Second Post", Body: "Content 2"}, }, } data, _ := jsonapi.Marshal(userWithPosts, jsonapi.IncludeRelatedResources(), jsonapi.WithMarshaler(func(out interface{}) ([]byte, error) { return json.MarshalIndent(out, "", " ") })) fmt.Println(string(data)) }
Output: { "data": { "id": "1", "type": "users", "attributes": { "name": "John Doe" }, "relationships": { "posts": { "data": [ { "id": "1", "type": "posts" }, { "id": "2", "type": "posts" } ] } } }, "included": [ { "id": "1", "type": "posts", "attributes": { "body": "Content 1", "title": "First Post" } }, { "id": "2", "type": "posts", "attributes": { "body": "Content 2", "title": "Second Post" } } ] }
Example (SingleResource) ¶
ExampleMarshalSingleResource demonstrates marshaling a single resource to JSON:API format
package main import ( "encoding/json" "fmt" "github.com/nisimpson/jsonapi" ) // Example structs type User struct { ID string `jsonapi:"primary,users"` Name string `jsonapi:"attr,name"` Email string `jsonapi:"attr,email,omitempty"` } func main() { user := User{ ID: "1", Name: "John Doe", Email: "john@example.com", } data, _ := jsonapi.Marshal(user, jsonapi.WithMarshaler(func(out interface{}) ([]byte, error) { return json.MarshalIndent(out, "", " ") })) fmt.Println(string(data)) }
Output: { "data": { "id": "1", "type": "users", "attributes": { "email": "john@example.com", "name": "John Doe" } } }
func MarshalWithContext ¶
func MarshalWithContext(ctx context.Context, out interface{}, opts ...func(*MarshalOptions)) ([]byte, error)
MarshalWithContext marshals a Go struct into a JSON:API Resource with a provided context.
func PermitReadOnly ¶ added in v0.2.0
func PermitReadOnly(enabled bool) func(*UnmarshalOptions)
PermitReadOnly returns an option that toggles unmarshaling of read-only fields. When disabled, any attempts to Unmarshal documents with read-only fields will return an error wrapping ErrReadOnly.
func PopulateFromIncluded ¶
func PopulateFromIncluded() func(*UnmarshalOptions)
PopulateFromIncluded instructs the Unmarshaler to populate relationship fields from the included array when available.
func SparseFieldsets ¶
func SparseFieldsets(resourceType string, fields []string) func(*MarshalOptions)
SparseFieldsets returns a function that modifies MarshalOptions to apply sparse fieldsets to resources. The function takes a resourceType string to identify which resources to modify and a fields slice containing the field names to include in the output. When applied, this function will filter both the primary data and included resources to only include the specified fields for resources matching the given type.
func StrictMode ¶
func StrictMode() func(*UnmarshalOptions)
StrictMode enables strict mode unmarshaling which returns errors for invalid JSON:API structure.
func Unmarshal ¶
func Unmarshal(data []byte, out interface{}, opts ...func(*UnmarshalOptions)) error
Unmarshal unmarshals JSON:API data into a Go struct using the default context.
Example (Custom) ¶
ExampleUnmarshalCustom demonstrates custom unmarshaling using the UnmarshalJSONAPIResource interface
package main import ( "context" "fmt" "github.com/nisimpson/jsonapi" ) // Custom unmarshaler example type CustomUnmarshalUser struct { ID string Name string CustomField string } func (u *CustomUnmarshalUser) UnmarshalJSONAPIResource(ctx context.Context, resource jsonapi.Resource) error { u.ID = resource.ID if name, ok := resource.Attributes["name"].(string); ok { u.Name = name } if customField, ok := resource.Attributes["custom_field"].(string); ok { u.CustomField = customField } return nil } func main() { customJsonData := `{ "data": { "type": "users", "id": "1", "attributes": { "name": "John Doe", "custom_field": "custom_value" } } }` var customUnmarshaledUser CustomUnmarshalUser err := jsonapi.Unmarshal([]byte(customJsonData), &customUnmarshaledUser) if err != nil { fmt.Printf("Error: %v\n", err) } else { fmt.Printf("Custom User: ID=%s, Name=%s, CustomField=%s\n", customUnmarshaledUser.ID, customUnmarshaledUser.Name, customUnmarshaledUser.CustomField) } }
Output: Custom User: ID=1, Name=John Doe, CustomField=custom_value
Example (MultipleResources) ¶
ExampleUnmarshalMultipleResources demonstrates unmarshaling multiple resources from JSON:API format
package main import ( "fmt" "github.com/nisimpson/jsonapi" ) // Example structs type User struct { ID string `jsonapi:"primary,users"` Name string `jsonapi:"attr,name"` Email string `jsonapi:"attr,email,omitempty"` } func main() { multipleJsonData := `{ "data": [ { "type": "users", "id": "1", "attributes": { "name": "John Doe", "email": "john@example.com" } }, { "type": "users", "id": "2", "attributes": { "name": "Jane Doe", "email": "jane@example.com" } } ] }` var unmarshaledUsers []User err := jsonapi.Unmarshal([]byte(multipleJsonData), &unmarshaledUsers) if err != nil { fmt.Printf("Error: %v\n", err) } else { fmt.Printf("Number of users: %d\n", len(unmarshaledUsers)) for i, user := range unmarshaledUsers { fmt.Printf("User %d: ID=%s, Name=%s, Email=%s\n", i+1, user.ID, user.Name, user.Email) } } }
Output: Number of users: 2 User 1: ID=1, Name=John Doe, Email=john@example.com User 2: ID=2, Name=Jane Doe, Email=jane@example.com
Example (NestedStruct) ¶
ExampleUnmarshalNestedStruct demonstrates unmarshaling nested struct attributes
package main import ( "fmt" "github.com/nisimpson/jsonapi" ) type Address struct { Street string `json:"street"` City string `json:"city"` } type UserWithAddress struct { ID string `jsonapi:"primary,users"` Name string `jsonapi:"attr,name"` Address Address `jsonapi:"attr,address"` } func main() { nestedJsonData := `{ "data": { "type": "users", "id": "1", "attributes": { "name": "John Doe", "address": { "street": "123 Main St", "city": "Anytown" } } } }` var userWithAddressUnmarshaled UserWithAddress err := jsonapi.Unmarshal([]byte(nestedJsonData), &userWithAddressUnmarshaled) if err != nil { fmt.Printf("Error: %v\n", err) } else { fmt.Printf("User: ID=%s, Name=%s\n", userWithAddressUnmarshaled.ID, userWithAddressUnmarshaled.Name) fmt.Printf("Address: Street=%s, City=%s\n", userWithAddressUnmarshaled.Address.Street, userWithAddressUnmarshaled.Address.City) } }
Output: User: ID=1, Name=John Doe Address: Street=123 Main St, City=Anytown
Example (Relationships) ¶
ExampleUnmarshalRelationships demonstrates unmarshaling relationships with included resources
package main import ( "fmt" "github.com/nisimpson/jsonapi" ) type Post struct { ID string `jsonapi:"primary,posts"` Title string `jsonapi:"attr,title"` Body string `jsonapi:"attr,body"` } type UserWithPosts struct { ID string `jsonapi:"primary,users"` Name string `jsonapi:"attr,name"` Posts []Post `jsonapi:"relation,posts"` } func main() { relationshipJsonData := `{ "data": { "type": "users", "id": "1", "attributes": { "name": "John Doe" }, "relationships": { "posts": { "data": [ {"type": "posts", "id": "1"}, {"type": "posts", "id": "2"} ] } } }, "included": [ { "type": "posts", "id": "1", "attributes": { "title": "First Post", "body": "Content 1" } }, { "type": "posts", "id": "2", "attributes": { "title": "Second Post", "body": "Content 2" } } ] }` var userWithPostsUnmarshaled UserWithPosts err := jsonapi.Unmarshal([]byte(relationshipJsonData), &userWithPostsUnmarshaled, jsonapi.PopulateFromIncluded()) if err != nil { fmt.Printf("Error: %v\n", err) } else { fmt.Printf("User: ID=%s, Name=%s\n", userWithPostsUnmarshaled.ID, userWithPostsUnmarshaled.Name) fmt.Printf("Number of posts: %d\n", len(userWithPostsUnmarshaled.Posts)) for i, post := range userWithPostsUnmarshaled.Posts { fmt.Printf("Post %d: ID=%s, Title=%s, Body=%s\n", i+1, post.ID, post.Title, post.Body) } } }
Output: User: ID=1, Name=John Doe Number of posts: 2 Post 1: ID=1, Title=First Post, Body=Content 1 Post 2: ID=2, Title=Second Post, Body=Content 2
Example (SingleResource) ¶
ExampleUnmarshalSingleResource demonstrates unmarshaling a single resource from JSON:API format
package main import ( "fmt" "github.com/nisimpson/jsonapi" ) // Example structs type User struct { ID string `jsonapi:"primary,users"` Name string `jsonapi:"attr,name"` Email string `jsonapi:"attr,email,omitempty"` } func main() { jsonData := `{ "data": { "type": "users", "id": "1", "attributes": { "name": "John Doe", "email": "john@example.com" } } }` var unmarshaledUser User err := jsonapi.Unmarshal([]byte(jsonData), &unmarshaledUser) if err != nil { fmt.Printf("Error: %v\n", err) } else { fmt.Printf("Unmarshaled User: ID=%s, Name=%s, Email=%s\n", unmarshaledUser.ID, unmarshaledUser.Name, unmarshaledUser.Email) } }
Output: Unmarshaled User: ID=1, Name=John Doe, Email=john@example.com
func UnmarshalDocument ¶
func UnmarshalDocument(ctx context.Context, doc *Document, out interface{}, opts ...func(*UnmarshalOptions)) error
UnmarshalDocument unmarshals a Document into the target struct.
func UnmarshalWithContext ¶
func UnmarshalWithContext(ctx context.Context, data []byte, out interface{}, opts ...func(*UnmarshalOptions)) error
UnmarshalWithContext unmarshals JSON:API data into a Go struct with a provided context.
func WithMarshaler ¶
func WithMarshaler(fn func(interface{}) ([]byte, error)) func(*MarshalOptions)
WithMarshaler uses a custom JSON marshaler to serialize documents.
func WithUnmarshaler ¶
func WithUnmarshaler(fn func([]byte, interface{}) error) func(*UnmarshalOptions)
WithUnmarshaler uses a custom JSON unmarshaler to deserialize documents.
Types ¶
type Document ¶
type Document struct { Meta map[string]interface{} `json:"meta,omitempty"` Data PrimaryData `json:"data,omitempty"` Errors []Error `json:"errors,omitempty"` Links map[string]Link `json:"links,omitempty"` Included []Resource `json:"included,omitempty"` }
Document represents the top-level JSON:API document structure.
func MarshalDocument ¶
func MarshalDocument(ctx context.Context, out interface{}, opts ...func(*MarshalOptions)) (*Document, error)
MarshalDocument converts a Go value into a JSON:API Document structure. It accepts a context for injection of request-scoped values in marshaling operations. Optional marshaling options can be provided to customize the marshaling behavior. It returns a Document pointer and any error encountered during marshaling. If the input is nil or a nil pointer, it returns an error.
This function is the core marshaling function used by Marshal and MarshalWithContext. It can be used directly when you need access to the Document structure before serialization.
func NewErrorDocument ¶ added in v0.1.3
NewErrorDocument creates a new Document to represent errors in a JSON:API compliant format. This function handles different types of errors and converts them into the appropriate Document structure.
- When the provided error wraps or is of type Error, the function will use that error directly in the document
- If the error wraps or is of type MultiError, all of the contained errors will be included in the document.
For any other error type, the function creates a generic error entry using the error's message as the detail field. This ensures that even standard Go errors can be represented in the JSON:API format.
If a nil error is provided, the function creates a document with a generic "Unknown error" message. This prevents returning empty error documents.
type Error ¶
type Error struct { ID string `json:"id,omitempty"` Status string `json:"status"` Code string `json:"code"` Title string `json:"title"` Detail string `json:"detail"` Source map[string]interface{} `json:"source,omitempty"` Links map[string]interface{} `json:"links,omitempty"` }
Error represents a JSON:API error object.
func (Error) Error ¶
Error returns a string representation of the error. The returned string will include the title, detail, and code if they are available. If only the title and detail are available, it returns them formatted as "title: detail". If only the detail is available, it returns just the detail string.
type Link ¶
type Link struct { Href string `json:"href,omitempty"` Meta map[string]interface{} `json:"meta,omitempty"` }
Link represents a JSON:API link object.
func (Link) MarshalJSON ¶
func (*Link) UnmarshalJSON ¶
type LinksMarshaler ¶
LinksMarshaler allows types to provide custom links marshaling.
type LinksUnmarshaler ¶
type LinksUnmarshaler interface {
UnmarshalJSONAPILinks(ctx context.Context, links map[string]Link) error
}
LinksUnmarshaler allows types to provide custom links unmarshaling.
type MarshalOptions ¶
type MarshalOptions struct {
// contains filtered or unexported fields
}
MarshalOptions contains options for marshaling operations.
type MetaMarshaler ¶
type MetaMarshaler interface {
MarshalJSONAPIMeta(ctx context.Context) (map[string]interface{}, error)
}
MetaMarshaler allows types to provide custom meta marshaling.
type MetaUnmarshaler ¶
type MetaUnmarshaler interface {
UnmarshalJSONAPIMeta(ctx context.Context, meta map[string]interface{}) error
}
MetaUnmarshaler allows types to provide custom meta unmarshaling.
type MultiError ¶
type MultiError []Error
MultiError represents a collection of JSON:API errors that can be combined into a single error. It implements the error interface and provides a way to handle multiple errors as one.
func (MultiError) Error ¶
func (me MultiError) Error() string
Error returns a string representation of multiple errors combined into one. If the MultiError is empty, it panics with "multi error is empty". If the MultiError contains only one error, it returns that error's string representation. For multiple errors, it joins them together using errors.Join and returns the combined string.
type PrimaryData ¶
type PrimaryData struct {
// contains filtered or unexported fields
}
PrimaryData represents the primary data in a JSON:API document. It can be a single resource, multiple resources, or null.
func MultiResource ¶
func MultiResource(resources ...Resource) PrimaryData
MultiResource creates primary data with multiple resources.
func SingleResource ¶
func SingleResource(resource Resource) PrimaryData
SingleResource creates primary data with a single resource.
func (*PrimaryData) Iter ¶
func (pd *PrimaryData) Iter() iter.Seq[*Resource]
Iter returns an iterator over the resources.
func (PrimaryData) Many ¶
func (pd PrimaryData) Many() ([]Resource, bool)
Many returns the resource slice and true if data contains multiple resources.
func (PrimaryData) MarshalJSON ¶
func (pd PrimaryData) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler for PrimaryData.
func (PrimaryData) One ¶
func (pd PrimaryData) One() (Resource, bool)
One returns the single resource and true if data contains one resource.
func (*PrimaryData) UnmarshalJSON ¶
func (pd *PrimaryData) UnmarshalJSON(data []byte) error
UnmarshalJSON implements json.Unmarshaler for PrimaryData.
type Relationship ¶
type Relationship struct { Meta map[string]interface{} `json:"meta,omitempty"` Links map[string]Link `json:"links,omitempty"` Data PrimaryData `json:"data,omitempty"` }
Relationship represents a JSON:API relationship object.
type RelationshipLinksMarshaler ¶
type RelationshipLinksMarshaler interface {
MarshalJSONAPIRelationshipLinks(ctx context.Context, name string) (map[string]Link, error)
}
RelationshipLinksMarshaler allows types to provide custom relationship links marshaling.
type RelationshipLinksUnmarshaler ¶
type RelationshipLinksUnmarshaler interface {
UnmarshalJSONAPIRelationshipLinks(ctx context.Context, name string, links map[string]Link) error
}
RelationshipLinksUnmarshaler allows types to provide custom relationship links unmarshaling.
type RelationshipMetaMarshaler ¶
type RelationshipMetaMarshaler interface {
MarshalJSONAPIRelationshipMeta(ctx context.Context, name string) (map[string]interface{}, error)
}
RelationshipMetaMarshaler allows types to provide custom relationship meta marshaling.
type RelationshipMetaUnmarshaler ¶
type RelationshipMetaUnmarshaler interface {
UnmarshalJSONAPIRelationshipMeta(ctx context.Context, name string, meta map[string]interface{}) error
}
RelationshipMetaUnmarshaler allows types to provide custom relationship meta unmarshaling.
type Resource ¶
type Resource struct { ID string `json:"id"` Type string `json:"type"` Meta map[string]interface{} `json:"meta,omitempty"` Attributes map[string]interface{} `json:"attributes,omitempty"` Relationships map[string]Relationship `json:"relationships,omitempty"` Links map[string]Link `json:"links,omitempty"` }
Resource represents a JSON:API resource object.
func (*Resource) ApplySparseFieldsets ¶
ApplySparseFieldsets filters the resource's attributes to only include the specified fields. If fields is empty, all attributes are retained. Otherwise, only attributes whose names match one of the provided fields are kept, and all other attributes are removed.
type ResourceMarshaler ¶
ResourceMarshaler allows types to provide custom JSON:API resource marshaling.
type ResourceUnmarshaler ¶
type ResourceUnmarshaler interface {
UnmarshalJSONAPIResource(ctx context.Context, resource Resource) error
}
ResourceUnmarshaler allows types to provide custom JSON:API resource unmarshaling.
type UnmarshalOptions ¶
type UnmarshalOptions struct {
// contains filtered or unexported fields
}
UnmarshalOptions contains options for unmarshaling operations.