izeni-team / retrolux Goto Github PK
View Code? Open in Web Editor NEWThe all in one networking solution for iOS.
License: MIT License
The all in one networking solution for iOS.
License: MIT License
Surely there's a better way?
See Title.
Current State:
When Retrolux is unable to parse the JSON, we get an error message and the entire response from the server via the response.data object.
Desired State:
Along with the retrolux generated error message, it would be nice to get the section of the server response that is not matching the retrolux object. This will improve error reporting and help speed up the debug process.
Related: #11
Right now it just logs <Reflection: 0x0923f7a7a02>
, but should be more like Realm.
Retrolux v1.0 is currently being designed and prototyped. It's primary goals are:
Currently, we are working on redesigning the Builder API towards accomplishing goals 3, 4, 6, 7, and 8.
They are in the RetroluxTests directory, but part of the Retrolux target. Confusing.
They need to show up in response.request.
This results in the callback being executed twice. Also, it doesn't print the error.
It's just too dang convenient. Realm does this too, IIRC.
ReflectionJSONSerializer's fault! I.e., when using afterSerialization
to inject an unsupported type into the raw dictionary before it gets passed into JSONSerialization
.
Some IDs are integers and it's annoying to have to pass in String(myInt) all the time.
Specifically, tests for this specific case.
They are actually supported, but it's hard to use and confusing.
I evaluated the code and realized why NestedTransformer's code appears to be stupid. tl;dr, delete the
?
fromtypealias TypeOfProperty = NSNumber?
.
class OptionalAnyToOptionalNSNumberTransformer: NestedTransformer {
typealias TypeOfData = Any?
typealias TypeOfProperty = NSNumber? // Should be NSNumber, but most people will put in an optional.
func setter(_ dataValue: Any?, type: Any.Type) throws -> NSNumber {
if let number = dataValue as? NSNumber {
return number
} else if let string = dataValue as? String, let integer = Int(string) {
return NSNumber(integerLiteral: integer)
} else {
throw OptionalAnyToOptionalNSNumberTransformerError.unrecognizedNumber(given: dataValue)
}
}
func getter(_ propertyValue: NSNumber) throws -> Any? {
return propertyValue
}
}
TaskService taskService = ServiceGenerator.createService(TaskService.class);
Call<List<Task>> call = taskService.getTasks();
call.enqueue(new Callback<List<Task>>() {
@Override
public void onResponse(Call<List<Task>> call, Response<List<Task>> response) {
if (response.isSuccessful()) {
// tasks available
} else {
// error response, no access to resource?
}
}
@Override
public void onFailure(Call<List<Task>> call, Throwable t) {
// something went completely south (like no internet connection)
Log.d("Error", t.getMessage());
}
}
Plan:
//: Playground - noun: a place where people can play
import UIKit
enum PropertyCustomizations {
case ignored
case nullable
case mapped(to: String)
case transformed(TransformerType)
}
class Config {
internal var storage = [String: [PropertyCustomizations]]()
subscript(property: String) -> [PropertyCustomizations] {
get {
return storage[property] ?? []
}
set {
storage[property] = newValue
}
}
}
protocol Reflectable: NSObjectProtocol {
static func config(_ c: Config)
}
class Reflection: NSObject, Reflectable {
class func config(_ c: Config) {
}
}
protocol TransformerType {
var propertyType: Any.Type { get }
var dataType: Any.Type { get }
func set(value: Any, propertyName: String, instance: Reflectable)
func getValue(propertyName: String, instance: Reflectable) -> Any
}
class Transformer<PropertyType, DataType>: TransformerType {
var propertyType: Any.Type {
return PropertyType.self
}
var dataType: Any.Type {
return DataType.self
}
private let setter: (DataType) -> PropertyType
private let getter: (PropertyType) -> DataType
init(setter: @escaping (DataType) -> PropertyType, getter: @escaping (PropertyType) -> DataType) {
self.setter = setter
self.getter = getter
}
func set(value: Any, propertyName: String, instance: Reflectable) {
let transformed = setter(value as! DataType)
(instance as! NSObject).setValue(transformed, forKey: propertyName)
}
func getValue(propertyName: String, instance: Reflectable) -> Any {
return getter((instance as! NSObject).value(forKey: propertyName) as! PropertyType)
}
}
class CustomTransformer<ClassType, PropertyType, DataType>: TransformerType {
var propertyType: Any.Type {
return PropertyType.self
}
var dataType: Any.Type {
return DataType.self
}
init(setter: @escaping (ClassType, DataType) -> Void, getter: @escaping (ClassType, PropertyType) -> DataType) {
}
func set(value: Any, propertyName: String, instance: Reflectable) {
}
func getValue(propertyName: String, instance: Reflectable) -> Any {
return () as Any
}
}
let dateTransformer: TransformerType =
Transformer<Date, String>(setter: { (value) -> Date in
print("input: \(value)")
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'hh:mm:ssZ"
return formatter.date(from: value as! String)!
}, getter: { (value: Date) -> String in
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'hh:mm:ssZ"
return formatter.string(from: value)
})
class Person: Reflection {
var name = ""
var age = Data()
var born = Date()
override class func config(_ c: Config) {
c["name"] = [.ignored]
c["born"] = [.transformed(dateTransformer)]
let customTransformer = CustomTransformer<Person, Data, Int>(setter: { (person, value) in
person.age = Data()
}, getter: { _ in
Int()
})
c["age"] = [.nullable, .mapped(to: "user_age"), .transformed(customTransformer)]
}
}
let config = Config()
let p = Person()
let type: Reflectable.Type = type(of: p)
type.config(config)
config["name"]
if case PropertyCustomizations.transformed(let transformer) = PropertyCustomizations.transformed(dateTransformer) {
let value: Any = "2005-04-03T12:00:00Z"
if type(of: value) == transformer.dataType || transformer.dataType == Any.self {
transformer.set(value: value, propertyName: "born", instance: p)
} else {
print("Type \(type(of: value)) is not supported type. Should be \(transformer.dataType).")
}
print("person.born:", p.born)
let gotBack = transformer.getValue(propertyName: "born", instance: p)
print("Value back was: \(gotBack)")
}
This library won't compile on Swift 4 with multiple errors.
It includes JSON and Multipart, but is missing URLEncoded.
Have the following transformer which doesn't work:
class OptionalNSNumberToIntTranformer: NestedTransformer {
typealias TypeOfData = NSNumber?
typealias TypeOfProperty = Int
func setter(_ dataValue: NSNumber?, type: Any.Type) throws -> Int {
return dataValue?.intValue ?? 0
}
func getter(_ propertyValue: Int) throws -> NSNumber? {
return NSNumber(integerLiteral: propertyValue)
}
}
Specifically, persisting the requests and sending them when the network becomes available. And coming up with a productive mechanism for notifying the GUI when the task is actually sent.
Related: background uploads/downloads
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.