forked from iZettle/Lift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Måns Bernhardt
committed
Nov 27, 2017
0 parents
commit 72a3a9e
Showing
25 changed files
with
3,441 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
xcuserdata | ||
*.pbxuser | ||
*.perspectivev3 | ||
*.mode1v3 | ||
.DS_Store | ||
*.xccheckout | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# 1.0 | ||
|
||
This is the first public release of the Lift library. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
**Copyright (c) 2016 - 2017, iZettle AB** | ||
**All rights reserved.** | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
Pod::Spec.new do |s| | ||
s.name = "Lift" | ||
s.version = "1.0.0" | ||
s.summary = "Working with JSON-like structures" | ||
s.description = <<-DESC | ||
Lift is a Swift library for generating and extracting values into and out of JSON-like structures. | ||
DESC | ||
s.homepage = "https://github.com/iZettle/Lift" | ||
s.license = { :type => "MIT", :file => "LICENSE.md" } | ||
s.author = { 'iZettle AB' => '[email protected]' } | ||
|
||
s.osx.deployment_target = "10.9" | ||
s.ios.deployment_target = "9.0" | ||
|
||
s.source = { :git => "https://github.com/iZettle/Lift", :tag => "#{s.version}" } | ||
s.source_files = "Lift/*.{swift}" | ||
end |
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Scheme | ||
LastUpgradeVersion = "0910" | ||
version = "1.3"> | ||
<BuildAction | ||
parallelizeBuildables = "YES" | ||
buildImplicitDependencies = "NO"> | ||
<BuildActionEntries> | ||
<BuildActionEntry | ||
buildForTesting = "YES" | ||
buildForRunning = "YES" | ||
buildForProfiling = "YES" | ||
buildForArchiving = "YES" | ||
buildForAnalyzing = "YES"> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "21E1D39E1D9410F000A91CA0" | ||
BuildableName = "Lift.framework" | ||
BlueprintName = "Lift" | ||
ReferencedContainer = "container:Lift.xcodeproj"> | ||
</BuildableReference> | ||
</BuildActionEntry> | ||
</BuildActionEntries> | ||
</BuildAction> | ||
<TestAction | ||
buildConfiguration = "Debug" | ||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" | ||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" | ||
language = "" | ||
shouldUseLaunchSchemeArgsEnv = "YES"> | ||
<Testables> | ||
<TestableReference | ||
skipped = "NO"> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "21E1D3A71D9410F000A91CA0" | ||
BuildableName = "Tests.xctest" | ||
BlueprintName = "Tests" | ||
ReferencedContainer = "container:Lift.xcodeproj"> | ||
</BuildableReference> | ||
</TestableReference> | ||
</Testables> | ||
<MacroExpansion> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "21E1D39E1D9410F000A91CA0" | ||
BuildableName = "Lift.framework" | ||
BlueprintName = "Lift" | ||
ReferencedContainer = "container:Lift.xcodeproj"> | ||
</BuildableReference> | ||
</MacroExpansion> | ||
<AdditionalOptions> | ||
</AdditionalOptions> | ||
</TestAction> | ||
<LaunchAction | ||
buildConfiguration = "Debug" | ||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" | ||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" | ||
language = "" | ||
launchStyle = "0" | ||
useCustomWorkingDirectory = "NO" | ||
ignoresPersistentStateOnLaunch = "NO" | ||
debugDocumentVersioning = "YES" | ||
debugServiceExtension = "internal" | ||
allowLocationSimulation = "YES"> | ||
<MacroExpansion> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "21E1D39E1D9410F000A91CA0" | ||
BuildableName = "Lift.framework" | ||
BlueprintName = "Lift" | ||
ReferencedContainer = "container:Lift.xcodeproj"> | ||
</BuildableReference> | ||
</MacroExpansion> | ||
<AdditionalOptions> | ||
</AdditionalOptions> | ||
</LaunchAction> | ||
<ProfileAction | ||
buildConfiguration = "Release" | ||
shouldUseLaunchSchemeArgsEnv = "YES" | ||
savedToolIdentifier = "" | ||
useCustomWorkingDirectory = "NO" | ||
debugDocumentVersioning = "YES"> | ||
<MacroExpansion> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "21E1D39E1D9410F000A91CA0" | ||
BuildableName = "Lift.framework" | ||
BlueprintName = "Lift" | ||
ReferencedContainer = "container:Lift.xcodeproj"> | ||
</BuildableReference> | ||
</MacroExpansion> | ||
</ProfileAction> | ||
<AnalyzeAction | ||
buildConfiguration = "Debug"> | ||
</AnalyzeAction> | ||
<ArchiveAction | ||
buildConfiguration = "Release" | ||
revealArchiveInOrganizer = "YES"> | ||
</ArchiveAction> | ||
</Scheme> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?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>CFBundleDevelopmentRegion</key> | ||
<string>en</string> | ||
<key>CFBundleDisplayName</key> | ||
<string>Lift</string> | ||
<key>CFBundleExecutable</key> | ||
<string>$(EXECUTABLE_NAME)</string> | ||
<key>CFBundleIdentifier</key> | ||
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string> | ||
<key>CFBundleInfoDictionaryVersion</key> | ||
<string>6.0</string> | ||
<key>CFBundleName</key> | ||
<string>$(PRODUCT_NAME)</string> | ||
<key>CFBundlePackageType</key> | ||
<string>FMWK</string> | ||
<key>CFBundleShortVersionString</key> | ||
<string>1.0</string> | ||
<key>CFBundleVersion</key> | ||
<string>$(CURRENT_PROJECT_VERSION)</string> | ||
<key>NSPrincipalClass</key> | ||
<string></string> | ||
</dict> | ||
</plist> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// | ||
// Jar+Additions.swift | ||
// Lift | ||
// | ||
// Created by Måns Bernhardt on 2016-05-23. | ||
// Copyright © 2016 iZettle. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
public extension Data { | ||
/// Construct a JSON string `Data` from a `Jar` | ||
init(json jar: Jar, prettyPrinted: Bool = true) throws { | ||
let any = try jar.asAny() | ||
|
||
if any is [Any] || any is [String: Any] { | ||
self = try JSONSerialization.data(withJSONObject: try jar.asAny(), options: prettyPrinted ? .prettyPrinted : []) | ||
} else if any is Null { | ||
self = try jar.assertNotNil("null".data(using: .utf8)) | ||
} else if let n = any as? NSNumber, String(cString: n.objCType) == "c" { | ||
self = try jar.assertNotNil("\((any as? Bool) ?? any)".data(using: .utf8)) | ||
} else { | ||
self = try jar.assertNotNil("\(any)".data(using: .utf8)) | ||
} | ||
} | ||
} | ||
public extension String { | ||
/// Construct a JSON `String` from a `Jar` | ||
init(json jar: Jar, prettyPrinted: Bool = true) throws { | ||
let data = try Data(json: jar, prettyPrinted: prettyPrinted) | ||
self = try jar.assertNotNil(String(data: data, encoding: .utf8)) | ||
} | ||
} | ||
public extension Jar { | ||
/// Construct a `Jar` from the content of the `json` data | ||
init(json data: Data) throws { | ||
try self.init(unchecked: JSONSerialization.jsonObject(with: data, options: .allowFragments)) | ||
} | ||
/// Construct a `Jar` from the content of the `json` string | ||
init(json string: String) throws { | ||
try self.init(json: string.data(using: String.Encoding.utf8).assertNotNil("Not an UTF8 string")) | ||
} | ||
/// Construct a `Jar` from the JSON at `url` | ||
init(json url: URL) throws { | ||
let data = try Data(contentsOf: url) | ||
try self.init(json: data) | ||
} | ||
} | ||
extension Jar: CustomStringConvertible, CustomDebugStringConvertible { | ||
public var description: String { | ||
do { | ||
return try String(json: self, prettyPrinted: true) | ||
} catch { | ||
return error.localizedDescription | ||
} | ||
} | ||
public var debugDescription: String { | ||
return description | ||
} | ||
} | ||
extension UserDefaults: MutatingValueForKey { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// | ||
// Jar+Array.swift | ||
// Lift | ||
// | ||
// Created by Måns Bernhardt on 2017-04-03. | ||
// Copyright © 2017 iZettle. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
|
||
public extension Jar { | ||
/// Returns the wrapped value if it's an array and it is successfully converted | ||
var array: [Any]? { | ||
return (try? object.asAny(context)) as? [Any] | ||
} | ||
|
||
/// Set the element at `index` to a value conforming to `JarRepresentable` | ||
/// - Note: Setting a value where `self` is not an array or if the index is out of bounds will trap. | ||
/// - Note: The getter is typically never used. Instead use the subscript overload that returns a `Jar` | ||
subscript(index: Int) -> JarRepresentable { | ||
get { | ||
return self[index] as Jar | ||
} | ||
set { | ||
arrayReplace(at: index, with: { [ try newValue.asJar(using: $0).asAny() ] }) | ||
} | ||
} | ||
|
||
/// Extract the element at `index` and return it in a `Jar` | ||
/// When a value is lifted out of the returned jar it might throw if `self` is not an array or if the access was out of bounds. | ||
/// - Note: Setting a value where `self` is not an array or if the index is out of bounds will trap. | ||
/// - Note: The setter is typically never used. Instead use the subscript overload that takes a `JarRepresentable` | ||
subscript(index: Int) -> Jar { | ||
get { | ||
let key: () -> String = { self.key() + "[\(index)]" } | ||
|
||
switch object { | ||
case .error, .none, .null: | ||
return Jar(object: object, context: context, key: self.key) | ||
case .array: | ||
let array = self.array! | ||
guard index >= array.startIndex && index < array.endIndex else { | ||
return Jar(object: .error(LiftError("Index out of bounds", key: "", context: self)), context: context, key: key) | ||
} | ||
return Jar(object: Object(array[index]), context: context, key: key) | ||
default: | ||
return Jar(object: .error(LiftError("Not an array", key: "", context: self)), context: context, key: self.key) | ||
} | ||
} | ||
set { | ||
arrayReplace(at: index, with: { _ in [ try newValue.asAny() ] }) | ||
} | ||
} | ||
|
||
/// Appends a `jar` to `self` if `self` is an array or set `self` to an array holding `jar` if not | ||
mutating func append(_ value: JarRepresentable) { | ||
arrayReplace(at: nil, with: { [ try value.asJar(using: $0).asAny() ] }) | ||
} | ||
|
||
/// Appends a `jar` to `self` if `self` is an array or set `self` to an array holding `jar` if not | ||
mutating func append(_ jar: Jar) { | ||
append(jar as JarRepresentable) | ||
} | ||
} | ||
|
||
|
||
/// Lift an array value out of a Jar | ||
public postfix func ^<T: Liftable>(jar: Jar) throws -> [T] where T.To == T { | ||
return try jar.assertNotNil(jar.array, "Not an array").enumerated().map { i, any in | ||
let itemJar = Jar(object: Jar.Object(any), context: jar.context, key: { jar.key() + "[\(i)]" }) | ||
return try T.lift(from: itemJar) | ||
} | ||
} | ||
|
||
/// Lift an optional array value out of a Jar | ||
public postfix func ^<T: Liftable>(jar: Jar) throws -> [T]? where T.To == T { | ||
return try jar.map { try $0^ } | ||
} | ||
|
||
public extension Jar { | ||
/// Lifts an array of type `[T]` and applies `transform` to it's elements | ||
func map<T: Liftable, O>(_ transform: (T) throws -> O) throws -> [O] where T.To == T { | ||
return try (self^).map(transform) | ||
} | ||
|
||
/// Lifts an array of type `[T]`, and if not nil, applies `transform` to it's elements | ||
func map<T: Liftable, O>(_ transform: (T) throws -> O) throws -> [O]? where T.To == T { | ||
return try map { try $0.map(transform) } | ||
} | ||
} | ||
|
||
private extension Jar { | ||
mutating func arrayReplace(at range: Range<Int>?, with toAny: @escaping ToAny) { | ||
switch object { | ||
case let .array(ops): | ||
object = .array(ops + [ (range, toAny) ]) | ||
case .none: | ||
object = .array([ (nil, toAny) ]) | ||
case let .primitive(val): | ||
object = .array([ (nil, val), (nil, toAny) ]) | ||
default: | ||
object = .error(LiftError("Not an array", context: self)) | ||
} | ||
} | ||
|
||
mutating func arrayReplace(at index: Int, with toAny: @escaping ToAny) { | ||
arrayReplace(at: Range(uncheckedBounds: (index, index)), with: toAny) | ||
} | ||
} |
Oops, something went wrong.