Skip to content

Commit

Permalink
Merge pull request #12 from mrbandler/develop
Browse files Browse the repository at this point in the history
Merge develop into master and release version v2.6.2
  • Loading branch information
HeneryHawk authored Sep 7, 2021
2 parents 22fb368 + 6071a11 commit 7519979
Show file tree
Hide file tree
Showing 14 changed files with 325 additions and 58 deletions.
1 change: 1 addition & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
tags:
- "v*"
- "!v*-beta*"

jobs:
build:
Expand Down
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,12 @@ $RECYCLE.BIN/
bin
service.ts
service.ts.json
test/dist

.yarn/*
!.yarn/patches
!.yarn/releases
!.yarn/plugins
!.yarn/sdks
!.yarn/versions
.pnp.*
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cds2types",
"version": "2.6.1",
"version": "2.6.2",
"description": "CLI to convert CDS models to Typescript interfaces and enumerations",
"main": "./bin/cli.js",
"repository": "[email protected]:mrbandler/cds2types.git",
Expand All @@ -18,7 +18,8 @@
],
"scripts": {
"start": "node",
"build": "tsc",
"build": "tsc --project tsconfig.build.json",
"test": "tsc --project tsconfig.test.json",
"build:start": "yarn build && yarn start",
"build:link": "yarn build && npm link"
},
Expand Down
85 changes: 54 additions & 31 deletions schema.cds
Original file line number Diff line number Diff line change
@@ -1,54 +1,77 @@
using { Currency, managed, sap } from '@sap/cds/common';
using {
Currency,
managed,
sap,
cuid
} from '@sap/cds/common';

namespace sap.capire.bookshop;

entity ArrayUsingEntity : cuid {
inlineArray : array of {
id : String;
quantity : Integer
};
adressArray : array of Address;
compositoinField : Composition of many {
idComposition : String;
quantityComposition : Integer;
}
}

entity Books : managed {
key ID : Integer;
title : localized String(111);
descr : localized String(1111);
author : Association to Authors;
genre : Association to Genres;
stock : Integer;
price : Decimal(9,2);
currency : Currency;
key ID : Integer;
title : localized String(111);
descr : localized String(1111);
author : Association to Authors;
genre : Association to Genres;
stock : Integer;
price : Decimal(9, 2);
currency : Currency;
}


type Gender : Integer enum {
NonBinary;
Male;
Female;
NonBinary = 1;
Male = 2;
Female = 3;
}

type NameStr : String(111);

type Name {
firstname: NameStr;
lastname: NameStr;
firstname : NameStr;
lastname : NameStr;
}

type Address {
street: String;
houseNo: String;
town: String;
country: String;
street : String;
houseNo : String;
town : String;
country : String;
}

type Addresses : many Address;

entity Authors : managed {
key ID : Integer;
name : Name;
gender : Gender;
addresses : Addresses;
dateOfBirth : Date;
dateOfDeath : Date;
placeOfBirth : String;
placeOfDeath : String;
books : Association to many Books on books.author = $self;
key ID : Integer;
name : Name;
gender : Gender;
addresses : Addresses;
dateOfBirth : Date;
dateOfDeath : Date;
placeOfBirth : String;
placeOfDeath : String;
books : Association to many Books
on books.author = $self;
}

/** Hierarchically organized Code List for Genres */
/**
* Hierarchically organized Code List for Genres
*/
entity Genres : sap.common.CodeList {
key ID : Integer;
parent : Association to Genres;
children : Composition of many Genres on children.parent = $self;
key ID : Integer;
parent : Association to Genres;
children : Composition of many Genres
on children.parent = $self;
}
52 changes: 41 additions & 11 deletions service.cds
Original file line number Diff line number Diff line change
@@ -1,18 +1,48 @@
using { sap.capire.bookshop as my } from './schema';
service CatalogService @(path:'/browse') {
using {sap.capire.bookshop as my} from './schema';

@readonly entity Books as SELECT from my.Books {*,
author.name as author
} excluding { createdBy, modifiedBy }
service CatalogService @(path : '/browse') {

actions {
action addRating (stars: Integer);
function getViewsCount() returns Integer;
entity ServiceEntity {
key id : UUID;
arrayComplex : array of arrayParameterType;
arraySimple : array of String;
}

function getBooks(author : my.Authors.ID) returns array of my.Books;
entity ArrayUsingEntity as projection on my.ArrayUsingEntity;

@requires_: 'authenticated-user'
action submitOrder (book : Books.ID, amount: Integer);
@readonly
entity Books as
select from my.Books {
*,
author.name as author
}
excluding {
createdBy,
modifiedBy
}

actions {
action addRating(stars : Integer);
function getViewsCount() returns Integer;
}

function getBooks(author : my.Authors:ID) returns array of my.Books;
action unboudAction(simpleParameter : String, arrayParameter : array of arrayParameterType, typedParameter : typedParameterType) returns ActionReturnType;

@requires_ : 'authenticated-user'
action submitOrder(book : Books:ID, amount : Integer);


type arrayParameterType : {
value : String;
}

type typedParameterType : {
value : String;
}

type ActionReturnType : {
success : Boolean;
}

}
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
68 changes: 68 additions & 0 deletions test/schema.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// This file should be compilable if the types were created correctly.

import { sap } from "../service";

const address: sap.capire.bookshop.IAddress = {
street: "",
houseNo: "",
town: "",
country: "",
};

const author: sap.capire.bookshop.IAuthors = {
ID: 1,
name: {
firstname: "",
lastname: "",
},
gender: sap.capire.bookshop.Gender.Female,
addresses: [[address]],
dateOfBirth: new Date(),
dateOfDeath: new Date(),
placeOfBirth: "",
placeOfDeath: "",
books: [],
};

const book: sap.capire.bookshop.IBooks = {
ID: 1,
title: "",
descr: "",
author: author,
author_ID: 1,
genre: {
ID: 1,
parent: {
ID: 2,
children: [],
name: "",
descr: "",
},
name: "",
descr: "",
children: [],
},
genre_ID: 1,
stock: 1,
price: 1,
currency: {
code: "",
descr: "",
name: "",
symbol: "",
},
currency_code: "",
};

const arrayUsingEntity: sap.capire.bookshop.IArrayUsingEntity = {
ID: "",
inlineArray: [],
adressArray: [address],
compositoinField: [
{
idComposition: "",
quantityComposition: 1,
up__ID: "1",
},
],
};
Loading

0 comments on commit 7519979

Please sign in to comment.