This awesome question asks almost exactly the same question, "how can I replace an existing Java enum with value data with a Proto enum", except I have a twist: my original java Enum implements an interface.
I have this Java enum implementing an interface, and I want to convert it to a Proto Enum:
interface VehicleWithModelName {
String getCarModel();
}
public enum CarModel implements VehicleWithModelName {
NOMODEL("NOMODEL"),
X("X"),
XS("XS");
private final String carModel;
CarModel(String carModel) {
this.carModel = carModel;
}
@Override
public String getCarModel() { return carModel; }
}
Now, I can reproduce the enum that carries a string using the awesome answer to that question:
extend google.protobuf.EnumValueOptions {
optional string car_name = 50000;
// Be sure to read the docs about choosing the number here.
}
enum CarModelProtoEnum {
NOMODEL = 0 [(car_name) = "NOMODEL"];
X = 1 [(car_name) = "X"];
XS = 2 [(car_name) = "XS"];
}
But now this new Proto generated Enum class doesn't implement my interface. :( Is there a way to either
A) add an option telling the generated code to in fact implement the interface and also supply the option to the Overridden method? (seems unlikely) or
B) somehow wrap the generated Proto Enum in an adapter or a wrapper class that exposes an enum-like interface but still implements the interface?
I want to be able to continue to support legacy code that expects CarModel to both 1. Be an enum and 2. implement VehicleWithModelName. i.e. both of these need to work:
void PrintModelName(VehicleWithModelName v);
CarModel xs = CarModel.XS; // 1. Use CarModel like an enum.
PrintModelName(xs); // 2. Use CarModel like a VehicleWithModelName.
What I really want to do is this:
public class CarModel extends CarModelProtoEnum implements VehicleWithModelName {
@Override
public String getCarModel() { return getValueDescriptor()
.getOptions().getExtension(CarModel.carName); }
}
But of course Java enums are final. Any other ideas?
Aucun commentaire:
Enregistrer un commentaire