Skip to content

Commit ad9ba60

Browse files
JshhhhJustin Shih
andauthored
fix: parse operand value when field is number type (#1013)
Co-authored-by: Justin Shih <[email protected]>
1 parent 5e23c0c commit ad9ba60

File tree

7 files changed

+152
-13
lines changed

7 files changed

+152
-13
lines changed

packages/codegen-ui-react/lib/__tests__/__snapshots__/studio-ui-codegen-react.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1094,7 +1094,7 @@ export default function CollectionOfCustomButtons(
10941094
} = props;
10951095
const itemsFilterObj = {
10961096
and: [
1097-
{ field: \\"age\\", operand: \\"10\\", operator: \\"gt\\" },
1097+
{ field: \\"age\\", operand: 10, operator: \\"eq\\" },
10981098
{ field: \\"lastName\\", operand: \\"L\\", operator: \\"beginsWith\\" },
10991099
],
11001100
};

packages/codegen-ui-react/lib/__tests__/__utils__/mock-data-schemas.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,3 +480,90 @@ export const compositePersonSchema: GenericDataSchema = getGenericFromDataStore(
480480
codegenVersion: '3.3.2',
481481
version: 'accea0d7a2f24829740c710ceb3264a8',
482482
});
483+
484+
export const userSchema: GenericDataSchema = getGenericFromDataStore({
485+
models: {
486+
User: {
487+
name: 'User',
488+
fields: {
489+
id: {
490+
name: 'id',
491+
isArray: false,
492+
type: 'ID',
493+
isRequired: true,
494+
attributes: [],
495+
},
496+
firstName: {
497+
name: 'firstName',
498+
isArray: false,
499+
type: 'String',
500+
isRequired: false,
501+
attributes: [],
502+
},
503+
lastName: {
504+
name: 'lastName',
505+
isArray: false,
506+
type: 'String',
507+
isRequired: false,
508+
attributes: [],
509+
},
510+
age: {
511+
name: 'age',
512+
isArray: false,
513+
type: 'Int',
514+
isRequired: false,
515+
attributes: [],
516+
},
517+
isLoggedIn: {
518+
name: 'isLoggedIn',
519+
isArray: false,
520+
type: 'Boolean',
521+
isRequired: false,
522+
attributes: [],
523+
},
524+
loggedInColor: {
525+
name: 'loggedInColor',
526+
isArray: false,
527+
type: 'String',
528+
isRequired: false,
529+
attributes: [],
530+
},
531+
loggedOutColor: {
532+
name: 'loggedOutColor',
533+
isArray: false,
534+
type: 'String',
535+
isRequired: false,
536+
attributes: [],
537+
},
538+
createdAt: {
539+
name: 'createdAt',
540+
isArray: false,
541+
type: 'AWSDateTime',
542+
isRequired: false,
543+
attributes: [],
544+
isReadOnly: true,
545+
},
546+
updatedAt: {
547+
name: 'updatedAt',
548+
isArray: false,
549+
type: 'AWSDateTime',
550+
isRequired: false,
551+
attributes: [],
552+
isReadOnly: true,
553+
},
554+
},
555+
syncable: true,
556+
pluralName: 'Users',
557+
attributes: [
558+
{
559+
type: 'model',
560+
properties: {},
561+
},
562+
],
563+
},
564+
},
565+
enums: {},
566+
nonModels: {},
567+
codegenVersion: '3.3.2',
568+
version: 'accea0d7a2f24829740c710ceb3264a8',
569+
});

packages/codegen-ui-react/lib/__tests__/react-component-render-helper.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import {
3535
buildConditionalExpression,
3636
hasChildrenProp,
3737
buildConcatExpression,
38+
parseNumberOperand,
3839
} from '../react-component-render-helper';
3940

4041
import { assertASTMatchesSnapshot } from './__utils__';
@@ -345,4 +346,15 @@ describe('react-component-render-helper', () => {
345346
assertASTMatchesSnapshot(exp);
346347
});
347348
});
349+
350+
describe('parseNumberOperand', () => {
351+
test('should parse int if field data type is Int', () => {
352+
expect(parseNumberOperand('10', { dataType: 'Int', readOnly: false, required: false, isArray: false })).toBe(10);
353+
});
354+
test('should parse int if field data type is Int', () => {
355+
expect(parseNumberOperand('10.01', { dataType: 'Float', readOnly: false, required: false, isArray: false })).toBe(
356+
10.01,
357+
);
358+
});
359+
});
348360
});

packages/codegen-ui-react/lib/__tests__/studio-ui-codegen-react.test.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
limitations under the License.
1515
*/
1616
import { ModuleKind, ScriptTarget, ScriptKind } from '..';
17-
import { authorHasManySchema, compositePersonSchema, generateWithAmplifyRenderer } from './__utils__';
17+
import { authorHasManySchema, compositePersonSchema, generateWithAmplifyRenderer, userSchema } from './__utils__';
1818

1919
describe('amplify render tests', () => {
2020
describe('basic component tests', () => {
@@ -121,7 +121,12 @@ describe('amplify render tests', () => {
121121
});
122122

123123
it('should render collection with data binding if binding name is items', () => {
124-
const generatedCode = generateWithAmplifyRenderer('collectionWithBindingItemsName');
124+
const generatedCode = generateWithAmplifyRenderer(
125+
'collectionWithBindingItemsName',
126+
undefined,
127+
undefined,
128+
userSchema,
129+
);
125130
expect(generatedCode.componentText).toMatchSnapshot();
126131
});
127132

packages/codegen-ui-react/lib/react-component-render-helper.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,13 @@ import {
5252
ArrayLiteralExpression,
5353
} from 'typescript';
5454

55-
import { FormMetadata, FormStyleConfig, StudioFormInputFieldProperty } from '@aws-amplify/codegen-ui/lib/types';
55+
import {
56+
DataFieldDataType,
57+
FormMetadata,
58+
FormStyleConfig,
59+
GenericDataField,
60+
StudioFormInputFieldProperty,
61+
} from '@aws-amplify/codegen-ui/lib/types';
5662
import { ImportCollection, ImportSource } from './imports';
5763
import { json, jsonToLiteral } from './react-studio-template-renderer-helper';
5864
import { getChildPropMappingForComponentName } from './workflow/utils';
@@ -458,6 +464,19 @@ export function getSyntaxKindToken(operator: RelationalOperator): BinaryOperator
458464
}
459465
}
460466

467+
export function parseNumberOperand(operand: string | number | boolean, dataField: GenericDataField | undefined) {
468+
if (dataField) {
469+
const numberOperandType: DataFieldDataType[] = ['Int', 'Float'];
470+
if (numberOperandType.includes(dataField.dataType)) {
471+
const parsedOperand = parseFloat(`${operand}`);
472+
if (!Number.isNaN(parsedOperand) && Number.isFinite(parsedOperand)) {
473+
return parsedOperand;
474+
}
475+
}
476+
}
477+
return operand;
478+
}
479+
461480
export function getConditionalOperandExpression(
462481
operand: string | number | boolean,
463482
operandType: string | undefined,

packages/codegen-ui-react/lib/react-studio-template-renderer.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ import {
7272
addBindingPropertiesImports,
7373
getComponentPropName,
7474
getConditionalOperandExpression,
75+
parseNumberOperand,
7576
} from './react-component-render-helper';
7677
import {
7778
transpile,
@@ -934,7 +935,7 @@ export abstract class ReactStudioTemplateRenderer extends StudioTemplateRenderer
934935
const modelName = this.importCollection.addImport(ImportSource.LOCAL_MODELS, model);
935936

936937
if (predicate) {
937-
statements.push(this.buildPredicateDeclaration(propName, predicate));
938+
statements.push(this.buildPredicateDeclaration(propName, predicate, model));
938939
statements.push(this.buildCreateDataStorePredicateCall(modelName, propName));
939940
}
940941
if (sort) {
@@ -1255,7 +1256,9 @@ export abstract class ReactStudioTemplateRenderer extends StudioTemplateRenderer
12551256
* operator: "eq",
12561257
* }
12571258
*/
1258-
statements.push(this.buildPredicateDeclaration(propName, bindingProperties.predicate));
1259+
statements.push(
1260+
this.buildPredicateDeclaration(propName, bindingProperties.predicate, bindingProperties.model),
1261+
);
12591262
statements.push(this.buildCreateDataStorePredicateCall(modelName, propName));
12601263
/**
12611264
* const buttonColorDataStore = useDataStoreBinding({
@@ -1436,11 +1439,14 @@ export abstract class ReactStudioTemplateRenderer extends StudioTemplateRenderer
14361439
]);
14371440
}
14381441

1439-
private predicateToObjectLiteralExpression(predicate: StudioComponentPredicate): ObjectLiteralExpression {
1442+
private predicateToObjectLiteralExpression(
1443+
predicate: StudioComponentPredicate,
1444+
model: string,
1445+
): ObjectLiteralExpression {
14401446
const { operandType, ...filteredPredicate } = predicate;
14411447

14421448
if (filteredPredicate.operator === 'between') {
1443-
return this.predicateToObjectLiteralExpression(resolveBetweenPredicateToMultiplePredicates(predicate));
1449+
return this.predicateToObjectLiteralExpression(resolveBetweenPredicateToMultiplePredicates(predicate), model);
14441450
}
14451451

14461452
const objectAssignments = Object.entries(filteredPredicate).map(([key, value]) => {
@@ -1449,7 +1455,7 @@ export abstract class ReactStudioTemplateRenderer extends StudioTemplateRenderer
14491455
factory.createIdentifier(key),
14501456
factory.createArrayLiteralExpression(
14511457
(predicate[key] as StudioComponentPredicate[]).map(
1452-
(pred: StudioComponentPredicate) => this.predicateToObjectLiteralExpression(pred),
1458+
(pred: StudioComponentPredicate) => this.predicateToObjectLiteralExpression(pred, model),
14531459
false,
14541460
),
14551461
),
@@ -1458,7 +1464,13 @@ export abstract class ReactStudioTemplateRenderer extends StudioTemplateRenderer
14581464
if (key === 'operand' && typeof value === 'string') {
14591465
return factory.createPropertyAssignment(
14601466
factory.createIdentifier(key),
1461-
getConditionalOperandExpression(value, operandType),
1467+
getConditionalOperandExpression(
1468+
parseNumberOperand(
1469+
value,
1470+
this.componentMetadata.dataSchemaMetadata?.models[model]?.fields[predicate.field || ''],
1471+
),
1472+
operandType,
1473+
),
14621474
);
14631475
}
14641476
return factory.createPropertyAssignment(
@@ -1480,7 +1492,11 @@ export abstract class ReactStudioTemplateRenderer extends StudioTemplateRenderer
14801492
return [];
14811493
}
14821494

1483-
private buildPredicateDeclaration(name: string, predicate: StudioComponentPredicate): VariableStatement {
1495+
private buildPredicateDeclaration(
1496+
name: string,
1497+
predicate: StudioComponentPredicate,
1498+
model: string,
1499+
): VariableStatement {
14841500
return factory.createVariableStatement(
14851501
undefined,
14861502
factory.createVariableDeclarationList(
@@ -1489,7 +1505,7 @@ export abstract class ReactStudioTemplateRenderer extends StudioTemplateRenderer
14891505
factory.createIdentifier(this.getFilterObjName(name)),
14901506
undefined,
14911507
undefined,
1492-
this.predicateToObjectLiteralExpression(predicate),
1508+
this.predicateToObjectLiteralExpression(predicate, model),
14931509
),
14941510
],
14951511
ts.NodeFlags.Const,

packages/codegen-ui/example-schemas/collectionWithBindingItemsName.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
{
4646
"field": "age",
4747
"operand": "10",
48-
"operator": "gt"
48+
"operator": "eq"
4949
},
5050
{
5151
"field": "lastName",

0 commit comments

Comments
 (0)