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

Skip null values in message initializer object #862

Merged
merged 1 commit into from
May 30, 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 @@ -15,11 +15,11 @@ usually do. We repeat this for an increasing number of files.
<!--- TABLE-START -->
| code generator | files | bundle size | minified | compressed |
|-----------------|----------|------------------------:|-----------------------:|-------------------:|
| protobuf-es | 1 | 79,466 b | 34,303 b | 9,800 b |
| protobuf-es | 4 | 92,563 b | 37,277 b | 10,113 b |
| protobuf-es | 8 | 101,904 b | 41,775 b | 10,824 b |
| protobuf-es | 16 | 165,584 b | 67,020 b | 13,312 b |
| protobuf-es | 32 | 344,962 b | 147,972 b | 20,177 b |
| protobuf-es | 1 | 79,463 b | 34,300 b | 9,781 b |
| protobuf-es | 4 | 92,560 b | 37,274 b | 10,114 b |
| protobuf-es | 8 | 101,901 b | 41,772 b | 10,808 b |
| protobuf-es | 16 | 165,581 b | 67,017 b | 13,320 b |
| protobuf-es | 32 | 344,959 b | 147,969 b | 20,175 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.
46 changes: 46 additions & 0 deletions packages/protobuf-test/src/constructor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import { TestAllTypesProto3 as JS_TestAllTypesProto3 } from "./gen/js/google/pro
import { JSTypeStringMessage as TS_JSTypeStringMessage } from "./gen/ts/extra/jstype_pb.js";
import { JSTypeStringMessage as JS_JSTypeStringMessage } from "./gen/js/extra/jstype_pb.js";
import { testMT } from "./helpers.js";
import { Proto3OptionalMessage as TS_Proto3OptionalMessage } from "./gen/ts/extra/proto3_pb.js";
import { Proto3OptionalMessage as JS_Proto3OptionalMessage } from "./gen/js/extra/proto3_pb.js";

describe("constructor initializes jstype=JS_STRING with string", function () {
testMT(
Expand Down Expand Up @@ -179,3 +181,47 @@ describe("constructor takes partial message for map value", function () {
},
);
});

describe("constructor skips undefined values", () => {
testMT(
{ ts: TS_Proto3OptionalMessage, js: JS_Proto3OptionalMessage },
(messageType) => {
const m = new messageType({
stringField: undefined,
});
expect(m.stringField).toBeUndefined();
},
);
testMT(
{ ts: TS_TestAllTypesProto3, js: JS_TestAllTypesProto3 },
(messageType) => {
const m = new messageType({
optionalInt32: undefined,
});
expect(m.optionalInt32).toBe(0);
},
);
});

describe("constructor skips null values", () => {
testMT(
{ ts: TS_Proto3OptionalMessage, js: JS_Proto3OptionalMessage },
(messageType) => {
const m = new messageType({
// @ts-expect-error TS 2322
stringField: null,
});
expect(m.stringField).toBeUndefined();
},
);
testMT(
{ ts: TS_TestAllTypesProto3, js: JS_TestAllTypesProto3 },
(messageType) => {
const m = new messageType({
// @ts-expect-error TS 2322
optionalInt32: null,
});
expect(m.optionalInt32).toBe(0);
},
);
});
2 changes: 1 addition & 1 deletion packages/protobuf/src/private/util-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function makeUtilCommon(): Omit<Util, "newFieldList" | "initFields"> {
const localName = member.localName,
t = target as AnyMessage,
s = source as PartialMessage<AnyMessage>;
if (s[localName] === undefined) {
if (s[localName] == null) {
// TODO if source is a Message instance, we should use isFieldSet() here to support future field presence
continue;
}
Expand Down