Skip to content

Commit

Permalink
Codec name itself (#232)
Browse files Browse the repository at this point in the history
* impl

* bump
  • Loading branch information
lucasavila00 authored Jun 26, 2024
1 parent bef849b commit f6f4710
Show file tree
Hide file tree
Showing 16 changed files with 52 additions and 19 deletions.
1 change: 1 addition & 0 deletions e2e-tests/bug-rust-repro/src/generated/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ function buildParsers(args) {
parse,
safeParse,
zod,
name: k,
};
});
return decoders;
Expand Down
8 changes: 8 additions & 0 deletions e2e-tests/standalone-parser/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# node-server

## 1.0.50

### Patch Changes

- Updated dependencies
- @beff/client@0.0.53
- @beff/cli@0.0.53

## 1.0.49

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions e2e-tests/standalone-parser/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "standalone-parser",
"version": "1.0.49",
"version": "1.0.50",
"description": "",
"main": "index.js",
"scripts": {
Expand All @@ -11,7 +11,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"@beff/cli": "workspace:^0.0.52",
"@beff/cli": "workspace:^0.0.53",
"@beff/client": "workspace:^",
"vitest": "^0.34.4",
"zod": "^3.23.5"
Expand Down
1 change: 1 addition & 0 deletions e2e-tests/standalone-parser/src/generated/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ function buildParsers(args) {
parse,
safeParse,
zod,
name: k,
};
});
return decoders;
Expand Down
1 change: 1 addition & 0 deletions e2e-tests/standalone-parser/tests/b.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ it("parse", () => {
"x": 1,
}
`);
expect(T3.name).toMatchInlineSnapshot('"b.Object"');
expect(
T3.zod().parse({
kind: "square",
Expand Down
3 changes: 3 additions & 0 deletions e2e-tests/standalone-parser/tests/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,8 @@ it("RequiredPartialObject", () => {
a: "a",
b: 1,
};
expect(RequiredPartialObject.name).toMatchInlineSnapshot('"RequiredPartialObject"');

expect(RequiredPartialObject.parse(valid)).toMatchInlineSnapshot(`
{
"a": "a",
Expand Down Expand Up @@ -605,6 +607,7 @@ it("works on recursive type", () => {
key: "value",
},
};
expect(User.name).toMatchInlineSnapshot('"User"');
expect(User.parse(valid)).toMatchInlineSnapshot(`
{
"accessLevel": "ADMIN",
Expand Down
6 changes: 6 additions & 0 deletions packages/beff-cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @beff/cli

## 0.0.53

### Patch Changes

- add name to schema

## 0.0.52

### Patch Changes
Expand Down
1 change: 1 addition & 0 deletions packages/beff-cli/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ export type BeffParser<T> = {
options?: ParseOptions
) => { success: true; data: T } | { success: false; errors: DecodeError[] };
zod: () => ZodType<T>;
name: string;
};
type Parsers<T> = {
[K in keyof T]: BeffParser<T[K]>;
Expand Down
2 changes: 1 addition & 1 deletion packages/beff-cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@beff/cli",
"version": "0.0.52",
"version": "0.0.53",
"description": "",
"bin": {
"beff": "./bin/index.js"
Expand Down
8 changes: 8 additions & 0 deletions packages/beff-client/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# @beff/client

## 0.0.53

### Patch Changes

- add name to schema
- Updated dependencies
- @beff/cli@0.0.53

## 0.0.52

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions packages/beff-client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@beff/client",
"version": "0.0.52",
"version": "0.0.53",
"description": "",
"main": "dist/cjs/index.js",
"scripts": {
Expand All @@ -20,7 +20,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"@beff/cli": "workspace:^0.0.52",
"@beff/cli": "workspace:^0.0.53",
"zod": "^3.23.5"
},
"devDependencies": {
Expand Down
22 changes: 12 additions & 10 deletions packages/beff-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export const printErrors = (it: DecodeError[], parentPath: string[] = []): strin
};

const buildParserFromSafeParser = <T>(
name: string,
safeParse: (
input: any,
options?: ParseOptions
Expand Down Expand Up @@ -117,6 +118,7 @@ const buildParserFromSafeParser = <T>(
parse,
//@ts-ignore
zod,
name,
};
};

Expand All @@ -125,7 +127,7 @@ const Object_ = <T extends Record<string, BeffParser<any>>>(
): BeffParser<{
[K in keyof T]: T[K] extends BeffParser<infer U> ? U : never;
}> =>
buildParserFromSafeParser((input: any, options?: ParseOptions) => {
buildParserFromSafeParser("b.Object", (input: any, options?: ParseOptions) => {
const disallowExtraProperties = options?.disallowExtraProperties ?? false;

const errors: DecodeError[] = [];
Expand Down Expand Up @@ -159,65 +161,65 @@ const Object_ = <T extends Record<string, BeffParser<any>>>(
});

const String_ = (): BeffParser<string> =>
buildParserFromSafeParser((input: any) => {
buildParserFromSafeParser("String", (input: any) => {
if (typeof input === "string") {
return { success: true, data: input };
}
return { success: false, errors: [{ message: "Expected string", path: [], received: input }] };
});

const Number_ = (): BeffParser<number> =>
buildParserFromSafeParser((input: any) => {
buildParserFromSafeParser("Number", (input: any) => {
if (typeof input === "number") {
return { success: true, data: input };
}
return { success: false, errors: [{ message: "Expected number", path: [], received: input }] };
});

const Boolean_ = (): BeffParser<boolean> =>
buildParserFromSafeParser((input: any) => {
buildParserFromSafeParser("Boolean", (input: any) => {
if (typeof input === "boolean") {
return { success: true, data: input };
}
return { success: false, errors: [{ message: "Expected boolean", path: [], received: input }] };
});

const Undefined_ = (): BeffParser<undefined> =>
buildParserFromSafeParser((input: any) => {
buildParserFromSafeParser("Undefined", (input: any) => {
if (input == undefined) {
return { success: true, data: input };
}
return { success: false, errors: [{ message: "Expected undefined", path: [], received: input }] };
});

const Void_ = (): BeffParser<void> =>
buildParserFromSafeParser((input: any) => {
buildParserFromSafeParser("Void", (input: any) => {
if (input == undefined) {
return { success: true, data: input };
}
return { success: false, errors: [{ message: "Expected void", path: [], received: input }] };
});

const Null_ = (): BeffParser<undefined> =>
buildParserFromSafeParser((input: any) => {
buildParserFromSafeParser("Null", (input: any) => {
if (input == null) {
return { success: true, data: input };
}
return { success: false, errors: [{ message: "Expected null", path: [], received: input }] };
});

const Any_ = (): BeffParser<any> =>
buildParserFromSafeParser((input: any) => {
buildParserFromSafeParser("Any", (input: any) => {
return { success: true, data: input };
});

const Unknown_ = (): BeffParser<unknown> =>
buildParserFromSafeParser((input: any) => {
buildParserFromSafeParser("Unknown", (input: any) => {
return { success: true, data: input };
});

const Array_ = <T>(parser: BeffParser<T>): BeffParser<T[]> =>
buildParserFromSafeParser((input: any) => {
buildParserFromSafeParser("b.Array", (input: any) => {
if (!Array.isArray(input)) {
return {
success: false,
Expand Down
1 change: 1 addition & 0 deletions packages/beff-wasm/bundled-code/build-parsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ function buildParsers(args) {
parse,
safeParse,
zod,
name: k,
};
});
return decoders;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ function buildParsers(args) {
parse,
safeParse,
zod,
name: k,
};
});
return decoders;
Expand Down
2 changes: 1 addition & 1 deletion packages/beff-wasm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
},
"devDependencies": {
"@babel/code-frame": "^7.22.13",
"@beff/cli": "workspace:^0.0.52",
"@beff/cli": "workspace:^0.0.53",
"@types/babel__code-frame": "^7.0.4",
"@types/node": "^20.6.2",
"@types/vscode": "^1.73.0",
Expand Down
6 changes: 3 additions & 3 deletions pnpm-lock.yaml

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

0 comments on commit f6f4710

Please sign in to comment.