mirror of
https://github.com/encounter/JSONAPI.git
synced 2026-03-30 11:18:38 -07:00
Add Sparse Fieldset support for Attributes
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
//
|
||||
// SparseField.swift
|
||||
//
|
||||
//
|
||||
// Created by Mathew Polzin on 8/4/19.
|
||||
//
|
||||
|
||||
public struct SparseField<
|
||||
Description: JSONAPI.ResourceObjectDescription,
|
||||
MetaType: JSONAPI.Meta,
|
||||
LinksType: JSONAPI.Links,
|
||||
EntityRawIdType: JSONAPI.MaybeRawId
|
||||
>: Encodable where Description.Attributes: SparsableAttributes {
|
||||
|
||||
public typealias Resource = JSONAPI.ResourceObject<Description, MetaType, LinksType, EntityRawIdType>
|
||||
|
||||
public let resourceObject: Resource
|
||||
public let fields: [Description.Attributes.CodingKeys]
|
||||
|
||||
public init(_ resourceObject: Resource, fields: [Description.Attributes.CodingKeys]) {
|
||||
self.resourceObject = resourceObject
|
||||
self.fields = fields
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
let sparseEncoder = SparseFieldEncoder(wrapping: encoder,
|
||||
encoding: fields)
|
||||
|
||||
try resourceObject.encode(to: sparseEncoder)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user