2021-05-21 13:49:06 +00:00
|
|
|
export type Type =
|
|
|
|
| { kind: 'union', variants: VariantMap } // zero: never
|
|
|
|
| SimpleType
|
|
|
|
|
2021-05-23 22:15:31 +00:00
|
|
|
export type SimpleType = FieldType | RecordType
|
2021-05-21 13:49:06 +00:00
|
|
|
|
2021-05-23 22:15:31 +00:00
|
|
|
export type FieldType =
|
2021-05-21 13:49:06 +00:00
|
|
|
| { kind: 'unit' }
|
2021-05-23 22:15:31 +00:00
|
|
|
| { kind: 'array', type: FieldType }
|
|
|
|
| { kind: 'set', type: FieldType }
|
|
|
|
| { kind: 'dictionary', key: FieldType, value: FieldType }
|
2021-05-21 13:49:06 +00:00
|
|
|
| { kind: 'ref', typeName: string } // also for base types
|
|
|
|
|
2021-05-23 22:15:31 +00:00
|
|
|
export type RecordType =
|
2021-05-21 13:49:06 +00:00
|
|
|
| { kind: 'record', fields: FieldMap }
|
|
|
|
|
|
|
|
export type VariantMap = Map<string, SimpleType>;
|
|
|
|
export type FieldMap = Map<string, FieldType>;
|
|
|
|
|
|
|
|
export namespace Type {
|
|
|
|
export const union = (variants: VariantMap): Type => ({ kind: 'union', variants });
|
2021-05-23 22:15:31 +00:00
|
|
|
export const unit = (): FieldType => ({ kind: 'unit' });
|
|
|
|
export const ref = (typeName: string): FieldType => ({ kind: 'ref', typeName });
|
|
|
|
export const array = (type: FieldType): FieldType => ({ kind: 'array', type });
|
|
|
|
export const set = (type: FieldType): FieldType => ({ kind: 'set', type });
|
|
|
|
export const dictionary = (key: FieldType, value: FieldType): FieldType => (
|
2021-05-21 13:49:06 +00:00
|
|
|
{ kind: 'dictionary', key, value });
|
2021-05-23 22:15:31 +00:00
|
|
|
export const record = (fields: FieldMap): RecordType => ({ kind: 'record', fields });
|
2021-05-21 13:49:06 +00:00
|
|
|
}
|
|
|
|
|
2021-05-23 22:15:31 +00:00
|
|
|
export const ANY_TYPE: FieldType = Type.ref('_val');
|