Giter Site home page Giter Site logo

crust's Issues

Fetch objects checks primary key's type

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?

Define Custom Transformations for NSDate()

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))
    }
}

Custom transformations for Optional Types

Is it possible to make a Transform whose MappedObject is an Optional? Following the example of the custom Transformof the NSDate type: I can expect that sometimes a null is received under certain circumstances but though the default NSDate : JSONable extension supports the nilreturn, 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?

Xcode 9.3 beta 4 support

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.