From ff31002d2c78326356211fa0474314c810af995c Mon Sep 17 00:00:00 2001 From: Jules Casteran Date: Thu, 3 Oct 2024 17:03:55 +0200 Subject: [PATCH] fix(scw): decimal json representation --- .../scw/__tests__/custom-marshalling.test.ts | 25 +++++++++++++++++++ .../clients/src/scw/custom-marshalling.ts | 25 ++++++++++++++++--- packages/clients/src/scw/custom-types.ts | 2 ++ 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/packages/clients/src/scw/__tests__/custom-marshalling.test.ts b/packages/clients/src/scw/__tests__/custom-marshalling.test.ts index 25adfcc7f..d78c728f1 100644 --- a/packages/clients/src/scw/__tests__/custom-marshalling.test.ts +++ b/packages/clients/src/scw/__tests__/custom-marshalling.test.ts @@ -4,12 +4,14 @@ import { marshalScwFile, marshalTimeSeries, marshalTimeSeriesPoint, + unmarshalDecimal, unmarshalMoney, unmarshalScwFile, unmarshalServiceInfo, unmarshalTimeSeries, unmarshalTimeSeriesPoint, } from '../custom-marshalling' +import { Decimal } from '../custom-types' describe('unmarshalMoney', () => { it('returns the proper object', () => { @@ -123,6 +125,29 @@ describe('unmarshalTimeSeries', () => { }) }) +describe('unmarshalDecimal', () => { + it('returns the proper object', () => { + const decimal = unmarshalDecimal({ + value: '0.01', + }) + expect(decimal).toBeInstanceOf(Decimal) + expect(decimal?.toString()).toStrictEqual('0.01') + }) + + it('throws for invalid input', () => { + expect(unmarshalDecimal(null)).toBeNull() + expect(() => { + unmarshalDecimal({}) + }).toThrow() + expect(() => { + unmarshalDecimal({ value: null }) + }).toThrow() + expect(() => { + unmarshalDecimal({ value: 0.02 }) + }).toThrow() + }) +}) + describe('marshalScwFile', () => { it('returns a the proper object', () => expect( diff --git a/packages/clients/src/scw/custom-marshalling.ts b/packages/clients/src/scw/custom-marshalling.ts index f2cc13e4e..8a0368741 100644 --- a/packages/clients/src/scw/custom-marshalling.ts +++ b/packages/clients/src/scw/custom-marshalling.ts @@ -115,13 +115,28 @@ export const unmarshalTimeSeries = (data: unknown) => { * @internal */ export const unmarshalDecimal = (data: unknown) => { - if (!(typeof data === 'string')) { + if (!(typeof data === 'object')) { throw new TypeError( - `Unmarshalling the type 'Decimal' failed as data isn't a string.`, + `Unmarshalling the type 'Decimal' failed as data isn't an object.`, + ) + } + if (data === null) { + return null + } + + if (!('value' in data)) { + throw new TypeError( + `Unmarshalling the type 'Decimal' failed as data object does not have a 'value' key.`, ) } - return new Decimal(data) + if (!(typeof data.value === 'string')) { + throw new TypeError( + `Unmarshalling the type 'Decimal' failed as 'value' is not a string.`, + ) + } + + return new Decimal(data.value) } /** @@ -189,4 +204,6 @@ export const marshalTimeSeries = ( * * @internal */ -export const marshalDecimal = (obj: Decimal): string => obj.toString() +export const marshalDecimal = (obj: Decimal): { value: string } => ({ + value: obj.toString(), +}) diff --git a/packages/clients/src/scw/custom-types.ts b/packages/clients/src/scw/custom-types.ts index 8e5d16c8b..026cdb795 100644 --- a/packages/clients/src/scw/custom-types.ts +++ b/packages/clients/src/scw/custom-types.ts @@ -79,4 +79,6 @@ export class Decimal { } public toString = (): string => this.str + + public marshal = (): object => ({ value: this.str }) }