Skip to content

Commit

Permalink
Merge branch 'release/v2.6.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbandler committed Jun 5, 2021
2 parents 4826fdb + 0898b5a commit ec468b1
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 14 deletions.
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cds2types",
"version": "2.6.0",
"version": "2.6.1",
"description": "CLI to convert CDS models to Typescript interfaces and enumerations",
"main": "./bin/cli.js",
"repository": "[email protected]:mrbandler/cds2types.git",
Expand All @@ -20,8 +20,7 @@
"start": "node",
"build": "tsc",
"build:start": "yarn build && yarn start",
"build:link": "yarn build && npm link",
"prettier:gen": "json2ts ./src/formatting/prettier/prettierrc-schema.json ./src/formatting/prettier/config.ts"
"build:link": "yarn build && npm link"
},
"devDependencies": {
"@commitlint/cli": "^8.3.5",
Expand All @@ -45,7 +44,7 @@
"@types/lodash": "^4.14.150",
"commander": "^4.0.1",
"fs-extra": "^8.1.0",
"lodash": "^4.17.15",
"lodash": "^4.17.21",
"prettier": "^2.2.1",
"ts-morph": "^7.1.1"
},
Expand Down
22 changes: 16 additions & 6 deletions src/cds.parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,15 +270,19 @@ export class CDSParser {
const element = elements[elementName];

if (this.isLocalizationField(element)) continue;
if (!element.type) {
if (!element.type && !element.items) {
if (element.type === undefined) {
throw new Error(
`Unable to parse element '${elementName}' on entity '${name}'. It seems to be a CDS expression without a type definition, please add a type to it.`
);
}
}

const _enum = this.parseEnum(element);
const type = element.type
? element.type
: element.items
? element.items.type
: "";

const canBeNull =
element["@Core.Computed"] ||
Expand All @@ -292,12 +296,18 @@ export class CDSParser {
elementName === Managed.ModifiedAt ||
elementName === Managed.ModifiedBy;

const cardinality = element.cardinality
? element.cardinality
: element.items
? { max: Cardinality.many }
: { max: Cardinality.one };

const _enum = this.parseEnum(element);

result.set(elementName, {
type: element.type,
type: type,
canBeNull: canBeNull,
cardinality: element.cardinality
? element.cardinality
: { max: Cardinality.one },
cardinality: cardinality,
target: element.target,
enum: _enum.size <= 0 ? undefined : _enum,
keys: element.keys,
Expand Down
2 changes: 1 addition & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Program } from "./program";
*/
function main(): void {
const cli = new commander.Command();
cli.version("2.6.0")
cli.version("2.6.1")
.description(
"CLI to convert CDS models to Typescript interfaces and enumerations"
)
Expand Down
8 changes: 5 additions & 3 deletions src/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,17 @@ export class Program {
* @memberof Program
*/
public async run(options: IOptions): Promise<void> {
// Load and parse compiled cds.
// Load compiled CDS.
const jsonObj = await this.loadCdsAndConvertToJSON(options.cds);
const parsed = new CDSParser().parse(jsonObj as ICsn);

// Write the compiled cds JSON to disc for debugging.
// Write the compiled CDS JSON to disc for debugging.
if (options.json) {
fs.writeFileSync(options.output + ".json", JSON.stringify(jsonObj));
}

// Parse compile CDS.
const parsed = new CDSParser().parse(jsonObj as ICsn);

// Remove the output file if it already exists.
if (fs.existsSync(options.output)) {
fs.removeSync(options.output);
Expand Down
6 changes: 6 additions & 0 deletions src/types/base.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,12 @@ export abstract class BaseType<O = unknown> {
result = this.resolveTargetType(element, prefix);
} else {
result = this.resolveType(element.type, types);
if (
element.cardinality &&
element.cardinality.max === Cardinality.many
) {
result = `${result}[]`;
}
}

break;
Expand Down
1 change: 1 addition & 0 deletions src/utils/cds.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export interface ICsnElement {
virtual?: boolean;
default?: ICsnValue;
enum?: IEnum;
items?: ICsnArrayTypeAliasTypeItems;
"@Core.Immutable"?: boolean;
"@Core.Computed"?: boolean;
}
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2057,6 +2057,11 @@ lodash@^4.17.11, lodash@^4.17.15, lodash@^4.17.19, lodash@^4.17.20, lodash@^4.2.
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.20.tgz#b44a9b6297bcb698f1c51a3545a2b3b368d59c52"
integrity sha512-PlhdFcillOINfeV7Ni6oF1TAEayyZBoZ8bcshTHqOYJYlrqzRK5hagpagky5o4HfCzzd1TRkXPMFq6cKk9rGmA==

lodash@^4.17.21:
version "4.17.21"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==

log-symbols@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/log-symbols/-/log-symbols-1.0.2.tgz#376ff7b58ea3086a0f09facc74617eca501e1a18"
Expand Down

0 comments on commit ec468b1

Please sign in to comment.