Javascript/NodeJS 中的协议缓冲区字段选项 [英] Protocol Buffer Field Options in Javascript/NodeJS

查看:67
本文介绍了Javascript/NodeJS 中的协议缓冲区字段选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获得与协议缓冲区字段关联的选项?

How does one get the options associated with a protocol buffer field?

假设我有一个带有自定义选项的字段,例如:

Suppose I have a field with a custom option like:

syntax = "proto3";

package main;

import "google/protobuf/descriptor.proto";

extend google.protobuf.FieldOptions {
   bool required = 7000;
}

message Person {
  string name = 1 [(required) = true];
}

使用protoc

protoc -I . *.proto --js_out=import_style=commonjs,binary:js

我已从此处阅读有关如何检索其他语言选项的信息,但似乎可以在 Javascript 中工作.

I have read on how to retrieve the option in other languages from here, but can seem to get any working in Javascript.

任何帮助将不胜感激!

推荐答案

很遗憾,不支持.

其他语言嵌入了描述符"对于生成的代码中的 proto 文件.描述符包含有关消息、其字段以及自定义选项的信息,所有这些信息均采用二进制 protobuf 格式.请参阅 descriptor.proto

Other languages embed a "descriptor" for the proto file in the generated code. The descriptor contains information about a message, its fields, and also the custom options, all in binary protobuf format. See descriptor.proto

读取扩展的代码生成.如果你有一个 FieldDescriptor,你可以阅读你的 FieldOption 扩展.但是您在 Javascript 生成的代码中没有这个描述符.

The code to read the extension is generated. If you had a FieldDescriptor, you could read your FieldOption extension. But you don't have this descriptor in Javascript generated code.

有一个可能的解决方法:您可以使用 protoc 为您的 .proto 文件转储 FileDescriptorSet(请参阅 --descriptor_set_out 选项).您可以使用 Javascript(来自 google-protobuf 的 proto.google.protobuf.FileDescriptorSet)读取此二进制消息,导航到您的消息,到相关字段,然后读取您的扩展数据以获取自定义选项值.

There is a possible workaround: You can use protoc to dump a FileDescriptorSet for your .proto file (see --descriptor_set_out option). You can read this binary message using Javascript (proto.google.protobuf.FileDescriptorSet from google-protobuf), navigate to your message, to the field in question, and then read your extension data to get the custom option value.

这篇关于Javascript/NodeJS 中的协议缓冲区字段选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆