From c62d06612cb68a00ca52ceebf5ea054ac86835d1 Mon Sep 17 00:00:00 2001 From: Jason Date: Mon, 26 Dec 2022 22:54:24 +0200 Subject: [PATCH] Continuation of derivedField fix - initial move to using non-polymorphic types --- package/src/GraphEntityGenTemplates.bs.js | 39 ++++--- package/src/GraphEntityGenTemplates.res | 24 +++-- package/src/Index.bs.js | 101 +++++++++--------- package/src/Index.res | 121 +++++++++++----------- package/src/Types.bs.js | 8 ++ package/src/Types.res | 31 ++++++ 6 files changed, 177 insertions(+), 147 deletions(-) create mode 100644 package/src/Types.bs.js create mode 100644 package/src/Types.res diff --git a/package/src/GraphEntityGenTemplates.bs.js b/package/src/GraphEntityGenTemplates.bs.js index 2c8be79..442728a 100644 --- a/package/src/GraphEntityGenTemplates.bs.js +++ b/package/src/GraphEntityGenTemplates.bs.js @@ -23,24 +23,21 @@ function setFieldNameToFieldType(fieldName, fieldType) { return " " + fieldName + ": " + fieldType + ";\n"; } -function getDefaultValues(typeString) { - switch (typeString) { - case "Address" : - return "Address.fromString(\"0x0000000000000000000000000000000000000000\")"; - case "BigDecimal" : - return "BigDecimal.zero()"; - case "BigInt" : - return "BigInt.zero()"; - case "Boolean" : - return "false"; - case "Bytes" : - return "Bytes.fromHexString(\"0x00\") as Bytes"; - case "Int" : - return "0"; - case "String" : - return "\"\""; - default: - return "\"" + typeString + " - Unknown type\""; +function getDefaultValues(graphType) { + if (graphType === "Boolean") { + return "false"; + } else if (graphType === "BigDecimal") { + return "BigDecimal.zero()"; + } else if (graphType === "Bytes") { + return "Bytes.fromHexString(\"0x00\") as Bytes"; + } else if (graphType === "Address") { + return "Address.fromString(\"0x0000000000000000000000000000000000000000\")"; + } else if (graphType === "Int") { + return "0"; + } else if (graphType === "BigInt") { + return "BigInt.zero()"; + } else { + return "\"\""; } } @@ -49,12 +46,12 @@ function toStringConverter(paramName, paramType) { case "Address" : case "Bytes" : return "" + paramName + ".toHex()"; + case "String" : + return paramName; case "BigDecimal" : case "BigInt" : - case "Int" : + case "i32" : return "" + paramName + ".toString()"; - case "String" : - return paramName; default: return "\"unhandled type in converter " + paramType + " - Please fix the converter\""; } diff --git a/package/src/GraphEntityGenTemplates.res b/package/src/GraphEntityGenTemplates.res index e1f1ad3..c64c92c 100644 --- a/package/src/GraphEntityGenTemplates.res +++ b/package/src/GraphEntityGenTemplates.res @@ -1,3 +1,5 @@ +open Types + let loadNewEntityId = (~name) => ` let loaded${name} = new ${name}(entityId); ` @@ -17,23 +19,23 @@ let setFieldNameToFieldType = (~fieldName, ~fieldType) => ` ${fieldName}: ${fieldType}; ` -let getDefaultValues = typeString => - switch typeString { - | "Bytes" => `Bytes.fromHexString("0x00") as Bytes` // needs to be even length for some reason - | "Address" => `Address.fromString("0x0000000000000000000000000000000000000000")` - | "Int" => "0" - | "String" => `""` - | "BigInt" => "BigInt.zero()" - | "BigDecimal" => "BigDecimal.zero()" - | "Boolean" => "false" - | unknownType => `"${unknownType} - Unknown type"` +let getDefaultValues = (graphType: GraphSchema.graphSchemaDataTypes) => + switch graphType { + | #Bytes => `Bytes.fromHexString("0x00") as Bytes` // needs to be even length for some reason + | #Address => `Address.fromString("0x0000000000000000000000000000000000000000")` + | #Int => "0" + | #String => `""` + | #BigInt => "BigInt.zero()" + | #BigDecimal => "BigDecimal.zero()" + | #Boolean => "false" + // | unknownType => `"${unknownType->Obj.magic} - Unknown type"` } let toStringConverter = (paramName, paramType) => switch paramType { | "Bytes" => `${paramName}.toHex()` | "Address" => `${paramName}.toHex()` - | "Int" + | "i32" | "BigInt" => `${paramName}.toString()` | "BigDecimal" => `${paramName}.toString()` diff --git a/package/src/Index.bs.js b/package/src/Index.bs.js index cba0873..3958b8e 100644 --- a/package/src/Index.bs.js +++ b/package/src/Index.bs.js @@ -97,47 +97,39 @@ function getFieldType(_entityAsIdStringOpt, _field) { var entityAsIdStringOpt = _entityAsIdStringOpt; var field = _field; var entityAsIdString = entityAsIdStringOpt !== undefined ? entityAsIdStringOpt : false; - var uncaught = field.kind; - if (uncaught === "NonNullType") { - _field = field.type; - _entityAsIdStringOpt = entityAsIdString; - continue ; - } - if (uncaught === "NamedType") { + var match = field.kind; + if (match === "NamedType") { return getNamedType(entityAsIdString, field.name); } - if (uncaught === "ListType") { - var innerType = getFieldType(entityAsIdString, field.type); + if (match === "ListType") { + var innerType = getFieldType(entityAsIdString, Belt_Option.getExn(field.type)); return "Array<" + innerType + ">"; } - console.log(uncaught); - return "unknown"; + _field = Belt_Option.getExn(field.type); + _entityAsIdStringOpt = entityAsIdString; + continue ; }; } function getFieldSetterType(_field) { while(true) { var field = _field; - var uncaught = field.kind; - if (uncaught !== "NonNullType") { - if (uncaught === "NamedType") { - if (Belt_Option.isSome(Js_dict.get(UncrashableValidation.entitiesMap, field.name.value))) { - return /* Entity */1; - } else { - return /* NormalValue */0; - } - } else if (uncaught === "ListType") { - if (getFieldSetterType(field.type) === /* Entity */1) { - return /* EntityArray */2; - } else { - return /* NormalValue */0; - } + var match = field.kind; + if (match === "NamedType") { + if (Belt_Option.isSome(Js_dict.get(UncrashableValidation.entitiesMap, field.name.value))) { + return /* Entity */1; + } else { + return /* NormalValue */0; + } + } + if (match === "ListType") { + if (getFieldSetterType(Belt_Option.getExn(field.type)) === /* Entity */1) { + return /* EntityArray */2; } else { - console.log("Uncaught entity type", uncaught); return /* NormalValue */0; } } - _field = field.type; + _field = Belt_Option.getExn(field.type); continue ; }; } @@ -171,18 +163,18 @@ function getDefaultValueForType(strictMode, recersivelyCreateUncreatedEntities, function getFieldDefaultTypeNonNull(strictMode, recersivelyCreateUncreatedEntities, _field) { while(true) { var field = _field; - var uncaught = field.kind; - if (uncaught !== "NonNullType") { - if (uncaught === "NamedType") { - return getDefaultValueForType(strictMode, recersivelyCreateUncreatedEntities, field.name.value); - } else if (uncaught === "ListType") { - return "[]"; - } else { - console.log(uncaught); - return "unknown"; - } + var match = field.kind; + if (match === "NamedType") { + return getDefaultValueForType(strictMode, recersivelyCreateUncreatedEntities, field.name.value); } - _field = field.type; + if (match === "ListType") { + return "[]"; + } + var fieldType = field.type; + if (fieldType === undefined) { + return ""; + } + _field = fieldType; continue ; }; } @@ -190,17 +182,24 @@ function getFieldDefaultTypeNonNull(strictMode, recersivelyCreateUncreatedEntiti function getFieldDefaultTypeWithNull(strictModeOpt, recersivelyCreateUncreatedEntitiesOpt, field) { var strictMode = strictModeOpt !== undefined ? strictModeOpt : true; var recersivelyCreateUncreatedEntities = recersivelyCreateUncreatedEntitiesOpt !== undefined ? recersivelyCreateUncreatedEntitiesOpt : false; - var uncaught = field.kind; - if (uncaught === "NonNullType") { - return getFieldDefaultTypeNonNull(strictMode, recersivelyCreateUncreatedEntities, field.type); - } else if (uncaught === "ListType" || uncaught === "NamedType") { + var match = field.kind; + if (match === "ListType" || match === "NamedType") { return "null"; + } + var fieldType = field.type; + if (fieldType !== undefined) { + return getFieldDefaultTypeNonNull(strictMode, recersivelyCreateUncreatedEntities, fieldType); } else { - console.log(uncaught); - return "unknown"; + return ""; } } +function isFieldDerived(field) { + return Belt_Array.keep(field.directives, (function (directive) { + return directive.name.value === "derivedFrom"; + })).length !== 0; +} + function run(entityDefinitions, codegenConfigPath, outputFilePath) { var uncrashableConfigString = setUncrashableConfigString(codegenConfigPath); var uncrashableConfig = JsYaml.load(uncrashableConfigString); @@ -241,18 +240,13 @@ function run(entityDefinitions, codegenConfigPath, outputFilePath) { var functions = Belt_Array.joinWith(Belt_Array.map(Object.keys(entitiesMap), (function (entityName) { var entity = entitiesMap[entityName]; var name = entity.name.value; - var fields = entity.fields; + var fields = Belt_Array.keep(entity.fields, (function (field) { + return !isFieldDerived(field); + })); var fieldsMap = {}; Belt_Array.map(fields, (function (field) { var fieldName = field.name.value; - var isDerivedFromField = Belt_Array.keep(field.directives, (function (directive) { - return directive.name.value === "derivedFrom"; - })).length !== 0; - if (!isDerivedFromField) { - fieldsMap[fieldName] = field; - return ; - } - + fieldsMap[fieldName] = field; })); var entityConfig = Belt_Option.getWithDefault(Js_dict.get(uncrashableConfig.entitySettings, name), { useDefault: {}, @@ -365,5 +359,6 @@ exports.getFieldValueToSave = getFieldValueToSave; exports.getDefaultValueForType = getDefaultValueForType; exports.getFieldDefaultTypeNonNull = getFieldDefaultTypeNonNull; exports.getFieldDefaultTypeWithNull = getFieldDefaultTypeWithNull; +exports.isFieldDerived = isFieldDerived; exports.run = run; /* Not a pure module */ diff --git a/package/src/Index.res b/package/src/Index.res index 4c34f8f..3fe620b 100644 --- a/package/src/Index.res +++ b/package/src/Index.res @@ -2,6 +2,7 @@ open GraphEntityGenTemplates open UncrashableValidation +open Types exception UncrashableFileNotFound(string) @@ -28,8 +29,8 @@ type enumItem type interfaceItem type entityItem -let getNamedType = (~entityAsIdString, name) => { - switch name["value"] { +let getNamedType = (~entityAsIdString, name: GraphSchema.schemaValueType) => { + switch name.value { | #String => "string" | #Int => "i32" | #BigInt => "BigInt" @@ -79,61 +80,55 @@ let getAssemblyScriptTypeFromConfigType = configType => { } } -let rec getFieldType = (~entityAsIdString=false, field) => - switch field["kind"] { - | #NamedType => field["name"]->getNamedType(~entityAsIdString) +let rec getFieldType = (~entityAsIdString=false, field: GraphSchema.entityFieldType) => + switch field.kind { + | #NamedType => field.name->getNamedType(~entityAsIdString) | #ListType => - let innerType = field["type"]->getFieldType(~entityAsIdString) + let innerType = field._type->Option.getExn->getFieldType(~entityAsIdString) `Array<${innerType}>` | #NonNullType => - let innerType = field["type"]->getFieldType(~entityAsIdString) + let innerType = field._type->Option.getExn->getFieldType(~entityAsIdString) innerType - | uncaught => - Js.log(uncaught) - "unknown" } type fieldType = NormalValue | Entity | EntityArray -let rec getFieldSetterType = field => - switch field["kind"] { +let rec getFieldSetterType = (field: GraphSchema.entityFieldType) => + switch field.kind { | #NamedType => - if entitiesMap->Js.Dict.get(field["name"]["value"])->Option.isSome { + if entitiesMap->Js.Dict.get(field.name.value :> string)->Option.isSome { Entity } else { NormalValue } - | #ListType => field["type"]->getFieldSetterType == Entity ? EntityArray : NormalValue - | #NonNullType => field["type"]->getFieldSetterType - | uncaught => - Js.log2("Uncaught entity type", uncaught) - NormalValue + | #ListType => field._type->Option.getExn->getFieldSetterType == Entity ? EntityArray : NormalValue + | #NonNullType => field._type->Option.getExn->getFieldSetterType } //all the same at the moment, ie users can pass the type for base parameters but for all //entity types and arrays of entities, the id as a string must be used. //Better yet, derivedFrom should be used for arrays of entities if possible -let getFieldValueToSave = (nameOfObject, field) => { - switch field["type"]->getFieldSetterType { - | NormalValue => `${nameOfObject}.${field["name"]["value"]}` - | EntityArray => `(${nameOfObject}.${field["name"]["value"]})` - | Entity => `${nameOfObject}.${field["name"]["value"]}` +let getFieldValueToSave = (nameOfObject, field: GraphSchema.entityField) => { + switch field._type->getFieldSetterType { + | NormalValue => `${nameOfObject}.${field.name.value}` + | EntityArray => `(${nameOfObject}.${field.name.value})` + | Entity => `${nameOfObject}.${field.name.value}` } } -let getDefaultValueForType = (~strictMode, ~recersivelyCreateUncreatedEntities, typeName) => { +let getDefaultValueForType = (~strictMode, ~recersivelyCreateUncreatedEntities, typeName: Types.GraphSchema.graphSchemaDataTypes) => { entitiesMap - ->Js.Dict.get(typeName) + ->Js.Dict.get(typeName :> string) ->Option.mapWithDefault( enumsMap - ->Js.Dict.get(typeName) + ->Js.Dict.get(typeName :> string) ->Option.mapWithDefault(typeName->getDefaultValues, enum => { `"${((enum->Obj.magic)["values"]->Array.getUnsafe(0))["name"]["value"]}"` }), _entityType => recersivelyCreateUncreatedEntities - ? `"UNINITIALISED - ${typeName}"` - : `getOrInitialize${typeName}Default("UNINITIALISED - ${typeName}", ${strictMode + ? `"UNINITIALISED - ${typeName :> string}"` + : `getOrInitialize${typeName :> string}Default("UNINITIALISED - ${typeName :> string}", ${strictMode ? "true" : "false"}).id`, ) @@ -141,33 +136,45 @@ let getDefaultValueForType = (~strictMode, ~recersivelyCreateUncreatedEntities, type entityIdPrefix = {networks: array, prefix: string} -let rec getFieldDefaultTypeNonNull = (~strictMode, ~recersivelyCreateUncreatedEntities, field) => - switch field["kind"] { +let rec getFieldDefaultTypeNonNull = (~strictMode, ~recersivelyCreateUncreatedEntities, field: Types.GraphSchema.entityFieldType) => + switch field.kind { | #ListType => "[]" | #NonNullType => // This case sholud be impossible... - field["type"]->getFieldDefaultTypeNonNull(~strictMode, ~recersivelyCreateUncreatedEntities) + switch (field._type) { + | Some(fieldType) => fieldType->getFieldDefaultTypeNonNull(~strictMode, ~recersivelyCreateUncreatedEntities) + | None => "" + } | #NamedType => - field["name"]["value"]->getDefaultValueForType(~strictMode, ~recersivelyCreateUncreatedEntities) - | uncaught => - Js.log(uncaught) - "unknown" + field.name.value->getDefaultValueForType(~strictMode, ~recersivelyCreateUncreatedEntities) } let getFieldDefaultTypeWithNull = ( ~strictMode=true, ~recersivelyCreateUncreatedEntities=false, - field, + field: GraphSchema.entityFieldType, ) => - switch field["kind"] { + switch field.kind { | #NonNullType => - field["type"]->getFieldDefaultTypeNonNull(~strictMode, ~recersivelyCreateUncreatedEntities) + switch (field._type) { + | Some(fieldType) => + fieldType->getFieldDefaultTypeNonNull(~strictMode, ~recersivelyCreateUncreatedEntities) + | None => "" + } | #ListType | #NamedType => "null" - | uncaught => - Js.log(uncaught) - "unknown" } + +let isFieldDerived = (field: GraphSchema.entityField) => + {field.directives + ->Array.keep( + directive => { + directive.name.value == "derivedFrom" + }, + ) + ->Array.length > 0 + } + let run = (~entityDefinitions, ~codegenConfigPath, ~outputFilePath) => { let uncrashableConfigString = setUncrashableConfigString(~codegenConfigPath) @@ -230,23 +237,13 @@ let run = (~entityDefinitions, ~codegenConfigPath, ~outputFilePath) => { let entity = entitiesMap->Js.Dict.unsafeGet(entityName)->Obj.magic let name = entity["name"]["value"] - let fields = entity["fields"] + let fields: array = entity["fields"]->Array.keep((field: GraphSchema.entityField) => !isFieldDerived(field)) let fieldsMap = Js.Dict.empty() let _ = fields->Array.map(field => { - let fieldName = field["name"]["value"] - let isDerivedFromField = - field["directives"] - ->Array.keep( - directive => { - directive["name"]["value"] == "derivedFrom" - }, - ) - ->Array.length > 0 + let fieldName = field.name.value - // NOTE: currently we completely ignore derived fields - if !isDerivedFromField { fieldsMap->Js.Dict.set(fieldName, field) - } + }) let entityConfig = uncrashableConfig["entitySettings"] @@ -256,14 +253,14 @@ let run = (~entityDefinitions, ~codegenConfigPath, ~outputFilePath) => { let fieldDefaultSettersStrict = fields ->Array.map(field => { - let fieldName = field["name"]["value"] + let fieldName = field.name.value fieldName == "id" ? "" : setInitializeFieldValue( ~name, ~fieldName, - ~fieldValue=field["type"]->getFieldDefaultTypeWithNull( + ~fieldValue=field._type->getFieldDefaultTypeWithNull( ~recersivelyCreateUncreatedEntities=true, ), ) @@ -275,7 +272,7 @@ let run = (~entityDefinitions, ~codegenConfigPath, ~outputFilePath) => { let fieldInitialValueSettersStrict = fields ->Array.map(field => { - let fieldName = field["name"]["value"] + let fieldName = field.name.value if fieldName == "id" { loadNewEntityId(~name) @@ -292,15 +289,15 @@ let run = (~entityDefinitions, ~codegenConfigPath, ~outputFilePath) => { setInitializeFieldValue( ~name, ~fieldName, - ~fieldValue=field["type"]->getFieldDefaultTypeWithNull(~strictMode=false), + ~fieldValue=field._type->getFieldDefaultTypeWithNull(~strictMode=false), ), ) } }) ->Array.joinWith("\n", a => a) - let fieldToFieldTyping = field => { - let fieldName = field["name"]["value"] + let fieldToFieldTyping = (field: GraphSchema.entityField) => { + let fieldName = field.name.value if fieldName == "id" { "" } else { @@ -309,7 +306,7 @@ let run = (~entityDefinitions, ~codegenConfigPath, ~outputFilePath) => { ->Option.mapWithDefault( setFieldNameToFieldType( ~fieldName, - ~fieldType=field["type"]->getFieldType(~entityAsIdString=true), + ~fieldType=field._type->getFieldType(~entityAsIdString=true), ), _ => "", ) @@ -363,7 +360,7 @@ let run = (~entityDefinitions, ~codegenConfigPath, ~outputFilePath) => { fieldDefinition => setField( ~field, - ~fieldValue=fieldDefinition["type"]->getFieldType( + ~fieldValue=fieldDefinition._type->getFieldType( ~entityAsIdString=true, ), ), diff --git a/package/src/Types.bs.js b/package/src/Types.bs.js new file mode 100644 index 0000000..f19e696 --- /dev/null +++ b/package/src/Types.bs.js @@ -0,0 +1,8 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE +'use strict'; + + +var GraphSchema = {}; + +exports.GraphSchema = GraphSchema; +/* No side effect */ diff --git a/package/src/Types.res b/package/src/Types.res new file mode 100644 index 0000000..b7104cd --- /dev/null +++ b/package/src/Types.res @@ -0,0 +1,31 @@ +module GraphSchema = { + type schemaValue = { + value: string + } + type graphSchemaDataTypes = [ | #String + | #Int + | #BigInt + | #Address + | #Bytes + | #Boolean + | #BigDecimal] + type schemaValueType = { + value: graphSchemaDataTypes + } + type entityFieldTypeKind = [ | #NonNullType + | #ListType + | #NamedType] + type rec entityFieldType = { + kind: entityFieldTypeKind, + name: schemaValueType, + @as("type") _type: option + } + type schemaLabel = { + name: schemaValue, + } + type entityField = { + name: schemaValue, + @as("type") _type: entityFieldType, + directives: array + } +}