Skip to content

Commit

Permalink
Add the DescSet type (#745)
Browse files Browse the repository at this point in the history
  • Loading branch information
timostamm authored Mar 13, 2024
1 parent 0b7cee0 commit a57887e
Show file tree
Hide file tree
Showing 15 changed files with 2,840 additions and 1,960 deletions.
761 changes: 29 additions & 732 deletions packages/protobuf-test/src/descriptor-set.test.ts

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions packages/protobuf-test/src/edition-feature-resolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import type {
DescExtension,
DescMessage,
} from "@bufbuild/protobuf";
import { FileDescriptorSet } from "@bufbuild/protobuf";
import {
clearField,
createDescriptorSet,
Edition,
FeatureSet,
FeatureSet_EnumType,
Expand All @@ -40,6 +40,7 @@ import {
protoInt64,
ScalarType,
} from "@bufbuild/protobuf";
import { createDescFileSet } from "@bufbuild/protobuf/next/reflect";

/**
* A rough implementation of the edition feature resolution. Does not support
Expand Down Expand Up @@ -534,8 +535,10 @@ function unescapeBytesDefaultValue(str: string): Uint8Array | false {
}

describe("FeatureResolver", function () {
const set = createDescriptorSet(readFileSync("./descriptorset.binpb"));
const descFeatureSet = set.messages.get(FeatureSet.typeName);
const set = createDescFileSet(
FileDescriptorSet.fromBinary(readFileSync("./descriptorset.binpb")),
);
const descFeatureSet = set.getMessage(FeatureSet.typeName);
assert(descFeatureSet !== undefined);

describe("default features", () => {
Expand Down
9 changes: 5 additions & 4 deletions packages/protobuf-test/src/jstype.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
// limitations under the License.

import {
createDescriptorSet,
createRegistryFromDescriptors,
FileDescriptorSet,
LongType,
protoInt64,
} from "@bufbuild/protobuf";
import { createDescFileSet } from "@bufbuild/protobuf/next/reflect";
import { readFileSync } from "fs";
import { describe, expect, test } from "@jest/globals";
import {
Expand Down Expand Up @@ -187,8 +188,8 @@ describe("createDescriptorSet with jstype", () => {
});

describe("createRegistryFromDescriptors with jstype", () => {
const descriptorSet = createDescriptorSet(
readFileSync("./descriptorset.binpb"),
const set = createDescFileSet(
FileDescriptorSet.fromBinary(readFileSync("./descriptorset.binpb")),
);
testAllFieldsLongType("spec.JSTypeOmittedMessage", LongType.BIGINT);
testAllFieldsLongType("spec.JSTypeStringMessage", LongType.STRING);
Expand All @@ -200,7 +201,7 @@ describe("createRegistryFromDescriptors with jstype", () => {
testAllFieldsLongType("spec.JSTypeProto2NumberMessage", LongType.BIGINT);

function testAllFieldsLongType(messageTypeName: string, longType: LongType) {
const mt = descriptorSet.messages.get(messageTypeName);
const mt = set.getMessage(messageTypeName);
assert(mt);
for (const field of mt.fields) {
test(`${messageTypeName} field #${field.number}`, () => {
Expand Down
30 changes: 19 additions & 11 deletions packages/protobuf-test/src/next/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@

import { describe, test } from "@jest/globals";
import type { DescMessage } from "@bufbuild/protobuf";
import { createDescriptorSet } from "@bufbuild/protobuf";
import { FileDescriptorSet } from "@bufbuild/protobuf";
import { UpstreamProtobuf } from "upstream-protobuf";
import { createDescFileSet } from "@bufbuild/protobuf/next/reflect";

export function describeGenerated<Desc extends DescMessage>(
ts: Desc,
Expand Down Expand Up @@ -45,19 +46,26 @@ export function testGenerated<Desc extends DescMessage>(
});
}

let upstreamProtobuf: UpstreamProtobuf | undefined;

export async function compileFileDescriptorSet(
files: Record<string, string>,
): Promise<FileDescriptorSet> {
upstreamProtobuf = upstreamProtobuf ?? new UpstreamProtobuf();
const bytes = await upstreamProtobuf.compileToDescriptorSet(files, {
includeImports: true,
retainOptions: true,
});
return FileDescriptorSet.fromBinary(bytes);
}

export async function compileMessage(proto: string): Promise<DescMessage> {
const upstream = new UpstreamProtobuf();
const setBin = await upstream.compileToDescriptorSet(
{
const set = createDescFileSet(
await compileFileDescriptorSet({
"input.proto": proto,
},
{
includeImports: true,
retainOptions: true,
},
}),
);
const set = createDescriptorSet(setBin);
const file = set.files.find((f) => f.proto.name === "input.proto");
const file = set.getFile("input.proto");
if (file === undefined) {
throw new Error("missing file descriptor for input.proto");
}
Expand Down
Loading

0 comments on commit a57887e

Please sign in to comment.