Objects are preferred to classes as they're easier to clone and serialise/deserialise
94 lines
2.1 KiB
TypeScript
94 lines
2.1 KiB
TypeScript
export enum MessageTypeEnum {
|
|
String = 'string',
|
|
Boolean = 'boolean',
|
|
Numeric = 'numeric',
|
|
List = 'list',
|
|
Map = 'map',
|
|
Object = 'object',
|
|
Raw = 'raw',
|
|
}
|
|
|
|
export interface StringMessage extends MessageConfiguration {
|
|
maxLength?: number;
|
|
}
|
|
|
|
export interface BooleanMessage extends MessageConfiguration {}
|
|
|
|
export interface NumericMessage extends MessageConfiguration {
|
|
min?: number;
|
|
max?: number;
|
|
}
|
|
|
|
export interface ListMessage extends MessageConfiguration {
|
|
subConfiguration: MessageConfiguration;
|
|
}
|
|
|
|
export interface MapMessage extends MessageConfiguration {
|
|
keyConfiguration: MessageConfiguration;
|
|
valueConfiguration: MessageConfiguration;
|
|
}
|
|
|
|
export interface ObjectMessage extends MessageConfiguration {
|
|
messageDefinition: ProtoMessage;
|
|
}
|
|
|
|
export interface RawMessage extends MessageConfiguration {}
|
|
|
|
export interface MessageConfiguration {
|
|
type: MessageTypeEnum;
|
|
}
|
|
|
|
export const StringMessage = (maxLength?: number): StringMessage => ({
|
|
type: MessageTypeEnum.String,
|
|
maxLength,
|
|
});
|
|
|
|
export const BooleanMessage = (): BooleanMessage => ({
|
|
type: MessageTypeEnum.Boolean,
|
|
});
|
|
|
|
export const NumericMessage = (min?: number, max?: number): NumericMessage => ({
|
|
type: MessageTypeEnum.Numeric,
|
|
min,
|
|
max,
|
|
});
|
|
|
|
export const ListMessage = (
|
|
subConfiguration: MessageConfiguration
|
|
): ListMessage => ({
|
|
type: MessageTypeEnum.List,
|
|
subConfiguration,
|
|
});
|
|
|
|
export const MapMessage = (
|
|
keyConfiguration: MessageConfiguration,
|
|
valueConfiguration: MessageConfiguration
|
|
): MapMessage => ({
|
|
type: MessageTypeEnum.Map,
|
|
keyConfiguration,
|
|
valueConfiguration,
|
|
});
|
|
|
|
export const ObjectMessage = (messageDefinition: ProtoMessage) => ({
|
|
type: MessageTypeEnum.Object,
|
|
messageDefinition,
|
|
});
|
|
|
|
export const RawMessage = (): RawMessage => ({ type: MessageTypeEnum.Raw });
|
|
|
|
export interface ProtoMessageField {
|
|
name: string;
|
|
configuration: MessageConfiguration;
|
|
value: any;
|
|
}
|
|
|
|
export interface ProtoMessage {
|
|
name: string;
|
|
values: ProtoMessageField[];
|
|
}
|
|
|
|
export const UnknownProto = (name: string): ProtoMessage => ({
|
|
name,
|
|
values: [{ name: 'Raw JSON', configuration: RawMessage(), value: '' }],
|
|
});
|