github.com/hernad/nomad@v1.6.112/ui/app/models/structured-attributes.js (about) 1 /** 2 * Copyright (c) HashiCorp, Inc. 3 * SPDX-License-Identifier: MPL-2.0 4 */ 5 6 import { set } from '@ember/object'; 7 import { get, computed } from '@ember/object'; 8 import { attr } from '@ember-data/model'; 9 import Fragment from 'ember-data-model-fragments/fragment'; 10 import flat from 'flat'; 11 12 const { unflatten } = flat; 13 14 export default class StructuredAttributes extends Fragment { 15 @attr() raw; 16 17 recomputeRawProperties(incoming) { 18 set(this, 'raw', incoming); 19 } 20 21 @computed('raw') 22 get structured() { 23 const original = this.raw; 24 25 if (!original) { 26 return undefined; 27 } 28 29 // `unflatten` doesn't sort keys before unflattening, so manual preprocessing is necessary. 30 const attrs = Object.keys(original) 31 .sort() 32 .reduce((obj, key) => { 33 obj[key] = original[key]; 34 return obj; 35 }, {}); 36 return unflatten(attrs, { overwrite: true }); 37 } 38 39 unknownProperty(key) { 40 // Returns the exact value in index 0 and the subtree in index 1 41 // 42 // ex: nodeAttrs.get('driver.docker') 43 // [ "1", { version: "17.05.0-ce", volumes: { enabled: "1" } } ] 44 if (this.structured) { 45 return get(this.structured, key); 46 } 47 } 48 }