299ecff703
There are many places in the API where we receive a property set to `null` which can then lead to defensive code deeper in the app in order to guard for this type of thing when usually we are expecting an array or for the property to be undefined using omitempty on the backend. Previously we had two places where we would deal with this in the serializer using our 'remove-null' util (KV and Intentions). This new decorator lets you declaritively define this type of data using a decorator @NullValue([]) (which would replce a null value with []. @NullValue in turn uses a more generic @replace helper, which we currently don't need but would let you replace any value with another, not just a null value. An additional benefit here is that the guard/replacement is executed lazily when we get the property instead of serializing all the values when they come in via the API. On super large datasets, where we only visualize part of the dataset (say in our scroll panes), this feels like a good improvement on the previous approach.
75 lines
1.9 KiB
JavaScript
75 lines
1.9 KiB
JavaScript
import Serializer from './application';
|
|
import { inject as service } from '@ember/service';
|
|
import { get } from '@ember/object';
|
|
import { PRIMARY_KEY, SLUG_KEY } from 'consul-ui/models/intention';
|
|
|
|
export default class IntentionSerializer extends Serializer {
|
|
@service('encoder') encoder;
|
|
|
|
primaryKey = PRIMARY_KEY;
|
|
slugKey = SLUG_KEY;
|
|
|
|
init() {
|
|
super.init(...arguments);
|
|
this.uri = this.encoder.uriTag();
|
|
}
|
|
|
|
ensureID(item) {
|
|
if (!get(item, 'ID.length')) {
|
|
item.Legacy = false;
|
|
} else {
|
|
item.Legacy = true;
|
|
item.LegacyID = item.ID;
|
|
}
|
|
item.ID = this
|
|
.uri`${item.SourceNS}:${item.SourceName}:${item.DestinationNS}:${item.DestinationName}`;
|
|
return item;
|
|
}
|
|
|
|
respondForQuery(respond, query) {
|
|
return super.respondForQuery(
|
|
cb =>
|
|
respond((headers, body) => {
|
|
return cb(
|
|
headers,
|
|
body.map(item => this.ensureID(item))
|
|
);
|
|
}),
|
|
query
|
|
);
|
|
}
|
|
|
|
respondForQueryRecord(respond, query) {
|
|
return super.respondForQueryRecord(
|
|
cb =>
|
|
respond((headers, body) => {
|
|
body = this.ensureID(body);
|
|
return cb(headers, body);
|
|
}),
|
|
query
|
|
);
|
|
}
|
|
|
|
respondForCreateRecord(respond, serialized, data) {
|
|
const slugKey = this.slugKey;
|
|
const primaryKey = this.primaryKey;
|
|
return respond((headers, body) => {
|
|
body = data;
|
|
body.ID = this
|
|
.uri`${serialized.SourceNS}:${serialized.SourceName}:${serialized.DestinationNS}:${serialized.DestinationName}`;
|
|
return this.fingerprint(primaryKey, slugKey, body.Datacenter)(body);
|
|
});
|
|
}
|
|
|
|
respondForUpdateRecord(respond, serialized, data) {
|
|
const slugKey = this.slugKey;
|
|
const primaryKey = this.primaryKey;
|
|
return respond((headers, body) => {
|
|
body = data;
|
|
body.LegacyID = body.ID;
|
|
body.ID = serialized.ID;
|
|
return this.fingerprint(primaryKey, slugKey, body.Datacenter)(body);
|
|
});
|
|
}
|
|
}
|