-
Notifications
You must be signed in to change notification settings - Fork 0
**Enhance property field handling with transformations** #115
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,9 +1,17 @@ | ||||||||||||||||||||||||||||||||||||||||
| import { Resource } from '@platform-mesh/portal-ui-lib/models'; | ||||||||||||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||||||||||||
| PropertyField, | ||||||||||||||||||||||||||||||||||||||||
| Resource, | ||||||||||||||||||||||||||||||||||||||||
| TransformType, | ||||||||||||||||||||||||||||||||||||||||
| } from '@platform-mesh/portal-ui-lib/models'; | ||||||||||||||||||||||||||||||||||||||||
| import jsonpath from 'jsonpath'; | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| export const getResourceValueByJsonPath = ( | ||||||||||||||||||||||||||||||||||||||||
| resource: Resource, | ||||||||||||||||||||||||||||||||||||||||
| field: { jsonPathExpression?: string; property?: string | string[] }, | ||||||||||||||||||||||||||||||||||||||||
| field: { | ||||||||||||||||||||||||||||||||||||||||
| jsonPathExpression?: string; | ||||||||||||||||||||||||||||||||||||||||
| property?: string | string[]; | ||||||||||||||||||||||||||||||||||||||||
| propertyField?: PropertyField; | ||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||
| ) => { | ||||||||||||||||||||||||||||||||||||||||
| const property = field.jsonPathExpression || field.property; | ||||||||||||||||||||||||||||||||||||||||
| if (!property) { | ||||||||||||||||||||||||||||||||||||||||
|
|
@@ -17,6 +25,77 @@ export const getResourceValueByJsonPath = ( | |||||||||||||||||||||||||||||||||||||||
| return undefined; | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| const value = jsonpath.query(resource, `$.${property}`); | ||||||||||||||||||||||||||||||||||||||||
| return value.length ? value[0] : undefined; | ||||||||||||||||||||||||||||||||||||||||
| const queryResult = jsonpath.query(resource, `$.${property}`); | ||||||||||||||||||||||||||||||||||||||||
| const value = queryResult.length ? queryResult[0] : undefined; | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| if (value && field.propertyField) { | ||||||||||||||||||||||||||||||||||||||||
| return executeTransform( | ||||||||||||||||||||||||||||||||||||||||
| value[field.propertyField.key], | ||||||||||||||||||||||||||||||||||||||||
| field.propertyField.transform, | ||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+31
to
+36
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add type guard for non-string values before transformation. The Apply this defensive check: if (value && field.propertyField) {
+ const fieldValue = value[field.propertyField.key];
+ if (typeof fieldValue !== 'string' && fieldValue != null) {
+ console.warn(
+ `Expected string for propertyField key "${field.propertyField.key}", got ${typeof fieldValue}. Skipping transforms.`,
+ );
+ return fieldValue;
+ }
return executeTransform(
- value[field.propertyField.key],
+ fieldValue,
field.propertyField.transform,
);
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| return value; | ||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| const executeTransform = ( | ||||||||||||||||||||||||||||||||||||||||
| value: string | undefined, | ||||||||||||||||||||||||||||||||||||||||
| transform: TransformType[] | undefined, | ||||||||||||||||||||||||||||||||||||||||
| ): string | undefined => { | ||||||||||||||||||||||||||||||||||||||||
| if (value == null || transform == null || !transform.length) return value; | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| return transform.reduce((acc, t) => { | ||||||||||||||||||||||||||||||||||||||||
| if (acc == null) return acc; | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| switch (t) { | ||||||||||||||||||||||||||||||||||||||||
| case 'uppercase': | ||||||||||||||||||||||||||||||||||||||||
| return acc.toUpperCase(); | ||||||||||||||||||||||||||||||||||||||||
| case 'lowercase': | ||||||||||||||||||||||||||||||||||||||||
| return acc.toLowerCase(); | ||||||||||||||||||||||||||||||||||||||||
| case 'capitalize': { | ||||||||||||||||||||||||||||||||||||||||
| const str = String(acc); | ||||||||||||||||||||||||||||||||||||||||
| return str.length ? str.charAt(0).toUpperCase() + str.slice(1) : str; | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| case 'decode': { | ||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||
| return decodeBase64(acc); | ||||||||||||||||||||||||||||||||||||||||
| } catch { | ||||||||||||||||||||||||||||||||||||||||
| return acc; | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| case 'encode': { | ||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||
| return encodeBase64(acc); | ||||||||||||||||||||||||||||||||||||||||
| } catch { | ||||||||||||||||||||||||||||||||||||||||
| return acc; | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| default: | ||||||||||||||||||||||||||||||||||||||||
| return acc; | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| }, value); | ||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| export const encodeBase64 = (str: string): string => { | ||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||
| const utf8Bytes = new TextEncoder().encode(str); | ||||||||||||||||||||||||||||||||||||||||
| const binaryString = Array.from(utf8Bytes, (byte) => | ||||||||||||||||||||||||||||||||||||||||
| String.fromCharCode(byte), | ||||||||||||||||||||||||||||||||||||||||
| ).join(''); | ||||||||||||||||||||||||||||||||||||||||
| return btoa(binaryString); | ||||||||||||||||||||||||||||||||||||||||
| } catch (error) { | ||||||||||||||||||||||||||||||||||||||||
| console.error('Base64 encoding failed:', error); | ||||||||||||||||||||||||||||||||||||||||
| throw new Error('Failed to encode string to Base64'); | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| export const decodeBase64 = (base64: string): string => { | ||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||
| const binaryString = atob(base64); | ||||||||||||||||||||||||||||||||||||||||
| const bytes = Uint8Array.from(binaryString, (char) => char.charCodeAt(0)); | ||||||||||||||||||||||||||||||||||||||||
| return new TextDecoder().decode(bytes); | ||||||||||||||||||||||||||||||||||||||||
| } catch (error) { | ||||||||||||||||||||||||||||||||||||||||
| console.error('Base64 decoding failed:', error); | ||||||||||||||||||||||||||||||||||||||||
| throw new Error('Failed to decode Base64 string'); | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doc