Search in sources :

Example 1 with EnumElement

use of com.squareup.protoparser.EnumElement in project teiid by teiid.

the class ProtobufMetadataProcessor method toTeiidSchema.

private void toTeiidSchema(String name, String contents, MetadataFactory mf, String cacheName) throws TranslatorException {
    LogManager.logDetail(LogConstants.CTX_CONNECTOR, "Processing Proto file:", name, "  with contents\n", contents);
    ProtoFile protoFile = ProtoParser.parse(name, contents);
    List<MessageElement> messageTypes = filter(protoFile.typeElements(), MessageElement.class);
    List<EnumElement> enumTypes = filter(protoFile.typeElements(), EnumElement.class);
    // add tables
    HashSet<String> deleteTables = new HashSet<>();
    for (MessageElement messageElement : messageTypes) {
        Table t = addTable(mf, messageTypes, enumTypes, messageElement, null, deleteTables);
        if (t != null) {
            if (t.getAnnotation() != null && findFromAnnotation(AT_CACHE, t.getAnnotation(), "name") != null) {
                t.setProperty(CACHE, findFromAnnotation(AT_CACHE, t.getAnnotation(), "name"));
            } else {
                // in metadata, as both parent child must be in single cache.
                if (getParentTag(t) == -1) {
                    t.setProperty(CACHE, cacheName);
                }
            }
        }
    }
    for (String tableName : deleteTables) {
        mf.getSchema().removeTable(tableName);
    }
}
Also used : Table(org.teiid.metadata.Table) ProtoFile(com.squareup.protoparser.ProtoFile) MessageElement(com.squareup.protoparser.MessageElement) EnumElement(com.squareup.protoparser.EnumElement) HashSet(java.util.HashSet)

Example 2 with EnumElement

use of com.squareup.protoparser.EnumElement in project teiid by teiid.

the class ProtobufMetadataProcessor method isEnum.

private boolean isEnum(List<MessageElement> messageTypes, List<EnumElement> enumTypes, DataType type) {
    if (type instanceof DataType.NamedType) {
        for (EnumElement element : enumTypes) {
            if (element.name().equals(((DataType.NamedType) type).name())) {
                return true;
            }
        }
        // enum does not nest, messages nest
        for (MessageElement element : messageTypes) {
            List<MessageElement> childMessageTypes = filter(element.nestedElements(), MessageElement.class);
            List<EnumElement> childEnumTypes = filter(element.nestedElements(), EnumElement.class);
            if (isEnum(childMessageTypes, childEnumTypes, type)) {
                return true;
            }
        }
    }
    return false;
}
Also used : DataType(com.squareup.protoparser.DataType) MessageElement(com.squareup.protoparser.MessageElement) EnumElement(com.squareup.protoparser.EnumElement)

Example 3 with EnumElement

use of com.squareup.protoparser.EnumElement in project teiid by teiid.

the class ProtobufMetadataProcessor method getEnumOrdinal.

private String getEnumOrdinal(List<MessageElement> messageTypes, List<EnumElement> enumTypes, String name, String value) {
    for (EnumElement element : enumTypes) {
        if (element.name().equals(name)) {
            for (EnumConstantElement constant : element.constants()) {
                if (constant.name().equals(value)) {
                    return String.valueOf(constant.tag());
                }
            }
        }
    }
    // enum does not nest, messages nest
    for (MessageElement element : messageTypes) {
        List<MessageElement> childMessageTypes = filter(element.nestedElements(), MessageElement.class);
        List<EnumElement> childEnumTypes = filter(element.nestedElements(), EnumElement.class);
        String child = getEnumOrdinal(childMessageTypes, childEnumTypes, name, value);
        if (child != null) {
            return child;
        }
    }
    return null;
}
Also used : MessageElement(com.squareup.protoparser.MessageElement) EnumConstantElement(com.squareup.protoparser.EnumConstantElement) EnumElement(com.squareup.protoparser.EnumElement)

Aggregations

EnumElement (com.squareup.protoparser.EnumElement)3 MessageElement (com.squareup.protoparser.MessageElement)3 DataType (com.squareup.protoparser.DataType)1 EnumConstantElement (com.squareup.protoparser.EnumConstantElement)1 ProtoFile (com.squareup.protoparser.ProtoFile)1 HashSet (java.util.HashSet)1 Table (org.teiid.metadata.Table)1