Skip to content

Commit

Permalink
EVCustomReflectable update
Browse files Browse the repository at this point in the history
  • Loading branch information
Vermeer, Edwin authored and Vermeer, Edwin committed Jun 8, 2017
1 parent 594ec29 commit 1ca08fc
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 53 deletions.
2 changes: 1 addition & 1 deletion EVReflection.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|
s.name = "EVReflection"
s.version = "4.16.3"
s.summary = "Reflection based (dictionary, JSON or XML) object mapping (including extensions for Alamofire and Moya with RxSwift or ReactiveSwift)"
s.summary = "Reflection based (Dictionary, CKRecord, NSManagedObject, Realm, JSON and XML) object mapping with extensions for Alamofire and Moya with RxSwift or ReactiveSwift"

s.description = <<-EOS
[Reflection](https://github.com/evermeer/EVReflection) based object mapping (dictionary, Json, XML, CKRecord)
Expand Down
Binary file not shown.
7 changes: 6 additions & 1 deletion Source/EVReflection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,11 @@ final public class EVReflection {
if theValue is Array<Any> {
return (theValue as AnyObject, valueType, false)
}
if theValue is EVCustomReflectable {
let value: AnyObject = (theValue as! EVCustomReflectable).toCodableValue() as AnyObject
return (value, valueType, false)
}

if theValue is EVReflectable && theValue is NSObject {
if valueType.contains("<") {
return (theValue as! EVReflectable, swiftStringFromClass(theValue as! NSObject), true)
Expand All @@ -880,7 +885,7 @@ final public class EVReflection {
if valueType.hasPrefix("Swift.Array<") && parentObject is EVArrayConvertable {
return ((parentObject as! EVArrayConvertable).convertArray(key ?? "_unknownKey", array: theValue), valueType, false)
}

(parentObject as? EVReflectable)?.addStatusMessage(.InvalidType, message: "valueForAny unkown type \(valueType) for value: \(theValue).")
evPrint(.InvalidType, "ERROR: valueForAny unkown type \(valueType) for key: \(key ?? "") and value: \(theValue).")
return (NSNull(), "NSNull", false)
Expand Down
3 changes: 3 additions & 0 deletions UnitTests/EVReflectionTests/EVReflectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,10 @@ class EVReflectionTests: XCTestCase {
print(nx)
XCTAssert(nx.beacons.count == 1, "Should also have an object")
}

}





75 changes: 75 additions & 0 deletions UnitTests/EVReflectionTests/EVReflectionTests2.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//
// EVReflectionTests2.swift
// UnitTests
//
// Created by Vermeer, Edwin on 06/06/2017.
// Copyright © 2017 evict. All rights reserved.
//


import XCTest
@testable import EVReflection


class EVReflectionTests2: XCTestCase {

/**
For now nothing to setUp
*/
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
EVReflection.setBundleIdentifier(Comment.self)
}

/**
For now nothing to tearDown
*/
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}

func testTemp() {

let test = "[\n {\n \"status\" : \"0\",\n \"content\" : \"Shuru\",\n \"ctime\" : \"1438250556\",\n \"img\" : \"\",\n \"testuserinfo\" : {\n \"avatar\" : \"/5602503cc79de.jpg\",\n \"uid\" : \"d8b81b21c72f1177300247e2d8d88ec5\",\n \"telnum\" : \"18565280137\",\n \"is_seller\" : \"0\",\n \"sex\" : \"\",\n \"name\" : \"\",\n \"interest\" : \"\"\n },\n \"fabric\" : null,\n \"commentid\" : \"22\",\n \"sound\" : \"\",\n \"vote\" : \"0\",\n \"isvote\" : 0,\n \"seller_card\" : null\n }\n]"
print("\(test)")
var comments = EVReflection.arrayFromJson(type: Comment(), json: test)
var comments2 = [Comment](json: test)
print(comments[0].testuserinfo?.uid ?? "")
print(comments2[0].testuserinfo?.uid ?? "")
}

func testTemp2() {
let test = "[\n {\n \"status\" : \"0\",\n \"content\" : \"Shuru\",\n \"ctime\" : \"1438250556\",\n \"img\" : \"\",\n \"isvote\" : 0,\n \"testuserinfo\" : {\n \"avatar\" : \"\",\n \"uid\" : \"d8b81b21c72f1177300247e2d8d88ec5\",\n \"telnum\" : \"18565280137\",\n \"is_seller\" : \"0\",\n \"sex\" : \"\",\n \"name\" : \"\",\n \"interest\" : \"\"\n },\n \"commentid\" : \"22\",\n \"fabric\" : null,\n \"sound\" : \"\",\n \"vote\" : \"0\",\n \"seller_card\" : null\n }\n]"
let comments = EVReflection.arrayFromJson(type: Comment(), json: test)
print(comments[0].testuserinfo?.telnum ?? "")
}
}

class Comment: EVObject {
var commentid = ""
var content = ""
var ctime = ""
var status = "2"
var img = ""
var vote = "0"
var sound: String?
var isvote: Bool = false
var seller_card: String?
var fabric: String?
var testuserinfo: UserInfo?
}

class UserInfo: EVObject {
var avatar = ""
var uid = ""
var telnum = ""
var is_seller = "0"
var sex = "M"
var name = "TestUser"
var interest = ""
}



81 changes: 30 additions & 51 deletions UnitTests/EVReflectionTests/WorkaroundEnumTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,71 +104,50 @@ class EnumWorkaroundsTests: XCTestCase {
let (val, _, _) = EVReflection.valueForAny(self, key: "a", anyValue: theEnum)
return val
}

func testEnumWrapper() {
let z = Z()
z.x = EnumWrapper(X.A)
let json = z.toJsonString()
print(json)
let z2 = Z(json: json)
print(z2)
XCTAssert(z2.x.rawValue == X.A.rawValue, "New value should also be X.A")
}
}



public class Z : EVObject {
var x: EnumWrapper = EnumWrapper(X.B)
}

public enum X: String, EVRaw {
case A,
B
}




class EVReflectionTests2: XCTestCase {
public class EnumWrapper: NSObject, EVCustomReflectable {

/**
For now nothing to setUp
*/
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
EVReflection.setBundleIdentifier(Comment.self)
var value: EVRaw?
var raw : Any?

var rawValue: Any? {
return (value?.anyRawValue ?? raw)
}

/**
For now nothing to tearDown
*/
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
public init(_ value: EVRaw) {
self.value = value
}

func testTemp() {

let test = "[\n {\n \"status\" : \"0\",\n \"content\" : \"Shuru\",\n \"ctime\" : \"1438250556\",\n \"img\" : \"\",\n \"testuserinfo\" : {\n \"avatar\" : \"/5602503cc79de.jpg\",\n \"uid\" : \"d8b81b21c72f1177300247e2d8d88ec5\",\n \"telnum\" : \"18565280137\",\n \"is_seller\" : \"0\",\n \"sex\" : \"\",\n \"name\" : \"\",\n \"interest\" : \"\"\n },\n \"fabric\" : null,\n \"commentid\" : \"22\",\n \"sound\" : \"\",\n \"vote\" : \"0\",\n \"isvote\" : 0,\n \"seller_card\" : null\n }\n]"
print("\(test)")
var comments = EVReflection.arrayFromJson(type: Comment(), json: test)
var comments2 = [Comment](json: test)
print(comments[0].testuserinfo?.uid ?? "")
print(comments2[0].testuserinfo?.uid ?? "")
public func constructWith(value: Any?) {
self.value = nil
self.raw = value
}

func testTemp2() {
let test = "[\n {\n \"status\" : \"0\",\n \"content\" : \"Shuru\",\n \"ctime\" : \"1438250556\",\n \"img\" : \"\",\n \"isvote\" : 0,\n \"testuserinfo\" : {\n \"avatar\" : \"\",\n \"uid\" : \"d8b81b21c72f1177300247e2d8d88ec5\",\n \"telnum\" : \"18565280137\",\n \"is_seller\" : \"0\",\n \"sex\" : \"\",\n \"name\" : \"\",\n \"interest\" : \"\"\n },\n \"commentid\" : \"22\",\n \"fabric\" : null,\n \"sound\" : \"\",\n \"vote\" : \"0\",\n \"seller_card\" : null\n }\n]"
let comments = EVReflection.arrayFromJson(type: Comment(), json: test)
print(comments[0].testuserinfo?.telnum ?? "")
public func toCodableValue() -> Any {
return (value?.anyRawValue ?? raw)!
}
}

class Comment: EVObject {
var commentid = ""
var content = ""
var ctime = ""
var status = "2"
var img = ""
var vote = "0"
var sound: String?
var isvote: Bool = false
var seller_card: String?
var fabric: String?
var testuserinfo: UserInfo?
}

class UserInfo: EVObject {
var avatar = ""
var uid = ""
var telnum = ""
var is_seller = "0"
var sex = "M"
var name = "TestUser"
var interest = ""
}
4 changes: 4 additions & 0 deletions UnitTests/UnitTests.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
13AC7EE31ED21DEE00BA89FB /* EVReflectionIssue197.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13AC7EE21ED21DED00BA89FB /* EVReflectionIssue197.swift */; };
13AC7EE41ED21E5600BA89FB /* EVReflectionIssue197.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13AC7EE21ED21DED00BA89FB /* EVReflectionIssue197.swift */; };
13AC7EE51ED21E5700BA89FB /* EVReflectionIssue197.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13AC7EE21ED21DED00BA89FB /* EVReflectionIssue197.swift */; };
13E937DF1EE74EB600541DC8 /* EVReflectionTests2.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13E937DE1EE74EB600541DC8 /* EVReflectionTests2.swift */; };
13EF3F691EDDF63700CDBFF4 /* CDUser+CoreDataClass.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13EF3F671EDDF63700CDBFF4 /* CDUser+CoreDataClass.swift */; };
13EF3F6A1EDDF63700CDBFF4 /* CDUser+CoreDataClass.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13EF3F671EDDF63700CDBFF4 /* CDUser+CoreDataClass.swift */; };
13EF3F6B1EDDF63700CDBFF4 /* CDUser+CoreDataClass.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13EF3F671EDDF63700CDBFF4 /* CDUser+CoreDataClass.swift */; };
Expand Down Expand Up @@ -267,6 +268,7 @@
13AC7ECE1ECB929600BA89FB /* EVReflectionEVCustomReflectable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EVReflectionEVCustomReflectable.swift; sourceTree = "<group>"; };
13AC7EDE1ED15A8E00BA89FB /* EVReflectionIssue202.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EVReflectionIssue202.swift; sourceTree = "<group>"; };
13AC7EE21ED21DED00BA89FB /* EVReflectionIssue197.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EVReflectionIssue197.swift; sourceTree = "<group>"; };
13E937DE1EE74EB600541DC8 /* EVReflectionTests2.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EVReflectionTests2.swift; sourceTree = "<group>"; };
13EF3F671EDDF63700CDBFF4 /* CDUser+CoreDataClass.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "CDUser+CoreDataClass.swift"; path = "CoreDataTests/CDUser+CoreDataClass.swift"; sourceTree = "<group>"; };
13F5E81C1EBC7AEE0038EDB4 /* EVReflectionTest.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = EVReflectionTest.xcdatamodel; sourceTree = "<group>"; };
16F24E2F358BA7E02E9739C7 /* Pods_UnitTestsOSX.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_UnitTestsOSX.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -501,6 +503,7 @@
isa = PBXGroup;
children = (
7F41ECAA1B91F306003A2236 /* WorkaroundEnumTest.swift */,
13E937DE1EE74EB600541DC8 /* EVReflectionTests2.swift */,
);
name = TemporaryTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -995,6 +998,7 @@
7F99301E1E2426B500792CF1 /* AlamofireXml2Tests.swift in Sources */,
13AC7ECF1ECB929600BA89FB /* EVReflectionEVCustomReflectable.swift in Sources */,
7F41ECAE1B91F306003A2236 /* EVReflectionJsonTests.swift in Sources */,
13E937DF1EE74EB600541DC8 /* EVReflectionTests2.swift in Sources */,
7FCD1D491D3FADA800F51BE0 /* EVReflectionIssue107.swift in Sources */,
131201131EBC4E6D00A189C8 /* CoreDataTests.swift in Sources */,
13345EDE1EC892F600912B37 /* EVReflectionIssueX.swift in Sources */,
Expand Down

0 comments on commit 1ca08fc

Please sign in to comment.