Comments (8)
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.
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.
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.
@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
toinitWithDictionary
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.
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.
@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.
@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.
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)
- Update Maven Bazel integration to new rules
- Add support for AndroidX nullability annotations HOT 1
- required fields don't do anything in Java HOT 2
- Java model _bits property should be transient HOT 2
- Java model's Builder-setters should annotate parameter with @Nullable or @NonNull
- Java model Builder.build() method should be annotated with @NonNull HOT 1
- Java model - mergeFrom should be annotated with @NonNull
- Java model Builders should apply @NonNull annotation to its getter methods if appropriate HOT 1
- Java model builders should order the fields checked in `equals()`
- Make it easier to detect use of outdated Plank binary HOT 3
- Custom merge strategies (Java) HOT 2
- Use Kotlin-friend Java method names for Boolean accessors
- Support nested object definitions
- Source code checksum changed HOT 6
- Java documentation is outdated
- Title field is not save when creating Pin from a direct link
- Any plans for dart/flutter support? HOT 1
- 'extends' isn't not a keyword in JSON-schema spec HOT 2
- iOS plank -initWithModelDictionary:error: assumes dictionary is dictionary
- [Security] Workflow ci.yml is using vulnerable action actions/checkout
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from plank.