@accordproject/concerto-tools
Advanced tools
Comparing version 3.0.1-20221129111555 to 3.0.1-20221202102312
@@ -18,3 +18,2 @@ /* | ||
const Writer = require('@accordproject/concerto-util').Writer; | ||
const TypedStack = require('@accordproject/concerto-util').TypedStack; | ||
const Ajv2019 = require('ajv/dist/2019'); | ||
@@ -36,2 +35,4 @@ const Ajv2020 = require('ajv/dist/2020'); | ||
const REGEX_ESCAPED_CHARS = /[\s\\.-]/g; | ||
/** | ||
@@ -49,3 +50,3 @@ * Remove whitespace and periods from a Type identifier | ||
// Replace delimiters with underscore | ||
.replace(/[\s\\.-]/g, '_') | ||
.replace(REGEX_ESCAPED_CHARS, '_') | ||
); | ||
@@ -90,5 +91,4 @@ } | ||
const name = context.parents.peek(); | ||
const { type } = parseIdUri(definition.$id) || | ||
{ type: definition.title || name }; | ||
const name = context.parents.slice(-1).pop(); | ||
const { type } = parseIdUri(definition.$id) || { type: name }; | ||
@@ -98,2 +98,14 @@ if (skipDictionary || context.dictionary.has(normalizeType(type))){ | ||
} | ||
// We've found an inline sub-schema | ||
if (definition.properties || definition.enum){ | ||
const subSchemaName = context.parents | ||
.map(normalizeType) | ||
.join('_'); | ||
// Come back to this later | ||
context.jobs.push({ name: subSchemaName, definition }); | ||
return subSchemaName; | ||
} | ||
// We fallback to a stringified object representation. This is "untyped". | ||
@@ -111,3 +123,3 @@ return 'String'; | ||
function inferType(definition, context) { | ||
const name = context.parents.peek(); | ||
const name = context.parents.slice(-1).pop(); | ||
if (definition.$ref) { | ||
@@ -117,3 +129,3 @@ // Recursive defintion | ||
const top = context.parents.pop(); | ||
const parent = context.parents.peek(); | ||
const parent = context.parents.slice(-1).pop(); | ||
context.parents.push(top); | ||
@@ -126,3 +138,2 @@ return parent; | ||
// TODO Also add local sub-schema definition | ||
if (definition.enum) { | ||
@@ -186,5 +197,12 @@ return inferTypeName(definition, context); | ||
definition.enum.forEach((value) => { | ||
let normalizedValue = value; | ||
// Concerto does not allow enum values to start with numbers or values such as `true` | ||
// If we can relax the parser rules, this branch could be removed | ||
if (typeof normalizedValue !== 'string' || normalizedValue.match(/^\d/)){ | ||
normalizedValue = `_${normalizedValue}`; | ||
} | ||
normalizedValue = normalizedValue.replace(REGEX_ESCAPED_CHARS, '_'); | ||
writer.writeLine( | ||
1, | ||
`o ${value}` | ||
`o ${normalizedValue}` | ||
); | ||
@@ -239,3 +257,3 @@ }); | ||
} else if (type === 'String' && propertyDefinition.pattern) { | ||
validator = ` regex=/${propertyDefinition.pattern}/`; | ||
validator = ` regex=/${propertyDefinition.pattern.replace(/\//g, '\\/')}/`; | ||
} | ||
@@ -268,3 +286,3 @@ | ||
function inferDeclaration(definition, context) { | ||
const name = context.parents.peek(); | ||
const name = context.parents.slice(-1).pop(); | ||
@@ -329,5 +347,6 @@ if (definition.enum) { | ||
const context = { | ||
parents: new TypedStack(), | ||
parents: new Array(), // Track ancestors in the tree | ||
writer: new Writer(), | ||
dictionary: new Set(), | ||
dictionary: new Set(), // Track types that we've seen before | ||
jobs: new Array(), // Queue of inline definitions to come-back to | ||
}; | ||
@@ -370,2 +389,11 @@ | ||
// Generate declarations for all inline sub-schemas | ||
while(context.jobs.length > 0){ | ||
const job = context.jobs.pop(); | ||
context.parents.push(job.name); | ||
context.dictionary.add(job.name); | ||
inferDeclaration(job.definition, context); | ||
context.parents.pop(); | ||
} | ||
return context.writer.getBuffer(); | ||
@@ -372,0 +400,0 @@ } |
{ | ||
"name": "@accordproject/concerto-tools", | ||
"version": "3.0.1-20221129111555", | ||
"version": "3.0.1-20221202102312", | ||
"description": "Tools for the Concerto Modeling Language", | ||
@@ -68,4 +68,4 @@ "homepage": "https://github.com/accordproject/concerto", | ||
"dependencies": { | ||
"@accordproject/concerto-core": "3.0.1-20221129111555", | ||
"@accordproject/concerto-util": "3.0.1-20221129111555", | ||
"@accordproject/concerto-core": "3.0.1-20221202102312", | ||
"@accordproject/concerto-util": "3.0.1-20221202102312", | ||
"ajv": "8.10.0", | ||
@@ -72,0 +72,0 @@ "ajv-formats": "2.1.1", |
/*! | ||
* Concerto Tools v3.0.1-20221129111555 | ||
* Concerto Tools v3.0.1-20221202102312 | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
@@ -4,0 +4,0 @@ * you may not use this file except in compliance with the License. |
Sorry, the diff of this file is too big to display
1481959
6797
+ Added@accordproject/concerto-core@3.0.1-20221202102312(transitive)
+ Added@accordproject/concerto-cto@3.0.1-20221202102312(transitive)
+ Added@accordproject/concerto-metamodel@3.0.1-20221202102312(transitive)
+ Added@accordproject/concerto-util@3.0.1-20221202102312(transitive)
- Removed@accordproject/concerto-core@3.0.1-20221129111555(transitive)
- Removed@accordproject/concerto-cto@3.0.1-20221129111555(transitive)
- Removed@accordproject/concerto-metamodel@3.0.1-20221129111555(transitive)
- Removed@accordproject/concerto-util@3.0.1-20221129111555(transitive)