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

Change clone to create a new array for unknown fields #760

Merged
merged 2 commits into from
Mar 22, 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
4 changes: 4 additions & 0 deletions packages/protobuf-test/src/next/clone.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,5 +147,9 @@ describe("clone()", () => {
expect(copy.singularMessageField?.$unknown).toStrictEqual(
msg.singularMessageField.$unknown,
);
// Make sure it is copy
expect(copy.singularMessageField?.$unknown).not.toBe(
msg.singularMessageField.$unknown,
);
});
});
2 changes: 1 addition & 1 deletion packages/protobuf/src/next/clone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export function clone<T extends Message>(message: T): T {
}
const unknown = i.getUnknown();
if (unknown && unknown.length > 0) {
o.setUnknown(unknown);
o.setUnknown([...unknown]);
}
return o.message as T;
}
Expand Down
11 changes: 3 additions & 8 deletions packages/protobuf/src/next/reflect/reflect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import type {
DescOneof,
} from "../../descriptor-set.js";
import type { ScalarValue, LongType } from "./scalar.js";
import type { Message } from "../types.js";
import type { Message, UnknownField } from "../types.js";
import {
addListItemPrivate,
clearFieldPrivate,
Expand All @@ -38,7 +38,6 @@ import {
import { FieldError } from "./error.js";
import type { MapEntryKey, ReflectMap } from "./reflect-map.js";
import { reflectMap } from "./reflect-map.js";
import type { WireType } from "../../binary-encoding.js";

export interface ReflectMessage {
readonly kind: "reflect_message";
Expand Down Expand Up @@ -77,13 +76,9 @@ export interface ReflectMessage {
value: NewMapEntryValue<Field>,
): FieldError | undefined;

getUnknown():
| { no: number; wireType: WireType; data: Uint8Array }[]
| undefined;
getUnknown(): UnknownField[] | undefined;

setUnknown(
value: { no: number; wireType: WireType; data: Uint8Array }[],
): void;
setUnknown(value: UnknownField[]): void;
}

// prettier-ignore
Expand Down
8 changes: 7 additions & 1 deletion packages/protobuf/src/next/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export type Message<TypeName extends string = string> = {
readonly $desc: DescMessage;
readonly $typeName: TypeName;

$unknown?: { no: number; wireType: WireType; data: Uint8Array }[];
$unknown?: UnknownField[];
};

// TODO docs
Expand All @@ -41,6 +41,12 @@ export type MessageInitShape<Desc extends DescMessage> =
export type EnumShape<Desc extends DescEnum> =
Desc extends TypedDescEnum<infer RuntimeShape> ? RuntimeShape : number;

export type UnknownField = {
readonly no: number;
readonly wireType: WireType;
readonly data: Uint8Array;
};

// TODO ServiceShape
// TODO MethodShape?

Expand Down
Loading