Files
languageservices/workflow-parser/src/templates/schema/sequence-definition.ts
T

54 lines
1.8 KiB
TypeScript
Raw Normal View History

2023-01-09 19:02:19 -05:00
import {DEFINITION, SEQUENCE, ITEM_TYPE} from "../template-constants";
import {MappingToken} from "../tokens";
import {Definition} from "./definition";
import {DefinitionType} from "./definition-type";
import {TemplateSchema} from "./template-schema";
export class SequenceDefinition extends Definition {
2023-01-09 19:02:19 -05:00
public itemType = "";
public constructor(key: string, definition?: MappingToken) {
2023-01-09 19:02:19 -05:00
super(key, definition);
if (definition) {
for (const definitionPair of definition) {
2023-01-09 19:02:19 -05:00
const definitionKey = definitionPair.key.assertString(`${DEFINITION} key`);
switch (definitionKey.value) {
case SEQUENCE: {
2023-01-09 19:02:19 -05:00
const mapping = definitionPair.value.assertMapping(`${DEFINITION} ${SEQUENCE}`);
for (const mappingPair of mapping) {
2023-01-09 19:02:19 -05:00
const mappingKey = mappingPair.key.assertString(`${DEFINITION} ${SEQUENCE} key`);
switch (mappingKey.value) {
case ITEM_TYPE: {
2023-01-09 19:02:19 -05:00
const itemType = mappingPair.value.assertString(`${DEFINITION} ${SEQUENCE} ${ITEM_TYPE}`);
this.itemType = itemType.value;
break;
}
default:
// throws
2023-01-09 19:02:19 -05:00
mappingKey.assertUnexpectedValue(`${DEFINITION} ${SEQUENCE} key`);
break;
}
}
2023-01-09 19:02:19 -05:00
break;
}
default:
2023-01-09 19:02:19 -05:00
definitionKey.assertUnexpectedValue(`${DEFINITION} key`); // throws
}
}
}
}
public override get definitionType(): DefinitionType {
2023-01-09 19:02:19 -05:00
return DefinitionType.Sequence;
}
public override validate(schema: TemplateSchema, name: string): void {
if (!this.itemType) {
2023-01-09 19:02:19 -05:00
throw new Error(`'${name}' does not defined '${ITEM_TYPE}'`);
}
// Lookup item type
2023-01-09 19:02:19 -05:00
schema.getDefinition(this.itemType);
}
}