Skip to content

Commit

Permalink
fix: Put schema files into a Map to allow direct access with a schema…
Browse files Browse the repository at this point in the history
… name

Following up KaotoIO#148
  • Loading branch information
igarashitm committed Sep 25, 2023
1 parent 46729b0 commit 82628e7
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 124 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ dist
**/cypress/videos/*

.vscode/*
.idea/*
**/*.iml

# storybook
storybook-static
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,8 @@ protected ObjectNode getIndex() throws Exception {
}

protected ObjectNode getSchema(String name) throws Exception {
var index = getIndex();
for (JsonNode schema : index.withArray("schemas")) {
var fileName = schema.get("file").asText();
var tokens = fileName.split("-");
if ("camelYamlDsl".equals(tokens[0]) && name.isEmpty() && tokens[1].matches("\\d+.*")) {
return (ObjectNode) jsonMapper.readTree(Paths.get("..").resolve("dist").resolve(fileName).toFile());
} else if ("camelYamlDsl".equals(tokens[0]) && tokens[1].equals(name)) {
return (ObjectNode) jsonMapper.readTree(Paths.get("..").resolve("dist").resolve(fileName).toFile());
}
}
return null;
var schema = getIndex().withObject("/schemas").withObject("/" + name);
return (ObjectNode) jsonMapper.readTree(
Paths.get("..").resolve("dist").resolve(schema.get("file").asText()).toFile());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class CamelYamlDslSchemaTest extends CamelCatalogTestSupport {

@Test
public void testRootSchema() throws Exception {
var rootSchema = getSchema("");
var rootSchema = getSchema("camelYamlDsl");
assertEquals(rootSchema.get("type").asText(), "array");
var definitions = rootSchema.withObject("/items").withObject("/definitions");
assertTrue(definitions.has("org.apache.camel.model.ProcessorDefinition"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,25 @@ public void test() throws Exception {
assertTrue(catalogs.has("kamelets"));
assertTrue(catalogs.has("dataformats"));
assertTrue(index.has("schemas"));
var schemas = index.withArray("/schemas");
List<String> names = new ArrayList<>();
schemas.forEach(schema -> names.add(schema.get("name").asText()));
assertTrue(names.contains("camelYamlDsl"));
assertTrue(names.contains("beans"));
assertTrue(names.contains("errorHandler"));
assertTrue(names.contains("from"));
assertTrue(names.contains("intercept"));
assertTrue(names.contains("interceptFrom"));
assertTrue(names.contains("interceptSendToEndpoint"));
assertTrue(names.contains("onCompletion"));
assertTrue(names.contains("onException"));
assertTrue(names.contains("rest"));
assertTrue(names.contains("restConfiguration"));
assertTrue(names.contains("route"));
assertTrue(names.contains("routeConfiguration"));
assertTrue(names.contains("routeTemplate"));
assertTrue(names.contains("templatedRoute"));
assertTrue(names.contains("Integration"));
assertTrue(names.contains("Kamelet"));
assertTrue(names.contains("KameletBinding"));
assertTrue(names.contains("Pipe"));
var schemas = index.withObject("/schemas");
assertTrue(schemas.has("camelYamlDsl"));
assertTrue(schemas.has("beans"));
assertTrue(schemas.has("errorHandler"));
assertTrue(schemas.has("from"));
assertTrue(schemas.has("intercept"));
assertTrue(schemas.has("interceptFrom"));
assertTrue(schemas.has("interceptSendToEndpoint"));
assertTrue(schemas.has("onCompletion"));
assertTrue(schemas.has("onException"));
assertTrue(schemas.has("rest"));
assertTrue(schemas.has("restConfiguration"));
assertTrue(schemas.has("route"));
assertTrue(schemas.has("routeConfiguration"));
assertTrue(schemas.has("routeTemplate"));
assertTrue(schemas.has("templatedRoute"));
assertTrue(schemas.has("Integration"));
assertTrue(schemas.has("Kamelet"));
assertTrue(schemas.has("KameletBinding"));
assertTrue(schemas.has("Pipe"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ public class Index {

private Map<String, Entry> catalogs = new HashMap<>();

private List<Entry> schemas = new ArrayList<>();
private Map<String, Entry> schemas = new HashMap<>();

public Map<String, Entry> getCatalogs() {
return catalogs;
}
public List<Entry> getSchemas() {
public Map<String, Entry> getSchemas() {
return schemas;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ private void processSchema(Path inputDir, Index index) {
"Camel YAML DSL JSON schema",
camelVersion,
outputFileName);
index.getSchemas().add(indexEntry);
index.getSchemas().put("camelYamlDsl", indexEntry);

try {
var rootSchema = (ObjectNode) jsonMapper.readTree(schema.toFile());
Expand Down Expand Up @@ -186,7 +186,7 @@ private void processSubSchema(
"Camel YAML DSL JSON schema: " + propName,
camelVersion,
outputFileName);
index.getSchemas().add(indexEntry);
index.getSchemas().put(propName, indexEntry);
} catch (Exception e) {
getLog().error(e);
}
Expand Down Expand Up @@ -326,7 +326,7 @@ private void processCRDFile(Path file, Index index) {
description,
camelKCRDVersion,
outputFileName);
index.getSchemas().add(indexEntry);
index.getSchemas().put(name, indexEntry);
} catch (Exception e) {
getLog().error(e);
}
Expand Down
57 changes: 24 additions & 33 deletions packages/camel-catalog/src/json-schema-to-typescript.mts
Original file line number Diff line number Diff line change
Expand Up @@ -53,44 +53,35 @@ async function main() {
const exportedFiles: string[] = [];

console.log('---');
const schemaPromises = index.schemas.map(async (schema) => {
const schemaFile = resolve(`./dist/${schema.file}`);

/**
* In windows, path starting with C:\ are not supported
* We need to add file:// to the path to make it work
* [pathToFileURL](https://nodejs.org/api/url.html#url_url_pathtofileurl_path)
* Related issue: https://github.com/nodejs/node/issues/31710
*/
const schemaFileUri = pathToFileURL(`./dist/${schema.file}`).toString();

const schemaContent = (await import(schemaFileUri, { assert: { type: 'json' } })).default;

let filename = schema.name;

if (schema.file.includes('camelYamlDsl')) {
if (schema.file.match(/camelYamlDsl-\d+.*\.json/)) {
addTitleToDefinitions(schemaContent);
filename = 'camelYamlDsl';
} else {
return;
}
}
const schema = index.schemas.camelYamlDsl;
const schemaFile = resolve(`./dist/${schema.file}`);

/** Remove the -4.0.0.json section of the filename */
const outputFile = resolve(`./dist/types/${filename}.d.ts`);
/**
* In windows, path starting with C:\ are not supported
* We need to add file:// to the path to make it work
* [pathToFileURL](https://nodejs.org/api/url.html#url_url_pathtofileurl_path)
* Related issue: https://github.com/nodejs/node/issues/31710
*/
const schemaFileUri = pathToFileURL(`./dist/${schema.file}`).toString();

/** Add the file to the exported files */
exportedFiles.push(filename);
const schemaContent = (await import(schemaFileUri, { assert: { type: 'json' } })).default;

console.log(`Input: '${schemaFile}'`);
console.log(`Output: ${outputFile}`);
console.log('---');
let filename = schema.name;

return compileSchema(schemaContent, filename, outputFile);
});
addTitleToDefinitions(schemaContent);
filename = 'camelYamlDsl';

/** Remove the -4.0.0.json section of the filename */
const outputFile = resolve(`./dist/types/${filename}.d.ts`);

/** Add the file to the exported files */
exportedFiles.push(filename);

console.log(`Input: '${schemaFile}'`);
console.log(`Output: ${outputFile}`);
console.log('---');

await Promise.all(schemaPromises);
await compileSchema(schemaContent, filename, outputFile);

/** Generate the index file */
const indexFile = resolve(`./dist/types/index.ts`);
Expand Down
66 changes: 38 additions & 28 deletions packages/ui/src/camel-utils/camel-schemas-processor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,41 @@ describe('camel-schemas-processor', () => {
},
};

const schemas = [
{
name: 'user-schema',
version: '3.2.0',
uri: 'https://camel.apache.org/schema/user-schema.json',
tags: [],
schema: userSchemaJson,
},
];
const schemas = {
'userSchema':
{
name: 'userSchema',
version: '3.2.0',
uri: 'https://camel.apache.org/schema/user-schema.json',
tags: [],
schema: userSchemaJson,
}
};

it('should return a list of schemas', () => {
it('should return a map of schemas', () => {
const result = CamelSchemasProcessor.getSchemas(schemas);

expect(result).toHaveLength(1);
expect(result[0].name).toEqual('user-schema');
expect(result[0].version).toEqual('3.2.0');
expect(result[0].tags).toEqual([]);
expect(result[0].uri).toEqual('https://camel.apache.org/schema/user-schema.json');
expect(result[0].schema).toEqual(userSchemaJson);
expect(Object.keys(result)).toHaveLength(1);
expect(result.userSchema.name).toEqual('userSchema');
expect(result.userSchema.version).toEqual('3.2.0');
expect(result.userSchema.tags).toEqual([]);
expect(result.userSchema.uri).toEqual('https://camel.apache.org/schema/user-schema.json');
expect(result.userSchema.schema).toEqual(userSchemaJson);
});

it('should return a list of schemas for camel schemas', () => {
const result = CamelSchemasProcessor.getSchemas([{ ...schemas[0], name: "Camel YAML DSL JSON schema", schema: camelSchema }]);
const expected = [
const result = CamelSchemasProcessor.getSchemas(
{
'userSchema' :
{
...schemas.userSchema,
name: "Camel YAML DSL JSON schema",
schema: camelSchema
}
}
);
const expected = {
'userSchema': {
name: 'Camel YAML DSL JSON schema',
schema: {
$schema: 'http://json-schema.org/draft-07/schema#',
Expand All @@ -73,26 +83,26 @@ describe('camel-schemas-processor', () => {
uri: 'https://camel.apache.org/schema/user-schema.json',
version: '3.2.0',
},
];
};

expect(result).toEqual(expected);
});

it.each([
[
'Beans',
{ ...schemas[0], schema: { ...camelSchema, items: { ...camelSchema.items, properties: { beans: {} } } } },
{ ...schemas.userSchema, schema: { ...camelSchema, items: { ...camelSchema.items, properties: { beans: {} } } } },
[],
],
['userSchema', { ...schemas[0], schema: userSchemaJson }, []],
['Route', { ...schemas[0], name: 'route', schema: camelSchema }, ['visualization']],
['Integration', { ...schemas[0], name: 'Integration' }, ['visualization']],
['Kamelet', { ...schemas[0], name: 'Kamelet' }, ['visualization']],
['KameletBinding', { ...schemas[0], name: 'KameletBinding' }, ['visualization']],
['Pipe', { ...schemas[0], name: 'Pipe' }, ['visualization']],
['userSchema', { ...schemas.userSchema, schema: userSchemaJson }, []],
['Route', { ...schemas.userSchema, name: 'route', schema: camelSchema }, ['visualization']],
['Integration', { ...schemas.userSchema, name: 'Integration' }, ['visualization']],
['Kamelet', { ...schemas.userSchema, name: 'Kamelet' }, ['visualization']],
['KameletBinding', { ...schemas.userSchema, name: 'KameletBinding' }, ['visualization']],
['Pipe', { ...schemas.userSchema, name: 'Pipe' }, ['visualization']],
])('should return a list of schemas with a tag property for: %s', (_name, schema, tags) => {
const result = CamelSchemasProcessor.getSchemas([schema]);
const result = CamelSchemasProcessor.getSchemas({name: schema});

expect(result[0].tags).toEqual(tags);
expect(result.name.tags).toEqual(tags);
});
});
10 changes: 5 additions & 5 deletions packages/ui/src/camel-utils/camel-schemas-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ export const DEFAULT_CATALOG_PATH = '/camel-catalog';
export class CamelSchemasProcessor {
static readonly VISUAL_FLOWS = ['route', 'Integration', 'Kamelet', 'KameletBinding', 'Pipe'];

static getSchemas(schemas: Schema[]): Schema[] {
return schemas.reduce((acc, schema) => {
static getSchemas(schemaMap: {[key: string]: Schema}): {[key: string]: Schema} {
return Object.entries(schemaMap).reduce((acc, [key, schema]) => {
/** Standard JSON Schemas (Kamelets, KameletBindings & Pipes) */
const tags = [];
if (this.VISUAL_FLOWS.includes(schema.name)) {
tags.push('visualization');
}

acc.push({
acc[key] = {
...schema,
name: schema.name,
tags,
});
};

return acc;
}, [] as Schema[]);
}, {} as {[key: string]: Schema});
}

}
3 changes: 1 addition & 2 deletions packages/ui/src/components/SourceCode/SourceCode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ interface SourceCodeProps {

export const SourceCode: FunctionComponent<SourceCodeProps> = (props) => {
const editorRef = useRef<Parameters<EditorDidMount>[0] | null>(null);
const schemas = useSchemasStore((state) => state.schemas);
const camelYamlDslSchema = schemas.find(s => s.name === 'camelYamlDsl');
const camelYamlDslSchema = useSchemasStore((state) => state.schemas.camelYamlDsl);

useEffect(() => {
setDiagnosticsOptions({
Expand Down
27 changes: 15 additions & 12 deletions packages/ui/src/providers/catalog-schema-loader.provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ export const CatalogSchemaLoaderProvider: FunctionComponent<PropsWithChildren> =

const schemaFiles = getSchemasFiles(catalogIndex.schemas);

Promise.all([camelComponentsFiles, camelProcessorsFiles, kameletsFiles, Promise.all(schemaFiles)]).then(
Promise.all([camelComponentsFiles, camelProcessorsFiles, kameletsFiles, schemaFiles]).then(
([camelComponents, camelProcessors, kamelets, schemas]) => {
setCatalog(CatalogKind.Component, camelComponents.body);
setCatalog(CatalogKind.Processor, camelProcessors.body);
setCatalog(CatalogKind.Kamelet, kamelets.body);

CamelSchemasProcessor.getSchemas(schemas).forEach(setSchema);
Object.entries(CamelSchemasProcessor.getSchemas(schemas))
.forEach(([key, schema]) => setSchema(key, schema));

setIsLoading(false);
},
Expand All @@ -53,16 +54,18 @@ async function fetchFile(file: string) {
return { body, uri: response.url };
}

function getSchemasFiles(schemaFiles: CatalogEntry[]): Promise<Schema>[] {
return schemaFiles.map(async (schemaDef) => {
function getSchemasFiles(schemaFiles: CatalogEntry[]): Promise<{[key: string]: Schema}> {
const answer: any = {};
Object.entries(schemaFiles).forEach(async ([schemaName, schemaDef]) => {
const fetchedSchema = await fetchFile(schemaDef.file);

return {
name: schemaDef.name,
tags: [],
version: schemaDef.version,
uri: fetchedSchema.uri,
schema: fetchedSchema.body,
};
answer[schemaName] =
{
name: schemaDef.name,
tags: [],
version: schemaDef.version,
uri: fetchedSchema.uri,
schema: fetchedSchema.body,
};
});
return Promise.resolve(answer);
}
Loading

0 comments on commit 82628e7

Please sign in to comment.