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

V2: Add localName property to DescField and others #834

Merged
merged 2 commits into from
May 9, 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 | 126,284 b | 65,142 b | 15,854 b |
| protobuf-es | 125,973 b | 65,131 b | 15,859 b |
| protobuf-javascript | 394,384 b | 288,654 b | 45,122 b |
6 changes: 3 additions & 3 deletions packages/protobuf-test/src/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { protoInt64 } from "@bufbuild/protobuf";
import type { MessageInitShape } from "@bufbuild/protobuf";
import { create, isFieldSet, isMessage } from "@bufbuild/protobuf";
import { describe, expect, test } from "@jest/globals";
import { localName, reflect } from "@bufbuild/protobuf/reflect";
import { reflect } from "@bufbuild/protobuf/reflect";
import * as example_ts from "./gen/ts/extra/example_pb.js";
import * as proto3_ts from "./gen/ts/extra/proto3_pb.js";
import * as proto2_ts from "./gen/ts/extra/proto2_pb.js";
Expand Down Expand Up @@ -596,7 +596,7 @@ describe("create()", () => {
test.each(desc.fields)("$name", (f) => {
const init: Record<string, unknown> = {};
for (const f of desc.members) {
init[localName(f)] = null;
init[f.localName] = null;
}
const msg = create(desc, init);
const r = reflect(desc, msg);
Expand All @@ -613,7 +613,7 @@ describe("create()", () => {
test.each(desc.fields)("$name", (f) => {
const init: Record<string, unknown> = {};
for (const f of desc.members) {
init[localName(f)] = undefined;
init[f.localName] = undefined;
}
const msg = create(desc, init);
const r = reflect(desc, msg);
Expand Down
7 changes: 3 additions & 4 deletions packages/protobuf-test/src/fields.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import { beforeEach, describe, expect, test } from "@jest/globals";
import type { DescMessage } from "@bufbuild/protobuf";
import { clearField, create, isFieldSet } from "@bufbuild/protobuf";
import { localName } from "@bufbuild/protobuf/reflect";
import * as proto3_ts from "./gen/ts/extra/proto3_pb.js";
import * as proto2_ts from "./gen/ts/extra/proto2_pb.js";
import * as edition2023_ts from "./gen/ts/extra/edition2023_pb.js";
Expand Down Expand Up @@ -55,7 +54,7 @@ describe("isFieldSet()", () => {
const desc = proto3_ts.Proto3MessageDesc;
test.each(desc.fields)("%s is initially unset", (field) => {
const msg = create(desc);
const set = isFieldSet(desc as DescMessage, msg, localName(field));
const set = isFieldSet(desc as DescMessage, msg, field.localName);
expect(set).toBe(false);
});
test.each(fillProto3MessageNames())("%s is set", (name) => {
Expand All @@ -69,7 +68,7 @@ describe("isFieldSet()", () => {
const desc = proto2_ts.Proto2MessageDesc;
test.each(desc.fields)("%s is initially unset", (field) => {
const msg = create(desc);
const set = isFieldSet(desc as DescMessage, msg, localName(field));
const set = isFieldSet(desc as DescMessage, msg, field.localName);
expect(set).toBe(false);
});
test.each(fillProto2MessageNames())("%s is set", (name) => {
Expand All @@ -83,7 +82,7 @@ describe("isFieldSet()", () => {
const desc = edition2023_ts.Edition2023MessageDesc;
test.each(desc.fields)("%s is initially unset", (field) => {
const msg = create(desc);
const set = isFieldSet(desc as DescMessage, msg, localName(field));
const set = isFieldSet(desc as DescMessage, msg, field.localName);
expect(set).toBe(false);
});
test.each(fillEdition2023MessageNames())("%s is set", (name) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/protobuf-test/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import type { DescMessage } from "@bufbuild/protobuf";
import { UpstreamProtobuf } from "upstream-protobuf";
import { createFileRegistry, localName } from "@bufbuild/protobuf/reflect";
import { createFileRegistry } from "@bufbuild/protobuf/reflect";
import * as proto3_ts from "./gen/ts/extra/proto3_pb.js";
import type { DescField } from "@bufbuild/protobuf";
import { fromBinary } from "@bufbuild/protobuf";
Expand Down Expand Up @@ -118,7 +118,7 @@ export function getFieldByLocalName(
fieldKind?: string,
): DescField {
const field = proto3_ts.Proto3MessageDesc.fields.find(
(f) => localName(f) === name,
(f) => f.localName === name,
);
if (!field) {
throw new Error(`getFieldByLocalName: ${name} not found`);
Expand Down
130 changes: 2 additions & 128 deletions packages/protobuf-test/src/reflect/names.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,134 +13,8 @@
// limitations under the License.

import { describe, expect, test } from "@jest/globals";
import {
safeObjectProperty,
protoCamelCase,
localName,
} from "@bufbuild/protobuf/reflect";
import { compileEnum, compileField, compileService } from "../helpers.js";

describe("localName", () => {
describe("with field", () => {
test("applies protoCamelCase", async () => {
const field = await compileField(`
syntax="proto3";
message M {
int32 __proto__ = 1;
}
`);
expect(localName(field)).toBe("Proto");
});
test("escapes reserved property name", async () => {
const field = await compileField(`
syntax="proto3";
message M {
int32 constructor = 1;
}
`);
expect(localName(field)).toBe("constructor$");
});
});
describe("with field in oneof", () => {
test("applies protoCamelCase", async () => {
const field = await compileField(`
syntax="proto3";
message M {
oneof kind {
int32 __proto__ = 1;
}
}
`);
expect(field.oneof).toBeDefined();
expect(localName(field)).toBe("Proto");
});
test("does not escape reserved property name", async () => {
const field = await compileField(`
syntax="proto3";
message M {
oneof kind {
int32 constructor = 1;
}
}
`);
expect(field.oneof).toBeDefined();
expect(localName(field)).toBe("constructor");
});
});
describe("with enum value", () => {
test("does not change case", async () => {
const value = (
await compileEnum(`
syntax="proto3";
enum E {
FooBAR_baz_1 = 0;
}
`)
).values[0];
expect(localName(value)).toBe("FooBAR_baz_1");
});
test("drops prefix", async () => {
const value = (
await compileEnum(`
syntax="proto3";
enum PrefixEnum {
PREFIX_ENUM_ZERO = 0;
PREFIX_ENUM_ONE = 1;
}
`)
).values[0];
expect(localName(value)).toBe("ZERO");
});
test("escapes reserved property name", async () => {
const value = (
await compileEnum(`
syntax="proto3";
enum EnumBuiltIn {
constructor = 0;
}
`)
).values[0];
expect(localName(value)).toBe("constructor$");
});
test("escapes reserved property name with dropped prefix", async () => {
const value = (
await compileEnum(`
syntax="proto3";
enum EnumBuiltInPrefixed {
ENUM_BUILT_IN_PREFIXED_constructor = 0;
}
`)
).values[0];
expect(localName(value)).toBe("constructor$");
});
});
describe("with rpc", () => {
test("makes first letter lowerCase", async () => {
const rpc = (
await compileService(`
syntax="proto3";
service Srv {
rpc Foo_bar_BAZ(E) returns (E);
}
message E {}
`)
).methods[0];
expect(localName(rpc)).toBe("foo_bar_BAZ");
});
test("escapes reserved property name", async () => {
const rpc = (
await compileService(`
syntax="proto3";
service Srv {
rpc constructor(E) returns (E);
}
message E {}
`)
).methods[0];
expect(localName(rpc)).toBe("constructor$");
});
});
});
import { safeObjectProperty, protoCamelCase } from "@bufbuild/protobuf/reflect";
import { compileField } from "../helpers.js";

describe("safeObjectProperty", () => {
test("escapes reserved object property names", () => {
Expand Down
Loading