open-nomad/ui/app/models/structured-attributes.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

39 lines
999 B
JavaScript
Raw Normal View History

import { get, computed } from '@ember/object';
import { attr } from '@ember-data/model';
2017-09-19 14:47:10 +00:00
import Fragment from 'ember-data-model-fragments/fragment';
import flat from 'flat';
2017-09-19 14:47:10 +00:00
const { unflatten } = flat;
2021-10-12 20:36:10 +00:00
export default class StructuredAttributes extends Fragment {
@attr() raw;
2017-09-19 14:47:10 +00:00
2021-10-12 20:36:10 +00:00
@computed('raw')
get structured() {
const original = this.raw;
if (!original) {
return undefined;
}
// `unflatten` doesn't sort keys before unflattening, so manual preprocessing is necessary.
const attrs = Object.keys(original)
.sort()
.reduce((obj, key) => {
obj[key] = original[key];
return obj;
}, {});
2017-09-19 14:47:10 +00:00
return unflatten(attrs, { overwrite: true });
}
2017-09-19 14:47:10 +00:00
unknownProperty(key) {
// Returns the exact value in index 0 and the subtree in index 1
//
// ex: nodeAttrs.get('driver.docker')
// [ "1", { version: "17.05.0-ce", volumes: { enabled: "1" } } ]
2021-10-12 20:36:10 +00:00
if (this.structured) {
return get(this.structured, key);
2019-10-08 18:44:19 +00:00
}
}
}