Skip to content

Commit

Permalink
Fix API2 generation
Browse files Browse the repository at this point in the history
  • Loading branch information
DamianReeves committed Sep 5, 2024
1 parent 7f6451a commit 32ea183
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
35 changes: 25 additions & 10 deletions redistributable/TypeScript/morphir/internal/Codecs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -24,6 +24,7 @@ type CodecFunction = (input: any) => any;

type CodecList = Array<CodecFunction>;
type CodecMap = Map<string, CodecFunction>;
type Decimal = number;

// Construct a codec map, avoiding spurious type errors.
//
Expand Down Expand Up @@ -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}`);
Expand All @@ -83,11 +91,14 @@ export function decodeFloat(input: any): number {
return input;
}

export function decodeMaybe<T>(decodeElement: (any) => T, input: any): T | null {
export function decodeMaybe<T>(
decodeElement: (any) => T,
input: any
): T | null {
if (input == null) {
return null
return null;
} else {
return decodeElement(input)
return decodeElement(input);
}
}
export function decodeDict<K, V>(
Expand All @@ -101,7 +112,6 @@ export function decodeDict<K, V>(

const inputArray: Array<any> = input;


return new Map(
inputArray.map((item: any) => {
if (!(item instanceof Array)) {
Expand Down Expand Up @@ -135,13 +145,14 @@ export function decodeRecord<recordType>(

const fieldNames: Array<string> = 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
}`
);
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -224,9 +239,9 @@ export function encodeFloat(value: number): number {

export function encodeMaybe<T>(encodeElement: (any) => T, value: T | null) {
if (value == null) {
return null
return null;
} else {
return encodeElement(value)
return encodeElement(value);
}
}
export function encodeDict<K, V>(
Expand Down Expand Up @@ -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}.`
);
}
8 changes: 7 additions & 1 deletion src/Morphir/TypeScript/Backend/Types.elm
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 ] }

Expand Down Expand Up @@ -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 ] }

Expand Down

0 comments on commit 32ea183

Please sign in to comment.