private FieldDescriptorProto createFieldDescriptorProto(String name, int index, Type type, String typeName, Label label, String defaultValue, String extendee, UnknownFieldSet unknownFields, FieldOptions options) { FieldDescriptorProto.Builder fieldBuilder = FieldDescriptorProto.newBuilder(); return fieldBuilder .setName(name) .setNumber(index) .setType(type) .setTypeName(typeName) .setLabel(label) .setDefaultValue(defaultValue) .setExtendee(extendee) .setUnknownFields(unknownFields) .setOptions(options) .build(); }
/** * TODO (guptasu): only needed to create hard coded Types (Struct, ListValue, and Value). Check * if this can be removed. Create the Protobuf.Type instance from descriptorProto. */ private static Type createType(String typeName, DescriptorProto descriptorProto, String fileName) { Type.Builder coreTypeBuilder = Type.newBuilder().setName(typeName); int count = 1; for (FieldDescriptorProto fieldProto : descriptorProto.getFieldList()) { Field.Kind fieldKind = Field.Kind.valueOf(fieldProto.getType().getNumber()); Cardinality cardinality = Cardinality.CARDINALITY_OPTIONAL; if (fieldProto.getLabel() == Label.LABEL_REPEATED) { cardinality = Cardinality.CARDINALITY_REPEATED; } Field.Builder coreFieldBuilder = Field .newBuilder() .setName(fieldProto.getName()) .setNumber(count++) .setKind(fieldKind) .setCardinality(cardinality); if (fieldKind == Kind.TYPE_MESSAGE || fieldKind == Kind.TYPE_ENUM) { String typeFullName = fieldProto.getTypeName().startsWith(".") ? fieldProto.getTypeName().substring(1) : fieldProto.getTypeName(); coreFieldBuilder.setTypeUrl(TYPE_SERVICE_BASE_URL + typeFullName); } coreTypeBuilder.addFields(coreFieldBuilder.build()); } coreTypeBuilder.setSourceContext(SourceContext.newBuilder().setFileName(fileName)); coreTypeBuilder.setSyntax(Syntax.SYNTAX_PROTO3); return coreTypeBuilder.build(); }
protected void addColumn(Column column) { String fieldName = uniqueIdent(ident(column.getName(), false), fieldNames); fieldBuilder = messageBuilder.addFieldBuilder(); fieldBuilder.setName(fieldName); fieldBuilder.setLabel(Label.LABEL_OPTIONAL); FieldOptions.Builder fieldBuilderOptions = FieldOptions.newBuilder(); ColumnOptions.Builder columnOptions = ColumnOptions.newBuilder(); if (!fieldName.equals(column.getName())) { columnOptions.setName(column.getName()); } columnOptions.setSqlType(column.getTypeDescription().toUpperCase()); columnOptions.setUuid(column.getUuid().toString()); priorField = null; if (priorMessage != null) { for (FieldDescriptorProto field : priorMessage.getFieldList()) { FieldOptions options = field.getOptions(); if ((options != null) && (options.hasExtension(ColumnOptions.fdbsql))) { ColumnOptions coptions = options.getExtension(ColumnOptions.fdbsql); if (coptions.getUuid().equals(columnOptions.getUuid())) { priorField = field; break; } } } } setColumnType(column, columnOptions); setFieldNumber(); fieldBuilderOptions.setExtension(ColumnOptions.fdbsql, columnOptions.build()); fieldBuilder.setOptions(fieldBuilderOptions); if (column.getNullable() && ((column.getDefaultValue() != null) || (column.getDefaultFunction() != null))) { addNullForField(column.getName(), fieldBuilder.getNumber()); } }
protected void addNullForField(String columnName, int forField) { String fieldName = uniqueIdent("_" + ident(columnName, false) + "_is_null", fieldNames); fieldBuilder = messageBuilder.addFieldBuilder(); fieldBuilder.setName(fieldName); fieldBuilder.setType(Type.TYPE_BOOL); fieldBuilder.setLabel(Label.LABEL_OPTIONAL); FieldOptions.Builder fieldBuilderOptions = FieldOptions.newBuilder(); ColumnOptions.Builder columnOptions = ColumnOptions.newBuilder(); columnOptions.setNullForField(forField); priorField = null; if (priorMessage != null) { for (FieldDescriptorProto field : priorMessage.getFieldList()) { FieldOptions options = field.getOptions(); if ((options != null) && (options.hasExtension(ColumnOptions.fdbsql))) { ColumnOptions coptions = options.getExtension(ColumnOptions.fdbsql); if (coptions.hasNullForField() && (coptions.getNullForField() == forField)) { priorField = field; break; } } } } setFieldNumber(); fieldBuilderOptions.setExtension(ColumnOptions.fdbsql, columnOptions.build()); fieldBuilder.setOptions(fieldBuilderOptions); }
protected void addChildTable(Table table) { String fieldName = uniqueIdent(ident(table.getName().getTableName(), false), fieldNames); fieldBuilder = messageBuilder.addFieldBuilder(); fieldBuilder.setName(fieldName); fieldBuilder.setLabel(Label.LABEL_REPEATED); fieldBuilder.setType(Type.TYPE_MESSAGE); fieldBuilder.setTypeName(tableMessageNames.get(table)); FieldOptions.Builder fieldBuilderOptions = FieldOptions.newBuilder(); ColumnOptions.Builder columnOptions = ColumnOptions.newBuilder(); columnOptions.setUuid(table.getUuid().toString()); priorField = null; if (priorMessage != null) { for (FieldDescriptorProto field : priorMessage.getFieldList()) { FieldOptions options = field.getOptions(); if ((options != null) && (options.hasExtension(ColumnOptions.fdbsql))) { ColumnOptions coptions = options.getExtension(ColumnOptions.fdbsql); if (coptions.getUuid().equals(columnOptions.getUuid())) { priorField = field; break; } } } } setFieldNumber(); fieldBuilderOptions.setExtension(ColumnOptions.fdbsql, columnOptions.build()); fieldBuilder.setOptions(fieldBuilderOptions); }
@Override protected List<String> collectFileData() { String sourePackageName = super.getSourcePackageName(); String className = super.getClassName(); String packageName = sourePackageName.toLowerCase(); List<String> packageData = Lists.newArrayList(); packageData.add("package " + packageName + ";"); packageData.add(""); List<String> importData = Lists.newArrayList(); importData.add("import com.quancheng.saluki.serializer.ProtobufAttribute;"); importData.add("import com.quancheng.saluki.serializer.ProtobufEntity;"); List<String> classAnnotationData = Lists.newArrayList(); classAnnotationData.add(""); classAnnotationData.add("@ProtobufEntity(" + sourePackageName + "." + className + ".class)"); boolean validator = false; List<String> fileData = Lists.newArrayList(); fileData.add("public class " + className + "{"); for (int i = 0; i < messageFields.size(); i++) { FieldDescriptorProto messageField = messageFields.get(i); String javaType = findJavaType(packageName, sourceMessageDesc, messageField); if (messageField.getLabel() == Label.LABEL_REPEATED && javaType != null) { if (!javaType.contains("java.util.Map")) { javaType = "java.util.ArrayList<" + javaType + ">"; } } fileData.add(""); String fieldName = messageField.getName(); UnknownFieldSet unknownFields = messageField.getOptions().getUnknownFields(); if (unknownFields != null) { for (Map.Entry<Integer, UnknownFieldSet.Field> integerFieldEntry : unknownFields.asMap() .entrySet()) { for (ByteString byteString : integerFieldEntry.getValue().getLengthDelimitedList()) { validator = true; String validateMsg = byteString.toStringUtf8(); fileData.add(" " + validateMsg); } } } fileData.add(" @ProtobufAttribute"); fileData.add(" private " + javaType + " " + fieldName + ";"); fileData.add(""); fileData.add(" public " + javaType + " get" + captureName(fieldName) + "() {"); fileData.add(" return this." + fieldName + ";"); fileData.add(" }"); fileData.add(""); fileData.add(" public void set" + captureName(fieldName) + "(" + javaType + " " + fieldName + ") {"); fileData.add(" this." + fieldName + "=" + fieldName + ";"); fileData.add(" }"); fileData.add(""); } fileData.add("}"); if (validator) { importData.add("import com.quancheng.saluki.core.grpc.annotation.ArgValidator;"); classAnnotationData.add("@ArgValidator"); } packageData.addAll(importData); packageData.addAll(classAnnotationData); packageData.addAll(fileData); return packageData; }
private Label toLabel(Cardinality cardinality) { return Label.valueOf(cardinality.getNumber()); }
static public String getLabelName(Label l) { return labelMap.get(l); }