从协议缓冲区字段中获取骆驼名称

发布于 2024-10-10 02:00:26 字数 1189 浏览 0 评论 0原文

我有一个这样的协议缓冲区消息:

    message Person {

    optional string last_name = 1; 
    optional string first_name = 2;

    }

java 生成的类如下所示:

 public static final class Person extends
      com.google.protobuf.GeneratedMessage {
    // Use Person.newBuilder() to construct.
    ...... constructor stuffs

    // optional string first_name = 1;
    public static final int FIRST_NAME_FIELD_NUMBER = 1;
    private boolean hasFirstName;
    private java.lang.String firstName_ = "";
    public boolean hasFirstName() { return hasFirstName; }
    public java.lang.String getFirstName() { return firstName_; }

    // optional string last_name = 2;
    public static final int LAST_NAME_FIELD_NUMBER = 2;
    private boolean hasLastName;
    private java.lang.String lastName_ = "";
    public boolean hasLastName() { return hasLastName; }
    public java.lang.String getLastName() { return lastName_; }
  ............
}

生成 java 对象时,它通过调用 getLastName() 和 getFirstName() 方法使用驼峰字段名称作为 lastName, firstName 。有没有办法获取骆驼字段名称?我不想获取原始字段名称 name :last_name、first_name 并在每次我想做的时候再次将其转换为驼峰字段名称 我的 java 对象上的 getter 和 setter。

I have a protocol buffer message like this:

    message Person {

    optional string last_name = 1; 
    optional string first_name = 2;

    }

The java generated class looks like this:

 public static final class Person extends
      com.google.protobuf.GeneratedMessage {
    // Use Person.newBuilder() to construct.
    ...... constructor stuffs

    // optional string first_name = 1;
    public static final int FIRST_NAME_FIELD_NUMBER = 1;
    private boolean hasFirstName;
    private java.lang.String firstName_ = "";
    public boolean hasFirstName() { return hasFirstName; }
    public java.lang.String getFirstName() { return firstName_; }

    // optional string last_name = 2;
    public static final int LAST_NAME_FIELD_NUMBER = 2;
    private boolean hasLastName;
    private java.lang.String lastName_ = "";
    public boolean hasLastName() { return hasLastName; }
    public java.lang.String getLastName() { return lastName_; }
  ............
}

When java object is generated it uses camel field name as lastName, firstName through method call getLastName() and getFirstName(). Is there a way to get the camel field name out? I don't want to get the original field name name : last_name, first_name and convert it again to camel field name every time that I want to do
getter and setter on my java object.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

欢你一世 2024-10-17 02:00:26

复制 Kenton Varda 在 protobuf列表

不,驼峰命名法名称不会存储在任何地方。你需要构建
你自己吧。如果性能是一个问题,只需将结果缓存在
地图。

Copying in an answer (hence wiki, as not my answer) from Kenton Varda on the protobuf list:

No, the camel-case name is not stored anywhere. You will need to construct
it yourself. If performance is a concern, just cache the results in a
Map.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文