Skip to content

Commit

Permalink
Maintaining trailing zeros for decimal precision in json output
Browse files Browse the repository at this point in the history
  • Loading branch information
florianschoffke committed Jul 26, 2024
1 parent 00415f1 commit 1f697db
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
4 changes: 3 additions & 1 deletion src/export/InstanceExporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,15 @@ export class InstanceExporter implements Fishable {
value = `#${matchingContainedReferenceId}`;
}
}
let rawValue = (rule instanceof AssignmentRule) ? rule.rawValue : null
const validatedRule = instanceOfStructureDefinition.validateValueAtPath(
rule.path,
value,
this.fisher,
inlineResourceTypes,
rule.sourceInfo,
manualSliceOrdering
manualSliceOrdering,
rawValue
);
// Record each valid rule in a map
// Choice elements on an instance must use a specific type, so if the path still has an unchosen choice element,
Expand Down
19 changes: 11 additions & 8 deletions src/fhirtypes/ElementDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1830,7 +1830,7 @@ export class ElementDefinition {
* @throws {ValueAlreadyAssignedError} when the value is already assigned to a different value
* @throws {MismatchedTypeError} when the value does not match the type of the ElementDefinition
*/
assignValue(value: AssignmentValueType, exactly = false, fisher?: Fishable): void {
assignValue(value: AssignmentValueType, exactly = false, fisher?: Fishable, rawValue: any = null): void {
let type: string;
if (value instanceof FshCode) {
type = 'Code';
Expand Down Expand Up @@ -1870,7 +1870,7 @@ export class ElementDefinition {
this.assignFHIRValue(value.toString(), value, exactly, 'boolean');
break;
case 'number':
this.assignNumber(value as number, exactly);
this.assignNumber(value as number, exactly, rawValue);
break;
case 'string':
this.assignString(value as string, exactly);
Expand Down Expand Up @@ -2230,14 +2230,17 @@ export class ElementDefinition {
* @throws {ValueAlreadyAssignedError} when the value is already assigned to a different value
* @throws {MismatchedTypeError} when the value does not match the type of the ElementDefinition
*/
private assignNumber(value: number | bigint, exactly = false): void {
private assignNumber(value: number | bigint, exactly = false, rawValue: any = null): void {
const type = this.type[0].code;
const valueAsNumber = Number(value);
if (
type === 'decimal' ||
(type === 'integer' && Number.isInteger(valueAsNumber)) ||
(type === 'unsignedInt' && Number.isInteger(valueAsNumber) && valueAsNumber >= 0) ||
(type === 'positiveInt' && Number.isInteger(valueAsNumber) && valueAsNumber > 0)
// In case of
if (type === 'decimal' && !isNaN(Number(rawValue)) && /^\d+\.0+$/.test(rawValue)) {
this.assignFHIRValue(rawValue.toString(), rawValue.toString(), exactly, type);
} else if (
type === 'decimal' ||
(type === 'integer' && Number.isInteger(valueAsNumber)) ||
(type === 'unsignedInt' && Number.isInteger(valueAsNumber) && valueAsNumber >= 0) ||
(type === 'positiveInt' && Number.isInteger(valueAsNumber) && valueAsNumber > 0)
) {
this.assignFHIRValue(value.toString(), valueAsNumber, exactly, type);
} else if (type === 'integer64' && typeof value === 'bigint') {
Expand Down
5 changes: 3 additions & 2 deletions src/fhirtypes/StructureDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,8 @@ export class StructureDefinition {
fisher: Fishable,
inlineResourceTypes: string[] = [],
sourceInfo: SourceInfo = null,
manualSliceOrdering = false
manualSliceOrdering = false,
rawValue: any = null
): { assignedValue: any; pathParts: PathPart[]; childPath?: string } {
const pathParts = parseFSHPath(path);
let currentPath = '';
Expand Down Expand Up @@ -773,7 +774,7 @@ export class StructureDefinition {
// assignValue will throw if it fails, but skip the check if value is null
if (value != null) {
// exactly must be true so that we always test assigning with the more strict fixed[x] approach
currentElement.assignValue(value, true, fisher);
currentElement.assignValue(value, true, fisher, rawValue);
}
// If there is a fixedValue or patternValue, find it and return it
const key = Object.keys(currentElement).find(
Expand Down

0 comments on commit 1f697db

Please sign in to comment.