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

Support unknown fields in clone #758

Merged
merged 3 commits into from
Mar 21, 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
2 changes: 1 addition & 1 deletion packages/protobuf-bench/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ server would usually do.

| code generator | bundle size | minified | compressed |
|---------------------|------------------------:|-----------------------:|-------------------:|
| protobuf-es | 97,510 b | 41,660 b | 10,845 b |
| protobuf-es | 97,734 b | 41,763 b | 10,857 b |
| protobuf-javascript | 394,384 b | 288,654 b | 45,122 b |
23 changes: 22 additions & 1 deletion packages/protobuf-test/src/clone.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import { describe, expect } from "@jest/globals";
import { MessageFieldMessage as TS_MessageFieldMessage } from "./gen/ts/extra/msg-message_pb.js";
import { MessageFieldMessage as JS_MessageFieldMessage } from "./gen/js/extra/msg-message_pb.js";
import { Empty as TS_Empty } from "./gen/ts/google/protobuf/empty_pb.js";
import { Empty as JS_Empty } from "./gen/js/google/protobuf/empty_pb.js";
import {
RepeatedScalarValuesMessage as TS_RepeatedScalarValuesMessage,
ScalarValuesMessage as TS_ScalarValuesMessage,
Expand All @@ -26,7 +28,7 @@ import {
import { WrappersMessage as TS_WrappersMessage } from "./gen/ts/extra/wkt-wrappers_pb.js";
import { WrappersMessage as JS_WrappersMessage } from "./gen/js/extra/wkt-wrappers_pb.js";
import { testMT } from "./helpers.js";
import { BoolValue, protoInt64 } from "@bufbuild/protobuf";
import { BoolValue, WireType, protoInt64 } from "@bufbuild/protobuf";

/* eslint-disable @typescript-eslint/ban-ts-comment */

Expand Down Expand Up @@ -146,4 +148,23 @@ describe("clone", function () {
a.doubleValueField = 0.1;
expect(b.doubleValueField).not.toBe(a.doubleValueField);
});
// We are using Empty wkt to test unknown fields.
testMT({ ts: TS_Empty, js: JS_Empty }, (messageType) => {
const a = new messageType();
const bin = messageType.runtime.bin;
const unknownFields = [
{ no: 1, wireType: WireType.Bit32, data: new Uint8Array([1, 2, 3, 4]) },
];
for (const unknowField of unknownFields) {
bin.onUnknownField(
a,
unknowField.no,
unknowField.wireType,
unknowField.data,
);
}
const b = a.clone();
expect(b).toStrictEqual(a);
expect(bin.listUnknownFields(b)).toStrictEqual(unknownFields);
});
});
1 change: 1 addition & 0 deletions packages/protobuf/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export interface AnyMessage extends Message<AnyMessage> {
export class Message<T extends Message<T> = AnyMessage> {
/**
* Compare with a message of the same type.
* Note that this function disregards extensions and unknown fields.
*/
equals(other: T | PlainMessage<T> | undefined | null): boolean {
return this.getType().runtime.util.equals(
Expand Down
3 changes: 3 additions & 0 deletions packages/protobuf/src/private/util-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,9 @@ export function makeUtilCommon(): Omit<Util, "newFieldList" | "initFields"> {
}
any[member.localName] = copy;
}
for (const uf of type.runtime.bin.listUnknownFields(message)) {
type.runtime.bin.onUnknownField(any, uf.no, uf.wireType, uf.data);
}
return target;
},
};
Expand Down
Loading