Concertino - 一个轻量级 Concerto 格式转换器
Concertino是Concerto元模格式的一种轻量级变体,被优化为客户端应用程序,这些应用程序需要在不依赖完整Concerto SDK的情况下进行程序内向声明.
音乐会格式的好处
Concertino格式提供了几个优点:
-
平分结构:通过使继承等级非正常化,消费者不需要穿越等级来理解一个定义.
-
更明确的元数据:额外的元数据使它更清楚地说明应该如何解释属性.
-
准备消费:转换准备数据,以便工具和渲染器更容易地消费,而不需要额外处理.
特征
- 完全解析后类型的引用: 无模糊的简称
- 平板声明: 声明不嵌入模型,从而便于类型检索
- 非正态属性: 继承属性完全非正常化,并注明来源类型
- 扩展继承链:概念列出其完整的继承链,而不仅仅是直系父母
- Scalar 类型非正常化: 为方便客户端应用程序
- 通过默认严格模式:命名空间总是被版本.
- 对部分模型的支持 允许客户端应用过滤模型,以定制有效载荷用于其使用案例.
- 无损失转换 Concertino被设计为100%无损失的圆通转换与Concerto模型.
音乐会 (Metamodel) → Concertino → Concerto (Metamodel) (英语)
安装
npm install @accordproject/concertino
使用量
使用协奏曲类
const { ModelManager } = require('@accordproject/concerto-core');
const { ConcertinoConverter } = require('@accordproject/concertino');
// Prepare the Concerto model
const mm = new ModelManager();
mm.addModel(MODEL_FILE_CONTENTS);
const model = mm.getModelFile(MODEL_FILE_NAMESPACE).getAst();
// Initialize Concertino
const converter = new ConcertinoConverter();
const models = { models: [model] };
// Convert from Concerto metamodel to Concertino
const concertino = converter.fromConcertoMetamodel(models);
console.log('#### Concertino:')
console.log(JSON.stringify(concertino, null, 2));
console.log();
// Convert from Concertino back to Concerto metamodel
const metamodel = converter.toConcertoMetamodel(concertino);
console.log('#### Concerto AST (Full Metamodel Instance):')
console.log(JSON.stringify(metamodel, null, 2));
对照 Concertino 计划检查文档
converter.isValid(concertino) 检查文档与 concertino.schema.json,以及 converter.getValidationErrors() 返回上次检查(jav出错对象)出错,或 null。 。 。 .
计划检查是在构建时编译的,所以软件包不需要 ajv 也没有 new Function (严格的内容-安全政策是好的)。 要检查没有转换器的文档,请导入 ./schema 子路径 :
const { isValid, checkSchema } = require('@accordproject/concertino/schema');
isValid(concertino); // true or false
checkSchema(concertino); // null, or the errors
convertToConcertino 财务报告和审定财务报表 convertToMetamodel 由他们自己放假进口 计划支票从捆绑.
模型大小
尽管元数据已经非正统化,但由于一种受宠若惊,字典式的设计以及去除类型歧视者(即"相克"),"相克"(Concerto AST)模型的JSON系列化在尺寸上往往要小于它们的"相克"等同. $class 属性).
预计大量使用遗产的模型将大于同等的Concerto AST.
请注意,在转换模型时,源 Concerto 模型中的命名空间应当被完全解决(包括用于本地类型引用).
示例音乐会 JSON 格式
以下是一个简单的 Concerto 模型如何在 Concertino 格式中体现的例子:
{
"declarations": {
"[email protected]": {
"properties": {
"city": {
"name": "city",
"type": "String",
"vocabulary": {
"label": "City/Town",
},
},
"country": {
"name": "country",
"type": "String",
},
"street": {
"name": "street",
"type": "String",
"vocabulary": {
"label": "Street Address",
},
},
"zipCode": {
"name": "zipCode",
"type": "String",
},
},
"type": "ConceptDeclaration",
"vocabulary": {
"label": "Physical Address",
},
},
"[email protected]": {
"regex": "/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$/",
"type": "StringScalar",
},
"[email protected]": {
"properties": {
"address": {
"isOptional": true,
"name": "address",
"type": "[email protected]",
"vocabulary": {
"label": "Mailing Address",
},
},
"age": {
"isOptional": true,
"name": "age",
"range": [
0,
null,
],
"type": "Integer",
},
"email": {
"isOptional": true,
"metadata": {
"sensitive": null,
},
"name": "email",
"regex": "/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$/",
"scalarType": "[email protected]",
"type": "String",
},
"firstName": {
"name": "firstName",
"type": "String",
"vocabulary": {
"label": "Given Name",
},
},
"lastName": {
"name": "lastName",
"type": "String",
"vocabulary": {
"label": "Family Name",
},
},
},
"type": "ConceptDeclaration",
"vocabulary": {
"additionalTerms": {
"plural": "People",
},
"label": "Individual",
},
},
},
"metadata": {
"concertinoVersion": "4.0.0-alpha.2",
"models": {
"[email protected]": {
"concertoVersion": "1.0.0",
"sourceUri": "org/example/models.cto",
"decorators": [
{
"name": "license",
"arguments": ["Apache-2.0"]
}
]
}
}
}
}
等同的 Concerto CTO 文件为:
@license("Apache-2.0")
namespace [email protected]
scalar Email extends String regex=/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
@Term("Individual")
@Term_plural("People")
concept Person {
@Term("Given Name")
o String firstName
@Term("Family Name")
o String lastName
o Integer age optional range=[0, ]
@sensitive
o Email email optional
@Term("Mailing Address")
o Address address optional
}
@Term("Physical Address")
concept Address {
@Term("Street Address")
o String street
@Term("City/Town")
o String city
o String zipCode
o String country
}