Git Product home page Git Product logo

Comments (8)

rahul-malik avatar rahul-malik commented on June 19, 2024 1

I would advise creating a fork of the repository and applying your changes on a branch there. Then you'll be able to create a pull request I can look at and provide feedback while you iterate.

from plank.

rahul-malik avatar rahul-malik commented on June 19, 2024

Adding a way to convert the object to a dictionary representation is certainly valuable. We've discussed adding this at Pinterest for a while but haven't had a strong reason to do so just yet.

We could certainly add it and I'd be happy to review a
patch if you'd like to work on it or I can take a look in a bit.

What do you think should happen if we have 'nil' properties? Store NSNull as values? What if a property hasn't been set yet?

from plank.

matelo avatar matelo commented on June 19, 2024

yes, I started to work on my own patch already:) I am currently facing this problem with nil values.
In our current project (where I want to use it) I would expect that if value == nil then this property will not be placed into final dictionary (JSON). I don't know if this is universal solution, maybe it could be configurable by some input parameter, but for now it enough for us.

However I am facing another issue, like when I receive object from REST which contains number, Plank's generated class store it to NSInteger. But NSInteger cannot be nil (if parameter was not received), so it is set to 0. But how can I check if this value is really 0, or it was not received at all. In some cases it makes difference, for example some update service.

from plank.

rahul-malik avatar rahul-malik commented on June 19, 2024

@matelo - Sounds great. We have a struct internally ("dirtyProperties") on each model that you can utilize for detecting if a field has been set. Below is an example of how we've utilized it.

From ObjectiveCBuilderExtension.swift:

self.properties.map({ (param, _) -> String in
  ObjCIR.ifStmt("\(self.dirtyPropertiesIVarName).\(dirtyPropertyOption(propertyName: param, className: self.className))") {
     ["_\(param.snakeCaseToPropertyName()) = modelObject.\(param.snakeCaseToPropertyName());"]
  }
}).joined(separator: "\n")

Couple things I'd like to see from the implementation:

  • The method signature should be: - (NSDictionary <NSString *, NSObject *> *)dictionaryRepresentation;
  • The keys of the dictionary should match the keys in the JSON schema declaration. Basically this will allow us to test passing the result of dictionaryRepresentation to initWithDictionary and testing if the models are equivalent.
  • Properties that have not been set (or received) should be omitted from the dictionary.
  • Properties that have been set but are null/nil should be represented as [NSNull null]
  • ADT classes should return the dictionary representation of whatever value they contain internally.

Sound good? Looking forward to your patch and will help get this in.

from plank.

bkase avatar bkase commented on June 19, 2024

It would be nice to have a test that for all example schemas and for a bunch of arbitrary dictionaries per schema:

dict == dictionaryRepresentation(initWithDictionary(dict))

@rahul-malik we could do this nicely with SwiftCheck https://github.com/typelift/SwiftCheck

from plank.

matelo avatar matelo commented on June 19, 2024

@rahul-malik I totally agree with all the points, just have a question regarding the point

Properties that have not been set (or received) should be omitted from the dictionary

Do you have some hint how can I check if the value was/wasn't set, when its type is NSInteger? Because AFAIK, unset value of NSInteger is 0 - which also can be desired (set) value.

from plank.

rahul-malik avatar rahul-malik commented on June 19, 2024

@matelo - When you're checking if a property is set, don't use it's value but rather the dirtyProperties struct. You can add an if statement around adding values to the dictionary.

This snippet below loops through all properties on an object and generates an if statement for each one. That way the only times you will add anything to the dictionary will be after you've confirmed the property is set.

You'll need to replace the "ADD STUFF TO DICTIONARY" portion with specific ways to convert each schema type into something that can be inserted into a dictionary.

self.properties.map({ (param, schema) -> String in
  ObjCIR.ifStmt("\(self.dirtyPropertiesIVarName).\(dirtyPropertyOption(propertyName: param, className: self.className))") {
       // ADD STUFF TO DICTIONARY  
  }
}).joined(separator: "\n")

You should be able to reference other files to get a sense of how this is accomplished but let me know if this is confusing. Thanks!

from plank.

matelo avatar matelo commented on June 19, 2024

can I create branch (or can you create branch) for this enhancement? I already have done almost all the types representation to NSDictionary. Thanks

from plank.

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.