How do we set values for Enum & map data types

893 views
Skip to first unread message

Jalaja Raj

unread,
Jul 18, 2022, 12:17:24 PM7/18/22
to Protocol Buffers
Hello,

Below is a snippet of my proto file

message UnitType {
    enum E {
        // Default - do not use
        UNDEFINED = 0;
        INTEGER = 1;
        DECIMAL = 2;
    }
}
message AdditionalKey {
    string name = 1;
    // If expected_values is empty, the value of the additional key can be anything.
    // Otherwise, it should be one of the expected_values.
    repeated string expected_values = 2;
}

// Definition of an accumulator for validation on writes
message Definition {
    UnitType.E unit_type = 1;
    // The stage in the adjudication pipeline that creates accumulators of this definition.
    UnitType.E stage = 3;
    repeated AdditionalKey additional_keys = 2;
}

message ReadAccumulatorDefinitionsResponse {
    // definitions is a map of accumulator name to corresponding definition.
    // accumulator names that were not found in the database
    // will not be included in the definitions map.
    map<string, Definition> definitions = 1;
}

I am trying to construct a test.java class for setting values to instances of the above messages and the code snippet is as follows: -

package accumulators;
import static org.testng.Assert.assertNotNull;

import com.OscarHealth.accumulators.ReadAccumulatorNamesRequest;
import com.OscarHealth.accumulators.UnitType;
import com.OscarHealth.accumulators.UnitType.Builder;
import com.OscarHealth.accumulators.UnitType.E;
public class accumulatorsTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
       
        /* ReadAccumulatorNamesRequest.Builder builder = ReadAccumulatorNamesRequest.newBuilder();
        ReadAccumulatorNamesRequest req1 = builder
            .setStagesValue(2, 10)
            .build();
       
        assertNotNull(req1); */
       
        UnitType.Builder build = UnitType.newBuilder();
        UnitType req2 = ((Builder) build
                .getDefaultInstanceForType()
                .getField(null))
                .build();
       

    }

}

However, I am not really sure on the correct values to be passed while setting Enaums & Map data types.

Kindly suggest and guide me here

Jalaja Raj

unread,
Jul 19, 2022, 7:40:00 AM7/19/22
to Protocol Buffers
Kindly share your thoughts/suggestions

David Jones

unread,
Jul 19, 2022, 3:23:07 PM7/19/22
to Jalaja Raj, Protocol Buffers
I think I understand your example (there seem to be some things missing, or maybe some typos?).

For enums, you should stick to the enumerator objects if you can, e.g. (this is just a hypothetical example):
  builder.setStage(E.INTEGER)
In proto3, you can also use the int value if you need to:
  builder.setStageValue(1)
See also:

For map fields, the generated code includes "put" functions, e.g.:
  String key = /* etc. */;
  builder.putDefinitions(key, Definition.newBuilder()./*...*/.build());
See also:

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/protobuf/905c8204-11a8-45e8-b94b-ea62fc9b525fn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages