Skip to content

Commit

Permalink
Merge pull request #26 from unsplash/getInternallyTaggedCodec-decode-…
Browse files Browse the repository at this point in the history
…preserve-tag

`getInternallyTaggedCodec`: preserve tag when decoding
  • Loading branch information
OliverJAsh committed Nov 9, 2023
2 parents 051df14 + 599ac75 commit dc6af59
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.direnv/
node_modules/
dist/
coverage/
docs/

61 changes: 61 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/master";
flake-utils.url = "github:numtide/flake-utils";
};

outputs =
{ self
, nixpkgs
, flake-utils
,
}:
flake-utils.lib.eachDefaultSystem
(system:
let
pkgs = import nixpkgs {
inherit system;
};
in
{
devShells.default = pkgs.mkShell {
nativeBuildInputs = [
pkgs.nodejs
pkgs.yarn
];
};
});
}
3 changes: 1 addition & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -682,8 +682,7 @@ const getInternallyTaggedMemberCodec =
(i, ctx) =>
pipe(
t.type({ [tagKey]: t.literal(tag) }).validate(i, ctx),
// eslint-disable-next-line @typescript-eslint/no-unused-vars
E.chain(({ [tagKey]: _, ...rest }) => vc.validate(rest, ctx)),
E.chain(x => vc.validate(x, ctx)),
E.map(x =>
Sum.deserialize(sum)([tag, x] as unknown as Sum.Serialized<A>),
),
Expand Down
14 changes: 14 additions & 0 deletions test/unit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,20 @@ describe("index", () => {
)
})

it("preserves tag after decode", () => {
type T = Sum.Member<"B", { readonly tag: "B" }>
const T = Sum.create<T>()
const {
mk: { B },
} = T

const c = getInternallyTaggedCodec("tag")(T)({
B: t.strict({ tag: t.literal("B") }),
})

expect(c.decode({ tag: "B" })).toEqual(E.right(B({ tag: "B" })))
})

// This is partially for documentative purposes.
it("handles different nullary encodings", () => {
type T = Sum.Member<"A">
Expand Down

0 comments on commit dc6af59

Please sign in to comment.