Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add withMetadata #95

Merged
merged 2 commits into from
Oct 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ export function createAsyncCodec<T, A extends unknown[]>(
return codec;
}

export function withMetadata<T, A extends unknown[]>(
codec: Codec<T>,
metadata: [(...args: A) => Codec<T>, ...A],
): Codec<T> {
return { ...codec, _metadata: metadata };
}

export type Native<T extends AnyCodec> = T extends Codec<infer U> ? U : never;

export class EncodeBuffer {
Expand Down
32 changes: 19 additions & 13 deletions iterable/codec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Codec, createCodec, DecodeError, EncodeError } from "../common.ts";
import { Codec, createCodec, DecodeError, EncodeError, withMetadata } from "../common.ts";
import { compactU32 } from "../compact/codec.ts";
import { tuple } from "../mod.ts";

Expand Down Expand Up @@ -40,18 +40,24 @@ export function iterable<T, I extends Iterable<T>>(
});
}

export function set<T>($el: Codec<T>) {
return iterable<T, Set<T>>({
$el,
calcLength: (set) => set.size,
rehydrate: (values) => new Set(values),
});
export function set<T>($el: Codec<T>): Codec<Set<T>> {
return withMetadata(
iterable({
$el,
calcLength: (set) => set.size,
rehydrate: (values) => new Set(values),
}),
[set, $el],
);
}

export function map<K, V>($key: Codec<K>, $value: Codec<V>) {
return iterable<[K, V], Map<K, V>>({
$el: tuple($key, $value),
calcLength: (map) => map.size,
rehydrate: (values) => new Map(values),
});
export function map<K, V>($key: Codec<K>, $value: Codec<V>): Codec<Map<K, V>> {
return withMetadata(
iterable({
$el: tuple($key, $value),
calcLength: (map) => map.size,
rehydrate: (values) => new Map(values),
}),
[map, $key, $value],
);
}
8 changes: 3 additions & 5 deletions union/codec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AnyCodec, Codec, createCodec, DecodeError, Expand, Narrow, Native } from "../common.ts";
import { AnyCodec, Codec, createCodec, DecodeError, Expand, Narrow, Native, withMetadata } from "../common.ts";
import { dummy } from "../dummy/codec.ts";
import { AnyField, NativeObject, object } from "../object/codec.ts";

Expand Down Expand Up @@ -65,14 +65,12 @@ export function taggedUnion<
...fields,
);
}
return Object.assign(
return withMetadata(
union(
(value) => tagToDiscriminant[value[tagKey]]!,
discriminantToMember,
),
{
metadata: [taggedUnion, tagKey, members],
},
[taggedUnion, tagKey, members],
);
}

Expand Down