Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api) refactor api for ergonomics and type safety #703

Merged
merged 11 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
1 change: 0 additions & 1 deletion MAINTAINERS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ The maintainers are listed in alphabetical order.

- Matt Roberts ([mttrbrts](https://github.com/mttrbrts))
- Daniel Selman ([dselman](https://github.com/dselman))
- Jerome Simeon ([jeromesimeon](https://github.com/jeromesimeon))

## License <a name="license"></a>
Accord Project Project source code files are made available under the Apache License, Version 2.0 (Apache-2.0), located in the LICENSE file. Hyperledger Project documentation files are made available under the Creative Commons Attribution 4.0 International License (CC-BY-4.0), available at http://creativecommons.org/licenses/by/4.0/.
7 changes: 6 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions packages/concerto-analysis/src/compare-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* limitations under the License.
*/

import { ClassDeclaration, Declaration, EnumValueDeclaration, Field, MapDeclaration, ScalarDeclaration, NumberValidator, Property, RelationshipDeclaration, StringValidator, Validator } from '@accordproject/concerto-core';
import { ClassDeclaration, Declaration, EnumValue, Field, MapDeclaration, ScalarDeclaration, NumberValidator, Property, RelationshipProperty, StringValidator, Validator } from '@accordproject/concerto-core';

export function getDeclarationType(declaration: Declaration) {
if (declaration instanceof ClassDeclaration) {
Expand Down Expand Up @@ -44,9 +44,9 @@ export function getDeclarationType(declaration: Declaration) {
export function getPropertyType(property: Property) {
if (property instanceof Field) {
return 'field';
} else if (property instanceof RelationshipDeclaration) {
} else if (property instanceof RelationshipProperty) {
return 'relationship';
} else if (property instanceof EnumValueDeclaration) {
} else if (property instanceof EnumValue) {
return 'enum value';
} else {
throw new Error(`unknown property type "${property}"`);
Expand Down
8 changes: 4 additions & 4 deletions packages/concerto-analysis/src/comparers/map-declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ const mapDeclarationTypeChanged: ComparerFactory = (context) => ({
return;
}

if(a.getKey().getType() !== b.getKey().getType()) {
if(a.getKey().getMapKeyType() !== b.getKey().getMapKeyType()) {
context.report({
key: 'map-key-type-changed',
message: `The map key type was changed to "${b.getKey().getType()}"`,
message: `The map key type was changed to "${b.getKey().getMapKeyType()}"`,
element: b
});
}

if(a.getValue().getType() !== b.getValue().getType()) {
if(a.getValue().getMapValueType() !== b.getValue().getMapValueType()) {
context.report({
key: 'map-value-type-changed',
message: `The map value type was changed to "${b.getValue().getType()}"`,
message: `The map value type was changed to "${b.getValue().getMapValueType()}"`,
element: b
});
}
Expand Down
8 changes: 4 additions & 4 deletions packages/concerto-analysis/src/comparers/properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* limitations under the License.
*/

import { EnumValueDeclaration, Field, ModelUtil } from '@accordproject/concerto-core';
import { EnumValue, Field, ModelUtil } from '@accordproject/concerto-core';
import * as semver from 'semver';
import { getDeclarationType, getPropertyType, getValidatorType } from '../compare-utils';
import { ComparerFactory } from '../comparer';
Expand All @@ -23,7 +23,7 @@ const propertyAdded: ComparerFactory = (context) => ({
return;
}
const classDeclarationType = getDeclarationType(b.getParent());
if (b instanceof EnumValueDeclaration) {
if (b instanceof EnumValue) {
context.report({
key: 'enum-value-added',
message: `The enum value "${b.getName()}" was added to the ${classDeclarationType} "${b.getParent().getName()}"`,
Expand Down Expand Up @@ -59,7 +59,7 @@ const propertyRemoved: ComparerFactory = (context) => ({
return;
}
const classDeclarationType = getDeclarationType(a.getParent());
if (a instanceof EnumValueDeclaration) {
if (a instanceof EnumValue) {
context.report({
key: 'enum-value-removed',
message: `The enum value "${a.getName()}" was removed from the ${classDeclarationType} "${a.getParent().getName()}"`,
Expand Down Expand Up @@ -104,7 +104,7 @@ const propertyTypeChanged: ComparerFactory = (context) => ({
element: a
});
return;
} else if (a instanceof EnumValueDeclaration || b instanceof EnumValueDeclaration) {
} else if (a instanceof EnumValue || b instanceof EnumValue) {
return;
}
const aIsArray = a.isArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ const scalarDeclarationExtendsChanged: ComparerFactory = (context) => ({
return;
}

if(a.getType() !== b.getType()) {
if(a.getScalarType() !== b.getScalarType()) {
context.report({
key: 'scalar-extends-changed',
message: `The scalar extends was changed from "${a.getType()}" to "${b.getType()}"`,
message: `The scalar extends was changed from "${a.getScalarType()}" to "${b.getScalarType()}"`,
element: b.getName()
});
}
Expand Down
7 changes: 5 additions & 2 deletions packages/concerto-analysis/test/unit/compare-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import { getDeclarationType, getPropertyType, getValidatorType } from '../../src

const modelManager = new ModelManager();
const propertyAst = {
$class: '[email protected]',
name: 'myProp',
type: 'Boolean'
};
const modelAst = {
$class: '[email protected]',
name: 'Unknown',
namespace: '[email protected]',
properties: []
};
Expand All @@ -19,7 +22,7 @@ const field = new Field(classDeclaration, propertyAst);
const validator = new Validator(field, {});

test('should throw for unknown class declaration type', () => {
expect(() => getDeclarationType(classDeclaration)).toThrow('unknown class declaration type "ClassDeclaration {[email protected].undefined super=Concept enum=false abstract=false}"');
expect(() => getDeclarationType(classDeclaration)).toThrow('unknown class declaration type "ClassDeclaration {[email protected].Unknown super=Concept declarationKind=UnknownDeclaration abstract=false idField=null}"');
});

test('should throw for unknown thing', () => {
Expand All @@ -28,7 +31,7 @@ test('should throw for unknown thing', () => {
});

test('should throw for unknown class property type', () => {
expect(() => getPropertyType(property)).toThrow('unknown property type "[object Object]');
expect(() => getPropertyType(property)).toThrow('unknown property type "BooleanProperty {[email protected]}"');
});

test('should throw for unknown validator type', () => {
Expand Down
50 changes: 45 additions & 5 deletions packages/concerto-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ import DecoratorFactory = require("./introspect/decoratorfactory");
import DecoratorManager = require("./decoratormanager");
import Declaration = require("./introspect/declaration");
import ClassDeclaration = require("./introspect/classdeclaration");
import IdentifiedDeclaration = require("./introspect/identifieddeclaration");
import AssetDeclaration = require("./introspect/assetdeclaration");
import ConceptDeclaration = require("./introspect/conceptdeclaration");
import EnumValueDeclaration = require("./introspect/enumvaluedeclaration");
import EnumDeclaration = require("./introspect/enumdeclaration");
import EventDeclaration = require("./introspect/eventdeclaration");
import ParticipantDeclaration = require("./introspect/participantdeclaration");
import TransactionDeclaration = require("./introspect/transactiondeclaration");
Expand All @@ -41,8 +40,8 @@ import MapKeyType = require("./introspect/mapkeytype");
import MapValueType = require("./introspect/mapvaluetype");
import Property = require("./introspect/property");
import Field = require("./introspect/field");
import EnumDeclaration = require("./introspect/enumdeclaration");
import RelationshipDeclaration = require("./introspect/relationshipdeclaration");
import RelationshipProperty = require("./introspect/relationshipproperty");
import EnumValue = require("./introspect/enumvalue");
import Validator = require("./introspect/validator");
import NumberValidator = require("./introspect/numbervalidator");
import StringValidator = require("./introspect/stringvalidator");
Expand All @@ -64,4 +63,45 @@ export type version = {
name: string;
version: string;
};
export { SecurityException, IllegalModelException, TypeNotFoundException, MetamodelException, Decorator, DecoratorFactory, DecoratorManager, Declaration, ClassDeclaration, IdentifiedDeclaration, AssetDeclaration, ConceptDeclaration, EnumValueDeclaration, EventDeclaration, ParticipantDeclaration, TransactionDeclaration, ScalarDeclaration, MapDeclaration, MapKeyType, MapValueType, Property, Field, EnumDeclaration, RelationshipDeclaration, Validator, NumberValidator, StringValidator, Typed, Identifiable, Relationship, Resource, Factory, Globalize, Introspector, ModelFile, ModelManager, Serializer, ModelUtil, ModelLoader, DateTimeUtil, MetaModel };
export {
SecurityException,
IllegalModelException,
TypeNotFoundException,
MetamodelException,
Decorator,
DecoratorFactory,
DecoratorManager,
Declaration,
ClassDeclaration,
AssetDeclaration,
ConceptDeclaration,
EnumDeclaration,
EventDeclaration,
ParticipantDeclaration,
TransactionDeclaration,
ScalarDeclaration,
MapDeclaration,
MapKeyType,
MapValueType,
Property,
Field,
RelationshipProperty,
EnumValue,
Validator,
NumberValidator,
StringValidator,
Typed,
Identifiable,
Relationship,
Resource,
Factory,
Globalize,
Introspector,
ModelFile,
ModelManager,
Serializer,
ModelUtil,
ModelLoader,
DateTimeUtil,
MetaModel
};
13 changes: 2 additions & 11 deletions packages/concerto-core/src/introspect/assetdeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

'use strict';

const IdentifiedDeclaration = require('./identifieddeclaration');
const ClassDeclaration = require('./classdeclaration');

// Types needed for TypeScript generation.
/* eslint-disable no-unused-vars */
Expand All @@ -35,7 +35,7 @@ if (global === undefined) {
* @class
* @memberof module:concerto-core
*/
class AssetDeclaration extends IdentifiedDeclaration {
class AssetDeclaration extends ClassDeclaration {
/**
* Create an AssetDeclaration.
* @param {ModelFile} modelFile the ModelFile for this class
Expand All @@ -45,15 +45,6 @@ class AssetDeclaration extends IdentifiedDeclaration {
constructor(modelFile, ast) {
super(modelFile, ast);
}

/**
* Returns the kind of declaration
*
* @return {string} what kind of declaration this is
*/
declarationKind() {
return 'AssetDeclaration';
}
}

module.exports = AssetDeclaration;
Loading
Loading