diff --git a/redistributable/TypeScript/morphir/internal/Codecs.ts b/redistributable/TypeScript/morphir/internal/Codecs.ts index d03dbf50f..dccdad958 100644 --- a/redistributable/TypeScript/morphir/internal/Codecs.ts +++ b/redistributable/TypeScript/morphir/internal/Codecs.ts @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -class DecodeError extends Error { } +class DecodeError extends Error {} Object.defineProperty(DecodeError.prototype, "name", { value: "DecodeError", @@ -24,6 +24,7 @@ type CodecFunction = (input: any) => any; type CodecList = Array; type CodecMap = Map; +type Decimal = number; // Construct a codec map, avoiding spurious type errors. // @@ -62,6 +63,13 @@ export function decodeChar(input: any): string { return input; } +export function decodeDecimal(input: any): Decimal { + if (typeof input != "number") { + throw new DecodeError(`Expected Decimal, got ${typeof input}`); + } + return input; +} + export function decodeString(input: any): string { if (typeof input != "string") { throw new DecodeError(`Expected string, got ${typeof input}`); @@ -83,11 +91,14 @@ export function decodeFloat(input: any): number { return input; } -export function decodeMaybe(decodeElement: (any) => T, input: any): T | null { +export function decodeMaybe( + decodeElement: (any) => T, + input: any +): T | null { if (input == null) { - return null + return null; } else { - return decodeElement(input) + return decodeElement(input); } } export function decodeDict( @@ -101,7 +112,6 @@ export function decodeDict( const inputArray: Array = input; - return new Map( inputArray.map((item: any) => { if (!(item instanceof Array)) { @@ -135,13 +145,14 @@ export function decodeRecord( const fieldNames: Array = Array.from(fieldDecoders.keys()); for (var field of fieldNames) { - if (!(Object.keys(input).includes(field))) { + if (!Object.keys(input).includes(field)) { throw new DecodeError(`Expected field ${field} was not found`); } } if (Object.keys(inputObject).length > fieldNames.length) { throw new DecodeError( - `Input object has extra fields, expected ${fieldNames.length}, got ${input.keys().length + `Input object has extra fields, expected ${fieldNames.length}, got ${ + input.keys().length }` ); } @@ -210,6 +221,10 @@ export function encodeChar(value: string): string { return value; } +export function encodeDecimal(value: Decimal): Decimal { + return value; +} + export function encodeString(value: string): string { return value; } @@ -224,9 +239,9 @@ export function encodeFloat(value: number): number { export function encodeMaybe(encodeElement: (any) => T, value: T | null) { if (value == null) { - return null + return null; } else { - return encodeElement(value) + return encodeElement(value); } } export function encodeDict( @@ -303,6 +318,6 @@ export function raiseDecodeErrorFromCustomType( ): void { throw new DecodeError( `Error while attempting to decode an instance of ${customTypeName}.` + - ` "${kind}" is not a valid 'kind' field for ${customTypeName}.` + ` "${kind}" is not a valid 'kind' field for ${customTypeName}.` ); } diff --git a/src/Morphir/TypeScript/Backend/Types.elm b/src/Morphir/TypeScript/Backend/Types.elm index 8156bba55..0e234571a 100644 --- a/src/Morphir/TypeScript/Backend/Types.elm +++ b/src/Morphir/TypeScript/Backend/Types.elm @@ -261,7 +261,7 @@ mapTypeExp tpe = Type.Reference _ ( [ [ "morphir" ], [ "s", "d", "k" ] ], [ [ "basics" ] ], [ "int" ] ) [] -> TS.Number - + Type.Reference _ ( [ [ "morphir" ], [ "s", "d", "k" ] ], [ [ "decimal" ] ], [ "decimal" ] ) [] -> TS.Number @@ -363,6 +363,9 @@ decoderExpression customTypeVars typeExp inputArg = Type.Reference _ ( [ [ "morphir" ], [ "s", "d", "k" ] ], [ [ "char" ] ], [ "char" ] ) [] -> { function = codecsModule "decodeChar", arguments = [ inputArg ] } + Type.Reference _ ( [ [ "morphir" ], [ "s", "d", "k" ] ], [ [ "decimal" ] ], [ "decimal" ] ) [] -> + { function = codecsModule "decodeDecimal", arguments = [ inputArg ] } + Type.Reference _ ( [ [ "morphir" ], [ "s", "d", "k" ] ], [ [ "string" ] ], [ "string" ] ) [] -> { function = codecsModule "decodeString", arguments = [ inputArg ] } @@ -687,6 +690,9 @@ encoderExpression customTypeVars typeExp valueArg = Type.Reference _ ( [ [ "morphir" ], [ "s", "d", "k" ] ], [ [ "char" ] ], [ "char" ] ) [] -> { function = codecsModule "encodeChar", arguments = [ valueArg ] } + Type.Reference _ ( [ [ "morphir" ], [ "s", "d", "k" ] ], [ [ "decimal" ] ], [ "decimal" ] ) [] -> + { function = codecsModule "encodeDecimal", arguments = [ valueArg ] } + Type.Reference _ ( [ [ "morphir" ], [ "s", "d", "k" ] ], [ [ "string" ] ], [ "string" ] ) [] -> { function = codecsModule "encodeString", arguments = [ valueArg ] }