Skip to content

Commit 6216fe9

Browse files
authored
Merge branch 'main' into v1.5539.0
2 parents 3eb7533 + cf904b0 commit 6216fe9

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

packages/clients/src/scw/__tests__/custom-marshalling.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import {
44
marshalScwFile,
55
marshalTimeSeries,
66
marshalTimeSeriesPoint,
7+
unmarshalDecimal,
78
unmarshalMoney,
89
unmarshalScwFile,
910
unmarshalServiceInfo,
1011
unmarshalTimeSeries,
1112
unmarshalTimeSeriesPoint,
1213
} from '../custom-marshalling'
14+
import { Decimal } from '../custom-types'
1315

1416
describe('unmarshalMoney', () => {
1517
it('returns the proper object', () => {
@@ -123,6 +125,29 @@ describe('unmarshalTimeSeries', () => {
123125
})
124126
})
125127

128+
describe('unmarshalDecimal', () => {
129+
it('returns the proper object', () => {
130+
const decimal = unmarshalDecimal({
131+
value: '0.01',
132+
})
133+
expect(decimal).toBeInstanceOf(Decimal)
134+
expect(decimal?.toString()).toStrictEqual('0.01')
135+
})
136+
137+
it('throws for invalid input', () => {
138+
expect(unmarshalDecimal(null)).toBeNull()
139+
expect(() => {
140+
unmarshalDecimal({})
141+
}).toThrow()
142+
expect(() => {
143+
unmarshalDecimal({ value: null })
144+
}).toThrow()
145+
expect(() => {
146+
unmarshalDecimal({ value: 0.02 })
147+
}).toThrow()
148+
})
149+
})
150+
126151
describe('marshalScwFile', () => {
127152
it('returns a the proper object', () =>
128153
expect(

packages/clients/src/scw/custom-marshalling.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,28 @@ export const unmarshalTimeSeries = (data: unknown) => {
115115
* @internal
116116
*/
117117
export const unmarshalDecimal = (data: unknown) => {
118-
if (!(typeof data === 'string')) {
118+
if (!(typeof data === 'object')) {
119119
throw new TypeError(
120-
`Unmarshalling the type 'Decimal' failed as data isn't a string.`,
120+
`Unmarshalling the type 'Decimal' failed as data isn't an object.`,
121+
)
122+
}
123+
if (data === null) {
124+
return null
125+
}
126+
127+
if (!('value' in data)) {
128+
throw new TypeError(
129+
`Unmarshalling the type 'Decimal' failed as data object does not have a 'value' key.`,
121130
)
122131
}
123132

124-
return new Decimal(data)
133+
if (!(typeof data.value === 'string')) {
134+
throw new TypeError(
135+
`Unmarshalling the type 'Decimal' failed as 'value' is not a string.`,
136+
)
137+
}
138+
139+
return new Decimal(data.value)
125140
}
126141

127142
/**
@@ -189,4 +204,6 @@ export const marshalTimeSeries = (
189204
*
190205
* @internal
191206
*/
192-
export const marshalDecimal = (obj: Decimal): string => obj.toString()
207+
export const marshalDecimal = (obj: Decimal): { value: string } => ({
208+
value: obj.toString(),
209+
})

packages/clients/src/scw/custom-types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,6 @@ export class Decimal {
7979
}
8080

8181
public toString = (): string => this.str
82+
83+
public marshal = (): object => ({ value: this.str })
8284
}

0 commit comments

Comments
 (0)