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

V2: Use JsonObject to represent google.protobuf.Struct #893

Merged
merged 3 commits into from
Jun 17, 2024
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
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,138 b | 64,107 b | 14,966 b |
| protobuf-es | 4 | 125,327 b | 65,617 b | 15,608 b |
| protobuf-es | 8 | 128,089 b | 67,388 b | 16,135 b |
| protobuf-es | 16 | 138,539 b | 75,369 b | 18,451 b |
| protobuf-es | 32 | 166,330 b | 97,384 b | 23,898 b |
| protobuf-es | 1 | 125,758 b | 65,554 b | 15,210 b |
| protobuf-es | 4 | 127,947 b | 67,062 b | 15,882 b |
| protobuf-es | 8 | 130,709 b | 68,833 b | 16,446 b |
| protobuf-es | 16 | 141,159 b | 76,814 b | 18,708 b |
| protobuf-es | 32 | 168,950 b | 98,830 b | 24,189 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.

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

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

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

3 changes: 3 additions & 0 deletions packages/protobuf-test/extra/extensions-proto2.proto
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package proto2ext;

import "extra/example.proto";
import "google/protobuf/wrappers.proto";
import "google/protobuf/struct.proto";

// The message we're going to extend
message Proto2Extendee {
Expand Down Expand Up @@ -63,8 +64,10 @@ extend Proto2Extendee {
repeated uint32 packed_uint32_ext = 7003 [packed = true];
repeated uint32 unpacked_uint32_ext = 7004; // unpacked by default in proto2
repeated uint64 repeated_uint64_ext_js_string = 7006 [jstype = JS_STRING];
repeated google.protobuf.Struct repeated_struct_ext = 7007;

optional google.protobuf.UInt32Value wrapper_ext = 8001;
optional google.protobuf.Struct struct_ext = 8002;

optional group GroupExt = 8100 {
optional int32 a = 1;
Expand Down
6 changes: 6 additions & 0 deletions packages/protobuf-test/extra/proto3.proto
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ syntax = "proto3";
package spec;
option go_package = "github.com/bufbuild/protobuf-es";
import "google/protobuf/wrappers.proto";
import "google/protobuf/struct.proto";

// Note: We do not exhaust all field types
message Proto3Message {
Expand All @@ -30,6 +31,7 @@ message Proto3Message {
Proto3Enum singular_enum_field = 7;
Proto3Message singular_message_field = 8;
google.protobuf.UInt32Value singular_wrapped_uint32_field = 211;
google.protobuf.Struct singular_struct_field = 214;

optional string optional_string_field = 9;
optional bytes optional_bytes_field = 10;
Expand All @@ -42,6 +44,7 @@ message Proto3Message {
optional Proto3Enum optional_enum_field = 15;
optional Proto3Message optional_message_field = 16;
optional google.protobuf.UInt32Value optional_wrapped_uint32_field = 212;
optional google.protobuf.Struct optional_struct_field = 215;

repeated string repeated_string_field = 17;
repeated bytes repeated_bytes_field = 18;
Expand All @@ -54,6 +57,7 @@ message Proto3Message {
repeated Proto3Enum repeated_enum_field = 23;
repeated Proto3Message repeated_message_field = 24;
repeated google.protobuf.UInt32Value repeated_wrapped_uint32_field = 213;
repeated google.protobuf.Struct repeated_struct_field = 216;

repeated double packed_double_field = 25 [packed = true];
repeated uint32 packed_uint32_field = 26 [packed = true];
Expand All @@ -75,6 +79,7 @@ message Proto3Message {
Proto3Enum oneof_enum_field = 37;
Proto3Message oneof_message_field = 38;
google.protobuf.UInt32Value oneof_wrapped_uint32_field = 204;
google.protobuf.Struct oneof_struct_field = 217;
}
map<string, string> map_string_string_field = 39;
map<int32, int32> map_int32_int32_field = 40;
Expand All @@ -83,6 +88,7 @@ message Proto3Message {
map<int32, Proto3Enum> map_int32_enum_field = 43;
map<int32, Proto3Message> map_int32_message_field = 44;
map<int32, google.protobuf.UInt32Value> map_int32_wrapped_uint32_field = 205;
map<int32, google.protobuf.Struct> map_int32_struct_field = 218;
}

enum Proto3Enum {
Expand Down
47 changes: 47 additions & 0 deletions packages/protobuf-test/src/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -875,5 +875,52 @@ describe("create()", () => {
expect(msg.mapInt32WrappedUint32Field[123].value).toBe(123);
});
});
describe("wkt struct field", () => {
test("accepts JsonObject", () => {
const msg = create(proto3_ts.Proto3MessageSchema, {
singularStructField: {
shouldBeJson: true,
},
repeatedStructField: [
{
shouldBeJson: 1,
},
{
shouldBeJson: 2,
},
],
either: {
case: "oneofStructField",
value: {
shouldBeJson: true,
},
},
mapInt32StructField: {
123: {
shouldBeJson: true,
},
},
});

expect(msg.singularStructField).toStrictEqual({ shouldBeJson: true });
expect(msg.repeatedStructField).toStrictEqual([
{
shouldBeJson: 1,
},
{
shouldBeJson: 2,
},
]);
expect(msg.either.case).toBe("oneofStructField");
if (msg.either.case == "oneofStructField") {
expect(msg.either.value).toStrictEqual({
shouldBeJson: true,
});
}
expect(msg.mapInt32StructField[123]).toStrictEqual({
shouldBeJson: true,
});
});
});
});
});
6 changes: 6 additions & 0 deletions packages/protobuf-test/src/extension-accessor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ import {
uint64_ext_js_string,
unpacked_uint32_ext,
wrapper_ext,
struct_ext,
repeated_struct_ext,
} from "./gen/ts/extra/extensions-proto2_pb.js";
import { BinaryWriter, WireType } from "@bufbuild/protobuf/wire";
import type { DescExtension, DescMessage } from "@bufbuild/protobuf";
Expand Down Expand Up @@ -101,7 +103,9 @@ const goldenValues: extensionWithValueCollection = [
{ ext: packed_uint32_ext, val: [1, 2, 3] },
{ ext: unpacked_uint32_ext, val: [4, 5, 6] },
{ ext: repeated_uint64_ext_js_string, val: ["1", "2", "3"] },
{ ext: repeated_struct_ext, val: [{ shouldBeJson: 1 }, { shouldBeJson: 2 }] },
{ ext: wrapper_ext, val: 123 },
{ ext: struct_ext, val: { shouldBeJson: true } },
{ ext: groupext, val: create(GroupExtSchema, { a: 123 }) },
{
ext: repeatedgroupext,
Expand Down Expand Up @@ -133,7 +137,9 @@ const goldenValuesZero: extensionWithValueCollection = [
{ ext: packed_uint32_ext, val: [] },
{ ext: unpacked_uint32_ext, val: [] },
{ ext: repeated_uint64_ext_js_string, val: [] },
{ ext: repeated_struct_ext, val: [] },
{ ext: wrapper_ext, val: 0 },
{ ext: struct_ext, val: {} },
{ ext: groupext, val: create(GroupExtSchema) },
{ ext: repeatedgroupext, val: [] },
{ ext: Proto2ExtContainer_uint32_ext, val: 0 },
Expand Down

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

Loading