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.

5
  • This is how you can define enums: docs.oracle.com/javase/tutorial/java/javaOO/enum.html Commented Aug 23, 2015 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. Commented Aug 23, 2015 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. Commented Aug 24, 2015 at 7:13
  • Looks like this answer might be what I am looking for: stackoverflow.com/questions/28961605/…. I'll give it a go and write an answer if it actually solves my problem Commented Aug 24, 2015 at 7: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. Commented Aug 24, 2015 at 8:19

1 Answer 1

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.

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

Comments

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.