在弃用协议缓冲区字段时是否可以添加“这就是您应该使用的”消息?

问题描述 投票:0回答:1

我有一个 Web 服务,迄今为止我们一直在使用整数 ID 或 guid 值来引用数据库中的内容。

message Request {
  sint64 Id = 1;
  optional string Guid = 2;
  optional bool GetSoftDeletedRecords = 3;
}

我想更新要弃用的 ID 值,并强制用户仅使用 GUID 值。我知道我能行

message Request {
  sint64 Id = 1 [deprecated = true];
  optional string Guid = 2;
  optional bool GetSoftDeletedRecords = 3;
}

但我想添加一条简单的消息

Please use "Guid" field instead
呈现给用户。我正在使用 c#,所以我很确定 [deprecated = true] 会被转换为过时的属性,有没有办法确保过时的属性具有消息值?

c# protocol-buffers deprecation-warning
1个回答
0
投票

有没有办法确保过时的属性具有消息值?

不,没有。生成过时属性的代码位于 csharp_field_base.cc 中,它看起来像这样:

  if (descriptor_->options().deprecated()) {
    printer->Print("[global::System.ObsoleteAttribute]\n");
  } else if (descriptor_->type() == FieldDescriptor::TYPE_MESSAGE &&
           descriptor_->message_type()->options().deprecated()) {
    printer->Print("[global::System.ObsoleteAttribute]\n");
  }

但是,如果您对该字段本身发表评论,该评论最终将出现在摘要中。所以我就写:

message Request {
  // Use the Guid field instead.
  sint64 Id = 1 [deprecated = true];
  optional string Guid = 2;
  optional bool GetSoftDeletedRecords = 3;
}

(我个人会将这些字段重命名为更原始的常规名称,即

id
guid
get_soft_deleted_records
- 无论如何,它们都会被转换为惯用的 C# 属性名称。)

© www.soinside.com 2019 - 2024. All rights reserved.