Skip to content

Commit

Permalink
feat(types): build uniontypes too (#765)
Browse files Browse the repository at this point in the history
* feat(types): build uniontypes too

Signed-off-by: Matt Roberts <code@rbrts.uk>

* fix(build): include unions in index

Signed-off-by: Matt Roberts <code@rbrts.uk>

* chore(deps): upgrade codegen to latest release

Signed-off-by: Matt Roberts <code@rbrts.uk>

---------

Signed-off-by: Matt Roberts <code@rbrts.uk>
  • Loading branch information
mttrbrts committed Nov 24, 2023
1 parent 5b72c19 commit fc35a40
Show file tree
Hide file tree
Showing 10 changed files with 7,392 additions and 103 deletions.
6,768 changes: 6,673 additions & 95 deletions packages/concerto-types/package-lock.json

Large diffs are not rendered by default.

26 changes: 21 additions & 5 deletions packages/concerto-types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"pretest": "npm-run-all licchk lint",
"lint": "eslint .",
"licchk": "license-check-and-add",
"test": "echo no tests"
"test": "jest",
"test:watch": "jest --watchAll"
},
"repository": {
"type": "git",
Expand All @@ -32,14 +33,19 @@
"author": "accordproject.org",
"license": "Apache-2.0",
"devDependencies": {
"@accordproject/concerto-codegen": "3.14.0",
"@accordproject/concerto-codegen": "3.17.0",
"@accordproject/concerto-core": "3.15.0",
"@accordproject/concerto-metamodel": "3.9.0",
"@accordproject/concerto-util": "3.15.0",
"@types/jest": "28.1.1",
"ajv": "8.12.0",
"eslint": "8.2.0",
"jest": "28.1.1",
"license-check-and-add": "2.3.6",
"npm-run-all": "4.1.5",
"rimraf": "3.0.2",
"ts-jest": "28.0.4",
"ts-json-schema-generator": "1.4.0",
"typescript": "4.6.3"
},
"license-check-and-add-config": {
Expand All @@ -58,9 +64,7 @@
".nyc-output",
"out",
".tern-project",
"./generated/concerto.metamodel@1.0.0.ts",
"./generated/concerto.ts",
"./generated/concerto@1.0.0.ts"
"./generated/"
],
"file_type_method": "EXCLUDE",
"file_types": [
Expand All @@ -87,5 +91,17 @@
"file": "./HEADER.md"
}
}
},
"jest": {
"preset": "ts-jest",
"testEnvironment": "node",
"testPathIgnorePatterns": [
"<rootDir>/dist/",
"/node_modules/"
],
"collectCoverage": true,
"collectCoverageFrom": [
"src/**/*.ts"
]
}
}
7 changes: 5 additions & 2 deletions packages/concerto-types/scripts/codegen.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ const path = require('path');
async function main() {
const modelManager = await ModelLoader.loadModelManagerFromModelFiles([metaModelCto], {strict: true});
const visitor = new TypescriptVisitor();

const fileWriter = new FileWriter(path.resolve(__dirname, '..', 'src', 'generated'));
const parameters = { fileWriter };
modelManager.accept(visitor, parameters);
modelManager.accept(visitor, { fileWriter });

const fileWriter2 = new FileWriter(path.resolve(__dirname, '..', 'src', 'generated/unions'));
modelManager.accept(visitor, { fileWriter: fileWriter2, flattenSubclassesToUnion: true });
}

main().catch(error => {
Expand Down
57 changes: 57 additions & 0 deletions packages/concerto-types/src/concerto.metamodel.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { ModelManager, ModelFile, Concerto } from '@accordproject/concerto-core';
const { MetaModelUtil, MetaModelNamespace } = require('@accordproject/concerto-metamodel');
import { createGenerator } from "ts-json-schema-generator";
import Ajv from "ajv";
import path from "path";
import fs from 'fs';

test('Chained TypeScript and JSONSchema conversion respects inheritance when flattening subclasses to a union type', () => {

// TS to JSON Schema conversion
const config = {
path: path.resolve('../src/generated/union/concerto-metamodel@1.0.0.ts'),
tsconfig: path.resolve('../concerto-types/tsconfig.json'),
type: 'IDecorator',
}
const jsonSchema = createGenerator(config).createSchema(config.type);

const staticJsonSchemaResult = JSON.parse(fs.readFileSync('src/fixtures/decorator.jsonschema.json', 'utf8'));
expect(jsonSchema).toStrictEqual(staticJsonSchemaResult);

// Test instance
const data = {
$class: `concerto.metamodel@1.0.0.Decorator`,
name: 'displayName',
arguments: [
{
value: 'Account ID',
$class: `concerto.metamodel@1.0.0.DecoratorString`,
}
],
}

// Validate the instance with Ajv, a JSON Schema validator
const ajv = new Ajv()
const validate = ajv.compile(jsonSchema)
const valid = validate(data)
expect(validate.errors).toBeNull();

// Validate the instance with Concerto
const modelManager = new ModelManager();
modelManager.addModelFile(new ModelFile(modelManager, MetaModelUtil.metaModelAst, undefined, MetaModelNamespace));
const concerto = new Concerto(modelManager);
expect(() => concerto.validate(data)).not.toThrow();
});
193 changes: 193 additions & 0 deletions packages/concerto-types/src/fixtures/decorator.jsonschema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$ref": "#/definitions/IDecorator",
"definitions": {
"IDecorator": {
"type": "object",
"properties": {
"$class": {
"type": "string"
},
"name": {
"type": "string"
},
"arguments": {
"type": "array",
"items": {
"$ref": "#/definitions/DecoratorLiteralUnion"
}
},
"location": {
"$ref": "#/definitions/IRange"
}
},
"required": [
"$class",
"name"
],
"additionalProperties": false
},
"DecoratorLiteralUnion": {
"anyOf": [
{
"$ref": "#/definitions/IDecoratorString"
},
{
"$ref": "#/definitions/IDecoratorNumber"
},
{
"$ref": "#/definitions/IDecoratorBoolean"
},
{
"$ref": "#/definitions/IDecoratorTypeReference"
}
]
},
"IDecoratorString": {
"type": "object",
"properties": {
"$class": {
"type": "string"
},
"location": {
"$ref": "#/definitions/IRange"
},
"value": {
"type": "string"
}
},
"required": [
"$class",
"value"
],
"additionalProperties": false
},
"IRange": {
"type": "object",
"properties": {
"$class": {
"type": "string"
},
"start": {
"$ref": "#/definitions/IPosition"
},
"end": {
"$ref": "#/definitions/IPosition"
},
"source": {
"type": "string"
}
},
"required": [
"$class",
"end",
"start"
],
"additionalProperties": false
},
"IPosition": {
"type": "object",
"properties": {
"$class": {
"type": "string"
},
"line": {
"type": "number"
},
"column": {
"type": "number"
},
"offset": {
"type": "number"
}
},
"required": [
"$class",
"column",
"line",
"offset"
],
"additionalProperties": false
},
"IDecoratorNumber": {
"type": "object",
"properties": {
"$class": {
"type": "string"
},
"location": {
"$ref": "#/definitions/IRange"
},
"value": {
"type": "number"
}
},
"required": [
"$class",
"value"
],
"additionalProperties": false
},
"IDecoratorBoolean": {
"type": "object",
"properties": {
"$class": {
"type": "string"
},
"location": {
"$ref": "#/definitions/IRange"
},
"value": {
"type": "boolean"
}
},
"required": [
"$class",
"value"
],
"additionalProperties": false
},
"IDecoratorTypeReference": {
"type": "object",
"properties": {
"$class": {
"type": "string"
},
"location": {
"$ref": "#/definitions/IRange"
},
"type": {
"$ref": "#/definitions/ITypeIdentifier"
},
"isArray": {
"type": "boolean"
}
},
"required": [
"$class",
"isArray",
"type"
],
"additionalProperties": false
},
"ITypeIdentifier": {
"type": "object",
"properties": {
"$class": {
"type": "string"
},
"name": {
"type": "string"
},
"namespace": {
"type": "string"
}
},
"required": [
"$class",
"name"
],
"additionalProperties": false
}
}
}

0 comments on commit fc35a40

Please sign in to comment.