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

Do Not Emit Elements That Are Part of excluding Clause #118

Merged
merged 4 commits into from
Dec 5, 2023
Merged
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
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 };
}