-
Notifications
You must be signed in to change notification settings - Fork 13
chore: Improve handling of custom ORD content #443
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
Open
mlakov
wants to merge
16
commits into
main
Choose a base branch
from
chore/improve-handling-of-custom-ord-content
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
ca3d667
chore: Improve handling of custom ORD content
mlakov 3aebdad
minor updates
mlakov d18baec
remove wrong filter
mlakov 3f8be66
Merge branch 'main' into chore/improve-handling-of-custom-ord-content
mlakov 3331a95
Merge branch 'main' into chore/improve-handling-of-custom-ord-content
mlakov 0e3d09b
adjust lib/extend-ord-with-custom.js
mlakov 966722a
Merge branch 'main' into chore/improve-handling-of-custom-ord-content
mlakov cd3552e
Merge branch 'main' into chore/improve-handling-of-custom-ord-content
mlakov 3a65014
Merge branch 'main' into chore/improve-handling-of-custom-ord-content
mlakov 6d619aa
Merge branch 'main' into chore/improve-handling-of-custom-ord-content
mlakov fc44932
add additional test
mlakov f13f401
add test for checking ORD schema version
mlakov 002130b
Merge branch 'main' into chore/improve-handling-of-custom-ord-content
mlakov fe369c2
improve tests
mlakov e0f6d79
improve tests
mlakov 66e55eb
revert to using ORD spec 1.14 due to lack of support for 1.15 in aggr…
mlakov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3550,4 +3550,4 @@ exports[`Tests for products and packages should use valid custom products ordId | |
| }, | ||
| ], | ||
| } | ||
| `; | ||
| `; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,68 +1,97 @@ | ||
| const { CONTENT_MERGE_KEY } = require("./constants"); | ||
| const cds = require("@sap/cds"); | ||
| const fs = require("fs"); | ||
| const Logger = require("./logger"); | ||
| const path = require("path"); | ||
| const _ = require("lodash"); | ||
|
|
||
| function cleanNullProperties(obj) { | ||
| for (const key in obj) { | ||
| if (obj[key] === null) { | ||
| delete obj[key]; | ||
| } else if (typeof obj[key] === "object") { | ||
| cleanNullProperties(obj[key]); | ||
| } | ||
| } | ||
| return obj; | ||
| } | ||
|
|
||
| function patchGeneratedOrdResources(destinationObj, sourceObj) { | ||
| const destObj = _.keyBy(destinationObj, CONTENT_MERGE_KEY); | ||
| const srcObj = _.keyBy(sourceObj, CONTENT_MERGE_KEY); | ||
| const SCALAR_TYPES = Object.freeze(["number", "string", "boolean"]); | ||
|
|
||
| for (const ordId in srcObj) { | ||
| destObj[ordId] = | ||
| ordId in destObj | ||
| ? _.assignWith(structuredClone(destObj[ordId]), structuredClone(srcObj[ordId])) | ||
| : srcObj[ordId]; | ||
| } | ||
| const MERGE_STRATEGIES = Object.freeze({ | ||
| // Directly replace scalar values and scalar value arrays | ||
| $schema: (current, custom) => custom, | ||
| description: (current, custom) => custom, | ||
| perspective: (current, custom) => custom, | ||
| openResourceDiscovery: (current, custom) => custom, | ||
| policyLevel: (current, custom) => custom, | ||
| customPolicyLevel: (current, custom) => custom, | ||
| policyLevels: (current, custom) => prune(custom), | ||
|
|
||
| return cleanNullProperties(Object.values(destObj)); | ||
| } | ||
| // Perform simple merge for objects | ||
| describedSystemType: (current, custom) => prune(_.assign(current, custom)), | ||
| describedSystemVersion: (current, custom) => prune(_.assign(current, custom)), | ||
| describedSystemInstance: (current, custom) => prune(_.assign(current, custom)), | ||
|
|
||
| function compareAndHandleCustomORDContentWithExistingContent(ordContent, customORDContent) { | ||
| const clonedOrdContent = structuredClone(ordContent); | ||
| for (const key in customORDContent) { | ||
| const propertyType = typeof customORDContent[key]; | ||
| if (propertyType !== "object" && propertyType !== "array") { | ||
| Logger.warn( | ||
| "Found ord top level primitive ord property in customOrdFile:", | ||
| key, | ||
| ". Please define it in .cdsrc.json.", | ||
| ); | ||
| // Perform smart merge for arrays of objects | ||
| agents: (current, custom) => merge(current, custom, ["ordId"]), | ||
| vendors: (current, custom) => merge(current, custom, ["ordId"]), | ||
| products: (current, custom) => merge(current, custom, ["ordId"]), | ||
| packages: (current, custom) => merge(current, custom, ["ordId"]), | ||
| groups: (current, custom) => merge(current, custom, ["groupId"]), | ||
| entityTypes: (current, custom) => merge(current, custom, ["ordId"]), | ||
| apiResources: (current, custom) => merge(current, custom, ["ordId"]), | ||
| capabilities: (current, custom) => merge(current, custom, ["ordId"]), | ||
| dataProducts: (current, custom) => merge(current, custom, ["ordId"]), | ||
| eventResources: (current, custom) => merge(current, custom, ["ordId"]), | ||
| groupTypes: (current, custom) => merge(current, custom, ["groupTypeId"]), | ||
| consumptionBundles: (current, custom) => merge(current, custom, ["ordId"]), | ||
| integrationDependencies: (current, custom) => merge(current, custom, ["ordId"]), | ||
| tombstones: (current, custom) => merge(current, custom, ["ordId", "groupId", "groupTypeId"]), | ||
| }); | ||
|
|
||
| continue; | ||
| } | ||
| if (key in ordContent) { | ||
| clonedOrdContent[key] = patchGeneratedOrdResources(clonedOrdContent[key], customORDContent[key]); | ||
| } else { | ||
| clonedOrdContent[key] = customORDContent[key]; | ||
| } | ||
| function prune(entity) { | ||
| if (Array.isArray(entity)) { | ||
| return entity | ||
| .filter((value) => ![null, undefined].includes(value)) | ||
| .map((value) => (SCALAR_TYPES.includes(typeof value) ? value : prune(value))); | ||
| } | ||
| return clonedOrdContent; | ||
|
|
||
| return Object.fromEntries( | ||
| Object.entries(entity) | ||
| .filter(([, value]) => ![null, undefined].includes(value)) | ||
| .map(([key, value]) => [key, SCALAR_TYPES.includes(typeof value) ? value : prune(value)]), | ||
| ); | ||
| } | ||
|
|
||
| function getCustomORDContent(appConfig) { | ||
| if (!appConfig.env?.customOrdContentFile) return; | ||
| const pathToCustomORDContent = path.join(cds.root, appConfig.env?.customOrdContentFile); | ||
| if (!fs.existsSync(pathToCustomORDContent)) { | ||
| Logger.error("Custom ORD content file not found at", pathToCustomORDContent); | ||
| return; | ||
| } | ||
| return require(pathToCustomORDContent); | ||
| function merge(target, source, keys) { | ||
| const iteratee = (entity) => keys.map((key) => entity[key] || "").join("."); | ||
| const sources = _.keyBy(source || [], iteratee); | ||
| const targets = _.keyBy(target || [], iteratee); | ||
|
|
||
| return Array.from(new Set([...Object.keys(targets), ...Object.keys(sources)])) // | ||
| .map((key) => _.assign(structuredClone(targets[key]), structuredClone(sources[key]))) | ||
| .map(prune); | ||
| } | ||
|
|
||
| module.exports = { | ||
| getCustomORDContent, | ||
| compareAndHandleCustomORDContentWithExistingContent, | ||
| MERGE_STRATEGIES, | ||
| getCustomORDContent: (configuration) => { | ||
| if (!configuration.env?.customOrdContentFile) return {}; | ||
|
|
||
| const file = path.join(cds.root, configuration.env?.customOrdContentFile); | ||
| if (!fs.existsSync(file)) { | ||
| Logger.error("Custom ORD content file not found at", file); | ||
| return {}; | ||
| } | ||
|
|
||
| return JSON.parse(fs.readFileSync(file, "utf8")); | ||
| }, | ||
| compareAndHandleCustomORDContentWithExistingContent: (ordContent, customORDContent) => { | ||
| return Object.fromEntries([ | ||
| // Process elements found only in 'ordContent' | ||
| ...Object.entries(ordContent) | ||
| .filter(([key]) => !(key in customORDContent)) | ||
| .map(([key, value]) => [key, structuredClone(value)]), | ||
|
|
||
| // Process elements found only in 'customORDContent' | ||
| ...Object.entries(customORDContent) | ||
| .filter(([key]) => !(key in ordContent)) | ||
| .map(([key, value]) => [key, structuredClone(value)]), | ||
|
|
||
| // Process elements found in both 'ordContent' and 'customORDContent' | ||
| ...Object.entries(customORDContent) | ||
| .filter(([key]) => key in ordContent) | ||
| .filter(([, value]) => ![null, undefined].includes(value)) | ||
| .map(([key, value]) => [key, MERGE_STRATEGIES[key](structuredClone(ordContent[key]), value)]), | ||
| ]); | ||
| }, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.