Feedback on TypeScript Code Using Effect Library for Schema Transformation
Hey all... It would appreciate if I could get some feedback on this chunk of code:
It does what I want, but was wondering if it there's anything else it could be improved. Maybe with class based schemas (not comfortable with that) ?!
enum InputEventType {
D = 'D',
E = 'E',
F = 'F'
}
enum EventType {
MloEvent = 'MloEvent',
DealEvent = 'DealEvent',
BloEvent = 'BloEvent'
}
const InputSchema = Schema.Struct({
Header: Schema.Struct({
MsgType: Schema.String,
}),
Body: Schema.Struct({
foo: Schema.String,
}),
Trailer: Schema.Struct({}),
});
const OutputSchema = Schema.Struct({
EventType: Schema.Enums(EventType),
Body: Schema.Struct({
foo: Schema.String,
}),
});
const TransformedSchema = Schema.transform(
InputSchema,
OutputSchema,
{
strict: true,
decode: (input) => {
const typeMap = { D: EventType.MloEvent, E: EventType.DealEvent, F: EventType.BloEvent }
return {
EventType: typeMap[input.Header.MsgType as InputEventType],
Body: input.Body,
}
},
encode: (output) => {
const reverseTypeMap = { MloEvent: InputEventType.D, DealEvent: InputEventType.E, BloEvent: InputEventType.F }
return {
Header: { MsgType: reverseTypeMap[output.EventType] },
Body: output.Body,
Trailer: {},
}
}
}
)
const messageDecoded = Schema.decode(TransformedSchema)({
Header: { MsgType: "D" },
Body: { foo: "foo" },
Trailer: {},
})enum InputEventType {
D = 'D',
E = 'E',
F = 'F'
}
enum EventType {
MloEvent = 'MloEvent',
DealEvent = 'DealEvent',
BloEvent = 'BloEvent'
}
const InputSchema = Schema.Struct({
Header: Schema.Struct({
MsgType: Schema.String,
}),
Body: Schema.Struct({
foo: Schema.String,
}),
Trailer: Schema.Struct({}),
});
const OutputSchema = Schema.Struct({
EventType: Schema.Enums(EventType),
Body: Schema.Struct({
foo: Schema.String,
}),
});
const TransformedSchema = Schema.transform(
InputSchema,
OutputSchema,
{
strict: true,
decode: (input) => {
const typeMap = { D: EventType.MloEvent, E: EventType.DealEvent, F: EventType.BloEvent }
return {
EventType: typeMap[input.Header.MsgType as InputEventType],
Body: input.Body,
}
},
encode: (output) => {
const reverseTypeMap = { MloEvent: InputEventType.D, DealEvent: InputEventType.E, BloEvent: InputEventType.F }
return {
Header: { MsgType: reverseTypeMap[output.EventType] },
Body: output.Body,
Trailer: {},
}
}
}
)
const messageDecoded = Schema.decode(TransformedSchema)({
Header: { MsgType: "D" },
Body: { foo: "foo" },
Trailer: {},
})It does what I want, but was wondering if it there's anything else it could be improved. Maybe with class based schemas (not comfortable with that) ?!
