Skip to content
This repository was archived by the owner on Oct 8, 2024. It is now read-only.

Fix display output of certain types during build #180

Merged
merged 2 commits into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
74 changes: 57 additions & 17 deletions src/transform/src/extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, TypeDefinition>();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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")) {
Expand Down Expand Up @@ -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 ["", ""];
}