Skip to content

Commit

Permalink
fix protobuf generation for builtins
Browse files Browse the repository at this point in the history
  • Loading branch information
elguardian committed May 27, 2024
1 parent 3bee8ea commit efbc2fd
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> implements ProtoGenerator {

private static final Logger LOGGER = LoggerFactory.getLogger(AbstractProtoGenerator.class);

protected static final List<String> 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";

Expand All @@ -59,6 +66,11 @@ public Proto protoOfDataClasses(String packageName, String... headers) {
return generate(null, null, packageName, dataClasses, headers);
}

@Override
public List<String> protoBuiltins() {
return PROTO_BUILTINS;
}

@Override
public Collection<GeneratedFile> generateProtoFiles() {
validateClasses();
Expand Down Expand Up @@ -98,6 +110,10 @@ private void validateClasses() {

protected abstract boolean isEnum(T dataModel);

protected Optional<String> fqn(T dataModel) {
return extractName(dataModel);
}

protected abstract Optional<String> extractName(T dataModel);

protected abstract ProtoEnum enumFromClass(Proto proto, T clazz) throws Exception;
Expand Down Expand Up @@ -152,8 +168,10 @@ protected Proto generate(String messageComment, String fieldComment, String pack
protected Proto generate(String messageComment, String fieldComment, String packageName, Collection<T> dataModels, String... headers) {
Proto proto = new Proto(packageName, headers);
Set<String> 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,
Expand All @@ -167,6 +185,15 @@ protected Proto generate(String messageComment, String fieldComment, String pack
return proto;
}

private boolean filterDataModels(T type) {
Optional<String> stringType = fqn(type);
if (stringType.isEmpty()) {
return false;
}

return !protoBuiltins().contains(stringType.get());
}

protected abstract String modelClassName(T dataModel);

protected Optional<String> internalGenerate(Proto proto, Set<String> alreadyGenerated, String messageComment, String fieldComment, T dataModel) throws Exception {
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -272,4 +304,5 @@ protected boolean hasDefaultConstructor(Class<?> cls) {
}
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,6 +37,8 @@ public interface ProtoGenerator {

Collection<GeneratedFile> generateProtoFiles();

List<String> protoBuiltins();

interface Builder<E, T extends ProtoGenerator> {

Builder<E, T> withDataClasses(Collection<E> dataClasses);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Class<?>> {

private static final Logger LOGGER = LoggerFactory.getLogger(ReflectionProtoGenerator.class);
private static final List<Class<?>> PROTO_BUILTINS = List.of(JsonNode.class, Document.class);

private ReflectionProtoGenerator(Collection<Class<?>> modelClasses, Collection<Class<?>> dataClasses) {
super(modelClasses, dataClasses);
Expand All @@ -66,9 +62,12 @@ protected boolean isEnum(Class<?> dataModel) {

@Override
protected ProtoMessage messageFromClass(Proto proto, Set<String> 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());
Expand Down Expand Up @@ -130,9 +129,6 @@ protected ProtoMessage messageFromClass(Proto proto, Set<String> alreadyGenerate
}

protected boolean shouldGenerateProto(Class<?> clazz) {
if (PROTO_BUILTINS.contains(clazz)) {
return false;
}
return extractName(clazz).isPresent();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
import static java.util.stream.Collectors.toList;

public class JandexProtoGenerator extends AbstractProtoGenerator<ClassInfo> {

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());
Expand Down Expand Up @@ -92,15 +92,29 @@ protected Optional<String> extractName(ClassInfo clazz) {
return Optional.of(name);
}

protected Optional<String> 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<String> alreadyGenerated, ClassInfo clazz, String messageComment, String fieldComment) throws Exception {
Optional<String> 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)) {
Expand Down Expand Up @@ -157,7 +171,7 @@ protected ProtoMessage messageFromClass(Proto proto, Set<String> 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()));
Expand All @@ -168,6 +182,10 @@ protected ProtoMessage messageFromClass(Proto proto, Set<String> 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 {
Expand Down

0 comments on commit efbc2fd

Please sign in to comment.