github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/ui/app/models/structured-attributes.js (about)

     1  import { get, computed } from '@ember/object';
     2  import { attr } from '@ember-data/model';
     3  import Fragment from 'ember-data-model-fragments/fragment';
     4  import flat from 'flat';
     5  
     6  const { unflatten } = flat;
     7  
     8  export default class StructuredAttributes extends Fragment {
     9    @attr() raw;
    10  
    11    @computed('raw')
    12    get structured() {
    13      const original = this.raw;
    14  
    15      if (!original) {
    16        return undefined;
    17      }
    18  
    19      // `unflatten` doesn't sort keys before unflattening, so manual preprocessing is necessary.
    20      const attrs = Object.keys(original)
    21        .sort()
    22        .reduce((obj, key) => {
    23          obj[key] = original[key];
    24          return obj;
    25        }, {});
    26      return unflatten(attrs, { overwrite: true });
    27    }
    28  
    29    unknownProperty(key) {
    30      // Returns the exact value in index 0 and the subtree in index 1
    31      //
    32      // ex: nodeAttrs.get('driver.docker')
    33      // [ "1", { version: "17.05.0-ce", volumes: { enabled: "1" } } ]
    34      if (this.structured) {
    35        return get(this.structured, key);
    36      }
    37    }
    38  }