Closed nathanhammond closed 8 years ago
Do you know of library that implement good plucking ability like this that we could use for inspiration/or for actual code?
Here's what I coded up today to make it a bit less painful by making it easier to dive into a model. It simply extends normalizeUsingDeclaredMapping
to support key paths. For bonus points it plays nicely with EmbeddedRecord.
Usage:
export default ApplicationSerializer.extend({
attrs: {
actor: { key: 'value.actor', embedded: 'always' },
}
});
Would you be interested in a PR to allow key paths?
Code:
import DS from 'ember-data';
import Ember from 'ember';
var get = Ember.get;
export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
normalizeUsingDeclaredMapping(type, hash) {
var attrs = get(this, 'attrs');
var payloadKey, key, value, segments, last;
if (attrs) {
for (key in attrs) {
payloadKey = this._getMappedKey(key);
value = get(hash, payloadKey);
if (!value) { continue; }
if (payloadKey !== key) {
hash[key] = value;
if (~payloadKey.indexOf('.')) {
segments = payloadKey.split('.');
last = segments.pop();
delete get(hash, segments.join('.'))[last];
} else {
delete hash[payloadKey];
}
}
}
}
}
});
Any sort of big API change like this should go through the rfc process. There's a lot of code here; it would be good to flesh out the specified behavior.
Ember Data makes working with highly nested and irregular APIs incredibly difficult. Below you'll find an example response from a real API that is very difficult to model well.
Things that are worth noting:
Writing imperative transformations using
normalize
and friends makes for incredibly tedious and error prone work with lots of indirection. (Passing between serializers.) So what could we do to make it better? How about theattrs
property allowing for arbitrarily nested keys?Moving to a pattern like this would make the behavior more declarative, easier to understand, and make the work I'm doing much less painful. I'm also open to other straw men that might help reduce the pain I encounter working with this. I'm opening this bug for conversation.
Note: this bug comes with the offer of my writing the code to enable the conclusion. :)
/cc @tomdale @wycats @chadhietala