-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HCK-6309: american express support for couchbasev7plus fle (#34)
* HCK-6309: add PP config for PK structure * HCK-6309: add randexp module * HCK-6309: add generating key sample by PK structure * HCK-6309: clear js docs * update package-lock * fix prettier issue
- Loading branch information
1 parent
0421f99
commit ac87bac
Showing
7 changed files
with
242 additions
and
14 deletions.
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
forward_engineering/services/statements/getPrimaryKeySampleByStructure.js
This file contains 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 |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/** | ||
* @typedef {import('../../../shared/types').PkSegment} PkSegment | ||
* @typedef {import('../../../shared/types').UUID} UUID | ||
*/ | ||
const RandExp = require('randexp'); | ||
const { PK_SEGMENT_TYPE } = require('../../../shared/constants'); | ||
|
||
/** | ||
* @param {{ collection: object, jsonData: object }} | ||
* @returns {string} | ||
*/ | ||
const getPrimaryKeySampleByStructure = ({ collection, jsonData }) => { | ||
const keyField = Object.values(collection.properties || {}).find(field => field.primaryKeyStructure); | ||
const primaryKeyStructure = keyField?.primaryKeyStructure; | ||
|
||
if (!Array.isArray(primaryKeyStructure)) { | ||
return ''; | ||
} | ||
|
||
return primaryKeyStructure.reduce((result, segment) => { | ||
switch (segment.segmentType) { | ||
case PK_SEGMENT_TYPE.field: { | ||
const segmentValue = getPrimaryKeyStructureFieldValue({ segment, collection, jsonData }); | ||
return result + segmentValue; | ||
} | ||
case PK_SEGMENT_TYPE.pattern: { | ||
const segmentValue = getPrimaryKeyStructurePatternValue({ segment }); | ||
return result + segmentValue; | ||
} | ||
case PK_SEGMENT_TYPE.constant: | ||
case PK_SEGMENT_TYPE.separator: { | ||
return result + (segment.segmentValue ?? ''); | ||
} | ||
default: | ||
return result; | ||
} | ||
}, ''); | ||
}; | ||
|
||
/** | ||
* @param {{ segment: PkSegment, collection: object, jsonData: object }} | ||
* @returns {string} | ||
*/ | ||
const getPrimaryKeyStructureFieldValue = ({ segment, collection, jsonData }) => { | ||
const fieldNames = (segment.segmentKey || []).map(({ keyId }) => { | ||
return findFieldNameById({ collection, id: keyId }); | ||
}); | ||
|
||
return fieldNames | ||
.filter(Boolean) | ||
.map(fieldName => jsonData[fieldName]) | ||
.join(''); | ||
}; | ||
|
||
/** | ||
* @param {{ segment: PkSegment }} | ||
* @returns {string} | ||
*/ | ||
const getPrimaryKeyStructurePatternValue = ({ segment }) => { | ||
try { | ||
const randExpInstance = new RandExp(segment.segmentRegex); | ||
|
||
return randExpInstance.gen(); | ||
} catch (e) { | ||
return segment.segmentSample ?? ''; | ||
} | ||
}; | ||
|
||
/** | ||
* @param {{ collection: object, id: UUID }} | ||
* @returns {string} | ||
*/ | ||
const findFieldNameById = ({ collection, id }) => { | ||
return Object.entries(collection.properties || {}).reduce((result, [fieldName, field]) => { | ||
if (result) { | ||
return result; | ||
} | ||
|
||
if (field.GUID === id) { | ||
return fieldName; | ||
} | ||
|
||
return findFieldNameById({ collection: field, id }); | ||
}, ''); | ||
}; | ||
|
||
module.exports = { | ||
getPrimaryKeySampleByStructure, | ||
}; |
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 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