Skip to content

Commit

Permalink
V2: Move assertInt32 and related functions (#861)
Browse files Browse the repository at this point in the history
  • Loading branch information
timostamm authored May 30, 2024
1 parent 2e29541 commit ab40533
Show file tree
Hide file tree
Showing 14 changed files with 375 additions and 264 deletions.
10 changes: 5 additions & 5 deletions packages/bundle-size/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ usually do. We repeat this for an increasing number of files.
<!--- TABLE-START -->
| code generator | files | bundle size | minified | compressed |
|-----------------|----------|------------------------:|-----------------------:|-------------------:|
| protobuf-es | 1 | 123,995 b | 64,336 b | 14,983 b |
| protobuf-es | 4 | 126,190 b | 65,845 b | 15,664 b |
| protobuf-es | 8 | 128,968 b | 67,616 b | 16,187 b |
| protobuf-es | 16 | 139,476 b | 75,597 b | 18,492 b |
| protobuf-es | 32 | 167,371 b | 97,619 b | 23,949 b |
| protobuf-es | 1 | 123,775 b | 64,241 b | 14,974 b |
| protobuf-es | 4 | 125,970 b | 65,745 b | 15,652 b |
| protobuf-es | 8 | 128,748 b | 67,516 b | 16,179 b |
| protobuf-es | 16 | 139,256 b | 75,497 b | 18,508 b |
| protobuf-es | 32 | 167,151 b | 97,519 b | 23,990 b |
| protobuf-javascript | 1 | 339,613 b | 255,820 b | 42,481 b |
| protobuf-javascript | 4 | 366,281 b | 271,092 b | 43,912 b |
| protobuf-javascript | 8 | 388,324 b | 283,409 b | 45,038 b |
Expand Down
12 changes: 6 additions & 6 deletions packages/bundle-size/chart.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 52 additions & 0 deletions packages/protobuf-test/src/json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,58 @@ describe("JSON parse errors", () => {
}
});

describe("JSON serialize errors", () => {
const desc = proto3_ts.Proto3MessageDesc;
test("enum field not a number throws", () => {
const msg = create(desc);
// @ts-expect-error TS2322
msg.singularEnumField = "abc";
expect(() => toJson(desc, msg)).toThrow(
/^cannot encode enum spec.Proto3Enum to JSON: expected number, got "abc"$/,
);
});
test("INT32 field not a number throws", () => {
const msg = create(desc);
// @ts-expect-error TS2322
msg.singularInt32Field = "abc";
expect(() => toJson(desc, msg)).toThrow(
/^cannot encode field spec.Proto3Message.singular_int32_field to JSON: expected number \(int32\), got "abc"$/,
);
});
test("STRING field not a number throws", () => {
const msg = create(desc);
// @ts-expect-error TS2322
msg.singularStringField = 123;
expect(() => toJson(desc, msg)).toThrow(
/^cannot encode field spec.Proto3Message.singular_string_field to JSON: expected string, got 123$/,
);
});
test("BOOL field not a boolean throws", () => {
const msg = create(desc);
// @ts-expect-error TS2322
msg.optionalBoolField = "abc";
expect(() => toJson(desc, msg)).toThrow(
/^cannot encode field spec.Proto3Message.optional_bool_field to JSON: expected boolean, got "abc"$/,
);
});
test("INT64 field not a bigint throws", () => {
const msg = create(desc);
// @ts-expect-error TS2322
msg.singularInt64Field = true;
expect(() => toJson(desc, msg)).toThrow(
/^cannot encode field spec.Proto3Message.singular_int64_field to JSON: expected bigint \(int64\), got true$/,
);
});
test("BYTES field not a Uint8Array throws", () => {
const msg = create(desc);
// @ts-expect-error TS2322
msg.singularBytesField = true;
expect(() => toJson(desc, msg)).toThrow(
/^cannot encode field spec.Proto3Message.singular_bytes_field to JSON: expected Uint8Array, got true$/,
);
});
});

function testJson<Desc extends DescMessage>(
desc: Desc,
init: MessageInitShape<Desc>,
Expand Down
11 changes: 10 additions & 1 deletion packages/protobuf/src/codegenv1/embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import type {
DescService,
} from "../descriptors.js";
import { protoCamelCase } from "../reflect/names.js";
import { assert } from "../reflect/assert.js";
import { isFieldSet, clearField } from "../fields.js";
import { base64Encode } from "../wire/base64-encoding.js";
import { toBinary } from "../to-binary.js";
Expand Down Expand Up @@ -292,3 +291,13 @@ function createEnumDescriptorBoot(
}),
};
}

/**
* Assert that condition is truthy or throw error.
*/
function assert(condition: unknown): asserts condition {
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions -- we want the implicit conversion to boolean
if (!condition) {
throw new Error();
}
}
1 change: 1 addition & 0 deletions packages/protobuf/src/codegenv1/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ export * from "./file.js";
export * from "./message.js";
export * from "./service.js";
export * from "./symbols.js";
export * from "./scalar.js";
export * from "./types.js";
46 changes: 46 additions & 0 deletions packages/protobuf/src/codegenv1/scalar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2021-2024 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import { ScalarType } from "../descriptors.js";

/**
* Return the TypeScript type (as a string) for the given scalar type.
*/
export function scalarTypeScriptType(
scalar: ScalarType,
longAsString: boolean,
):
| "string"
| "boolean"
| "bigint"
| "bigint | string"
| "Uint8Array"
| "number" {
switch (scalar) {
case ScalarType.STRING:
return "string";
case ScalarType.BOOL:
return "boolean";
case ScalarType.UINT64:
case ScalarType.SFIXED64:
case ScalarType.FIXED64:
case ScalarType.SINT64:
case ScalarType.INT64:
return longAsString ? "string" : "bigint";
case ScalarType.BYTES:
return "Uint8Array";
default:
return "number";
}
}
22 changes: 10 additions & 12 deletions packages/protobuf/src/from-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,10 +455,10 @@ function readEnum(
const tokenNull = Symbol();

/**
* Try to parse a scalar value from a JSON value.
* Try to parse a JSON value to a scalar value for the reflect API.
*
* Returns the input if the JSON value cannot be converted to the representation
* expected by reflect. Raises a FieldError if conversion would be ambiguous.
* Returns the input if the JSON value cannot be converted. Raises a FieldError
* if conversion would be ambiguous.
*
* JSON null returns the scalar zero value.
*/
Expand All @@ -468,10 +468,10 @@ function scalarFromJson(
nullAsZeroValue: true,
): ScalarValue;
/**
* Try to parse a scalar value from a JSON value.
* Try to parse a JSON value to a scalar value for the reflect API.
*
* Returns the input if the JSON value cannot be converted to the representation
* expected by reflect. Raises a FieldError if conversion would be ambiguous.
* Returns the input if the JSON value cannot be converted. Raises a FieldError
* if conversion would be ambiguous.
*
* JSON null returns the symbol `tokenNull`.
*/
Expand Down Expand Up @@ -559,10 +559,9 @@ function scalarFromJson(
}

/**
* Try to parse a map key from a JSON value.
* Try to parse a JSON value to a map key for the reflect API.
*
* Returns the input if the JSON value cannot be converted to the representation
* expected by reflect.
* Returns the input if the JSON value cannot be converted.
*/
function mapKeyFromJson(
type: Exclude<
Expand Down Expand Up @@ -593,10 +592,9 @@ function mapKeyFromJson(
}

/**
* Try to parse a 32-bit integer from a JSON value.
* Try to parse a JSON value to a 32-bit integer for the reflect API.
*
* Returns the input if the JSON value cannot be converted to the representation
* expected by reflect.
* Returns the input if the JSON value cannot be converted.
*/
function int32FromJson(json: JsonValue) {
if (typeof json == "string") {
Expand Down
57 changes: 0 additions & 57 deletions packages/protobuf/src/reflect/assert.ts

This file was deleted.

Loading

0 comments on commit ab40533

Please sign in to comment.