Skip to content

Commit

Permalink
fix(parser): Fix parsing of array of #9
Browse files Browse the repository at this point in the history
The parser has been enhanced based on Issue #9 so that it also works with array of definitions and resolves them correctly.
  • Loading branch information
Simon Gaudek authored and Simon Gaudek committed Aug 31, 2021
1 parent 04ec58f commit 113259e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
17 changes: 13 additions & 4 deletions src/cds.parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import {
ITypeAliasDefinition,
} from "./utils/types";

import _, { isElement } from "lodash";
import _ from "lodash";

/**
* Parses a compiled CDS JSON object.
Expand Down Expand Up @@ -384,12 +384,21 @@ export class CDSParser {
*/
private parseParams(
definition: ICsnActionDefinition | ICsnFunctionDefinition
): Map<string, ICsnParam> {
const result: Map<string, ICsnParam> = new Map<string, ICsnParam>();
): Map<string, ICsnParam | ITypeAliasDefinition> {
const result: Map<string, ICsnParam | ITypeAliasDefinition> = new Map<
string,
ICsnParam | ITypeAliasDefinition
>();

if (definition.params) {
for (const key in definition.params) {
if (definition.params.hasOwnProperty(key)) {
if (definition.params[key].items !== undefined) {
const value = this.parseArrayTypeAliasDef(
key,
definition.params[key]
);
result.set(key, value);
} else if (definition.params.hasOwnProperty(key)) {
const value = definition.params[key];
result.set(key, value as ICsnParam);
}
Expand Down
21 changes: 17 additions & 4 deletions src/types/action.func.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import * as morph from "ts-morph";

import { ICsnTypeRef, Kind, isTypeRef } from "../utils/cds.types";
import { ICsnTypeRef, Kind, isTypeRef, Cardinality } from "../utils/cds.types";

import { BaseType } from "./base.type";
import { Entity } from "./entity";
import { IActionFunctionDefinition } from "../utils/types";
import {
IActionFunctionDefinition,
ITypeAliasDefinition,
} from "../utils/types";

/**
* Action/Function toType return type.
Expand Down Expand Up @@ -172,7 +175,7 @@ export class ActionFunction extends BaseType<IActionFunctionDeclarationStructure
result = this.createInterface(prefix, "Params");

for (const [key, value] of this.def.params) {
if (isTypeRef(value.type)) {
if (isTypeRef(value.type as ICsnTypeRef)) {
const typeRef = value.type as ICsnTypeRef;
const type = types.find((t) => t.Name === typeRef.ref[0]);

Expand All @@ -189,9 +192,19 @@ export class ActionFunction extends BaseType<IActionFunctionDeclarationStructure
}
}
} else {
const type = this.cdsElementToType(
{
type: value.type as string,
canBeNull: false,
cardinality: (value as ITypeAliasDefinition).isArray
? { max: Cardinality.many }
: { max: Cardinality.one },
},
types
);
result.properties?.push({
name: key,
type: this.cdsTypeToType(value.type),
type: type,
});
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/utils/cds.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ export interface ICsnTypeRef {
ref: string[];
}

export interface ICsnArrayParam {
items: ICsnParam;
}

export interface ICsnParam {
type: Type | ICsnTypeRef;
}
Expand All @@ -144,7 +148,7 @@ export interface ICsnParams {

export interface ICsnActionDefinition {
kind: Kind;
params?: ICsnParams;
params?: ICsnParams | ICsnArrayParam;
returns?: CsnReturns;
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export interface IActionFunctionReturns {

export interface IActionFunctionDefinition {
kind: Kind;
params?: Map<string, ICsnParam>;
params?: Map<string, ICsnParam | ITypeAliasDefinition>;
returns?: IActionFunctionReturns;
}

Expand Down

0 comments on commit 113259e

Please sign in to comment.