Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enhance class properties of CSN/LinkedCSN #265

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions apis/csn.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ export interface context extends any_ { }
export interface service extends any_ { }

export interface type extends any_ {
type?: FQN
type?: 'cds.Boolean' |
'cds.UUID' | 'cds.String' | 'cds.LargeString' | 'cds.Binary' | 'cds.LargeBinary' | 'cds.Vector' |
'cds.Integer' | 'cds.UInt8' | 'cds.Int16' | 'cds.Int32' | 'cds.Int64' | 'cds.Float' | 'cds.Double' | 'cds.Decimal' |
'cds.Date' | 'cds.Time' | 'cds.DateTime' | 'cds.Timestamp' |
'cds.Association' | 'cds.Composition' |
FQN & Record<never,never> // allow any other CDS type as well (e.g. 'User')
items?: type
}

Expand Down Expand Up @@ -102,11 +107,13 @@ export type EntityElements = {
virtual?: boolean,
unique?: boolean,
notNull?: boolean,
precision?: number,
scale?: number,
length?: number,
},
}

export interface Association extends type {
type: 'cds.Association' | 'cds.Composition'
target: FQN

/**
Expand Down
10 changes: 8 additions & 2 deletions apis/linked/classes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ declare class aspect<K extends kinds = 'aspect'> extends type<K> implements With
elements: Definitions<type<'type'>>
}
declare interface type extends Omit<csn.type, 'items'> {
items: type
items?: type
key?: boolean
notNull?: boolean
virtual?: boolean
}
declare class type<K extends kinds = 'type'> extends any_<K> { }

Expand All @@ -79,7 +82,10 @@ declare class Int32 extends Integer { }
declare class Int64 extends Integer { }
declare class Float extends number { }
declare class Double extends Float { }
declare class Decimal extends Float { }
declare class Decimal extends Float {
precision?: number
scale?: number
}

declare class date extends scalar { }
declare class Date extends date { }
Expand Down
4 changes: 3 additions & 1 deletion apis/linked/internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@
import type { scalar } from './classes'

declare class number_ extends scalar { }
declare class string_ extends scalar { }
declare class string_ extends scalar {
length?: number
}
declare class boolean_ extends scalar { }
12 changes: 10 additions & 2 deletions test/typescript/apis/project/cds-linked.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import cds from '@sap/cds';
import { csn } from '../../../..';
import { as } from './dummy';

const { action, aspect, entity, event, mixin, scalar, struct, type } = cds.linked.classes
const { action, aspect, entity, event, mixin, scalar, struct, type, Decimal, String } = cds.linked.classes

// is exported from top level
as<LinkedCSN>() === as<cds.linked.LinkedCSN>()
Expand Down Expand Up @@ -47,7 +47,7 @@ new entity().texts?.kind === 'entity'
new entity().drafts?.kind === 'entity'
new entity().is_entity === true
new entity().is_struct === true;
[...new entity().elements].find(x => x.items.kind === 'type')
[...new entity().elements].find(x => x.items && x.items.kind === 'type')
new entity().items?.kind
new entity().name
// @ts-expect-error
Expand All @@ -73,6 +73,9 @@ new scalar().kind === 'scalar_'
new type().kind === 'type'
// @ts-expect-error
new type().kind === 'type_'
new type().key
new type().virtual
new type().notNull

new event().elements
new event().kind === 'event'
Expand All @@ -83,6 +86,11 @@ new action().kind === 'action'
// @ts-expect-error
new action().kind === 'action_'

new Decimal().precision
new Decimal().scale

new String().length

mixin(class {}, class {})
// @ts-expect-error
mixin(42)
Expand Down