Skip to content

Commit

Permalink
Do Not Emit Elements That Are Part of excluding Clause (#118)
Browse files Browse the repository at this point in the history
* Do not print elements specified via `excluding` keyword

* Add test cases

* Add changelog entry

* Lint
  • Loading branch information
daogrady authored Dec 5, 2023
1 parent 4129cbd commit 4645dc9
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
## Version 0.13.0 - TBD
### Changes
- Enums are now generated ecplicitly in the respective _index.js_ files and don't have to extract their values from the model at runtime anymore
- The `excluding` clause in projections now actually excludes the specified properties in the generated types

## Version 0.12.0 - 2023-11-23

Expand Down
4 changes: 3 additions & 1 deletion lib/visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ class Visitor {
buffer.indent()

const enums = []
for (const [ename, element] of Object.entries(entity.elements ?? {})) {
const exclusions = new Set(entity.projection?.excluding ?? [])
const elements = Object.entries(entity.elements ?? {}).filter(([ename]) => !exclusions.has(ename))
for (const [ename, element] of elements) {
this.visitElement(ename, element, file, buffer)

// make foreign keys explicit
Expand Down
28 changes: 28 additions & 0 deletions test/unit/excluding.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict'

const fs = require('fs').promises
const path = require('path')
const cds2ts = require('../../lib/compile')
const { ASTWrapper } = require('../ast')
const { locations } = require('../util')
const dir = locations.testOutput('excluding_test')

describe('Excluding Clause', () => {
let paths

beforeEach(async () => await fs.unlink(dir).catch(() => {}))
beforeAll(async () => {
paths = await cds2ts
.compileFromFile(locations.unit.files('excluding/model.cds'), { outputDirectory: dir })
})

test('Element Present in Original', async () =>
expect(new ASTWrapper(path.join(paths[1], 'index.ts')).exists('_TestObjectAspect', 'dependencies')).toBeTruthy())

test('Element Gone in Projection', async () =>
expect(new ASTWrapper(path.join(paths[2], 'index.ts'))
.getAspect('_SlimTestObjectAspect')
.members
.find(({name}) => name === 'dependencies')
).toBeFalsy())
})
14 changes: 14 additions & 0 deletions test/unit/files/excluding/model.cds
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace excluding_test;

entity Foo {}

entity TestObjects {
key ID: UUID;
name: String not null;
description: String;
dependencies: Association to many Foo;
}

service TestService {
entity SlimTestObjects as projection on TestObjects excluding { dependencies };
}

0 comments on commit 4645dc9

Please sign in to comment.