rexmas / crust Goto Github PK
View Code? Open in Web Editor NEWWhat sits between the cloud and the core of an app
License: MIT License
What sits between the cloud and the core of an app
License: MIT License
Currently we print the enum
json. We should convert back to values and print since it's more readable.
Segfault during compilation.
1. While emitting IR SIL function "@_T05Crust2lsoiq1_xz_AA7BindingOyq0_q_G3key_q1_7payloadtt12MappedObjectQy_RszAA7MappingR_AA0H3KeyR0_AA0H7PayloadCyq0_GRb1_r2_lF".
for '<-(_:_:)' at /Users/pwebster/Desktop/Crust/Crust/Mapper/MappingOperator.swift:80:8
Is it possible to make a Transform
whose MappedObject
is an Optional? Following the example of the custom Transform
of the NSDate
type: I can expect that sometimes a null
is received under certain circumstances but though the default NSDate : JSONable
extension supports the nil
return, through the Transform
protocol it doesn't seems to be able to return nil
(which is normal because MappedObject
must be a non-optional).
public class DateMapping: Transform {
// This doesn't work for now. It does not conform to the Transform Protocol
public typealias MappedObject = NSDate?
This should throw and we should check that the primary key's type and value's sanitized type match.
Otherwise we get an exception from Realm here.
public func fetchObjects(type: RLMObject.Type, primaryKeyValues: [[String : CVarArg]], isMapping: Bool) -> ResultsType?
I don't have too much clue on how using a custom transformation with Crust but I like a lot the compability with Realm in Swift.
So I have a value in my JSON that is a String
which represents a NSDate
with a custom transformation (easily done using a NSDateFormatter
) but I'm not sure if I'm doing it right, I show you my code:
public class User: Object {
dynamic var identifier: Int = 0
dynamic var name: String? = nil
dynamic var surname: String? = nil
dynamic var birthDate: NSDate? = nil
override public static func primaryKey() -> String?{
return "identifier"
}
// Specify properties to ignore (Realm won't persist these)
// override static func ignoredProperties() -> [String] {
// return []
// }
}
extension User: Mappable { }
public class UserMapping: RealmMapping {
public var adaptor: RealmAdaptor
public var primaryKeys: Array<CRMappingKey> {
return ["identifier"]
}
public required init(adaptor: RealmAdaptor) {
self.adaptor = adaptor
}
public func mapping(inout toMap: User, context: MappingContext) {
let userBirthdateMapping = UserBirthdateMapping()
toMap.identifier <- "data.identifier" >*<
toMap.birthDate <- KeyExtensions.Mapping("data.birthdate", userBirthdateMapping) >*<
context
}
}
struct UserBirthdateTransformable: AnyMappable {
var value: NSDate = NSDate()
}
class UserBirthdateMapping: Transform {
typealias MappedObject = UserBirthdateTransformable
func fromJSON(json: JSONValue) throws -> MappedObject {
switch json {
case .JSONString(let date):
var transformable = UserBirthdateTransformable()
transformable.value = NSDateFormatter.birthdateFormatter().dateFromString(date)!
return transformable
default:
throw NSError(domain: "", code: 0, userInfo: nil)
}
}
func toJSON(obj: MappedObject) -> JSONValue {
return .JSONString(NSDateFormatter.birthdateFormatter().stringFromDate(obj.value))
}
}
extension NSDateFormatter {
class func birthdateFormatter() -> NSDateFormatter {
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
formatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)
return formatter
}
}
This should be working? Is this the idea of custom transformations used inside a mapping of a entire class? (I post it here because is not important that it's a Realm Object
)
Thank you!
UPDATE:
Also, this can be less specific and work with any NSDateFormatter
:
struct DateTransformable: AnyMappable {
var value: NSDate = NSDate()
}
class DateMapping: Transform {
typealias MappedObject = DateTransformable
let dateFormatter: NSDateFormatter
init(dateFormatter: NSDateFormatter){
self.dateFormatter = dateFormatter
}
func fromJSON(json: JSONValue) throws -> MappedObject {
switch json {
case .JSONString(let date):
var transformable = DateTransformable()
transformable.value = self.dateFormatter.dateFromString(date)!
return transformable
default:
throw NSError(domain: "", code: 0, userInfo: nil)
}
}
func toJSON(obj: MappedObject) -> JSONValue {
return .JSONString(self.dateFormatter.stringFromDate(obj.value))
}
}
Right now a null
will always throw an error.
Some mappings should be optional. This would allow them to be skipped if no data is present.
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.