From 512149a5312ae9b81c5a306de6963cbcf74a98b9 Mon Sep 17 00:00:00 2001 From: Jason Date: Mon, 26 Dec 2022 11:41:57 +0200 Subject: [PATCH] Properly support the `i32`/`Int` type in the uncrashable codegen --- package/src/GraphEntityGenTemplates.bs.js | 7 ++-- package/src/GraphEntityGenTemplates.res | 4 ++- package/src/Index.bs.js | 31 ++++++++++++++-- package/src/Index.res | 36 ++++++++++++++++--- .../validation/UncrashableValidation.bs.js | 1 + .../src/validation/UncrashableValidation.res | 1 + 6 files changed, 70 insertions(+), 10 deletions(-) diff --git a/package/src/GraphEntityGenTemplates.bs.js b/package/src/GraphEntityGenTemplates.bs.js index c8b7ba9..2c8be79 100644 --- a/package/src/GraphEntityGenTemplates.bs.js +++ b/package/src/GraphEntityGenTemplates.bs.js @@ -46,12 +46,13 @@ function getDefaultValues(typeString) { function toStringConverter(paramName, paramType) { switch (paramType) { - case "BigDecimal" : - case "BigInt" : - return "" + paramName + ".toString()"; case "Address" : case "Bytes" : return "" + paramName + ".toHex()"; + case "BigDecimal" : + case "BigInt" : + case "Int" : + return "" + paramName + ".toString()"; case "String" : return paramName; default: diff --git a/package/src/GraphEntityGenTemplates.res b/package/src/GraphEntityGenTemplates.res index f0b6e90..e1f1ad3 100644 --- a/package/src/GraphEntityGenTemplates.res +++ b/package/src/GraphEntityGenTemplates.res @@ -33,7 +33,9 @@ let toStringConverter = (paramName, paramType) => switch paramType { | "Bytes" => `${paramName}.toHex()` | "Address" => `${paramName}.toHex()` - | "BigInt" => `${paramName}.toString()` + | "Int" + | "BigInt" => + `${paramName}.toString()` | "BigDecimal" => `${paramName}.toString()` | "String" => paramName | unknownType => `"unhandled type in converter ${unknownType} - Please fix the converter"` diff --git a/package/src/Index.bs.js b/package/src/Index.bs.js index d40768e..f8b38a0 100644 --- a/package/src/Index.bs.js +++ b/package/src/Index.bs.js @@ -66,6 +66,32 @@ function getNamedType(entityAsIdString, name) { } } +function getAssemblyScriptTypeFromConfigType(configType) { + if (configType === "String") { + return "string"; + } else if (configType === "Boolean") { + return "boolean"; + } else if (configType === "BigDecimal") { + return "BigDecimal"; + } else if (configType === "Bytes") { + return "Bytes"; + } else if (configType === "Int") { + return "i32"; + } else if (configType === "BigInt") { + return "BigInt"; + } else if (configType === "constant") { + console.log("Please report a bug on github. This case shouldn't happen"); + return ""; + } else if (Belt_Option.isSome(Js_dict.get(UncrashableValidation.entitiesMap, configType))) { + return configType; + } else if (Belt_Option.isSome(Js_dict.get(UncrashableValidation.enumsMap, configType))) { + return "string"; + } else { + console.log(GraphEntityGenTemplates.unhandledTypeMessage(configType)); + return "UNHANDLED_TYPE"; + } +} + function getFieldType(_entityAsIdStringOpt, _field) { while(true) { var entityAsIdStringOpt = _entityAsIdStringOpt; @@ -266,11 +292,11 @@ function run(entityDefinitions, codegenConfigPath, outputFilePath) { var argsDefinition = Belt_Array.joinWith(Belt_Array.keep(idArgs, (function (arg) { return arg.type !== "constant"; })), ",", (function (arg) { - return "" + arg.name + ": " + arg.type + ""; + return "" + arg.name + ": " + getAssemblyScriptTypeFromConfigType(arg.type) + ""; })); var idString = Belt_Array.joinWith(idArgs, " + \"-\" + ", (function (arg) { if (arg.type !== "constant") { - return GraphEntityGenTemplates.toStringConverter(arg.name, arg.type); + return GraphEntityGenTemplates.toStringConverter(arg.name, getAssemblyScriptTypeFromConfigType(arg.type)); } else { return "\"" + arg.value + "\""; } @@ -325,6 +351,7 @@ function run(entityDefinitions, codegenConfigPath, outputFilePath) { exports.UncrashableFileNotFound = UncrashableFileNotFound; exports.setUncrashableConfigString = setUncrashableConfigString; exports.getNamedType = getNamedType; +exports.getAssemblyScriptTypeFromConfigType = getAssemblyScriptTypeFromConfigType; exports.getFieldType = getFieldType; exports.getFieldSetterType = getFieldSetterType; exports.getFieldValueToSave = getFieldValueToSave; diff --git a/package/src/Index.res b/package/src/Index.res index 743bb68..d6200c2 100644 --- a/package/src/Index.res +++ b/package/src/Index.res @@ -54,6 +54,31 @@ let getNamedType = (~entityAsIdString, name) => { } } +let getAssemblyScriptTypeFromConfigType = configType => { + switch configType { + | #String => "string" + | #Int => "i32" + | #BigInt => "BigInt" + | #Bytes => "Bytes" + | #Boolean => "boolean" + | #BigDecimal => "BigDecimal" + | #constant => + Js.log("Please report a bug on github. This case shouldn't happen") + "" + | uncaught => + let nonStandardTypeString = uncaught->Obj.magic + + if entitiesMap->Js.Dict.get(nonStandardTypeString)->Option.isSome { + nonStandardTypeString + } else if enumsMap->Js.Dict.get(nonStandardTypeString)->Option.isSome { + "string" + } else { + Js.log(unhandledTypeMessage(~uncaught)) + "UNHANDLED_TYPE" + } + } +} + let rec getFieldType = (~entityAsIdString=false, field) => switch field["kind"] { | #NamedType => field["name"]->getNamedType(~entityAsIdString) @@ -285,14 +310,17 @@ let run = (~entityDefinitions, ~codegenConfigPath, ~outputFilePath) => { ->Option.map(idArgs => { let argsDefinition = idArgs - ->Array.keep(arg => arg["type"] != "constant") - ->Array.joinWith(",", arg => `${arg["name"]}: ${arg["type"]}`) + ->Array.keep(arg => arg["type"] != #constant) + ->Array.joinWith( + ",", + arg => `${arg["name"]}: ${getAssemblyScriptTypeFromConfigType(arg["type"])}`, + ) // no string interpolation in assemblyscript :( let idString = idArgs->Array.joinWith( ` + "-" + `, arg => - if arg["type"] != "constant" { - toStringConverter(arg["name"], arg["type"]) + if arg["type"] != #constant { + toStringConverter(arg["name"], getAssemblyScriptTypeFromConfigType(arg["type"])) } else { `"${arg["value"]}"` }, diff --git a/package/src/validation/UncrashableValidation.bs.js b/package/src/validation/UncrashableValidation.bs.js index 9e24210..e235186 100644 --- a/package/src/validation/UncrashableValidation.bs.js +++ b/package/src/validation/UncrashableValidation.bs.js @@ -25,6 +25,7 @@ function confirmTypeIsSupported(argType) { case "BigInt" : case "Boolean" : case "Bytes" : + case "Int" : case "String" : case "constant" : return true; diff --git a/package/src/validation/UncrashableValidation.res b/package/src/validation/UncrashableValidation.res index 80143e9..cbed67d 100644 --- a/package/src/validation/UncrashableValidation.res +++ b/package/src/validation/UncrashableValidation.res @@ -15,6 +15,7 @@ let confirmTypeIsSupported = argType => { switch argType { | "String" | "BigInt" + | "Int" | "Bytes" | "Boolean" | "Address"