Skip to content

Commit 2cfa8b4

Browse files
committed
fix type name generation
1 parent 950fe03 commit 2cfa8b4

2 files changed

Lines changed: 29 additions & 54 deletions

File tree

apps/events/src/utils/convertTypesData.test.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it } from 'vitest';
2-
import { convertTypesDataToSchemaAndMapping, generateMappingFile, generateSchemaFile } from './convertTypesData';
2+
import { convertTypesDataToSchemaAndMapping, generateSchemaFile } from './convertTypesData';
33

44
describe('convertTypesData', () => {
55
it('should convert typesData to schema and mapping', () => {
@@ -18,14 +18,6 @@ describe('convertTypesData', () => {
1818
expect(schema).toContain('export class');
1919
});
2020

21-
it('should generate mapping file with proper imports', () => {
22-
const mapping = generateMappingFile();
23-
24-
expect(mapping).toContain("import { Id } from '@graphprotocol/grc-20';");
25-
expect(mapping).toContain("import type { Mapping } from '@graphprotocol/hypergraph';");
26-
expect(mapping).toContain('export const mapping: Mapping = {');
27-
});
28-
2921
it('should handle relation properties correctly', () => {
3022
const result = convertTypesDataToSchemaAndMapping();
3123

apps/events/src/utils/convertTypesData.ts

Lines changed: 28 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ export function convertTypesDataToSchemaAndMapping() {
140140
const mappingProperties: Record<string, string> = {};
141141
const mappingRelations: Record<string, string> = {};
142142

143+
console.log('type.properties', type.properties);
144+
143145
for (const property of type.properties) {
144146
const propertyName = toCamelCase(property.entity.name);
145147

@@ -151,8 +153,17 @@ export function convertTypesDataToSchemaAndMapping() {
151153
mappingRelations[propertyName] = `Id.Id('${property.id}')`;
152154
}
153155
} else {
156+
console.log('property.dataType', property.dataType);
154157
const typeInstance = dataTypeToType(property.dataType);
155-
const typeName = typeInstance.name.endsWith('$') ? typeInstance.name.slice(0, -1) : typeInstance.name;
158+
let typeName = typeInstance.name.endsWith('$') ? typeInstance.name.slice(0, -1) : typeInstance.name;
159+
160+
// Replace String with Text and Boolean with Checkbox
161+
if (typeName === 'String') {
162+
typeName = 'Text';
163+
} else if (typeName === 'Boolean') {
164+
typeName = 'Checkbox';
165+
}
166+
156167
properties.push(` ${propertyName}: Type.${typeName}`);
157168
mappingProperties[propertyName] = `Id.Id('${property.id}')`;
158169
}
@@ -188,47 +199,6 @@ ${schema}
188199
`;
189200
}
190201

191-
// Function to generate the complete mapping file content
192-
export function generateMappingFile(): string {
193-
const { mapping } = convertTypesDataToSchemaAndMapping();
194-
195-
const mappingEntries = Object.entries(mapping)
196-
.map(([className, mappingData]) => {
197-
const properties = Object.entries(mappingData.properties || {})
198-
.map(([key, value]) => ` ${key}: ${value}`)
199-
.join(',\n');
200-
201-
const relations = mappingData.relations
202-
? Object.entries(mappingData.relations)
203-
.map(([key, value]) => ` ${key}: ${value}`)
204-
.join(',\n')
205-
: '';
206-
207-
return ` ${className}: {
208-
typeIds: [${mappingData.typeIds.join(', ')}],
209-
properties: {
210-
${properties}
211-
},
212-
${
213-
relations
214-
? ` relations: {
215-
${relations}
216-
},`
217-
: ''
218-
}
219-
}`;
220-
})
221-
.join(',\n\n');
222-
223-
return `import { Id } from '@graphprotocol/grc-20';
224-
import type { Mapping } from '@graphprotocol/hypergraph';
225-
226-
export const mapping: Mapping = {
227-
${mappingEntries}
228-
};
229-
`;
230-
}
231-
232202
// Function to generate schema for a single type
233203
export function generateSchemaForType(type: TypeData): string {
234204
const className = createClassName(type.name);
@@ -245,9 +215,22 @@ export function generateSchemaForType(type: TypeData): string {
245215
properties.push(` ${propertyName}: Type.Relation(${targetClass})`);
246216
}
247217
} else {
248-
const typeInstance = dataTypeToType(property.dataType);
249-
const typeName = typeInstance.name.endsWith('$') ? typeInstance.name.slice(0, -1) : typeInstance.name;
250-
properties.push(` ${propertyName}: Type.${typeName}`);
218+
let typeName = '';
219+
if (property.dataType === 'TEXT') {
220+
typeName = 'Type.Text';
221+
} else if (property.dataType === 'NUMBER') {
222+
typeName = 'Type.Number';
223+
} else if (property.dataType === 'CHECKBOX') {
224+
typeName = 'Type.Checkbox';
225+
} else if (property.dataType === 'POINT') {
226+
typeName = 'Type.Point';
227+
} else if (property.dataType === 'TIME') {
228+
typeName = 'Type.Date';
229+
} else {
230+
throw new Error(`Unknown data type: ${property.dataType}`);
231+
}
232+
233+
properties.push(` ${propertyName}: ${typeName}`);
251234
}
252235
}
253236

0 commit comments

Comments
 (0)