Skip to content

Commit

Permalink
Merge pull request #21 from edwardaux/argo_3_0_and_bool_support
Browse files Browse the repository at this point in the history
Argo 3 0 and bool support
  • Loading branch information
edwardaux committed Apr 7, 2016
2 parents f71fb1b + 62008e4 commit 2eac657
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 70 deletions.
2 changes: 1 addition & 1 deletion Cartfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
github "thoughtbot/Argo" ~> 2.2
github "thoughtbot/Argo" ~> 3.0
2 changes: 1 addition & 1 deletion Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1 +1 @@
github "thoughtbot/Argo" "v2.2.0"
github "thoughtbot/Argo" "v3.0.0"
2 changes: 1 addition & 1 deletion Carthage/Checkouts/Argo
Submodule Argo updated 44 files
+2 −0 .hound.yml
+1 −1 Argo.podspec
+20 −18 Argo.xcodeproj/project.pbxproj
+13 −6 Argo/Extensions/Dictionary.swift
+7 −0 Argo/Extensions/NSNumber.swift
+10 −2 Argo/Extensions/RawRepresentable.swift
+50 −4 Argo/Functions/catDecoded.swift
+213 −16 Argo/Functions/decode.swift
+20 −1 Argo/Functions/flatReduce.swift
+50 −5 Argo/Functions/sequence.swift
+150 −0 Argo/Operators/Decode.swift
+0 −43 Argo/Operators/DecodeDecoded.swift
+0 −43 Argo/Operators/DecodeOptional.swift
+1 −1 Argo/Resources/Info.plist
+31 −1 Argo/Types/Decodable.swift
+35 −0 Argo/Types/DecodeError.swift
+27 −8 Argo/Types/Decoded/Alternative.swift
+30 −5 Argo/Types/Decoded/Applicative.swift
+92 −11 Argo/Types/Decoded/Decoded.swift
+6 −4 Argo/Types/Decoded/FailureCoalescing.swift
+19 −5 Argo/Types/Decoded/Functor.swift
+36 −5 Argo/Types/Decoded/Monad.swift
+52 −6 Argo/Types/JSON.swift
+179 −8 Argo/Types/StandardTypes.swift
+298,215 −3,829 ArgoTests/JSON/big_data.json
+1 −0 ArgoTests/JSON/types.json
+2 −0 ArgoTests/Models/TestModel.swift
+24 −0 ArgoTests/Tests/DecodedTests.swift
+4 −4 ArgoTests/Tests/EquatableTests.swift
+6 −4 ArgoTests/Tests/ExampleTests.swift
+10 −3 ArgoTests/Tests/PerformanceTests.swift
+4 −4 ArgoTests/Tests/RawRepresentableTests.swift
+1 −0 ArgoTests/Tests/SwiftDictionaryDecodingTests.swift
+1 −0 ArgoTests/Tests/TypeTests.swift
+2 −0 ArgoTests/plists/types.plist
+2 −2 Cartfile.resolved
+1 −1 Carthage/Checkouts/Curry
+1 −1 Carthage/Checkouts/Runes
+2 −6 Documentation/Basic-Usage.md
+70 −0 Documentation/Decode-Enums.md
+5 −5 Documentation/Decode-Root-Keys.md
+1 −0 Documentation/README.md
+30 −2 bin/test
+12 −3 circle.yml
4 changes: 2 additions & 2 deletions Ogra.podspec
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
Pod::Spec.new do |s|
s.name = "Ogra"
s.version = "2.2.0"
s.version = "3.0.0"
s.summary = "Provides the ability to convert from a model object into an Argo JSON representation."
s.description = "A companion project for the Argo library that facilitates converting back from model objects into JSON"
s.homepage = "https://github.com/edwardaux/Ogra"
s.license = 'MIT'
s.author = "Craig Edwards"
s.source = { :git => "https://github.com/edwardaux/Ogra.git", :tag => "#{s.version.to_s}" }

s.dependency 'Argo', '~> 2.2'
s.dependency 'Argo', '~> 3.0'
s.source_files = 'Ogra/**/*.{h,swift}'
s.ios.deployment_target = '8.0'
s.osx.deployment_target = '10.9'
Expand Down
3 changes: 2 additions & 1 deletion Ogra/Encodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extension String: Encodable {

extension Bool: Encodable {
public func encode() -> JSON {
return .Number(self)
return .Bool(self)
}
}

Expand Down Expand Up @@ -129,6 +129,7 @@ extension JSON {
case .String(let value): return value
case .Number(let value): return value
case .Array(let array): return array.map { $0.JSONObject() }
case .Bool(let value): return value
case .Object(let object):
var dict: [Swift.String : AnyObject] = [:]
for (key, value) in object {
Expand Down
9 changes: 6 additions & 3 deletions OgraTests/OgraModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ struct User {
let pet: Pet?
let nicknames: [String]?
let accounts: [String:String]?
let happy: Bool
}
struct Pet {
let name: String
Expand Down Expand Up @@ -56,8 +57,8 @@ enum UIntDialingCode: UInt {

// MARK: - JSON Encoding and Decoding
extension User: Decodable, Encodable {
static func create(id: Int)(name: String)(email: String?)(pet: Pet?)(nicknames: [String]?)(accounts: [String:String]?) -> User {
return User(id:id, name:name, email:email, pet:pet, nicknames:nicknames, accounts:accounts)
static func create(id: Int)(name: String)(email: String?)(pet: Pet?)(nicknames: [String]?)(accounts: [String:String]?)(happy: Bool) -> User {
return User(id:id, name:name, email:email, pet:pet, nicknames:nicknames, accounts:accounts, happy:happy)
}

static func decode(j: JSON) -> Decoded<User> {
Expand All @@ -68,6 +69,7 @@ extension User: Decodable, Encodable {
<*> j <|? "pet"
<*> j <||? "nicknames"
<*> .optional(flatReduce(["accounts"], initial: j, combine: decodedJSON) >>- Dictionary<String, String>.decode)
<*> j <| "happy"
}

func encode() -> JSON {
Expand All @@ -77,7 +79,8 @@ extension User: Decodable, Encodable {
"email" : self.email.encode(),
"pet" : self.pet.encode(),
"nicknames" : self.nicknames.encode(),
"accounts" : self.accounts.encode()
"accounts" : self.accounts.encode(),
"happy" : self.happy.encode()
])
}
}
Expand Down
122 changes: 61 additions & 61 deletions OgraTests/OgraTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,43 @@ class OgraTests: XCTestCase {
private func toJSON(jsonString: String) -> JSON {
let jsonData = jsonString.dataUsingEncoding(NSUTF8StringEncoding)!
let jsonObject: AnyObject = try! NSJSONSerialization.JSONObjectWithData(jsonData, options:NSJSONReadingOptions())
return JSON.parse(jsonObject)
return JSON(jsonObject)
}

func testNullPet() {
let jsonIn = toJSON("{ \"id\":123, \"name\":\"John\", \"email\":\"[email protected]\", \"pet\":null, \"nicknames\":[\"Johnny\"], \"accounts\":{\"gmail\":\"john\"} }")
let jsonIn = toJSON("{ \"id\":123, \"name\":\"John\", \"email\":\"[email protected]\", \"pet\":null, \"nicknames\":[\"Johnny\"], \"accounts\":{\"gmail\":\"john\"}, \"happy\":true }")
let user = User.decode(jsonIn).value!
let jsonOut = user.encode()

XCTAssertEqual(jsonIn, jsonOut)
}

func testNullNicknames() {
let jsonIn = toJSON("{ \"id\":123, \"name\":\"John\", \"email\":\"[email protected]\", \"pet\":null, \"nicknames\":null, \"accounts\":{\"gmail\":\"john\"} }")
let jsonIn = toJSON("{ \"id\":123, \"name\":\"John\", \"email\":\"[email protected]\", \"pet\":null, \"nicknames\":null, \"accounts\":{\"gmail\":\"john\"}, \"happy\":true }")
let user = User.decode(jsonIn).value!
let jsonOut = user.encode()

XCTAssertEqual(jsonIn, jsonOut)
}

func testNullAccounts() {
let jsonIn = toJSON("{ \"id\":123, \"name\":\"John\", \"email\":\"[email protected]\", \"pet\":null, \"nicknames\":[\"Johnny\"], \"accounts\":null }")
let jsonIn = toJSON("{ \"id\":123, \"name\":\"John\", \"email\":\"[email protected]\", \"pet\":null, \"nicknames\":[\"Johnny\"], \"accounts\":null, \"happy\":true }")
let user = User.decode(jsonIn).value!
let jsonOut = user.encode()

XCTAssertEqual(jsonIn, jsonOut)
}

func testWithPet() {
let jsonIn = toJSON("{ \"id\":123, \"name\":\"John\", \"email\":\"[email protected]\", \"pet\":{\"name\":\"Rex\"}, \"nicknames\":[\"Johnny\"], \"accounts\":{\"gmail\":\"john\"} }")
let jsonIn = toJSON("{ \"id\":123, \"name\":\"John\", \"email\":\"[email protected]\", \"pet\":{\"name\":\"Rex\"}, \"nicknames\":[\"Johnny\"], \"accounts\":{\"gmail\":\"john\"}, \"happy\":true }")
let user = User.decode(jsonIn).value!
let jsonOut = user.encode()

XCTAssertEqual(jsonIn, jsonOut)
}

func testPassingToJSONSerialization() {
let json = toJSON("{ \"id\":123, \"name\":\"John\", \"email\":\"[email protected]\", \"pet\":{\"name\":\"Rex\"}, \"nicknames\":[\"Johnny\"], \"accounts\":{\"gmail\":\"john\"} }")
let json = toJSON("{ \"id\":123, \"name\":\"John\", \"email\":\"[email protected]\", \"pet\":{\"name\":\"Rex\"}, \"nicknames\":[\"Johnny\"], \"accounts\":{\"gmail\":\"john\"}, \"happy\":true }")
let user = User.decode(json).value!

let jsonObject = user.encode().JSONObject()
Expand All @@ -62,59 +62,59 @@ class OgraTests: XCTestCase {
print(string)
}

func testRawRepresentableStringType() {
let continent: Continent = .NorthAmerica
let json: JSON = .String(continent.rawValue)
let encoded = continent.encode()
XCTAssertEqual(json, encoded)
}

func testRawRepresentableIntType() {
let dialingCode: IntDialingCode = .UnitedStates
let json: JSON = .Number(dialingCode.rawValue)
let encoded = dialingCode.encode()
XCTAssertEqual(json, encoded)
}

func testRawRepresentableDoubleType() {
let dialingCode: DoubleDialingCode = .UnitedStates
let json: JSON = .Number(dialingCode.rawValue)
let encoded = dialingCode.encode()
XCTAssertEqual(json, encoded)
}

func testRawRepresentableFloatType() {
let dialingCode: FloatDialingCode = .UnitedStates
let json: JSON = .Number(dialingCode.rawValue)
let encoded = dialingCode.encode()
XCTAssertEqual(json, encoded)
}

func testRawRepresentableUIntType() {
let dialingCode: UIntDialingCode = .UnitedStates
let json: JSON = .Number(dialingCode.rawValue)
let encoded = dialingCode.encode()
XCTAssertEqual(json, encoded)
}

func testConversionToAnyObject() {
XCTAssertEqual(JSON.Null.JSONObject() as? NSNull, NSNull())
XCTAssertEqual(JSON.String("42").JSONObject() as? String, "42")
XCTAssertEqual(JSON.Number(NSNumber(integer: 42)).JSONObject() as? Int, 42)
XCTAssertEqual(JSON.Array([JSON.String("42")]).JSONObject() as! [String], ["42"])
XCTAssertEqual(JSON.Object(["life" : JSON.String("42")]).JSONObject() as! [String : String], ["life" : "42"])
}

func testTypesEncodeProperly() {
XCTAssertEqual(JSON.Null.encode(), JSON.Null)
XCTAssertEqual("42".encode(), JSON.String("42"))
XCTAssertEqual(true.encode(), JSON.Number(NSNumber(bool: true)))
XCTAssertEqual(false.encode(), JSON.Number(NSNumber(bool: false)))
XCTAssertEqual(Int(42).encode(), JSON.Number(NSNumber(integer: 42)))
XCTAssertEqual(Double(42.42).encode(), JSON.Number(NSNumber(double: 42.42)))
XCTAssertEqual(Float(42.42).encode(), JSON.Number(NSNumber(float: 42.42)))
XCTAssertEqual(UInt(42).encode(), JSON.Number(NSNumber(unsignedLong: 42)))
XCTAssertEqual(("42" as String?).encode(), JSON.String("42"))
XCTAssertEqual((nil as String?).encode(), JSON.Null)
}
func testRawRepresentableStringType() {
let continent: Continent = .NorthAmerica
let json: JSON = .String(continent.rawValue)
let encoded = continent.encode()
XCTAssertEqual(json, encoded)
}
func testRawRepresentableIntType() {
let dialingCode: IntDialingCode = .UnitedStates
let json: JSON = .Number(dialingCode.rawValue)
let encoded = dialingCode.encode()
XCTAssertEqual(json, encoded)
}
func testRawRepresentableDoubleType() {
let dialingCode: DoubleDialingCode = .UnitedStates
let json: JSON = .Number(dialingCode.rawValue)
let encoded = dialingCode.encode()
XCTAssertEqual(json, encoded)
}
func testRawRepresentableFloatType() {
let dialingCode: FloatDialingCode = .UnitedStates
let json: JSON = .Number(dialingCode.rawValue)
let encoded = dialingCode.encode()
XCTAssertEqual(json, encoded)
}
func testRawRepresentableUIntType() {
let dialingCode: UIntDialingCode = .UnitedStates
let json: JSON = .Number(dialingCode.rawValue)
let encoded = dialingCode.encode()
XCTAssertEqual(json, encoded)
}
func testConversionToAnyObject() {
XCTAssertEqual(JSON.Null.JSONObject() as? NSNull, NSNull())
XCTAssertEqual(JSON.String("42").JSONObject() as? String, "42")
XCTAssertEqual(JSON.Number(NSNumber(integer: 42)).JSONObject() as? Int, 42)
XCTAssertEqual(JSON.Array([JSON.String("42")]).JSONObject() as! [String], ["42"])
XCTAssertEqual(JSON.Object(["life" : JSON.String("42")]).JSONObject() as! [String : String], ["life" : "42"])
}
func testTypesEncodeProperly() {
XCTAssertEqual(JSON.Null.encode(), JSON.Null)
XCTAssertEqual("42".encode(), JSON.String("42"))
XCTAssertEqual(true.encode(), JSON.Bool(true))
XCTAssertEqual(false.encode(), JSON.Bool(false))
XCTAssertEqual(Int(42).encode(), JSON.Number(NSNumber(integer: 42)))
XCTAssertEqual(Double(42.42).encode(), JSON.Number(NSNumber(double: 42.42)))
XCTAssertEqual(Float(42.42).encode(), JSON.Number(NSNumber(float: 42.42)))
XCTAssertEqual(UInt(42).encode(), JSON.Number(NSNumber(unsignedLong: 42)))
XCTAssertEqual(("42" as String?).encode(), JSON.String("42"))
XCTAssertEqual((nil as String?).encode(), JSON.Null)
}
}
9 changes: 9 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,14 @@ github "edwardaux/Ogra"

Then run `carthage update`.

## Extending Ogra
Additions and enhancements are most welcome. I'd suggest the following steps:

* Create a fork of the project and clone to your local machine
* Run the following command: `carthage update --use-submodules`
* Make your changes
* Add test cases and make sure they all pass
* Create a pull request to merge your changes

## Licence
Ogra is Copyright(c) 2015 Craig Edwards. It may be redistributed under the terms specified by the [MIT licence](licence.md).

0 comments on commit 2eac657

Please sign in to comment.