1

What's the best way to represent this in 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);
  }
}

Where the field properties can only accept the following, EntityStringProperty, EntityBooleanProperty, EntityDoubleProperty, etc.

With some special classes:

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);
  }
}

How can this complex classes can be modeled with Protocol Buffers? Specifically the Map<String,EntityProperty> properties?

1 Answer 1

2

The Map property isn't too bad: maps are supported in protobuf. For the property types, you'll use a message wrapping a oneof. So it'll go something like

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;
}

Alternately, it looks like EntityProperty might be equivalent to google.protobuf.Value, so you may not have to write it yourself but may be able to use the pre-defined message type.

Sign up to request clarification or add additional context in comments.

1 Comment

How about the Multimap<String, EntityId> relatedEntities it's not a map or list per se but a multi map, where the it can have the same key

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.