Skip to content

Commit

Permalink
Expose syntax "editions" in descriptor sets
Browse files Browse the repository at this point in the history
  • Loading branch information
timostamm committed Nov 17, 2023
1 parent d33b2ca commit 955c955
Show file tree
Hide file tree
Showing 11 changed files with 445 additions and 227 deletions.
38 changes: 38 additions & 0 deletions packages/protobuf-test/editions/edition2023-default-features.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2021-2023 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

edition = "2023";

package spec;

// This file tests the default features in the absence of any dependencies.
// Also see protobuf/src/google/protobuf/editions/proto/test_editions_default_features.proto

enum EditionsDefaultEnum {
EDITIONS_DEFAULT_ENUM_UNKNOWN = 0;
EDITIONS_DEFAULT_ENUM_VALUE1 = 1;
}

message EditionsDefaultMessage {
int32 int32_field = 1;
string string_field = 2;
EditionsDefaultEnum enum_field = 3;

repeated int32 repeated_int32_field = 4;

message SubMessage {
int32 nested_int32_field = 1;
}
SubMessage sub_message_field = 6;
}
2 changes: 1 addition & 1 deletion packages/protobuf-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"generate:js": "protoc --es_out=src/gen/js --es_opt=ts_nocheck=false,target=js+dts --proto_path=. $(buf ls-files extra) --proto_path=$(upstream-include test) $(upstream-files test) google/protobuf/type.proto",
"generate:wkt:ts": "protoc --es_out=src/gen/ts --es_opt=ts_nocheck=false,target=ts $(upstream-files wkt)",
"generate:wkt:js": "protoc --es_out=src/gen/js --es_opt=ts_nocheck=false,target=js+dts $(upstream-files wkt)",
"generate:desc": "protoc --descriptor_set_out descriptorset.bin --include_imports --include_source_info --proto_path=. --proto_path=$(upstream-include test) $(buf ls-files extra) $(upstream-files test)",
"generate:desc": "protoc --experimental_editions --descriptor_set_out descriptorset.bin --include_imports --include_source_info --proto_path=. --proto_path=$(upstream-include test) $(buf ls-files extra) $(upstream-files test) --proto_path=. $(buf ls-files editions)",
"postgenerate": "license-header src/gen",
"perf": "tsx src/perf.ts",
"test": "npm run test:bigint && npm run test:string",
Expand Down
27 changes: 26 additions & 1 deletion packages/protobuf-test/src/descriptor-set.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@

import { describe, expect, test } from "@jest/globals";
import { readFileSync } from "fs";
import { createDescriptorSet, proto3, ScalarType } from "@bufbuild/protobuf";
import {
createDescriptorSet,
Edition,
proto3,
ScalarType,
} from "@bufbuild/protobuf";
import { JsonNamesMessage } from "./gen/ts/extra/msg-json-names_pb.js";
import { MapsMessage } from "./gen/ts/extra/msg-maps_pb.js";
import { RepeatedScalarValuesMessage } from "./gen/ts/extra/msg-scalar_pb.js";
Expand All @@ -29,6 +34,26 @@ const fdsBytes = readFileSync("./descriptorset.bin");

describe("DescriptorSet", () => {
const set = createDescriptorSet(fdsBytes);
test("proto2 syntax", () => {
const descFile = set.files.find((f) => f.name == "extra/proto2");
expect(descFile).toBeDefined();
expect(descFile?.syntax).toBe("proto2");
expect(descFile?.edition).toBe(Edition.EDITION_PROTO2);
});
test("proto3 syntax", () => {
const descFile = set.files.find((f) => f.name == "extra/proto3");
expect(descFile).toBeDefined();
expect(descFile?.syntax).toBe("proto3");
expect(descFile?.edition).toBe(Edition.EDITION_PROTO3);
});
test("edition 2023", () => {
const descFile = set.files.find(
(f) => f.name == "editions/edition2023-default-features",
);
expect(descFile).toBeDefined();
expect(descFile?.syntax).toBe("editions");
expect(descFile?.edition).toBe(Edition.EDITION_2023);
});
test("knows extension", () => {
const ext = set.extensions.get(
"protobuf_unittest.optional_int32_extension",
Expand Down
Loading

0 comments on commit 955c955

Please sign in to comment.