Search in sources :

Example 11 with PEnumValue

use of net.morimekta.providence.PEnumValue in project providence by morimekta.

the class ConstParser method findEnumValue.

private int findEnumValue(String identifier, Token token, ThriftTokenizer tokenizer, String expectedType) throws IOException {
    String[] parts = identifier.split("[.]");
    String typeName;
    String valueName;
    if (parts.length == 3) {
        typeName = parts[0] + "." + parts[1];
        valueName = parts[2];
    } else if (parts.length == 2) {
        typeName = parts[0];
        valueName = parts[1];
    } else {
        throw tokenizer.failure(token, identifier + " is not a valid " + expectedType + " value.");
    }
    try {
        @SuppressWarnings("unchecked") PDeclaredDescriptor descriptor = registry.getDeclaredType(typeName, programContext);
        if (descriptor instanceof PEnumDescriptor) {
            PEnumDescriptor desc = (PEnumDescriptor) descriptor;
            PEnumValue value = desc.findByName(valueName);
            if (value != null) {
                return value.asInteger();
            }
        }
        throw tokenizer.failure(token, typeName + " is not an enum.");
    } catch (IllegalArgumentException e) {
        throw tokenizer.failure(token, "No type named " + typeName + ".");
    }
}
Also used : PDeclaredDescriptor(net.morimekta.providence.descriptor.PDeclaredDescriptor) PEnumValue(net.morimekta.providence.PEnumValue) PEnumDescriptor(net.morimekta.providence.descriptor.PEnumDescriptor)

Aggregations

PEnumValue (net.morimekta.providence.PEnumValue)11 PMessage (net.morimekta.providence.PMessage)6 Binary (net.morimekta.util.Binary)6 Map (java.util.Map)5 List (java.util.List)3 Set (java.util.Set)3 PEnumDescriptor (net.morimekta.providence.descriptor.PEnumDescriptor)3 PMap (net.morimekta.providence.descriptor.PMap)3 PMessageDescriptor (net.morimekta.providence.descriptor.PMessageDescriptor)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 PrintWriter (java.io.PrintWriter)2 StringReader (java.io.StringReader)2 StringWriter (java.io.StringWriter)2 Timestamp (java.sql.Timestamp)2 PMessageBuilder (net.morimekta.providence.PMessageBuilder)2 PField (net.morimekta.providence.descriptor.PField)2 PList (net.morimekta.providence.descriptor.PList)2 PSet (net.morimekta.providence.descriptor.PSet)2