Skip to content

Commit

Permalink
Retrieve keys from both flavours of CSN
Browse files Browse the repository at this point in the history
  • Loading branch information
daogrady committed Dec 18, 2023
1 parent 203dd2d commit 43a291d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/csn.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,4 @@ const isView = entity => entity.query && !entity.projection
*/
const isUnresolved = entity => entity._unresolved === true

module.exports = { amendCSN, isView, isUnresolved }
module.exports = { amendCSN, isView, isUnresolved, propagateForeignKeys }
23 changes: 21 additions & 2 deletions lib/visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const util = require('./util')

const { amendCSN, isView, isUnresolved } = require('./csn')
const { amendCSN, isView, isUnresolved, propagateForeignKeys } = require('./csn')
// eslint-disable-next-line no-unused-vars
const { SourceFile, baseDefinitions, Buffer } = require('./file')
const { FlatInlineDeclarationResolver, StructuredInlineDeclarationResolver } = require('./components/inline')
Expand Down Expand Up @@ -63,6 +63,7 @@ class Visitor {
*/
constructor(csn, options = {}, logger = new Logger()) {
amendCSN(csn.xtended)
propagateForeignKeys(csn.inferred)
this.options = { ...defaults, ...options }
this.logger = logger
this.csn = csn
Expand Down Expand Up @@ -130,6 +131,24 @@ class Visitor {
}
}

/**
* Retrieves all the keys from an entity.
* That is: all keys that are present in both inferred, as well as xtended flavour.
* @returns {[string, object][]} array of key name and key element pairs
*/
#keys(name) {
// FIXME: this is actually pretty bad, as not only have to propagate keys through
// both flavours of CSN (see constructor), but we are now also collecting them from
// both flavours and deduplicating them.
// xtended contains keys that have been inherited from parents
// inferred contains keys from queried entities (thing `entity Foo as select from Bar`, where Bar has keys)
// So we currently need them both.
return Object.entries({
...this.csn.inferred.definitions[name]?.keys ?? {},
...this.csn.xtended.definitions[name]?.keys ?? {}
})
}

/**
* Transforms an entity or CDS aspect into a JS aspect (aka mixin).
* That is, for an element A we get:
Expand Down Expand Up @@ -168,7 +187,7 @@ class Visitor {
// lookup in cds.definitions can fail for inline structs.
// We don't really have to care for this case, as keys from such structs are _not_ propagated to
// the containing entity.
for (const [kname, kelement] of Object.entries(this.csn.xtended.definitions[element.target]?.keys ?? {})) {
for (const [kname, kelement] of this.#keys(element.target)) {
if (this.resolver.getMaxCardinality(element) === 1) {
kelement.isRefNotNull = !!element.notNull || !!element.key
this.visitElement(`${ename}_${kname}`, kelement, file, buffer)
Expand Down

0 comments on commit 43a291d

Please sign in to comment.