将带有接口字段的类转换为Protobuf。

huangapple 未分类评论41阅读模式
英文:

Convert class with interface field to protobuf

问题

syntax = "proto3";

message A {
  string name = 1;
  string occupation = 2;
  oneof operable {
    OperableA operable_a = 3;
    OperableB operable_b = 4;
  }
}

message OperableA {
  string job = 1;
  repeated string tasks = 2;
}

message OperableB {
  int32 number_of_workers = 1;
  repeated string skills = 2;
  repeated string name_of_workers = 3;
}
英文:

I have a class with interface field and the interface has multiple implementations. I need to convert my class to protobuf, but I am not able to map the interface field, which is actually a type interface.
Here is my Java code for class structure:

class A {
   String name;
   String occupation;
   Operable operable;
}
interface operable {
}
class OperableA implements operable {
    String job;
    List<String> tasks; 
}
class OperableB implements operable {
    int numberOfWorkers;
    List<String> skills;
    List<String> nameOfWorkers;
}

How can this structure be mapped in proto file, please help.

答案1

得分: 0

你不能在 Protocol Buffers 中像 这里 所描述的那样对类或接口进行继承建模,文档中提到:

> 不要寻找类继承相似的功能 - Protocol Buffers 并不支持这样的特性。

英文:

You cannot model class or interface inheritance in protobuf as described here which says

> Don't go looking for facilities similar to class inheritance, though – protocol buffers don't do that.

huangapple
  • 本文由 发表于 2020年7月24日 15:19:21
  • 转载请务必保留本文链接:https://java.coder-hub.com/63068698.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定