Compare commits

..

1 Commits

21 changed files with 327 additions and 150 deletions
+2 -2
View File
@@ -16,7 +16,7 @@ Pod::Spec.new do |spec|
#
spec.name = "MP-JSONAPI"
spec.version = "2.3.0"
spec.version = "2.2.0"
spec.summary = "Swift Codable JSON API framework."
# This description is used to generate tags and improve search results.
@@ -136,6 +136,6 @@ See the JSON API Spec here: https://jsonapi.org/format/
# spec.requires_arc = true
# spec.xcconfig = { "HEADER_SEARCH_PATHS" => "$(SDKROOT)/usr/include/libxml2" }
spec.dependency "Poly", "~> 2.2"
spec.dependency "Poly", "~> 2.1"
end
+2 -2
View File
@@ -6,8 +6,8 @@
"repositoryURL": "https://github.com/mattpolzin/Poly.git",
"state": {
"branch": null,
"revision": "b24fd3b41bf3126d4c6dede3708135182172af60",
"version": "2.2.0"
"revision": "4a08517b24f8e9f6dd8c02ec7da316aac5c00e2e",
"version": "2.1.0"
}
}
]
+1 -1
View File
@@ -18,7 +18,7 @@ let package = Package(
targets: ["JSONAPITesting"])
],
dependencies: [
.package(url: "https://github.com/mattpolzin/Poly.git", .upToNextMajor(from: "2.2.0")),
.package(url: "https://github.com/mattpolzin/Poly.git", .upToNextMajor(from: "2.1.0")),
],
targets: [
.target(
-8
View File
@@ -169,11 +169,3 @@ extension Includes where I: _Poly10 {
return values.compactMap { $0.j }
}
}
// MARK: - 11 includes
public typealias Include11 = Poly11
extension Includes where I: _Poly11 {
public subscript(_ lookup: I.K.Type) -> [I.K] {
return values.compactMap { $0.k }
}
}
@@ -1,5 +1,5 @@
//
// BasicJSONAPIError.swift
// BasicError.swift
// JSONAPI
//
// Created by Mathew Polzin on 9/29/19.
@@ -1,5 +1,5 @@
//
// GenericJSONAPIError.swift
// GenericError.swift
// JSONAPI
//
// Created by Mathew Polzin on 9/29/19.
+1 -1
View File
@@ -1,5 +1,5 @@
//
// JSONAPIError.swift
// Error.swift
// JSONAPI
//
// Created by Mathew Polzin on 11/10/18.
@@ -0,0 +1,141 @@
//
// TransformedAttribute.swift
//
// Created by Mathew Polzin on 9/30/19.
//
public protocol AttrType {
associatedtype SerializedType
}
@propertyWrapper
public struct Attr<Serializer, Deserializer, Value>: AttrType where Serializer: Transformer, Deserializer: Transformer, Serializer.From == Value, Deserializer.To == Value {
public typealias SerializedType = Value
private var _wrappedValue: Value?
public var wrappedValue: Value {
guard let ret = _wrappedValue else {
fatalError("Accessed Transformed Value prior to initializing or decoding it.")
}
return ret
}
public init(wrappedValue: Value) {
_wrappedValue = wrappedValue
}
public init(wrappedValue: Value, serializer: Serializer.Type, deserializer: Deserializer.Type) {
_wrappedValue = wrappedValue
}
public init(serializer: Serializer.Type, deserializer: Deserializer.Type) {
_wrappedValue = nil
}
}
extension Attr where
Serializer == IdentityTransformer<Value>,
Deserializer == IdentityTransformer<Value> {
public init(wrappedValue: Value) {
_wrappedValue = wrappedValue
}
public init() {
_wrappedValue = nil
}
}
extension Attr where
Deserializer == IdentityTransformer<Value>,
Serializer: Transformer, Serializer.From == Value {
public init(wrappedValue: Value, serializer: Serializer.Type) {
_wrappedValue = wrappedValue
}
public init(serializer: Serializer.Type) {
_wrappedValue = nil
}
}
extension Attr where
Serializer == IdentityTransformer<Value>,
Deserializer: Transformer, Deserializer.To == Value {
public init(wrappedValue: Value, deserializer: Deserializer.Type) {
_wrappedValue = wrappedValue
}
public init(deserializer: Deserializer.Type) {
_wrappedValue = nil
}
}
extension Attr: Encodable where
Serializer: Transformer, Serializer.To: Encodable {
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(Serializer.transform(wrappedValue))
}
}
extension Attr: Decodable where
Deserializer: Transformer, Deserializer.From: Decodable {
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let anyNil: Any? = nil
if container.decodeNil(),
let val = anyNil as? Value {
_wrappedValue = val
} else {
_wrappedValue = try Deserializer.transform(container.decode(Deserializer.From.self))
}
}
}
public protocol _Omittable {
static var nilValue: Self { get }
}
@propertyWrapper
public struct Omittable<T>: AttrType, _Omittable {
public typealias SerializedType = T?
public let wrappedValue: T?
public init(wrappedValue: T?) {
self.wrappedValue = wrappedValue
}
public static var nilValue: Omittable<T> { return .init(wrappedValue: nil) }
}
extension Omittable: Encodable where T: Encodable {
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
if Optional(wrappedValue) != nil {
try container.encode(wrappedValue)
}
}
}
extension Omittable: Decodable where T: Decodable {
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
self = .init(wrappedValue: try container.decode(T.self))
}
}
extension KeyedDecodingContainer {
public func decode<T>(_ type: T.Type, forKey key: KeyedDecodingContainer<K>.Key) throws -> T where T : Decodable, T: _Omittable {
return try decodeIfPresent(T.self, forKey: key) ?? T.nilValue
}
}
@@ -29,6 +29,12 @@ public enum IdentityTransformer<T>: ReversibleTransformer {
public static func reverse(_ value: T) throws -> T { return value }
}
extension Optional: Transformer where Wrapped: Transformer {
public static func transform(_ value: Wrapped.From?) throws -> Wrapped.To? {
return try value.map { try Wrapped.transform($0) }
}
}
// MARK: - Validator
/// A Validator is a Transformer that throws an error if an invalid value
@@ -79,8 +79,3 @@ extension Poly9: PrimaryResource, OptionalPrimaryResource where A: PolyWrapped,
extension Poly10: EncodablePrimaryResource, OptionalEncodablePrimaryResource where A: EncodablePolyWrapped, B: EncodablePolyWrapped, C: EncodablePolyWrapped, D: EncodablePolyWrapped, E: EncodablePolyWrapped, F: EncodablePolyWrapped, G: EncodablePolyWrapped, H: EncodablePolyWrapped, I: EncodablePolyWrapped, J: EncodablePolyWrapped {}
extension Poly10: PrimaryResource, OptionalPrimaryResource where A: PolyWrapped, B: PolyWrapped, C: PolyWrapped, D: PolyWrapped, E: PolyWrapped, F: PolyWrapped, G: PolyWrapped, H: PolyWrapped, I: PolyWrapped, J: PolyWrapped {}
// MARK: - 11 types
extension Poly11: EncodablePrimaryResource, OptionalEncodablePrimaryResource where A: EncodablePolyWrapped, B: EncodablePolyWrapped, C: EncodablePolyWrapped, D: EncodablePolyWrapped, E: EncodablePolyWrapped, F: EncodablePolyWrapped, G: EncodablePolyWrapped, H: EncodablePolyWrapped, I: EncodablePolyWrapped, J: EncodablePolyWrapped, K: EncodablePolyWrapped {}
extension Poly11: PrimaryResource, OptionalPrimaryResource where A: PolyWrapped, B: PolyWrapped, C: PolyWrapped, D: PolyWrapped, E: PolyWrapped, F: PolyWrapped, G: PolyWrapped, H: PolyWrapped, I: PolyWrapped, J: PolyWrapped, K: PolyWrapped {}
@@ -9,7 +9,74 @@ import XCTest
import JSONAPI
import JSONAPITesting
struct T1: Encodable {
@Attr(serializer: IntToString.self)
var x: Int
init(x: Int) {
self._x = .init(wrappedValue: x)
}
}
struct T2: Decodable {
@Attr(deserializer: IntToString.self)
var y: String
}
struct Tmp {
@Attr(serializer: IntToString.self)
var x: Int = 5
@Attr(deserializer: IntToString.self)
var y: String = "2"
}
struct T3: Codable {
@Attr(serializer: StringToInt.self, deserializer: IntToString.self)
var y: String = "1"
}
struct T4: Encodable {
@Attr(serializer: StringToInt?.self)
var w: String?
}
struct T5: Codable {
@Attr(serializer: StringToInt?.self, deserializer: IntToString?.self)
var x: String?
}
struct T6: Decodable {
@Omittable
@Attr(deserializer: IntToString?.self)
var y: String?
}
class TransformerTests: XCTestCase {
func test_tmp() {
let t1 = T1(x: 2)
print(encodable: t1)
let t3 = T3(y: "3")
print(encodable: t3)
let t2 = decoded(type: T2.self, data: #"{"y":63}"#.data(using: .utf8)!)
print(t2.y)
let t4 = T4(w: .init(wrappedValue: nil))
let t4_2 = T4(w: .init(wrappedValue: "12"))
print(encodable: t4)
print(encodable: t4_2)
let t5 = decoded(type: T5.self, data: #"{"x":null}"#.data(using: .utf8)!)
let t5_2 = decoded(type: T5.self, data: #"{"x":10}"#.data(using: .utf8)!)
XCTAssertThrowsError(try JSONDecoder().decode(T5.self, from: #"{}"#.data(using: .utf8)!))
print(t5.x)
print(t5_2.x)
let t6 = decoded(type: T6.self, data: #"{}"#.data(using: .utf8)!)
}
func testIdentityTransform() {
let inString = "hello world"
@@ -49,3 +116,23 @@ enum MoreThanFiveCharValidator: Validator {
case fewerThanFiveChars
}
}
enum StringToInt: Transformer {
public static func transform(_ value: String) throws -> Int {
let res = Int(value)
guard let ret = res else {
throw Error.nonIntegerString
}
return ret
}
enum Error: Swift.Error {
case nonIntegerString
}
}
enum IntToString: Transformer {
public static func transform(_ value: Int) throws -> String {
return String(value)
}
}
@@ -197,28 +197,6 @@ class IncludedTests: XCTestCase {
test_DecodeEncodeEquality(type: Includes<Include10<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity5, TestEntity6, TestEntity7, TestEntity8, TestEntity9, TestEntity10>>.self,
data: ten_different_type_includes)
}
func test_ElevenDifferentIncludes() {
let includes = decoded(type: Includes<Include11<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity5, TestEntity6, TestEntity7, TestEntity8, TestEntity9, TestEntity10, TestEntity11>>.self,
data: eleven_different_type_includes)
XCTAssertEqual(includes[TestEntity.self].count, 1)
XCTAssertEqual(includes[TestEntity2.self].count, 1)
XCTAssertEqual(includes[TestEntity3.self].count, 1)
XCTAssertEqual(includes[TestEntity4.self].count, 1)
XCTAssertEqual(includes[TestEntity5.self].count, 1)
XCTAssertEqual(includes[TestEntity6.self].count, 1)
XCTAssertEqual(includes[TestEntity7.self].count, 1)
XCTAssertEqual(includes[TestEntity8.self].count, 1)
XCTAssertEqual(includes[TestEntity9.self].count, 1)
XCTAssertEqual(includes[TestEntity10.self].count, 1)
XCTAssertEqual(includes[TestEntity11.self].count, 1)
}
func test_ElevenDifferentIncludes_encode() {
test_DecodeEncodeEquality(type: Includes<Include11<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity5, TestEntity6, TestEntity7, TestEntity8, TestEntity9, TestEntity10, TestEntity11>>.self,
data: eleven_different_type_includes)
}
}
// MARK: - Appending
@@ -525,15 +503,4 @@ extension IncludedTests {
}
typealias TestEntity10 = BasicEntity<TestEntityType10>
enum TestEntityType11: ResourceObjectDescription {
typealias Attributes = NoAttributes
public static var jsonType: String { return "test_entity11" }
typealias Relationships = NoRelationships
}
typealias TestEntity11 = BasicEntity<TestEntityType11>
}
@@ -597,92 +597,3 @@ let ten_different_type_includes = """
}
]
""".data(using: .utf8)!
let eleven_different_type_includes = """
[
{
"type": "test_entity1",
"id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF",
"attributes": {
"foo": "Hello",
"bar": 123
}
},
{
"type": "test_entity2",
"id": "90F03B69-4DF1-467F-B52E-B0C9E44FC333",
"attributes": {
"foo": "World",
"bar": 456
},
"relationships": {
"entity1": {
"data": {
"type": "test_entity1",
"id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF"
}
}
}
},
{
"type": "test_entity3",
"id": "11223B69-4DF1-467F-B52E-B0C9E44FC443",
"relationships": {
"entity1": {
"data": {
"type": "test_entity1",
"id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF"
}
},
"entity2": {
"data": [
{
"type": "test_entity2",
"id": "90F03B69-4DF1-467F-B52E-B0C9E44FC333"
}
]
}
}
},
{
"type": "test_entity6",
"id": "11113B69-4DF1-467F-B52E-B0C9E44FC444",
"relationships": {
"entity4": {
"data": {
"type": "test_entity4",
"id": "364B3B69-4DF1-467F-B52E-B0C9E44F666E"
}
}
}
},
{
"type": "test_entity5",
"id": "A24B3B69-4DF1-467F-B52E-B0C9E44F436A"
},
{
"type": "test_entity4",
"id": "364B3B69-4DF1-467F-B52E-B0C9E44F666E"
},
{
"type": "test_entity7",
"id": "364B3B69-4DF1-222F-B52E-B0C9E44F666E"
},
{
"type": "test_entity8",
"id": "364B3B69-4DF1-222F-B52E-B0C9E44F266F"
},
{
"type": "test_entity9",
"id": "364B3B69-4DF1-218F-B52E-B0C9E44F2661"
},
{
"type": "test_entity10",
"id": "264B3B69-4DF1-212F-B52E-B0C9E44F2660"
},
{
"type": "test_entity11",
"id": "264B3B69-4DF3-212F-B32E-A0C9E44F26C0B"
}
]
""".data(using: .utf8)!
@@ -0,0 +1,10 @@
[
{
"type": "test_entity1",
"id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF",
"attributes": {
"foo": "Hello",
"bar": 123
}
}
]
@@ -0,0 +1,30 @@
[
{
"type": "test_entity1",
"id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF",
"attributes": {
"foo": "Hello",
"bar": 123
}
},
{
"type": "test_entity2",
"id": "90F03B69-4DF1-467F-B52E-B0C9E44FC333",
"attributes": {
"foo": "World",
"bar": 456
},
"relationships": {
"entity1": {
"data": {
"type": "test_entity1",
"id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF"
}
}
}
},
{
"type": "test_entity4",
"id": "364B3B69-4DF1-467F-B52E-B0C9E44F666E"
}
]
@@ -0,0 +1,26 @@
[
{
"type": "test_entity1",
"id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF",
"attributes": {
"foo": "Hello",
"bar": 123
}
},
{
"type": "test_entity2",
"id": "90F03B69-4DF1-467F-B52E-B0C9E44FC333",
"attributes": {
"foo": "World",
"bar": 456
},
"relationships": {
"entity1": {
"data": {
"type": "test_entity1",
"id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF"
}
}
}
}
]
@@ -0,0 +1,18 @@
[
{
"type": "test_entity1",
"id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF",
"attributes": {
"foo": "Hello",
"bar": 123
}
},
{
"type": "test_entity1",
"id": "90F03B69-4DF1-467F-B52E-B0C9E44FC333",
"attributes": {
"foo": "World",
"bar": 456
}
}
]
@@ -114,10 +114,3 @@ let poly_entity10 = """
"id": "A24B3444-4DF1-467F-B52E-B0C9E12F436A"
}
""".data(using: .utf8)!
let poly_entity11 = """
{
"type": "test_entity11",
"id": "A24B3444-4DF1-467F-B52E-B0C9E12F4440"
}
""".data(using: .utf8)!

Some files were not shown because too many files have changed in this diff Show More