Skip to content
Open
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
4 changes: 2 additions & 2 deletions sdks/ts/packages/golem-ts-sdk/src/typescriptTypeRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
// A wrapper over golem-ts-types-core/TypeMetadata to be used from user's code
// for them to register its types with the SDk.

import { TypeMetadata } from '@golemcloud/golem-ts-types-core';
import { TypeMetadata, MetadataJSON } from '@golemcloud/golem-ts-types-core';

export const TypescriptTypeRegistry = {
register(typeMetadata: any): void {
register(typeMetadata: MetadataJSON): void {
TypeMetadata.loadFromJson(typeMetadata);
},
};
8 changes: 5 additions & 3 deletions sdks/ts/packages/golem-ts-typegen/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ import {
} from 'ts-morph';
import {
buildJSONFromType,
ClassMetadataJSON,
LiteTypeJSON,
MethodMetadataJSON,
Node,
Symbol,
Type,
Expand Down Expand Up @@ -709,15 +711,15 @@ export function saveAndClearInMemoryMetadata() {
fs.mkdirSync(METADATA_DIR);
}

const json: Record<string, any> = {};
const json: Record<string, ClassMetadataJSON> = {};

for (const [className, meta] of TypeMetadata.getAll().entries()) {
const constructorArgsJSON = meta.constructorArgs.map((arg) => ({
name: arg.name,
type: buildJSONFromType(arg.type),
}));

const methodsObj: Record<string, any> = {};
const methodsObj: Record<string, MethodMetadataJSON> = {};
for (const [methodName, { methodParams, returnType }] of meta.methods) {
const paramsJSON: Record<string, LiteTypeJSON> = {};
for (const [paramName, paramType] of methodParams.entries()) {
Expand All @@ -739,7 +741,7 @@ export function saveAndClearInMemoryMetadata() {
const tsFilePath = path.join(METADATA_DIR, METADATA_TS_FILE);
const jsonFilePath = path.join(METADATA_DIR, METADATA_JSON_FILE);

const tsContent = `export const Metadata = ${JSON.stringify(json, null, 2)};`;
const tsContent = `import type { MetadataJSON } from '@golemcloud/golem-ts-types-core';\n\nexport const Metadata = ${JSON.stringify(json, null, 2)} as const satisfies MetadataJSON;`;
const jsonContent = JSON.stringify(json, null, 2);

fs.writeFileSync(tsFilePath, tsContent, 'utf-8');
Expand Down
32 changes: 22 additions & 10 deletions sdks/ts/packages/golem-ts-types-core/src/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@ export type ClassMetadata = {
methods: Map<MethodNameString, { methodParams: MethodParams; returnType: Type }>;
};

export type MethodMetadataJSON = {
methodParams: Record<string, LiteTypeJSON>;
returnType: LiteTypeJSON;
};

export type ClassMetadataJSON = {
constructorArgs: Array<{
name: string;
type: LiteTypeJSON;
}>;
methods: Record<string, MethodMetadataJSON>;
};

export type MetadataJSON = Record<ClassNameString, ClassMetadataJSON>;

const Metadata = new Map<ClassNameString, ClassMetadata>();

export const TypeMetadata = {
Expand Down Expand Up @@ -60,14 +75,11 @@ export const TypeMetadata = {
Metadata.clear();
},

// TODO: avoid any. Here any simply represents the json representation of Metadata
// Represents the json representation of Metadata
// such that every Type is represented as LiteTypeJSON
loadFromJson(json: any) {
loadFromJson(json: MetadataJSON) {
for (const [className, meta] of Object.entries(json)) {
const constructorArgsJSON = (meta as any).constructorArgs as Array<{
name: string;
type: LiteTypeJSON;
}>;
const constructorArgsJSON = meta.constructorArgs;

const constructorArgs = constructorArgsJSON.map((arg) => ({
name: arg.name,
Expand All @@ -76,15 +88,15 @@ export const TypeMetadata = {

const methodsMap = new Map<string, { methodParams: Map<string, Type>; returnType: Type }>();

for (const [methodName, methodMeta] of Object.entries((meta as any).methods)) {
for (const [methodName, methodMeta] of Object.entries(meta.methods)) {
const methodParamsMap = new Map<string, Type>();
for (const [paramName, paramJSON] of Object.entries((methodMeta as any).methodParams)) {
methodParamsMap.set(paramName, buildTypeFromJSON(paramJSON as LiteTypeJSON));
for (const [paramName, paramJSON] of Object.entries(methodMeta.methodParams)) {
methodParamsMap.set(paramName, buildTypeFromJSON(paramJSON));
}

methodsMap.set(methodName, {
methodParams: methodParamsMap,
returnType: buildTypeFromJSON((methodMeta as any).returnType as LiteTypeJSON),
returnType: buildTypeFromJSON(methodMeta.returnType),
});
}

Expand Down
2 changes: 1 addition & 1 deletion sdks/ts/packages/golem-ts-types-core/src/type-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export type LiteTypeJSON =
types: LiteTypeJSON[];
typeParams: LiteTypeJSON[];
optional: boolean;
originalTypeName: string | undefined;
originalTypeName?: string | undefined;
}
| { kind: 'literal'; name?: string; literalValue?: string; optional: boolean }
| {
Expand Down
Loading