Coder Social home page Coder Social logo

Comments (16)

michaelmcguire avatar michaelmcguire commented on May 24, 2024

I notice you conspicuously left out "Role" in your translation 😉

For the model you have, that looks about right. But when you're dealing with types not handled by NSJSONSerialization, including arrays of those types, you get a lot more help from Argo.

from argo.

gfontenot avatar gfontenot commented on May 24, 2024

Totally valid concern. In addition to what you posted, you'd also need to add the create function to your user models.

Also, keep in mind that pulling Argo in as a dependency of your framework would require users to link Argo (and Runes, and soon Box as well) to your user's apps (assuming they are using frameworks. CocoaPods will do this for them). That might be a deal breaker.

from argo.

paulyoung avatar paulyoung commented on May 24, 2024

I notice you conspicuously left out "Role" in your translation

🙈 - updated to add that and fix one other issue.

from argo.

paulyoung avatar paulyoung commented on May 24, 2024

In addition to what you posted, you'd also need to add the create function to your user models.

Aware of that if using Argo, thanks. I've written too many curried create functions to be able to forget!

from argo.

paulyoung avatar paulyoung commented on May 24, 2024

when you're dealing with types not handled by NSJSONSerialization, including arrays of those types, you get a lot more help from Argo

@michaelmcguire could you elaborate on that?

from argo.

michaelmcguire avatar michaelmcguire commented on May 24, 2024

Sure, the line:

role = j["role"] as Role,

probably won't do what you want it to do. It is probably another NSDictionary that contains the key/values for the Role that will need to be properly mapped, so you would need code to do that also. Of course, you don't get that "for free" from Argo, as you would need to implement JSONEncodable for that also. But if you do that, then you get handling an array of them.

from argo.

gfontenot avatar gfontenot commented on May 24, 2024

yep. @michaelmcguire is exactly right. You'd have to do something like:

role = Role.decode(j["role"])

from argo.

paulyoung avatar paulyoung commented on May 24, 2024

Yeah, I realized that as soon as I'd asked. That seems totally reasonable though.

Any other concerns or gotchas?

from argo.

paulyoung avatar paulyoung commented on May 24, 2024

As for arrays?

friends = j["friends"].map { User.decode($0) }

from argo.

gfontenot avatar gfontenot commented on May 24, 2024

Yes, but you'd also need to bring in sequence to get the same behavior of Argo (when one decode fails, the entire array fails)

from argo.

gfontenot avatar gfontenot commented on May 24, 2024

You example also probably won't handle the case of actual optional values. The if let will fail if it's .None, where as you'd want to let that succeed. You might want to confirm this.

from argo.

paulyoung avatar paulyoung commented on May 24, 2024

when one decode fails, the entire array fails

TIL.

It looks like map succeeds until the first decode fails rather than all or nothing. In some cases it might actually be desirable to do:

friends = j["friends"].reduce([], combine: { accum, elem in
  if let user = User.decode(elem) {
    return accum + [user]
  } else {
    return accum
  }
})

(specialized for User, could be made generic)

from argo.

paulyoung avatar paulyoung commented on May 24, 2024

You example also probably won't handle the case of actual optional values. The if let will fail if it's .None, where as you'd want to let that succeed. You might want to confirm this.

@gfontenot do you mean if a key is missing from the JSON dictionary? If so, this works:

let j: NSDictionary = [
    "id": 123
]

// prints "success"
if let name = j["name"] as? String?, id = j["id"] as? Int {
    println("success")
} else {
    println("failure")
}

Sorry for the detour. This all helps in weighing things up.

from argo.

gfontenot avatar gfontenot commented on May 24, 2024

Good to know that using as? Foo? works as I'd hope. I hadn't ever tried it, so was taking the more pessimistic route.

re: your map/reduce question, map should return [User?] and your reduce version should return [User]. We chose to go with [User]? (using map/sequence) because it fits in line with the behavior of the rest of the lib (we never fail silently). I think it's probably fine for you to use reduce to get [User], but if you were trying to replicate Argo's behavior exactly, you'd want to use map/sequence.

from argo.

paulyoung avatar paulyoung commented on May 24, 2024

In addition to all of this, Argo also does JSON parsing which I'd need to handle.

I think there's a pretty compelling case for me to continue to use it.

Thanks @michaelmcguire @gfontenot!

from argo.

gfontenot avatar gfontenot commented on May 24, 2024

Argo doesn't actually parse JSON itself, to be clear. We rely on NSJSONSerialization to do the NSData -> AnyObject transformation. We just then turn it into an enum for performance/type safety reasons.

from argo.

Related Issues (20)

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.