Skip to content

Commit d5ad2e9

Browse files
authored
V2: Add localName property to DescField and others (#834)
1 parent 4a77a4c commit d5ad2e9

File tree

17 files changed

+271
-243
lines changed

17 files changed

+271
-243
lines changed

packages/protobuf-bench/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ server would usually do.
1010

1111
| code generator | bundle size | minified | compressed |
1212
|---------------------|------------------------:|-----------------------:|-------------------:|
13-
| protobuf-es | 126,284 b | 65,142 b | 15,854 b |
13+
| protobuf-es | 125,973 b | 65,131 b | 15,859 b |
1414
| protobuf-javascript | 394,384 b | 288,654 b | 45,122 b |

packages/protobuf-test/src/create.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { protoInt64 } from "@bufbuild/protobuf";
1717
import type { MessageInitShape } from "@bufbuild/protobuf";
1818
import { create, isFieldSet, isMessage } from "@bufbuild/protobuf";
1919
import { describe, expect, test } from "@jest/globals";
20-
import { localName, reflect } from "@bufbuild/protobuf/reflect";
20+
import { reflect } from "@bufbuild/protobuf/reflect";
2121
import * as example_ts from "./gen/ts/extra/example_pb.js";
2222
import * as proto3_ts from "./gen/ts/extra/proto3_pb.js";
2323
import * as proto2_ts from "./gen/ts/extra/proto2_pb.js";
@@ -596,7 +596,7 @@ describe("create()", () => {
596596
test.each(desc.fields)("$name", (f) => {
597597
const init: Record<string, unknown> = {};
598598
for (const f of desc.members) {
599-
init[localName(f)] = null;
599+
init[f.localName] = null;
600600
}
601601
const msg = create(desc, init);
602602
const r = reflect(desc, msg);
@@ -613,7 +613,7 @@ describe("create()", () => {
613613
test.each(desc.fields)("$name", (f) => {
614614
const init: Record<string, unknown> = {};
615615
for (const f of desc.members) {
616-
init[localName(f)] = undefined;
616+
init[f.localName] = undefined;
617617
}
618618
const msg = create(desc, init);
619619
const r = reflect(desc, msg);

packages/protobuf-test/src/fields.test.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import { beforeEach, describe, expect, test } from "@jest/globals";
1616
import type { DescMessage } from "@bufbuild/protobuf";
1717
import { clearField, create, isFieldSet } from "@bufbuild/protobuf";
18-
import { localName } from "@bufbuild/protobuf/reflect";
1918
import * as proto3_ts from "./gen/ts/extra/proto3_pb.js";
2019
import * as proto2_ts from "./gen/ts/extra/proto2_pb.js";
2120
import * as edition2023_ts from "./gen/ts/extra/edition2023_pb.js";
@@ -55,7 +54,7 @@ describe("isFieldSet()", () => {
5554
const desc = proto3_ts.Proto3MessageDesc;
5655
test.each(desc.fields)("%s is initially unset", (field) => {
5756
const msg = create(desc);
58-
const set = isFieldSet(desc as DescMessage, msg, localName(field));
57+
const set = isFieldSet(desc as DescMessage, msg, field.localName);
5958
expect(set).toBe(false);
6059
});
6160
test.each(fillProto3MessageNames())("%s is set", (name) => {
@@ -69,7 +68,7 @@ describe("isFieldSet()", () => {
6968
const desc = proto2_ts.Proto2MessageDesc;
7069
test.each(desc.fields)("%s is initially unset", (field) => {
7170
const msg = create(desc);
72-
const set = isFieldSet(desc as DescMessage, msg, localName(field));
71+
const set = isFieldSet(desc as DescMessage, msg, field.localName);
7372
expect(set).toBe(false);
7473
});
7574
test.each(fillProto2MessageNames())("%s is set", (name) => {
@@ -83,7 +82,7 @@ describe("isFieldSet()", () => {
8382
const desc = edition2023_ts.Edition2023MessageDesc;
8483
test.each(desc.fields)("%s is initially unset", (field) => {
8584
const msg = create(desc);
86-
const set = isFieldSet(desc as DescMessage, msg, localName(field));
85+
const set = isFieldSet(desc as DescMessage, msg, field.localName);
8786
expect(set).toBe(false);
8887
});
8988
test.each(fillEdition2023MessageNames())("%s is set", (name) => {

packages/protobuf-test/src/helpers.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import type { DescMessage } from "@bufbuild/protobuf";
1616
import { UpstreamProtobuf } from "upstream-protobuf";
17-
import { createFileRegistry, localName } from "@bufbuild/protobuf/reflect";
17+
import { createFileRegistry } from "@bufbuild/protobuf/reflect";
1818
import * as proto3_ts from "./gen/ts/extra/proto3_pb.js";
1919
import type { DescField } from "@bufbuild/protobuf";
2020
import { fromBinary } from "@bufbuild/protobuf";
@@ -118,7 +118,7 @@ export function getFieldByLocalName(
118118
fieldKind?: string,
119119
): DescField {
120120
const field = proto3_ts.Proto3MessageDesc.fields.find(
121-
(f) => localName(f) === name,
121+
(f) => f.localName === name,
122122
);
123123
if (!field) {
124124
throw new Error(`getFieldByLocalName: ${name} not found`);

packages/protobuf-test/src/reflect/names.test.ts

+2-128
Original file line numberDiff line numberDiff line change
@@ -13,134 +13,8 @@
1313
// limitations under the License.
1414

1515
import { describe, expect, test } from "@jest/globals";
16-
import {
17-
safeObjectProperty,
18-
protoCamelCase,
19-
localName,
20-
} from "@bufbuild/protobuf/reflect";
21-
import { compileEnum, compileField, compileService } from "../helpers.js";
22-
23-
describe("localName", () => {
24-
describe("with field", () => {
25-
test("applies protoCamelCase", async () => {
26-
const field = await compileField(`
27-
syntax="proto3";
28-
message M {
29-
int32 __proto__ = 1;
30-
}
31-
`);
32-
expect(localName(field)).toBe("Proto");
33-
});
34-
test("escapes reserved property name", async () => {
35-
const field = await compileField(`
36-
syntax="proto3";
37-
message M {
38-
int32 constructor = 1;
39-
}
40-
`);
41-
expect(localName(field)).toBe("constructor$");
42-
});
43-
});
44-
describe("with field in oneof", () => {
45-
test("applies protoCamelCase", async () => {
46-
const field = await compileField(`
47-
syntax="proto3";
48-
message M {
49-
oneof kind {
50-
int32 __proto__ = 1;
51-
}
52-
}
53-
`);
54-
expect(field.oneof).toBeDefined();
55-
expect(localName(field)).toBe("Proto");
56-
});
57-
test("does not escape reserved property name", async () => {
58-
const field = await compileField(`
59-
syntax="proto3";
60-
message M {
61-
oneof kind {
62-
int32 constructor = 1;
63-
}
64-
}
65-
`);
66-
expect(field.oneof).toBeDefined();
67-
expect(localName(field)).toBe("constructor");
68-
});
69-
});
70-
describe("with enum value", () => {
71-
test("does not change case", async () => {
72-
const value = (
73-
await compileEnum(`
74-
syntax="proto3";
75-
enum E {
76-
FooBAR_baz_1 = 0;
77-
}
78-
`)
79-
).values[0];
80-
expect(localName(value)).toBe("FooBAR_baz_1");
81-
});
82-
test("drops prefix", async () => {
83-
const value = (
84-
await compileEnum(`
85-
syntax="proto3";
86-
enum PrefixEnum {
87-
PREFIX_ENUM_ZERO = 0;
88-
PREFIX_ENUM_ONE = 1;
89-
}
90-
`)
91-
).values[0];
92-
expect(localName(value)).toBe("ZERO");
93-
});
94-
test("escapes reserved property name", async () => {
95-
const value = (
96-
await compileEnum(`
97-
syntax="proto3";
98-
enum EnumBuiltIn {
99-
constructor = 0;
100-
}
101-
`)
102-
).values[0];
103-
expect(localName(value)).toBe("constructor$");
104-
});
105-
test("escapes reserved property name with dropped prefix", async () => {
106-
const value = (
107-
await compileEnum(`
108-
syntax="proto3";
109-
enum EnumBuiltInPrefixed {
110-
ENUM_BUILT_IN_PREFIXED_constructor = 0;
111-
}
112-
`)
113-
).values[0];
114-
expect(localName(value)).toBe("constructor$");
115-
});
116-
});
117-
describe("with rpc", () => {
118-
test("makes first letter lowerCase", async () => {
119-
const rpc = (
120-
await compileService(`
121-
syntax="proto3";
122-
service Srv {
123-
rpc Foo_bar_BAZ(E) returns (E);
124-
}
125-
message E {}
126-
`)
127-
).methods[0];
128-
expect(localName(rpc)).toBe("foo_bar_BAZ");
129-
});
130-
test("escapes reserved property name", async () => {
131-
const rpc = (
132-
await compileService(`
133-
syntax="proto3";
134-
service Srv {
135-
rpc constructor(E) returns (E);
136-
}
137-
message E {}
138-
`)
139-
).methods[0];
140-
expect(localName(rpc)).toBe("constructor$");
141-
});
142-
});
143-
});
16+
import { safeObjectProperty, protoCamelCase } from "@bufbuild/protobuf/reflect";
17+
import { compileField } from "../helpers.js";
14418

14519
describe("safeObjectProperty", () => {
14620
test("escapes reserved object property names", () => {

0 commit comments

Comments
 (0)