Skip to content

Commit

Permalink
Add never decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasavila00 committed Dec 3, 2024
1 parent c34d2b3 commit 3d4fb61
Show file tree
Hide file tree
Showing 18 changed files with 113 additions and 17 deletions.
2 changes: 1 addition & 1 deletion e2e-tests/bug-rust-repro/src/generated/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import {printErrors} from '@beff/client';
import {z} from 'zod';
import validatorsMod from "./validators.js"; const { decodeObject, decodeArray, decodeString, decodeNumber, decodeCodec, decodeFunction, decodeStringWithFormat, decodeAnyOf, decodeAllOf, decodeBoolean, decodeAny, decodeTuple, decodeNull, decodeConst, registerCustomFormatter, validators, c } = validatorsMod;
import validatorsMod from "./validators.js"; const { decodeObject, decodeArray, decodeString, decodeNumber, decodeCodec, decodeFunction, decodeStringWithFormat, decodeAnyOf, decodeAllOf, decodeBoolean, decodeAny, decodeTuple, decodeNull, decodeNever, decodeConst, registerCustomFormatter, validators, c } = validatorsMod;
const RequiredCustomFormats = ["ValidCurrency"];
const buildParsersInput = {
"A": function(ctx, input, required = true) {
Expand Down
8 changes: 7 additions & 1 deletion e2e-tests/bug-rust-repro/src/generated/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,12 @@ function decodeNull(ctx, input, required) {
}
return buildError(input, ctx, "expected null");
}
function decodeNever(ctx, input, required) {
if (!required && input == null) {
return input;
}
return buildError(input, ctx, "never");
}
function decodeConst(ctx, input, required, constValue) {
if (!required && input == null) {
return input;
Expand Down Expand Up @@ -346,4 +352,4 @@ const validators = {
A: DecodeA
};

export default { decodeObject, decodeArray, decodeString, decodeNumber, decodeCodec, decodeFunction, decodeStringWithFormat, decodeAnyOf, decodeAllOf, decodeBoolean, decodeAny, decodeTuple, decodeNull, decodeConst, registerCustomFormatter, validators };
export default { decodeObject, decodeArray, decodeString, decodeNumber, decodeCodec, decodeFunction, decodeStringWithFormat, decodeAnyOf, decodeAllOf, decodeBoolean, decodeAny, decodeTuple, decodeNull, decodeNever, decodeConst, registerCustomFormatter, validators };
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.59

### Patch Changes

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

## 1.0.58

### 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.58",
"version": "1.0.59",
"description": "",
"main": "index.js",
"scripts": {
Expand All @@ -11,7 +11,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"@beff/cli": "workspace:^0.0.61",
"@beff/cli": "workspace:^0.0.62",
"@beff/client": "workspace:^",
"vitest": "^0.34.4",
"zod": "^3.23.5"
Expand Down
11 changes: 10 additions & 1 deletion e2e-tests/standalone-parser/src/generated/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import {printErrors} from '@beff/client';
import {z} from 'zod';
import validatorsMod from "./validators.js"; const { decodeObject, decodeArray, decodeString, decodeNumber, decodeCodec, decodeFunction, decodeStringWithFormat, decodeAnyOf, decodeAllOf, decodeBoolean, decodeAny, decodeTuple, decodeNull, decodeConst, registerCustomFormatter, validators, c } = validatorsMod;
import validatorsMod from "./validators.js"; const { decodeObject, decodeArray, decodeString, decodeNumber, decodeCodec, decodeFunction, decodeStringWithFormat, decodeAnyOf, decodeAllOf, decodeBoolean, decodeAny, decodeTuple, decodeNull, decodeNever, decodeConst, registerCustomFormatter, validators, c } = validatorsMod;
const RequiredCustomFormats = ["ValidCurrency"];
const buildParsersInput = {
"AObject": function(ctx, input, required = true) {
Expand Down Expand Up @@ -60,6 +60,15 @@ const buildParsersInput = {
"A": (ctx, input)=>(decodeConst(ctx, input, true, "a"))
});
},
"K": function(ctx, input, required = true) {
return validators.K(ctx, input, required);
},
"KABC": function(ctx, input, required = true) {
return validators.KABC(ctx, input, required);
},
"KDEF": function(ctx, input, required = true) {
return validators.KDEF(ctx, input, required);
},
"LevelAndDSettings": function(ctx, input, required = true) {
return validators.LevelAndDSettings(ctx, input, required);
},
Expand Down
35 changes: 33 additions & 2 deletions e2e-tests/standalone-parser/src/generated/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,12 @@ function decodeNull(ctx, input, required) {
}
return buildError(input, ctx, "expected null");
}
function decodeNever(ctx, input, required) {
if (!required && input == null) {
return input;
}
return buildError(input, ctx, "never");
}
function decodeConst(ctx, input, required, constValue) {
if (!required && input == null) {
return input;
Expand Down Expand Up @@ -646,6 +652,26 @@ function DecodeBObject(ctx, input, required = true) {
"tag": (ctx, input)=>(decodeConst(ctx, input, true, "b"))
});
}
function DecodeDEF(ctx, input, required = true) {
return decodeObject(ctx, input, required, {
"a": (ctx, input)=>(decodeString(ctx, input, true))
});
}
function DecodeKDEF(ctx, input, required = true) {
return decodeConst(ctx, input, required, "a");
}
function DecodeABC(ctx, input, required = true) {
return decodeObject(ctx, input, required, {});
}
function DecodeKABC(ctx, input, required = true) {
return decodeNever(ctx, input, required);
}
function DecodeK(ctx, input, required = true) {
return decodeAnyOf(ctx, input, required, [
(ctx, input)=>(validators.KABC(ctx, input, required)),
(ctx, input)=>(validators.KDEF(ctx, input, required))
]);
}
const validators = {
AllTs: DecodeAllTs,
AObject: DecodeAObject,
Expand Down Expand Up @@ -684,7 +710,12 @@ const validators = {
UnionWithEnumAccess: DecodeUnionWithEnumAccess,
Shape: DecodeShape,
T3: DecodeT3,
BObject: DecodeBObject
BObject: DecodeBObject,
DEF: DecodeDEF,
KDEF: DecodeKDEF,
ABC: DecodeABC,
KABC: DecodeKABC,
K: DecodeK
};

export default { decodeObject, decodeArray, decodeString, decodeNumber, decodeCodec, decodeFunction, decodeStringWithFormat, decodeAnyOf, decodeAllOf, decodeBoolean, decodeAny, decodeTuple, decodeNull, decodeConst, registerCustomFormatter, validators };
export default { decodeObject, decodeArray, decodeString, decodeNumber, decodeCodec, decodeFunction, decodeStringWithFormat, decodeAnyOf, decodeAllOf, decodeBoolean, decodeAny, decodeTuple, decodeNull, decodeNever, decodeConst, registerCustomFormatter, validators };
13 changes: 13 additions & 0 deletions e2e-tests/standalone-parser/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,16 @@ const ImportEnumTypeof = {
A: OtherEnum.A,
} as const;

export type ABC = {};
export type KABC = keyof ABC;

export type DEF = {
a: string;
};
export type KDEF = keyof DEF;

export type K = KABC | KDEF;

export const {
Version,
Version2,
Expand Down Expand Up @@ -245,6 +255,9 @@ export const {
AvatarSize: AvatarSize;
BObject: BObject;
ImportEnumTypeof: typeof ImportEnumTypeof;
KDEF: KDEF;
KABC: KABC;
K: K;
}>({
customFormats: {
ValidCurrency: (input: string) => {
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.62

### Patch Changes

- fix bugs never decoder

## 0.0.61

### Patch Changes
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.61",
"version": "0.0.62",
"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.62

### Patch Changes

- fix bugs never decoder
- Updated dependencies
- @beff/cli@0.0.62

## 0.0.61

### 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.61",
"version": "0.0.62",
"description": "",
"main": "dist/cjs/index.js",
"scripts": {
Expand All @@ -20,7 +20,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"@beff/cli": "workspace:^0.0.61",
"@beff/cli": "workspace:^0.0.62",
"zod": "^3.23.5"
},
"devDependencies": {
Expand Down
4 changes: 3 additions & 1 deletion packages/beff-core/src/print/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,9 +403,11 @@ impl DecoderFnGenerator<'_> {

fn decode_expr(&self, schema: &JsonSchema, required: Required) -> Expr {
match schema {
JsonSchema::StNever | JsonSchema::StNot(_) => {
JsonSchema::StNot(_) => {
unreachable!("should not create decoders for semantic types")
}

JsonSchema::StNever => Self::decode_call("decodeNever", required),
JsonSchema::AnyArrayLike => {
self.decode_expr(&JsonSchema::Array(JsonSchema::Any.into()), required)
}
Expand Down
6 changes: 6 additions & 0 deletions packages/beff-wasm/bundled-code/decoders.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,12 @@ function decodeNull(ctx, input, required) {
}
return buildError(input, ctx, "expected null");
}
function decodeNever(ctx, input, required) {
if (!required && input == null) {
return input;
}
return buildError(input, ctx, "never");
}
function decodeConst(ctx, input, required, constValue) {
if (!required && input == null) {
return input;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import {printErrors} from '@beff/client';
import {z} from 'zod';
import validatorsMod from "./validators.js"; const { decodeObject, decodeArray, decodeString, decodeNumber, decodeCodec, decodeFunction, decodeStringWithFormat, decodeAnyOf, decodeAllOf, decodeBoolean, decodeAny, decodeTuple, decodeNull, decodeConst, registerCustomFormatter, validators, c } = validatorsMod;
import validatorsMod from "./validators.js"; const { decodeObject, decodeArray, decodeString, decodeNumber, decodeCodec, decodeFunction, decodeStringWithFormat, decodeAnyOf, decodeAllOf, decodeBoolean, decodeAny, decodeTuple, decodeNull, decodeNever, decodeConst, registerCustomFormatter, validators, c } = validatorsMod;
const RequiredCustomFormats = ["password","StartsWithA"];
const buildParsersInput = {
"NotPublicRenamed": function(ctx, input, required = true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,12 @@ function decodeNull(ctx, input, required) {
}
return buildError(input, ctx, "expected null");
}
function decodeNever(ctx, input, required) {
if (!required && input == null) {
return input;
}
return buildError(input, ctx, "never");
}
function decodeConst(ctx, input, required, constValue) {
if (!required && input == null) {
return input;
Expand Down Expand Up @@ -402,4 +408,4 @@ const validators = {
UnionNested: DecodeUnionNested
};

export default { decodeObject, decodeArray, decodeString, decodeNumber, decodeCodec, decodeFunction, decodeStringWithFormat, decodeAnyOf, decodeAllOf, decodeBoolean, decodeAny, decodeTuple, decodeNull, decodeConst, registerCustomFormatter, validators };
export default { decodeObject, decodeArray, decodeString, decodeNumber, decodeCodec, decodeFunction, decodeStringWithFormat, decodeAnyOf, decodeAllOf, decodeBoolean, decodeAny, decodeTuple, decodeNull, decodeNever, decodeConst, registerCustomFormatter, validators };
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.61",
"@beff/cli": "workspace:^0.0.62",
"@types/babel__code-frame": "^7.0.4",
"@types/node": "^20.6.2",
"@types/vscode": "^1.73.0",
Expand Down
1 change: 1 addition & 0 deletions packages/beff-wasm/ts-node/bundle-to-disk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const decodersExported = [
"decodeAny",
"decodeTuple",
"decodeNull",
"decodeNever",
"decodeConst",
"registerCustomFormatter",
];
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 3d4fb61

Please sign in to comment.