2

Let's say I have the following enums defined in Java:

public enum DogBreed {
  GERMAN_SHEPHERD,
  FRENCH_BULLDOG,
  ...
}

public enum Dog {
  MAX(DogBreed.GERMAN_SHEPHERD),
  SCOOTER(DogBreed.FRENCH_BULLDOG),
  ...

  private final DogBreed breed;
}

Describing DogBreed in a proto file is simple enough, however I can't find a way to describe the Dog enum.

neric
  • 3,927
  • 3
  • 21
  • 25
  • This is how you can define enums: https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html – bobbel Aug 23 '15 at 17:31
  • @bobbel: The original poster appears to fully know how to create enum classes, but how does your link relate to his main problem -- that of representing nested enums as a proto file? I think that you might not fully understand his question. – Hovercraft Full Of Eels Aug 23 '15 at 21:36
  • Well, I thought "describing" is just another word for "writing" in this context. So, please explain to me, what you meant with "describing" this enum and what do you mean with a "proto file"? Perhaps you can give a little more context to this problem. – bobbel Aug 24 '15 at 07:13
  • Looks like this answer might be what I am looking for: http://stackoverflow.com/questions/28961605/how-to-define-value-and-id-for-enum-in-protobuf-proto-java-client. I'll give it a go and write an answer if it actually solves my problem – neric Aug 24 '15 at 07:21
  • I see... I didn't know about Protocol Buffers before.... But it's an interesting thing. Sorry for my dumb answer, this really won't help you in any way... But, if you have a solution for this, please write it here as an answer. – bobbel Aug 24 '15 at 08:19

1 Answers1

2

With the help of the answer I linked to in the comments I was able to write the following proto file:

import "google/protobuf/descriptor.proto";

extend google.protobuf.EnumValueOptions {
  DogMessage.DogBreed dogBreed = 51234;
}

message DogMessage {
  enum DogBreed {
    GERMAN_SHEPHERD = 0;
    FRENCH_BULLDOG  = 1;
  }

  enum  Dog {
    MAX     = 0 [(dogBreed) = GERMAN_SHEPHERD];
    SCOOTER = 1 [(dogBreed) = FRENCH_BULLDOG];
  }
}

The value of dogBreed is then accessible on the client side using getValueDescriptor.getOptions()

However I realized I actually don't need it in the end. I was trying to replicate exactly my data model class.That's because I come from a JSON serialization world with Jackson where you send and receive exactly your data model classes.
But since protobuf uses an intermediate representation class I might as well write the following:

message DogMessage {
  string dogName  = 0;
  string dogBreed = 1;
}

And I'll be able to reconstruct my data model Enums based on those string values on the other side.

Thanks for looking into this

EDIT: Further realization: it doesn't matter what the Dog enum is made of. As long as I transfer one of it's value (MAX, SCOOTER), I'll be able to reconstruct the enum fully, based on this single value.

neric
  • 3,927
  • 3
  • 21
  • 25