From efbc2fd1327a5d40e87562fc44daba8fd4cc90b3 Mon Sep 17 00:00:00 2001 From: Enrique Gonzalez Martinez Date: Thu, 16 May 2024 13:03:55 +0200 Subject: [PATCH] fix protobuf generation for builtins --- .../proto/AbstractProtoGenerator.java | 35 ++++++++++++++++++- .../persistence/proto/ProtoGenerator.java | 3 ++ .../proto/ReflectionProtoGenerator.java | 10 ++---- .../deployment/JandexProtoGenerator.java | 30 ++++++++++++---- 4 files changed, 64 insertions(+), 14 deletions(-) diff --git a/kogito-codegen-modules/kogito-codegen-processes/src/main/java/org/kie/kogito/codegen/process/persistence/proto/AbstractProtoGenerator.java b/kogito-codegen-modules/kogito-codegen-processes/src/main/java/org/kie/kogito/codegen/process/persistence/proto/AbstractProtoGenerator.java index cb8365228a7..6ab1799cda4 100644 --- a/kogito-codegen-modules/kogito-codegen-processes/src/main/java/org/kie/kogito/codegen/process/persistence/proto/AbstractProtoGenerator.java +++ b/kogito-codegen-modules/kogito-codegen-processes/src/main/java/org/kie/kogito/codegen/process/persistence/proto/AbstractProtoGenerator.java @@ -34,13 +34,20 @@ import java.util.stream.Collectors; import org.drools.codegen.common.GeneratedFile; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.w3c.dom.Document; +import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import static java.lang.String.format; public abstract class AbstractProtoGenerator implements ProtoGenerator { + private static final Logger LOGGER = LoggerFactory.getLogger(AbstractProtoGenerator.class); + + protected static final List PROTO_BUILTINS = List.of(JsonNode.class.getName(), Document.class.getName()); private static final String GENERATED_PROTO_RES_PATH = "persistence/protobuf/"; private static final String LISTING_FILE = "list.json"; @@ -59,6 +66,11 @@ public Proto protoOfDataClasses(String packageName, String... headers) { return generate(null, null, packageName, dataClasses, headers); } + @Override + public List protoBuiltins() { + return PROTO_BUILTINS; + } + @Override public Collection generateProtoFiles() { validateClasses(); @@ -98,6 +110,10 @@ private void validateClasses() { protected abstract boolean isEnum(T dataModel); + protected Optional fqn(T dataModel) { + return extractName(dataModel); + } + protected abstract Optional extractName(T dataModel); protected abstract ProtoEnum enumFromClass(Proto proto, T clazz) throws Exception; @@ -152,8 +168,10 @@ protected Proto generate(String messageComment, String fieldComment, String pack protected Proto generate(String messageComment, String fieldComment, String packageName, Collection dataModels, String... headers) { Proto proto = new Proto(packageName, headers); Set alreadyGenerated = new HashSet<>(); - for (T dataModel : dataModels) { + alreadyGenerated.addAll(protoBuiltins()); + for (T dataModel : dataModels.stream().filter(this::filterDataModels).toList()) { try { + LOGGER.info("internal proto geneartion {}", fqn(dataModel)); internalGenerate( proto, alreadyGenerated, @@ -167,6 +185,15 @@ protected Proto generate(String messageComment, String fieldComment, String pack return proto; } + private boolean filterDataModels(T type) { + Optional stringType = fqn(type); + if (stringType.isEmpty()) { + return false; + } + + return !protoBuiltins().contains(stringType.get()); + } + protected abstract String modelClassName(T dataModel); protected Optional internalGenerate(Proto proto, Set alreadyGenerated, String messageComment, String fieldComment, T dataModel) throws Exception { @@ -214,6 +241,11 @@ protected String computeCardinalityModifier(String type) { } protected String protoType(String type) { + if (protoBuiltins().contains(type)) { + return null; + } + LOGGER.debug("Computing proto type for {}", type); + if (String.class.getCanonicalName().equals(type) || String.class.getSimpleName().equalsIgnoreCase(type)) { return "string"; } else if (Integer.class.getCanonicalName().equals(type) || "int".equalsIgnoreCase(type)) { @@ -272,4 +304,5 @@ protected boolean hasDefaultConstructor(Class cls) { } return false; } + } diff --git a/kogito-codegen-modules/kogito-codegen-processes/src/main/java/org/kie/kogito/codegen/process/persistence/proto/ProtoGenerator.java b/kogito-codegen-modules/kogito-codegen-processes/src/main/java/org/kie/kogito/codegen/process/persistence/proto/ProtoGenerator.java index 0f3347f1a49..9009a29c7ec 100644 --- a/kogito-codegen-modules/kogito-codegen-processes/src/main/java/org/kie/kogito/codegen/process/persistence/proto/ProtoGenerator.java +++ b/kogito-codegen-modules/kogito-codegen-processes/src/main/java/org/kie/kogito/codegen/process/persistence/proto/ProtoGenerator.java @@ -19,6 +19,7 @@ package org.kie.kogito.codegen.process.persistence.proto; import java.util.Collection; +import java.util.List; import org.drools.codegen.common.GeneratedFile; import org.drools.codegen.common.GeneratedFileType; @@ -36,6 +37,8 @@ public interface ProtoGenerator { Collection generateProtoFiles(); + List protoBuiltins(); + interface Builder { Builder withDataClasses(Collection dataClasses); diff --git a/kogito-codegen-modules/kogito-codegen-processes/src/main/java/org/kie/kogito/codegen/process/persistence/proto/ReflectionProtoGenerator.java b/kogito-codegen-modules/kogito-codegen-processes/src/main/java/org/kie/kogito/codegen/process/persistence/proto/ReflectionProtoGenerator.java index cd348c681ff..730ed02dd34 100644 --- a/kogito-codegen-modules/kogito-codegen-processes/src/main/java/org/kie/kogito/codegen/process/persistence/proto/ReflectionProtoGenerator.java +++ b/kogito-codegen-modules/kogito-codegen-processes/src/main/java/org/kie/kogito/codegen/process/persistence/proto/ReflectionProtoGenerator.java @@ -44,16 +44,12 @@ import org.kie.kogito.codegen.process.persistence.ExclusionTypeUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.w3c.dom.Document; - -import com.fasterxml.jackson.databind.JsonNode; import static java.lang.String.format; public class ReflectionProtoGenerator extends AbstractProtoGenerator> { private static final Logger LOGGER = LoggerFactory.getLogger(ReflectionProtoGenerator.class); - private static final List> PROTO_BUILTINS = List.of(JsonNode.class, Document.class); private ReflectionProtoGenerator(Collection> modelClasses, Collection> dataClasses) { super(modelClasses, dataClasses); @@ -66,9 +62,12 @@ protected boolean isEnum(Class dataModel) { @Override protected ProtoMessage messageFromClass(Proto proto, Set alreadyGenerated, Class clazz, String messageComment, String fieldComment) throws Exception { + if (!shouldGenerateProto(clazz)) { + LOGGER.info("Skipping generating reflection proto for class {}", clazz); return null; } + LOGGER.debug("Generating reflection proto for class {}", clazz); String clazzName = extractName(clazz).get(); ProtoMessage message = new ProtoMessage(clazzName, clazz.getPackage().getName()); @@ -130,9 +129,6 @@ protected ProtoMessage messageFromClass(Proto proto, Set alreadyGenerate } protected boolean shouldGenerateProto(Class clazz) { - if (PROTO_BUILTINS.contains(clazz)) { - return false; - } return extractName(clazz).isPresent(); } diff --git a/quarkus/extensions/kogito-quarkus-workflow-extension-common/kogito-quarkus-workflow-common-deployment/src/main/java/org/kie/kogito/quarkus/workflow/deployment/JandexProtoGenerator.java b/quarkus/extensions/kogito-quarkus-workflow-extension-common/kogito-quarkus-workflow-common-deployment/src/main/java/org/kie/kogito/quarkus/workflow/deployment/JandexProtoGenerator.java index 558a042ae09..08ac15a972f 100644 --- a/quarkus/extensions/kogito-quarkus-workflow-extension-common/kogito-quarkus-workflow-common-deployment/src/main/java/org/kie/kogito/quarkus/workflow/deployment/JandexProtoGenerator.java +++ b/quarkus/extensions/kogito-quarkus-workflow-extension-common/kogito-quarkus-workflow-common-deployment/src/main/java/org/kie/kogito/quarkus/workflow/deployment/JandexProtoGenerator.java @@ -54,7 +54,7 @@ import static java.util.stream.Collectors.toList; public class JandexProtoGenerator extends AbstractProtoGenerator { - + private static final Logger LOGGER = LoggerFactory.getLogger(JandexProtoGenerator.class); private static final DotName ENUM_VALUE_ANNOTATION = DotName.createSimple(ProtoEnumValue.class.getName()); private static final DotName generatedAnnotation = DotName.createSimple(Generated.class.getCanonicalName()); private static final DotName variableInfoAnnotation = DotName.createSimple(VariableInfo.class.getCanonicalName()); @@ -92,15 +92,29 @@ protected Optional extractName(ClassInfo clazz) { return Optional.of(name); } + protected Optional fqn(ClassInfo dataModel) { + if (isHidden(dataModel)) { + // since class is marked as hidden skip processing of that class + return Optional.empty(); + } + + String name = dataModel.simpleName(); + String altName = getReferenceOfModel(dataModel, "name"); + if (altName != null) { + name = altName; + } + return Optional.of(dataModel.name().packagePrefix() + "." + name); + } + @Override protected ProtoMessage messageFromClass(Proto proto, Set alreadyGenerated, ClassInfo clazz, String messageComment, String fieldComment) throws Exception { - Optional optionalName = extractName(clazz); - if (!optionalName.isPresent()) { - // if name cannot be extracted let skip the object + if (!shouldGenerateProto(clazz)) { + LOGGER.info("Skipping generating jandex proto for class {}", clazz); return null; } + LOGGER.debug("Generating reflection proto for class {}", clazz); - String name = optionalName.get(); + String name = extractName(clazz).get(); ProtoMessage message = new ProtoMessage(name, clazz.name().prefix().toString()); for (FieldInfo pd : extractAllFields(clazz)) { @@ -157,7 +171,7 @@ protected ProtoMessage messageFromClass(Proto proto, Set alreadyGenerate protoType = optionalProtoType.get(); } - ProtoField protoField = message.addField(applicabilityByType(fieldTypeString), protoType, pd.name()); + ProtoField protoField = message.addField(computeCardinalityModifier(fieldTypeString), protoType, pd.name()); protoField.setComment(completeFieldComment); if (KOGITO_SERIALIZABLE.equals(protoType)) { protoField.setOption(format("[(%s) = \"%s\"]", KOGITO_JAVA_CLASS_OPTION, fieldTypeString.equals(ARRAY) ? pd.type().toString() : pd.type().name().toString())); @@ -168,6 +182,10 @@ protected ProtoMessage messageFromClass(Proto proto, Set alreadyGenerate return message; } + protected boolean shouldGenerateProto(ClassInfo clazz) { + return extractName(clazz).isPresent(); + } + private boolean isCollection(FieldInfo pd) { if (pd.type().kind() == Kind.PARAMETERIZED_TYPE || pd.type().kind() == Kind.CLASS) { try {