Skip to content

Commit

Permalink
V2: Support option retention (#827)
Browse files Browse the repository at this point in the history
  • Loading branch information
timostamm authored May 2, 2024
1 parent affbd17 commit b81a538
Show file tree
Hide file tree
Showing 17 changed files with 1,172 additions and 40 deletions.
61 changes: 61 additions & 0 deletions packages/protobuf-test/extra/option-usage.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2021-2024 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.

syntax = "proto3";
package spec;

import "extra/options.proto";

option (spec.file_option_retention_unknown) = "file option retention unknown";
option (spec.file_option_retention_runtime) = "file option retention runtime";
option (spec.file_option_retention_source) = "file option retention source";

message MessageWithOptions {
option (spec.message_option_retention_unknown) = "message option retention unknown";
option (spec.message_option_retention_runtime) = "message option retention runtime";
option (spec.message_option_retention_source) = "message option retention source";
int32 field = 1 [
(spec.field_option_retention_unknown) = "field option retention unknown",
(spec.field_option_retention_runtime) = "field option retention runtime",
(spec.field_option_retention_source) = "field option retention source"
];
oneof kind {
option (spec.oneof_option_retention_unknown) = "oneof option retention unknown";
option (spec.oneof_option_retention_runtime) = "oneof option retention runtime";
option (spec.oneof_option_retention_source) = "oneof option retention source";
int32 oneof_field = 2;
}
}

enum EnumWithOptions {
option (spec.enum_option_retention_unknown) = "enum option retention unknown";
option (spec.enum_option_retention_runtime) = "enum option retention runtime";
option (spec.enum_option_retention_source) = "enum option retention source";
ENUM_WITH_OPTIONS_UNSPECIFIED = 0 [
(spec.enum_value_option_retention_unknown) = "enum value option retention unknown",
(spec.enum_value_option_retention_runtime) = "enum value option retention runtime",
(spec.enum_value_option_retention_source) = "enum value option retention source"
];
}

service ServiceWithOptions {
option (spec.service_option_retention_unknown) = "service option retention unknown";
option (spec.service_option_retention_runtime) = "service option retention runtime";
option (spec.service_option_retention_source) = "service option retention source";
rpc Foo (MessageWithOptions) returns (MessageWithOptions) {
option (spec.method_option_retention_unknown) = "method option retention unknown";
option (spec.method_option_retention_runtime) = "method option retention runtime";
option (spec.method_option_retention_source) = "method option retention source";
}
}
66 changes: 66 additions & 0 deletions packages/protobuf-test/extra/options.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright 2021-2024 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.

syntax = "proto3";
package spec;

import "google/protobuf/descriptor.proto";

extend google.protobuf.FileOptions {
optional string file_option_retention_unknown = 70101;
optional string file_option_retention_runtime = 70102 [retention = RETENTION_RUNTIME];
optional string file_option_retention_source = 70103 [retention = RETENTION_SOURCE];
}

extend google.protobuf.MessageOptions {
optional string message_option_retention_unknown = 70201;
optional string message_option_retention_runtime = 70202 [retention = RETENTION_RUNTIME];
optional string message_option_retention_source = 70203 [retention = RETENTION_SOURCE];
}

extend google.protobuf.FieldOptions {
optional string field_option_retention_unknown = 70301;
optional string field_option_retention_runtime = 70302 [retention = RETENTION_RUNTIME];
optional string field_option_retention_source = 70303 [retention = RETENTION_SOURCE];
}

extend google.protobuf.OneofOptions {
optional string oneof_option_retention_unknown = 70401;
optional string oneof_option_retention_runtime = 70402 [retention = RETENTION_RUNTIME];
optional string oneof_option_retention_source = 70403 [retention = RETENTION_SOURCE];
}

extend google.protobuf.EnumOptions {
optional string enum_option_retention_unknown = 70501;
optional string enum_option_retention_runtime = 70502 [retention = RETENTION_RUNTIME];
optional string enum_option_retention_source = 70503 [retention = RETENTION_SOURCE];
}

extend google.protobuf.EnumValueOptions {
optional string enum_value_option_retention_unknown = 70601;
optional string enum_value_option_retention_runtime = 70602 [retention = RETENTION_RUNTIME];
optional string enum_value_option_retention_source = 70603 [retention = RETENTION_SOURCE];
}

extend google.protobuf.ServiceOptions {
optional string service_option_retention_unknown = 70701;
optional string service_option_retention_runtime = 70702 [retention = RETENTION_RUNTIME];
optional string service_option_retention_source = 70703 [retention = RETENTION_SOURCE];
}

extend google.protobuf.MethodOptions {
optional string method_option_retention_unknown = 70801;
optional string method_option_retention_runtime = 70802 [retention = RETENTION_RUNTIME];
optional string method_option_retention_source = 70803 [retention = RETENTION_SOURCE];
}
8 changes: 1 addition & 7 deletions packages/protobuf-test/src/codegenv1/boot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,12 @@ import {
createFileDescriptorProtoBoot,
embedFileDesc,
} from "@bufbuild/protobuf/codegenv1";
import { createFileRegistry } from "@bufbuild/protobuf/reflect";
import { boot } from "@bufbuild/protobuf/codegenv1";

describe("boot()", () => {
test("hydrates google/protobuf/descriptor.proto", async () => {
const fileDescriptorProto = await compileGoogleProtobufDescriptorProto();
const fileDesc = createFileRegistry(
fileDescriptorProto,
() => undefined,
).getFile(fileDescriptorProto.name);
assert(fileDesc);
const embedded = embedFileDesc(fileDesc);
const embedded = embedFileDesc(fileDescriptorProto);
assert(embedded.bootable);

const bootedFileDesc = boot(embedded.boot());
Expand Down
4 changes: 2 additions & 2 deletions packages/protobuf-test/src/codegenv1/embed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe("embedFileDesc()", () => {
int32 int32_field = 1;
}
`);
const embedded = embedFileDesc(file);
const embedded = embedFileDesc(file.proto);
expect(embedded.bootable).toBe(false);
expect(typeof embedded.base64()).toBe("string");
});
Expand All @@ -41,7 +41,7 @@ describe("embedFileDesc()", () => {
).getFile("google/protobuf/descriptor.proto");
assert(file);

const embedded = embedFileDesc(file);
const embedded = embedFileDesc(file.proto);
expect(embedded).toBeDefined();
expect(embedded.bootable).toBe(true);
if (embedded.bootable) {
Expand Down
4 changes: 2 additions & 2 deletions packages/protobuf-test/src/codegenv1/hydrate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe("fileDesc()", () => {
int32 int32_field = 1;
}
`);
const hydrated = fileDesc(embedFileDesc(file).base64());
const hydrated = fileDesc(embedFileDesc(file.proto).base64());
const field = hydrated.messages[0].fields[0];
expect(field.jsonName).toBe("int32Field");
expect(field.proto.jsonName).toBe("int32Field");
Expand All @@ -37,7 +37,7 @@ describe("fileDesc()", () => {
int32 int32_field = 1 [json_name="foo"];
}
`);
const hydrated = fileDesc(embedFileDesc(file).base64());
const hydrated = fileDesc(embedFileDesc(file.proto).base64());
const field = hydrated.messages[0].fields[0];
expect(field.jsonName).toBe("foo");
expect(field.proto.jsonName).toBe("foo");
Expand Down
83 changes: 83 additions & 0 deletions packages/protobuf-test/src/gen/js/extra/option-usage_pb.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 52 additions & 0 deletions packages/protobuf-test/src/gen/js/extra/option-usage_pb.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b81a538

Please sign in to comment.