From 5686dfa6988d0d9714bd5d7395df9bd19f5a9200 Mon Sep 17 00:00:00 2001 From: Matt Johnson-Pint Date: Fri, 6 Sep 2024 20:34:10 -0700 Subject: [PATCH 1/2] Fix display of nullable types and maps --- src/transform/src/extractor.ts | 74 ++++++++++++++++++++++++++-------- 1 file changed, 57 insertions(+), 17 deletions(-) diff --git a/src/transform/src/extractor.ts b/src/transform/src/extractor.ts index 6fbef9a..1829b6d 100644 --- a/src/transform/src/extractor.ts +++ b/src/transform/src/extractor.ts @@ -64,7 +64,7 @@ export class Extractor { .flatMap((f) => f.parameters.map((p) => p.type).concat(f.results[0]?.type), ) - .map((p) => p?.replace(/\|null$/, "")), + .map((p) => makeNonNullable(p)), ); const typesUsed = new Map(); @@ -101,10 +101,7 @@ export class Extractor { // include fields if (type.fields) { type.fields.forEach((f) => { - let path = f.type; - if (path.endsWith("|null")) { - path = path.slice(0, -5); - } + const path = makeNonNullable(f.type); const typeDef = allTypes.get(path); if (typeDef) { dependentTypes.add(typeDef); @@ -222,30 +219,26 @@ interface importExportInfo { } export function getTypeName(path: string): string { - const isNullable = path.endsWith("|null"); - if (path.startsWith("~lib/array/Array")) { const type = getTypeName( path.slice(path.indexOf("<") + 1, path.lastIndexOf(">")), ); - if (isNullable) return "(" + type + ")[]"; + if (isNullable(type)) { + return "(" + type + ")[]"; + } return type + "[]"; } - if (isNullable) - return getTypeName(path.slice(0, path.length - 5)) + " | null"; + if (isNullable(path)) { + return makeNullable(getTypeName(makeNonNullable(path))); + } const name = typeMap.get(path); if (name) return name; if (path.startsWith("~lib/map/Map")) { - const firstType = getTypeName( - path.slice(path.indexOf("<") + 1, path.indexOf(",")), - ); - const secondType = getTypeName( - path.slice(path.indexOf(",") + 1, path.lastIndexOf(">")), - ); - return "Map<" + firstType + ", " + secondType + ">"; + const [keyType, valueType] = getMapSubtypes(path); + return "Map<" + getTypeName(keyType) + ", " + getTypeName(valueType) + ">"; } if (path.startsWith("~lib/@hypermode")) { @@ -299,3 +292,50 @@ export function getLiteral(node: Expression | null): JsonLiteral { } return ""; } + +const nullableTypeRegex = /\s?\|\s?null$/; + +function isNullable(type: string) { + return nullableTypeRegex.test(type); +} + +function makeNonNullable(type?: string) { + return type?.replace(nullableTypeRegex, ""); +} + +function makeNullable(type?: string) { + if (isNullable(type)) { + return type; + } else { + return type + " | null"; + } +} + +function getMapSubtypes(type: string): [string, string] { + const prefix = "~lib/map/Map<"; + if (!type.startsWith(prefix)) { + return ["", ""]; + } + + let n = 1; + let c = 0; + for (let i = prefix.length; i < type.length; i++) { + switch (type.charAt(i)) { + case "<": + n++; + break; + case ",": + if (n == 1) { + c = i; + } + break; + case ">": + n--; + if (n == 0) { + return [type.slice(prefix.length, c), type.slice(c + 1, i)]; + } + } + } + + return ["", ""]; +} From 924334282eef3645099d1c345b72e9772b426bcf Mon Sep 17 00:00:00 2001 From: Matt Johnson-Pint Date: Fri, 6 Sep 2024 20:39:05 -0700 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5cd85d5..b2206fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,15 +3,16 @@ ## UNRELEASED - Update to v2 Hypermode metadata format [#176](https://github.com/hypermodeAI/functions-as/pull/176) +- Fix display output of certain types during build [#180](https://github.com/hypermodeAI/functions-as/pull/180) ## 2024-08-27 - Version 0.11.2 -- redo dgraph fns to use single execute host function [#171](https://github.com/hypermodeAI/functions-as/pull/171) +- Redo Dgraph host functions implementation [#171](https://github.com/hypermodeAI/functions-as/pull/171) - Add get vector and search by vector functions to collections [#172](https://github.com/hypermodeAI/functions-as/pull/172) ## 2024-08-13 - Version 0.11.1 -- Add dgraph support with host functions [#165](https://github.com/hypermodeAI/functions-as/pull/165) +- Add Dgraph support with host functions [#165](https://github.com/hypermodeAI/functions-as/pull/165) ## 2024-08-12 - Version 0.11.0