Skip to content

Commit

Permalink
refactor(cli): extract createPropertyTemplateData helper
Browse files Browse the repository at this point in the history
Signed-off-by: Miroslav Bajtoš <[email protected]>
  • Loading branch information
bajtos committed Sep 23, 2019
1 parent a6863e5 commit 78ebdc5
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 50 deletions.
57 changes: 7 additions & 50 deletions packages/cli/generators/model/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const utils = require('../../lib/utils');
const chalk = require('chalk');
const path = require('path');

const {createPropertyTemplateData} = require('./property-definition');

const PROMPT_BASE_MODEL_CLASS = 'Please select the model base class';
const ERROR_NO_MODELS_FOUND = 'Model was not found in';

Expand Down Expand Up @@ -476,56 +478,11 @@ module.exports = class ModelGenerator extends ArtifactGenerator {
this.artifactInfo.modelBaseClass,
);

// Set up types for Templating
const TS_TYPES = ['string', 'number', 'object', 'boolean', 'any'];
const NON_TS_TYPES = ['geopoint', 'date'];
Object.values(this.artifactInfo.properties).forEach(val => {
// Default tsType is the type property
val.tsType = val.type;

// Override tsType based on certain type values
if (val.type === 'array') {
if (TS_TYPES.includes(val.itemType)) {
val.tsType = `${val.itemType}[]`;
} else if (val.type === 'buffer') {
val.tsType = 'Buffer[]';
} else {
val.tsType = 'string[]';
}
} else if (val.type === 'buffer') {
val.tsType = 'Buffer';
}

if (NON_TS_TYPES.includes(val.tsType)) {
val.tsType = 'string';
}

if (
val.defaultValue &&
NON_TS_TYPES.concat(['string', 'any']).includes(val.type)
) {
val.defaultValue = `'${val.defaultValue}'`;
}

// Convert Type to include '' for template
val.type = `'${val.type}'`;
if (val.itemType) {
val.itemType = `'${val.itemType}'`;
}

// If required is false, we can delete it as that's the default assumption
// for this field if not present. This helps to avoid polluting the
// decorator with redundant properties.
if (!val.required) {
delete val.required;
}

// We only care about marking the `id` field as `id` and not fields that
// are not the id so if this is false we delete it similar to `required`.
if (!val.id) {
delete val.id;
}
});
const propDefs = this.artifactInfo.properties;
this.artifactInfo.properties = {};
for (const key in propDefs) {
this.artifactInfo.properties = createPropertyTemplateData(propDefs[key]);
}

if (this.artifactInfo.modelSettings) {
this.artifactInfo.modelSettings = utils.stringifyModelSettings(
Expand Down
73 changes: 73 additions & 0 deletions packages/cli/generators/model/property-definition.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright IBM Corp. 2018,2019. All Rights Reserved.
// Node module: @loopback/cli
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

'use strict';

const TS_TYPES = ['string', 'number', 'object', 'boolean', 'any'];
const NON_TS_TYPES = ['geopoint', 'date'];
const BUILTIN_TYPES = [...TS_TYPES, ...NON_TS_TYPES];

module.exports = {
createPropertyTemplateData,
BUILTIN_TYPES,
};

/**
* Convert property definition in LB4 style to data needed by model template
* @param {object} val The property definition
* @returns {object} Data for model-property template
*/
function createPropertyTemplateData(val) {
// shallow clone the object - don't modify original data!
val = {...val};

// Default tsType is the type property
val.tsType = val.type;

// Override tsType based on certain type values
if (val.type === 'array') {
if (TS_TYPES.includes(val.itemType)) {
val.tsType = `${val.itemType}[]`;
} else if (val.type === 'buffer') {
val.tsType = 'Buffer[]';
} else {
val.tsType = 'string[]';
}
} else if (val.type === 'buffer') {
val.tsType = 'Buffer';
}

if (NON_TS_TYPES.includes(val.tsType)) {
val.tsType = 'string';
}

if (
val.defaultValue &&
NON_TS_TYPES.concat(['string', 'any']).includes(val.type)
) {
val.defaultValue = `'${val.defaultValue}'`;
}

// Convert Type to include '' for template
val.type = `'${val.type}'`;
if (val.itemType) {
val.itemType = `'${val.itemType}'`;
}

// If required is false, we can delete it as that's the default assumption
// for this field if not present. This helps to avoid polluting the
// decorator with redundant properties.
if (!val.required) {
delete val.required;
}

// We only care about marking the `id` field as `id` and not fields that
// are not the id so if this is false we delete it similar to `required`.
if (!val.id) {
delete val.id;
}

return val;
}

0 comments on commit 78ebdc5

Please sign in to comment.