Skip to content

Commit

Permalink
Descriptors cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Duzhinsky committed Sep 8, 2023
1 parent 38cd9d2 commit 3d0a5c0
Show file tree
Hide file tree
Showing 21 changed files with 244 additions and 307 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.sudu.protogen.plugin.Generator;
import org.sudu.protogen.plugin.GeneratorException;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Stream;

Expand Down Expand Up @@ -44,14 +43,11 @@ private GenerationResult generate(GenerationRequest request, Configuration confi
.filter(file -> request.filesToGenerateNames().contains(file.getName()))
.toList();
var context = new GenerationContext(
filesToGenerate,
configuration,
TypeProcessor.Chain.getProcessingChain(),
FieldTypeProcessor.Chain.getProcessingChain(),
new HashMap<>(),
new HashMap<>()
FieldTypeProcessor.Chain.getProcessingChain()
);
return new org.sudu.protogen.generator.Generator(context).generate();
return new org.sudu.protogen.generator.Generator(context, filesToGenerate).generate();
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
public interface NamingManager {

@NotNull
String getDomainName(@NotNull String messageName);
String manageName(@NotNull String originalName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ public class SuduNamingManager implements NamingManager {
private static final String PREFIX = "Grpc";

@Override
public @NotNull String getDomainName(@NotNull String messageName) {
public @NotNull String manageName(@NotNull String originalName) {
Validate.validState(
messageName.startsWith(PREFIX),
"Naming policy violation for %s. Its name should start with \"Grpc\"", messageName
originalName.startsWith(PREFIX),
"Naming policy violation for %s. Its name should start with \"Grpc\"", originalName
);
return StringUtils.removeStart(messageName, PREFIX);
return StringUtils.removeStart(originalName, PREFIX);
}
}
48 changes: 27 additions & 21 deletions generator/src/main/java/org/sudu/protogen/descriptors/Enum.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,67 +11,71 @@

public class Enum extends EnumOrMessage {

private final Descriptors.EnumDescriptor descriptor;
private final Descriptors.EnumDescriptor enumDescriptor;

public Enum(Descriptors.EnumDescriptor descriptor) {
this.descriptor = descriptor;
public Enum(Descriptors.EnumDescriptor enumDescriptor) {
this.enumDescriptor = enumDescriptor;
}

public List<? extends Value> getValues() {
return descriptor.getValues().stream().map(Value::new).toList();
return enumDescriptor.getValues().stream().map(Value::new).toList();
}

@Override
public @NotNull String getName() {
return descriptor.getName();
return enumDescriptor.getName();
}

@Override
public @NotNull String getFullName() {
return descriptor.getFullName();
return enumDescriptor.getFullName();
}

@Override
public @NotNull List<EnumOrMessage> getNested() {
return List.of();
}

@Override
public @NotNull File getContainingFile() {
return new File(descriptor.getFile());
return new File(enumDescriptor.getFile());
}

@Override
public @Nullable Message getContainingType() {
return descriptor.getContainingType() == null ? null : new Message(descriptor.getContainingType());
return Optional.ofNullable(enumDescriptor.getContainingType())
.map(Message::new)
.orElse(null);
}

@Override
public List<EnumOrMessage> getNested() {
return List.of();
public @Nullable String getCustomClass() {
return Options.wrapExtension(enumDescriptor.getOptions(), protogen.Options.customEnum).orElse(null);
}

// -----------------

@Override
protected Optional<Boolean> getDoGenerateOption() {
return Options.wrapExtension(descriptor.getOptions(), protogen.Options.genEnum);
return Options.wrapExtension(enumDescriptor.getOptions(), protogen.Options.genEnum);
}

@Override
protected Optional<String> getOverriddenNameOption() {
return Options.wrapExtension(descriptor.getOptions(), protogen.Options.enumName);
}

@Override
protected Optional<String> getCustomClassNameOption() {
return Options.wrapExtension(descriptor.getOptions(), protogen.Options.customEnum);
return Options.wrapExtension(enumDescriptor.getOptions(), protogen.Options.enumName);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Enum anEnum = (Enum) o;
return Objects.equals(descriptor, anEnum.descriptor);
return Objects.equals(enumDescriptor, anEnum.enumDescriptor);
}

@Override
public int hashCode() {
return Objects.hash(descriptor);
return Objects.hash(enumDescriptor);
}

public static class Value {
Expand All @@ -82,18 +86,20 @@ public Value(Descriptors.EnumValueDescriptor valueDescriptor) {
this.valueDescriptor = valueDescriptor;
}

public final String generatedName() {
public String generatedName() {
return getOverriddenNameOption().orElse(getName());
}

public final boolean isUnused() {
public boolean isUnused() {
return getUnusedOption().orElse(false);
}

public String getName() {
return valueDescriptor.getName();
}

// -----------------

protected Optional<String> getOverriddenNameOption() {
return Options.wrapExtension(valueDescriptor.getOptions(), protogen.Options.enumValName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,85 +16,58 @@ public abstract class EnumOrMessage {

public abstract @NotNull String getFullName();

public abstract @NotNull List<? extends EnumOrMessage> getNested();

public abstract @NotNull File getContainingFile();

public abstract @Nullable Message getContainingType();

public abstract List<? extends EnumOrMessage> getNested();
public abstract @Nullable String getCustomClass();

// todo apply
public final void verify() {
int optCnt = 0;
if (getDoGenerateOption().isPresent() && getDoGenerateOption().get()) optCnt++;
if (getCustomClassNameOption().isPresent()) optCnt++;
if (this instanceof Message msg) {
if (msg.getUnfoldOption().isPresent() && msg.getUnfoldOption().get()) optCnt++;
}
if (optCnt > 1) {
throw new IllegalStateException("Only one of generate, decompose or customClass option could be set");
}
public final boolean isDomain() {
return doGenerate() || getCustomClass() != null;
}

public final ClassName getProtobufTypeName(NamingManager namingManager) {
public boolean doGenerate() {
if (getCustomClass() != null) return false;
return getDoGenerateOption()
.orElse(getContainingFile().doEnableGenerator()
&& !getName().endsWith("Request")
&& !getName().endsWith("Response")
);
}

public ClassName getProtobufTypeName() {
if (getContainingType() == null) {
String javaPackage = getContainingFile().getJavaPackage();
String enclosingClass = getContainingFile().getEnclosingClass();
String className = enclosingClass == null ? getName() : enclosingClass + "." + getName();
return ClassName.get(javaPackage, className);
} else {
ClassName containing = getContainingType().getProtobufTypeName(namingManager);
ClassName containing = getContainingType().getProtobufTypeName();
return ClassName.get(containing.packageName(), containing.simpleName(), getName());
}
}

public final ClassName getGeneratedTypeName(NamingManager namingManager) {
String customClass = customClass();
if (customClass != null) {
public ClassName getDomainTypeName(NamingManager namingManager) {
Validate.validState(isDomain());
if (getCustomClass() != null) {
String customClass = getCustomClass();
return ClassName.get(Name.getPackage(customClass), Name.getLastName(customClass));
}
if (getContainingType() == null) {
String javaPackage = getContainingFile().getGeneratePackage();
return ClassName.get(javaPackage, generatedName(namingManager));
return ClassName.get(javaPackage, getDomainName(namingManager));
} else {
ClassName containing = getContainingType().getGeneratedTypeName(namingManager);
return ClassName.get(containing.packageName(), containing.simpleName(), generatedName(namingManager));
}
}

public final String generatedName(NamingManager namingManager) {
Validate.validState(doGenerate(), "Check doGenerate() before calling generatedName()!");
return getOverriddenNameOption()
.orElseGet(() -> namingManager.getDomainName(getName()));
}

public final boolean doGenerate() {
if (this instanceof Message msg) {
if (msg.isMap()) return getDoGenerateOption().orElse(false);
if (msg.isUnfolded()) return getDoGenerateOption().orElse(false);
ClassName containing = getContainingType().getDomainTypeName(namingManager);
return ClassName.get(containing.packageName(), containing.simpleName(), getDomainName(namingManager));
}
if (customClass() != null) return false;
return getDoGenerateOption()
.orElse(getContainingFile().doGenerate() && !getName().endsWith("Request") && !getName().endsWith("Response"));
}

public final boolean isDomain() {
return doGenerate() || customClass() != null;
private String getDomainName(NamingManager namingManager) {
return getOverriddenNameOption().orElseGet(() -> namingManager.manageName(getName()));
}

@Nullable
public final String customClass() {
return getCustomClassNameOption().orElse(null);
}

/*
* Options are not supposed to be used at high-level logic.
* They return only the value of an option in .proto file.
* Advanced logic taking into account other options and configuration values
* is placed at top-level methods such as getGenerateOption for getGenerateOption.
*/

protected abstract Optional<String> getCustomClassNameOption();

protected abstract Optional<Boolean> getDoGenerateOption();

protected abstract Optional<String> getOverriddenNameOption();
Expand Down
26 changes: 8 additions & 18 deletions generator/src/main/java/org/sudu/protogen/descriptors/Field.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.google.protobuf.Descriptors;
import org.apache.commons.lang3.Validate;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.NotNull;
import org.sudu.protogen.Options;

import java.util.Objects;
Expand All @@ -28,16 +28,14 @@ public Type getType() {
return mapType(descriptor.getJavaType());
}

public @Nullable Message getMessageType() {
return descriptor.getJavaType() == Descriptors.FieldDescriptor.JavaType.MESSAGE
? new Message(descriptor.getMessageType())
: null;
public @NotNull Message getMessageType() {
Validate.validState(getType() == Type.MESSAGE);
return new Message(descriptor.getMessageType());
}

public @Nullable Enum getEnumType() {
return descriptor.getJavaType() == Descriptors.FieldDescriptor.JavaType.ENUM
? new Enum(descriptor.getEnumType())
: null;
public @NotNull Enum getEnumType() {
Validate.validState(getType() == Type.ENUM);
return new Enum(descriptor.getEnumType());
}

public Message getContainingMessage() {
Expand All @@ -53,13 +51,11 @@ public final boolean isList() {
}

public final boolean isMap() {
// noinspection DataFlowIssue because getMessageType() != null iff type == MESSAGE
return getType() == Type.MESSAGE
&& getMessageType().isMap();
}

public final boolean isUnfolded() {
//noinspection DataFlowIssue because getMessageType() != null iff type == MESSAGE
return getType() == Type.MESSAGE && getMessageType().isUnfolded();
}

Expand All @@ -68,7 +64,6 @@ public final boolean isIgnored() {
}

public final Field getUnfoldedField() {
//noinspection DataFlowIssue because isUnfolded() true iff getMessageType() != null
Validate.validState(isUnfolded());
return getMessageType().getFields().get(0);
}
Expand All @@ -82,12 +77,7 @@ public final RepeatedContainer getRepeatedContainer() {
return getRepeatedContainerOption().orElse(RepeatedContainer.LIST);
}

/*
* Options are not supposed to be used at high-level logic.
* They return only the value of an option in .proto file.
* Advanced logic taking into account other options and configuration values
* is placed at top-level methods such as getGenerateOption for getGenerateOption.
*/
// -----------

protected boolean isRepeated() {
return descriptor.isRepeated();
Expand Down
Loading

0 comments on commit 3d0a5c0

Please sign in to comment.