Skip to content

Commit

Permalink
Change clone to create a new array for unknown fields (#760)
Browse files Browse the repository at this point in the history
  • Loading branch information
srikrsna-buf authored Mar 22, 2024
1 parent 82fd2e4 commit cde009f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
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

0 comments on commit cde009f

Please sign in to comment.