Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: objective-c
# xcode_project: SwiftyJSONAPI.xcodeproj
# xcode_scheme: SwiftyJSONAPI
osx_image: xcode9.3beta
osx_image: xcode10.2
script:
- xcodebuild clean build test -project SwiftyJSONAPI.xcodeproj -scheme SwiftyJSONAPI -destination 'platform=iOS Simulator,name=iPhone 7,OS=11.3'
- xcodebuild clean build test -project SwiftyJSONAPI.xcodeproj -scheme SwiftyJSONAPI -destination 'platform=iOS Simulator,name=iPhone 7,OS=12.2'

34 changes: 34 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// swift-tools-version:5.3
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "SwiftyJSONAPI",
platforms: [.macOS(.v10_10),
.iOS(.v9)],
products: [
.library(
name: "SwiftyJSONAPI",
targets: ["SwiftyJSONAPI"]),
],
targets: [
.target(
name: "SwiftyJSONAPI",
dependencies: [],
path: "SwiftyJSONAPI",
exclude: ["Info.plist"]),
.testTarget(
name: "SwiftyJSONAPITests",
dependencies: ["SwiftyJSONAPI"],
path: "SwiftyJSONAPITests",
exclude: ["Info.plist"],
resources: [
.copy("example-bidirectional-relationship.json"),
.copy("example-document-1.json"),
.copy("example-error.json")
]),

],
swiftLanguageVersions: [.v5]
)
4 changes: 2 additions & 2 deletions SwiftyJSONAPI.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ Pod::Spec.new do |s|
#

s.name = "SwiftyJSONAPI"
s.version = "0.0.19"
s.version = "0.1.0"
s.summary = "JSONAPI document representation and serializing in Swift."
s.swift_version = "4.0"
s.swift_version = "5.0"

s.homepage = "https://github.com/thomassnielsen/SwiftyJSONAPI"
# s.screenshots = "www.example.com/screenshots_1.gif", "www.example.com/screenshots_2.gif"
Expand Down
29 changes: 15 additions & 14 deletions SwiftyJSONAPI.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
1C04AAC521370E2000547976 /* example-bidirectional-relationship.json in Resources */ = {isa = PBXBuildFile; fileRef = 1C04AAC421370E2000547976 /* example-bidirectional-relationship.json */; };
217C53B61BD042E300E82E37 /* JSONAPIError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 217C53B51BD042E300E82E37 /* JSONAPIError.swift */; };
21B93C981C8E593F00F67D9C /* JSONAPIErrorSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21B93C971C8E593F00F67D9C /* JSONAPIErrorSource.swift */; };
21E3BD5E1BD53DAE000E4E39 /* example-error.json in Resources */ = {isa = PBXBuildFile; fileRef = 21E3BD5D1BD53DAE000E4E39 /* example-error.json */; };
Expand All @@ -32,6 +33,7 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
1C04AAC421370E2000547976 /* example-bidirectional-relationship.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = "example-bidirectional-relationship.json"; sourceTree = "<group>"; };
217C53B51BD042E300E82E37 /* JSONAPIError.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = JSONAPIError.swift; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
21B93C971C8E593F00F67D9C /* JSONAPIErrorSource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = JSONAPIErrorSource.swift; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
21E3BD5D1BD53DAE000E4E39 /* example-error.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "example-error.json"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -109,6 +111,7 @@
E9D236E31BA2D40A00286911 /* Info.plist */,
E9D236F61BA2D7B900286911 /* example-document-1.json */,
21E3BD5D1BD53DAE000E4E39 /* example-error.json */,
1C04AAC421370E2000547976 /* example-bidirectional-relationship.json */,
);
path = SwiftyJSONAPITests;
sourceTree = "<group>";
Expand Down Expand Up @@ -169,25 +172,26 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0700;
LastUpgradeCheck = 0930;
LastUpgradeCheck = 1020;
ORGANIZATIONNAME = "Thomas Sunde Nielsen";
TargetAttributes = {
E9D236D11BA2D40A00286911 = {
CreatedOnToolsVersion = 7.0;
LastSwiftMigration = 0930;
LastSwiftMigration = 1020;
};
E9D236DB1BA2D40A00286911 = {
CreatedOnToolsVersion = 7.0;
LastSwiftMigration = 0930;
LastSwiftMigration = 1020;
};
};
};
buildConfigurationList = E9D236CC1BA2D40A00286911 /* Build configuration list for PBXProject "SwiftyJSONAPI" */;
compatibilityVersion = "Xcode 3.2";
developmentRegion = English;
developmentRegion = en;
hasScannedForEncodings = 0;
knownRegions = (
en,
Base,
);
mainGroup = E9D236C81BA2D40A00286911;
productRefGroup = E9D236D31BA2D40A00286911 /* Products */;
Expand All @@ -212,6 +216,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
1C04AAC521370E2000547976 /* example-bidirectional-relationship.json in Resources */,
E9D236F71BA2D7B900286911 /* example-document-1.json in Resources */,
21E3BD5E1BD53DAE000E4E39 /* example-error.json in Resources */,
);
Expand Down Expand Up @@ -258,6 +263,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
Expand Down Expand Up @@ -306,7 +312,6 @@
SDKROOT = macosx;
SUPPORTED_PLATFORMS = "iphonesimulator iphoneos macosx";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
VALID_ARCHS = "i386 x86_64 armv7s armv7 arm64";
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
};
Expand All @@ -316,6 +321,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
Expand Down Expand Up @@ -357,7 +363,6 @@
SDKROOT = macosx;
SUPPORTED_PLATFORMS = "iphonesimulator iphoneos macosx";
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
VALID_ARCHS = "i386 x86_64 armv7s armv7 arm64";
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
};
Expand All @@ -380,8 +385,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_SWIFT3_OBJC_INFERENCE = On;
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
};
name = Debug;
};
Expand All @@ -401,8 +405,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.thomassnielsen.SwiftyJSONAPI;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_SWIFT3_OBJC_INFERENCE = On;
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
};
name = Release;
};
Expand All @@ -418,8 +421,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks @loader_path/Frameworks @executable_path/Frameworks ";
PRODUCT_BUNDLE_IDENTIFIER = com.thomassnielsen.SwiftyJSONAPITests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_SWIFT3_OBJC_INFERENCE = On;
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
};
name = Debug;
};
Expand All @@ -435,8 +437,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks @loader_path/Frameworks @executable_path/Frameworks ";
PRODUCT_BUNDLE_IDENTIFIER = com.thomassnielsen.SwiftyJSONAPITests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_SWIFT3_OBJC_INFERENCE = On;
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
};
name = Release;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0930"
LastUpgradeVersion = "1020"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
40 changes: 37 additions & 3 deletions SwiftyJSONAPI/JSONAPIResource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public enum JSONAPIResourceLoaded {
case NotLoaded
}

typealias ResourceType = String
typealias ResourceIds = Set<String>
typealias CachedResources = [ResourceType : ResourceIds]

public class JSONAPIResource: JSONPrinter {
public var id = ""
Expand Down Expand Up @@ -93,7 +96,11 @@ public class JSONAPIResource: JSONPrinter {
}

func loadResources(withIncludedResources includedResources: ResourcesByTypeAndId) {

var cachedResources = CachedResources()
_loadResources(withIncludedResources: includedResources, cachedResources: &cachedResources)
}

fileprivate func _loadResources(withIncludedResources includedResources: ResourcesByTypeAndId, cachedResources: inout CachedResources) {
for relationship in self.relationships {

for resource in relationship.resources {
Expand All @@ -103,12 +110,39 @@ public class JSONAPIResource: JSONPrinter {
resource.attributes = includedResource.attributes
resource.relationships = includedResource.relationships

if !resource.relationships.isEmpty {

if !resource.relationships.isEmpty, resource.cacheIfNeeded(&cachedResources) {

resource.parent = self.parent
resource.loadResources(withIncludedResources: includedResources)
resource._loadResources(withIncludedResources: includedResources, cachedResources: &cachedResources)
}

resource.loaded = .Loaded
}
}
}
}

private extension JSONAPIResource {

// Checks if a resource has been loaded already, prevents bidirectional relationships from being recursively called
@discardableResult func cacheIfNeeded(_ cache: inout CachedResources) -> Bool {

if var cachedIds = cache[type] {

if cachedIds.contains(id) {

return false
} else {

cachedIds.insert(id)
cache[type] = cachedIds
return true
}
} else {

cache[type] = [id]
return true
}
}
}
Loading