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";
|
2023-01-06 15:54:31 -08:00
|
|
|
|
|
|
|
|
export class SequenceDefinition extends Definition {
|
2023-01-09 19:02:19 -05:00
|
|
|
public itemType = "";
|
2023-01-06 15:54:31 -08:00
|
|
|
|
|
|
|
|
public constructor(key: string, definition?: MappingToken) {
|
2023-01-09 19:02:19 -05:00
|
|
|
super(key, definition);
|
2023-01-06 15:54:31 -08:00
|
|
|
if (definition) {
|
|
|
|
|
for (const definitionPair of definition) {
|
2023-01-09 19:02:19 -05:00
|
|
|
const definitionKey = definitionPair.key.assertString(`${DEFINITION} key`);
|
2023-01-06 15:54:31 -08:00
|
|
|
switch (definitionKey.value) {
|
|
|
|
|
case SEQUENCE: {
|
2023-01-09 19:02:19 -05:00
|
|
|
const mapping = definitionPair.value.assertMapping(`${DEFINITION} ${SEQUENCE}`);
|
2023-01-06 15:54:31 -08:00
|
|
|
for (const mappingPair of mapping) {
|
2023-01-09 19:02:19 -05:00
|
|
|
const mappingKey = mappingPair.key.assertString(`${DEFINITION} ${SEQUENCE} key`);
|
2023-01-06 15:54:31 -08:00
|
|
|
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;
|
2023-01-06 15:54:31 -08:00
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
// throws
|
2023-01-09 19:02:19 -05:00
|
|
|
mappingKey.assertUnexpectedValue(`${DEFINITION} ${SEQUENCE} key`);
|
|
|
|
|
break;
|
2023-01-06 15:54:31 -08:00
|
|
|
}
|
|
|
|
|
}
|
2023-01-09 19:02:19 -05:00
|
|
|
break;
|
2023-01-06 15:54:31 -08:00
|
|
|
}
|
|
|
|
|
default:
|
2023-01-09 19:02:19 -05:00
|
|
|
definitionKey.assertUnexpectedValue(`${DEFINITION} key`); // throws
|
2023-01-06 15:54:31 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override get definitionType(): DefinitionType {
|
2023-01-09 19:02:19 -05:00
|
|
|
return DefinitionType.Sequence;
|
2023-01-06 15:54:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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}'`);
|
2023-01-06 15:54:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Lookup item type
|
2023-01-09 19:02:19 -05:00
|
|
|
schema.getDefinition(this.itemType);
|
2023-01-06 15:54:31 -08:00
|
|
|
}
|
|
|
|
|
}
|