-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add kind arg to encode/decode interface #75
Conversation
In the case of using specialized schema validation/encoding/decoding we would like to have some context on the data being passed to serialization functions which would allow us to make a judgement on which specialized schema [validator|decoder|encoder] to use. This is a non-breaking change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’m not fully sure why you benefit from this, but I approve the general idea. I do want to ask you to change the syntax a bit, but it should have no impact on the functionality.
@@ -1,6 +1,6 @@ | |||
export type Serialization = { | |||
encode: (value: unknown) => string; | |||
decode: (string: string) => unknown; | |||
encode: (value: unknown, kind: any) => string; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we future-proof the syntax? Let’s make the second parameter an object for possibilities to expand it in the future, and let’s include the id while we’re at it for symmetry.
export type Serialization = {
encode: (value: unknown, details: { kind: string, id: string }) => string;
decode: (string: string, details: { kind: string, id: string }) => unknown;
};
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
ensure future extensibility by making the second arg an object
1f479de
to
979821f
Compare
@@ -100,7 +107,7 @@ export function makeWsProtocolAdapter( | |||
id, | |||
pushId, | |||
lastSeenRevision, | |||
value: serialization.encode(value) | |||
value: serialization.encode(value, { id, kind: String(kind) }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@denis-sokolov is this correct? I haven't had time to dive too much into some of the type definitions but the type checker mentioned kind could be a few other type other than a string. Is converting to a string here ok?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
kind
variable comes from the definition of our type, so its keyof Domain
.
Technically we could type it more strictly and precisely, but we’re doing very dynamic things and as long as our API is clean type-wise, it’s not a big deal. You’ll find a few as any
in this file, too.
Let’s leave it like this for now and maybe later we’ll tighten up the types.
thanks @denis-sokolov for the review |
In the case of using specialized schema validation/encoding/decoding we
would like to have some context on the data being passed to
serialization functions which would allow us to make a judgement on
which specialized schema [validator|decoder|encoder] to use.
This is a non-breaking change.