/* * Copyright (c) 2018 Mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author David Sehnal */ type Type = | Type.Any | Type.AnyValue | Type.Variable | Type.Value | Type.Container | Type.Union | Type.OneOf namespace Type { export interface Any { kind: 'any', '@type': any } export interface Variable { kind: 'variable', name: string, type: Type, isConstraint: boolean, '@type': any } export interface AnyValue { kind: 'any-value', '@type': any } export interface Value { kind: 'value', namespace: string, name: string, parent?: Value, '@type': T } export interface Container { kind: 'container', namespace: string, name: string, alias?: string, child: Type, '@type': T } export interface Union { kind: 'union', types: Type[], '@type': T } export interface OneOf { kind: 'oneof', type: Value, namespace: string, name: string, values: { [v: string]: boolean | undefined }, '@type': T } export function Variable(name: string, type: Type, isConstraint?: boolean): Variable { return { kind: 'variable', name, type: type, isConstraint } as any; } export function Value(namespace: string, name: string, parent?: Value): Value { return { kind: 'value', namespace, name, parent } as any; } export function Container(namespace: string, name: string, child: Type, alias?: string): Container { return { kind: 'container', namespace, name, child, alias } as any; } export function Union(types: Type[]): Union { return { kind: 'union', types } as any; } export function OneOf(namespace: string, name: string, type: Value, values: any[]): OneOf { const vals = Object.create(null); for (const v of values) vals[v] = true; return { kind: 'oneof', namespace, name, type, values: vals } as any; } export const Any: Any = { kind: 'any' } as any; export const AnyValue: AnyValue = { kind: 'any-value' } as any; export const Num = Value('', 'Number'); export const Str = Value('', 'String'); export const Bool = OneOf('', 'Bool', Str as any, ['true', 'false']); export function oneOfValues({ values }: OneOf) { return Object.keys(values).sort(); } } export default Type