2023-03-15 16:00:52 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*/
|
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
import { expandProperties } from '@ember/object/computed';
|
2018-04-03 14:16:57 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* @param modelClass DS.Model
|
|
|
|
* @param attributeNames Array[String]
|
|
|
|
* @param prefixName String
|
|
|
|
* @param map Map
|
|
|
|
* @returns Array[Object]
|
|
|
|
*
|
|
|
|
* A function that takes a model and an array of attributes
|
|
|
|
* and expands them in-place to an array of metadata about the attributes
|
|
|
|
*
|
|
|
|
* if passed a Model with attributes `foo` and `bar` and the array ['foo', 'bar']
|
|
|
|
* the returned array would take the form of:
|
|
|
|
*
|
|
|
|
* [
|
|
|
|
* {
|
|
|
|
* name: 'foo',
|
|
|
|
* type: 'string',
|
|
|
|
* options: {
|
|
|
|
* defaultValue: 'Foo'
|
|
|
|
* }
|
|
|
|
* },
|
|
|
|
* {
|
|
|
|
* name: 'bar',
|
|
|
|
* type: 'string',
|
|
|
|
* options: {
|
|
|
|
* defaultValue: 'Bar',
|
|
|
|
* editType: 'textarea',
|
|
|
|
* label: 'The Bar Field'
|
|
|
|
* }
|
|
|
|
* },
|
|
|
|
* ]
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2022-10-18 15:46:02 +00:00
|
|
|
export const expandAttributeMeta = function (modelClass, attributeNames) {
|
|
|
|
const fields = [];
|
2018-04-03 14:16:57 +00:00
|
|
|
// expand all attributes
|
2022-10-18 15:46:02 +00:00
|
|
|
// see docs for examples of brace-expansion - https://api.emberjs.com/ember/4.4/functions/@ember%2Fobject%2Fcomputed/expandProperties
|
|
|
|
attributeNames.map((field) => expandProperties(field, (prop) => fields.push(prop)));
|
|
|
|
// cache results of eachAttribute so we don't call it on each iteration of fields loop
|
|
|
|
const modelAttrs = {};
|
|
|
|
|
|
|
|
const getAttributeMeta = (klass, attrKey) => {
|
|
|
|
// populate cache if empty
|
|
|
|
if (!modelAttrs[klass.modelName]) {
|
|
|
|
modelAttrs[klass.modelName] = [];
|
|
|
|
klass.eachAttribute((name, meta) => {
|
|
|
|
modelAttrs[klass.modelName].push(meta);
|
|
|
|
});
|
2018-04-03 14:16:57 +00:00
|
|
|
}
|
2022-10-18 15:46:02 +00:00
|
|
|
// lookup attr and return meta
|
|
|
|
return modelAttrs[klass.modelName].findBy('name', attrKey);
|
|
|
|
};
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2022-10-18 15:46:02 +00:00
|
|
|
return fields.map((field) => {
|
|
|
|
let meta = {};
|
|
|
|
// check for relationship by presence of dot nation in field name
|
|
|
|
if (field.includes('.')) {
|
|
|
|
const [relKey, prop] = field.split('.');
|
|
|
|
const rel = modelClass.belongsTo(relKey);
|
|
|
|
const relModelClass = modelClass.store.modelFor(rel.type);
|
|
|
|
meta = getAttributeMeta(relModelClass, prop);
|
|
|
|
} else {
|
|
|
|
meta = getAttributeMeta(modelClass, field);
|
2018-05-23 16:25:52 +00:00
|
|
|
}
|
2022-10-18 15:46:02 +00:00
|
|
|
const { type, options } = meta || {};
|
2018-04-03 14:16:57 +00:00
|
|
|
return {
|
|
|
|
// using field name here because it is the full path,
|
2022-10-18 15:46:02 +00:00
|
|
|
// name on the attribute meta will be relative to the relationship if applicable
|
2018-04-03 14:16:57 +00:00
|
|
|
name: field,
|
2022-10-18 15:46:02 +00:00
|
|
|
type,
|
|
|
|
options,
|
2018-04-03 14:16:57 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* @param modelClass DS.Model
|
|
|
|
* @param fieldGroups Array[Object]
|
|
|
|
* @returns Array
|
|
|
|
*
|
|
|
|
* A function meant for use on an Ember Data Model
|
|
|
|
*
|
|
|
|
* The function takes a array of groups, each group
|
|
|
|
* being a list of attributes on the model, for example
|
|
|
|
* `fieldGroups` could look like this
|
|
|
|
*
|
|
|
|
* [
|
|
|
|
* { default: ['commonName', 'format'] },
|
|
|
|
* { Options: ['altNames', 'ipSans', 'ttl', 'excludeCnFromSans'] },
|
|
|
|
* ]
|
|
|
|
*
|
|
|
|
* The array will get mapped over producing a new array with each attribute replaced with that attribute's metadata from the attr declaration
|
|
|
|
*/
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
export default function (modelClass, fieldGroups) {
|
|
|
|
return fieldGroups.map((group) => {
|
2018-04-03 14:16:57 +00:00
|
|
|
const groupKey = Object.keys(group)[0];
|
|
|
|
const fields = expandAttributeMeta(modelClass, group[groupKey]);
|
|
|
|
return { [groupKey]: fields };
|
|
|
|
});
|
|
|
|
}
|