Comments (7)
For what it's worth (and I've been using representable since 2014 or so), the documentation for nested
implies that it's a way of accommodating some structure of the document you are parsing which doesn't match your object structure. In my (apparently inaccurate) mental model of what is going on, nested
is just for handling parsing or rendering of the document. It was extremely surprising to have it suddenly acting like property
and trying to assign a null value to a non-existent attribute on the represented object!
from representable.
I've created a file to test this behavior in different combinations: https://gist.github.com/jestraw/7a88cf42984db2e5e3d560ea58970a11
Running this file against 3.1.1 gives the following output:
JsonSongRepresenter
{
"title": "json-with-details",
"details": {
"track": 1
}
}
#<struct Song title="json-with-details", track=1, length=nil>
{
"title": "json-empty-details",
"details": { }
}
#<struct Song title="json-empty-details", track=nil, length=nil>
{
"title": "json-null-details",
"details": null
}
undefined method `details=' for #<struct Song title="json-null-details", track=nil, length=nil>
HashSongRepresenter
{"title"=>"hash-with-details", "details"=>{"track"=>1}}
#<struct Song title="hash-with-details", track=1, length=nil>
{"title"=>"hash-empty-details", "details"=>{}}
#<struct Song title="hash-empty-details", track=nil, length=nil>
{"title"=>"hash-null-details", "details"=>nil}
undefined method `details=' for #<struct Song title="hash-null-details", track=nil, length=nil>
JsonHashSongRepresenter
{"title"=>"hash-with-details", "details"=>{"track"=>1}}
#<struct Song title="hash-with-details", track=1, length=nil>
{"title"=>"hash-empty-details", "details"=>{}}
#<struct Song title="hash-empty-details", track=nil, length=nil>
{"title"=>"hash-null-details", "details"=>nil}
undefined method `details=' for #<struct Song title="hash-null-details", track=nil, length=nil>
from representable.
Hi @jestraw! The problem here is that Song
doesn't have a details=
method!
from representable.
Yes, I know. But why is nested
trying to assign to the represented object?
from representable.
That's the design of Representable, in Reform (also using Representable), we have "populators" to solve this problem. When parsing, it simply writes the read values on represented
. You'd probably need the :class
option here. Hit me up on https://trailblazer.zulipchat.com if you need assistance! 🍻
It won't help you now but I will soon start working on Representable 4 which is, exactly like Reform 3, completely based on Trailblazer::Activity
meaning you can customize it much easier.
from representable.
@jestraw nested
will read the nested fragment, may or may not create a nested "target" object for that and than assign that to represented
- in your mental model, what would Rpr do with the parsed fragment instead?
from representable.
Fair enough! I think the actual document I was trying to parse was weird -- it was essentially relating a kind of left outer join where the joined table was empty by presenting the key for the join with a nil value. Since the document was already parsed to a hash, I could have done .compact
and gotten rid of the problem, but that wouldn't have looked as nice as just doing for_collection
. I ended up with some if
directives on the nested
to skip parsing if the value was nil, which does work.
from representable.
Related Issues (20)
- Using the :skip_parse option with JSON::Hash values produces Pipeline::Stop HOT 1
- XML Encoding? HOT 3
- Populator docs or examples? HOT 4
- XML namespaces
- Options hash passed to setter has incorrect value for collection properties
- Collection parsing raises an error if the value is nil HOT 4
- Document nested inherit: true
- Parsing null values results in property being set despite `reader` property present
- parsing array of arrays
- Bug in dependency package declarative-option update HOT 1
- Nested keys within hash are getting mutated HOT 5
- Custom writer: method is not invoked or not invoked correctly HOT 2
- Can't activate multi_json on (>= 1.14.1), already activated multi_json-1.11.3 HOT 3
- Representable doesn't work as expected on truffleruby-21.3.0 HOT 1
- Nokogiri deprecation warning
- Combining nested include/exclude with regular include/exclude HOT 1
- Representing objects into objects using Representable::Object#to_object HOT 8
- Representable 4: problem when parsing looks at one field but wants to create two
- Representable 4: how to collect/write data when parsing?
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 representable.