-
Notifications
You must be signed in to change notification settings - Fork 74
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
Remove internal fields #833
Comments
I'm not aware that google/api/visibility.proto is supported anywhere outside of google. I think the best approach would be to filter fields marked as internal before generating code. The first step is to generate code for the visibility option, for example - assuming you have npx buf generate buf.build/googleapis/googleapis --path google/api/visibility.proto This will generate the file With this extension, we can remove internal fields from a descriptorset: // filter-internal.ts
import {readFileSync, writeFileSync} from "node:fs";
import {FileDescriptorSet, getExtension, hasExtension} from "@bufbuild/protobuf";
import {field_visibility} from "./src/gen/google/api/visibility_pb";
const set = FileDescriptorSet.fromBinary(readFileSync("descriptorset.binpb"));
for (const file of set.file) {
for (const message of file.messageType) {
for (const field of message.field) {
if (field.options && hasExtension(field.options, field_visibility)) {
const visibilityRule = getExtension(field.options, field_visibility);
if (visibilityRule.restriction == "INTERNAL") {
// remove the field
message.field.splice(
message.field.indexOf(field),
1,
);
}
}
}
}
}
writeFileSync("descriptorset-filtered.binpb", set.toBinary()); Instead of generating code directly from Protobuf files, we can go from Protobuf files to a descriptor-set, remove internal fields from that descriptor-set, then generate code from the filtered set: # generate.bash
npx buf build proto -o descriptorset.binpb
npx tsx filter-internal.ts
npx buf generate descriptorset-filtered.binpb The result is generated code where fields marked with |
Thanks for the detailed response. I'll try it asap and let you know how it goes. Will it work also with internal rpc?
In the above example, we want to remove the SumBalances rpc. |
Btw, we're not limited to use INTERNAL. If you have an easier way to achieve it, we'd love to hear. |
The example above only considers fields. I'm sure you'll find it's quite easy to add the same filtering for RPCs if you peek into the code a bit, and look into descriptors. |
@timostamm it seems like we can't use buf generate descriptorset-filtered.binpb: Failure: descriptorset-filtered.binpb: not a directory |
I tested the example I gave you, so if it runs for me, it can also run for you 🙂 You're probably have a typo somewhere, or a similar small issue. Double check the command you are running that errors with "not a directory". |
I'm running |
The generate command takes an "input":
The "input" is specified here. You're probably using an old version of |
Works, thanks for your help :) |
Hi, will it work also with v2? I just saw it and it seems like it is a different Api. |
The API changes in a very straight-forward way: Methods on messages are now external functions. Here's the same script with v2: import {readFileSync, writeFileSync} from "node:fs";
import {fromBinary, toBinary, getExtension, hasExtension} from "@bufbuild/protobuf";
import {FileDescriptorSetDesc} from "@bufbuild/protobuf/wkt";
import {field_visibility} from "./src/gen/google/api/visibility_pb";
const set = fromBinary(FileDescriptorSetDesc, readFileSync("descriptorset.binpb"));
for (const file of set.file) {
for (const message of file.messageType) {
for (const field of message.field) {
if (field.options && hasExtension(field.options, field_visibility)) {
const visibilityRule = getExtension(field.options, field_visibility);
if (visibilityRule.restriction == "INTERNAL") {
// remove the field
message.field.splice(
message.field.indexOf(field),
1,
);
}
}
}
}
}
writeFileSync("descriptorset-filtered.binpb", toBinary(FileDescriptorSetDesc, set)); |
I'm trying v2 now, and have a few issues:
|
Ah, I didn't realize you use |
Oh OK. We'll wait for the releases. Thanks |
Hi @timostamm any news here? How can I track it? |
@netanel-utila, here's the PR for the connect pre-release: connectrpc/connect-es#1121 It should be available later today 🙂 |
Awesome. I'm waiting for this to release our SDK :) |
Hi, it seems that this code is broken on beta version. I don't find something useful in the change log.
|
Ok, founded it, should be FileDescriptorSetSchema now |
Works great. The code is much simpler now also when we don't have the connect files. Any estimate for official release? |
Hi, thanks for this great library. We are migrating to using it (from https://github.com/stephenh/ts-proto) and we are wondering how can we remove fields that marked as internal:
Thanks.
The text was updated successfully, but these errors were encountered: