将复杂的 Java 类 建模到协议缓冲区中
Modeling complex Java classes into Protocol Buffers
在 Protocol Buffer (.proto) 中表示它的最佳方式是什么?
public class EntityId {
private String type;
private String id;
}
public class EntityBlob {
private String blobName;
private byte[] blobBytes;
}
public class Entity {
private String dir;
private String entityType;
private String entityId;
private Set<EntityBlob> blobs;
private Map<String,EntityProperty> properties;
private Multimap<String, EntityId> relatedEntities;
}
public abstract class EntityProperty<T> {
// ...
}
// Example concrete EntityProperty:
public class EntityStringProperty extends EntityProperty<String> {
public EntityStringProperty(String value) {
super(value);
}
}
其中字段properties
只能接受以下EntityStringProperty
、EntityBooleanProperty
、EntityDoubleProperty
等
有些特别类:
public class EntityArrayProperty extends EntityProperty<List<EntityProperty>> {
public EntityArrayProperty(List<EntityProperty> value) {
super(value);
}
}
public class EntityObjectProperty extends EntityProperty<Map<String, EntityProperty>> {
public EntityObjectProperty(Map<String, EntityProperty> value) {
super(value);
}
}
如何使用 Protocol Buffers 对这种复杂的 类 进行建模?具体来说 Map<String,EntityProperty> properties
?
Map
属性 还不错:maps are supported in protobuf。对于 属性 类型,您将使用包装 oneof 的消息。所以它会像
message Entity {
string dir = 1;
string entity_type = 2;
string entity_id = 3;
repeated EntityBlob blobs = 4;
map<string, EntityProperty> properties = 5;
map<string, EntityIdList> related_entities = 6;
}
message EntityProperty {
oneof property_value {
string string_value = 1;
EntityArrayProperty array_value = 2;
EntityObjectProperty object_value = 3;
bool bool_value = 4;
double double_value = 5;
}
}
message EntityArrayProperty {
repeated EntityProperty values = 1;
}
message EntityObjectProperty {
map<string, EntityProperty> property_map = 1;
}
message EntityIdList {
repeated EntityId ids = 1;
}
message EntityBlob {
string blob_name = 1;
bytes blob_bytes = 2;
}
message EntityId {
string type = 1;
string id = 2;
}
另外,看起来 EntityProperty
可能等同于 google.protobuf.Value
,因此您可能不必自己编写,但可以使用预定义的消息类型。
在 Protocol Buffer (.proto) 中表示它的最佳方式是什么?
public class EntityId {
private String type;
private String id;
}
public class EntityBlob {
private String blobName;
private byte[] blobBytes;
}
public class Entity {
private String dir;
private String entityType;
private String entityId;
private Set<EntityBlob> blobs;
private Map<String,EntityProperty> properties;
private Multimap<String, EntityId> relatedEntities;
}
public abstract class EntityProperty<T> {
// ...
}
// Example concrete EntityProperty:
public class EntityStringProperty extends EntityProperty<String> {
public EntityStringProperty(String value) {
super(value);
}
}
其中字段properties
只能接受以下EntityStringProperty
、EntityBooleanProperty
、EntityDoubleProperty
等
有些特别类:
public class EntityArrayProperty extends EntityProperty<List<EntityProperty>> {
public EntityArrayProperty(List<EntityProperty> value) {
super(value);
}
}
public class EntityObjectProperty extends EntityProperty<Map<String, EntityProperty>> {
public EntityObjectProperty(Map<String, EntityProperty> value) {
super(value);
}
}
如何使用 Protocol Buffers 对这种复杂的 类 进行建模?具体来说 Map<String,EntityProperty> properties
?
Map
属性 还不错:maps are supported in protobuf。对于 属性 类型,您将使用包装 oneof 的消息。所以它会像
message Entity {
string dir = 1;
string entity_type = 2;
string entity_id = 3;
repeated EntityBlob blobs = 4;
map<string, EntityProperty> properties = 5;
map<string, EntityIdList> related_entities = 6;
}
message EntityProperty {
oneof property_value {
string string_value = 1;
EntityArrayProperty array_value = 2;
EntityObjectProperty object_value = 3;
bool bool_value = 4;
double double_value = 5;
}
}
message EntityArrayProperty {
repeated EntityProperty values = 1;
}
message EntityObjectProperty {
map<string, EntityProperty> property_map = 1;
}
message EntityIdList {
repeated EntityId ids = 1;
}
message EntityBlob {
string blob_name = 1;
bytes blob_bytes = 2;
}
message EntityId {
string type = 1;
string id = 2;
}
另外,看起来 EntityProperty
可能等同于 google.protobuf.Value
,因此您可能不必自己编写,但可以使用预定义的消息类型。