Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

95 Add stable equals/hashCode for PBJ objects #109

Merged
merged 10 commits into from
Nov 22, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

plugins {
id("com.hedera.pbj.repositories")
id("com.hedera.pbj.aggregate-reports")
id("com.hedera.pbj.spotless-conventions")
id("com.hedera.pbj.spotless-kotlin-conventions")
id("com.autonomousapps.dependency-analysis")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ public void apply(Project project) {
// get java src sets
final var mainSrcSet = javaPlugin.getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME);
final var testSrcSet = javaPlugin.getSourceSets().getByName(SourceSet.TEST_SOURCE_SET_NAME);
final String outputDirectory = "generated/source/pbj-proto/main/";
final String outputDirectory = "generated/source/pbj-proto/";
final Provider<Directory> outputDirectoryMain =
project.getLayout().getBuildDirectory().dir(outputDirectory + "java");
project.getLayout().getBuildDirectory().dir(outputDirectory + "main/java");
final Provider<Directory> outputDirectoryTest =
project.getLayout().getBuildDirectory().dir(outputDirectory + "test");
project.getLayout().getBuildDirectory().dir(outputDirectory + "test/java");

// for the 'main' source set we:
// 1) Add a new 'pbj' virtual directory mapping
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.hedera.pbj.compiler.impl;

import com.hedera.pbj.compiler.impl.grammar.Protobuf3Parser;
import edu.umd.cs.findbugs.annotations.NonNull;

import java.util.Set;

Expand Down Expand Up @@ -47,6 +48,7 @@ default String nameCamelFirstUpper() {
*
* @return this fields name converted
*/
@NonNull
default String nameCamelFirstLower() {
return snakeToCamel(name(),false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.stream.Collectors;

import static com.hedera.pbj.compiler.impl.Common.*;
import static com.hedera.pbj.compiler.impl.Common.DEFAULT_INDENT;

/**
* Code for generating enum code
Expand Down Expand Up @@ -70,15 +71,15 @@
try (FileWriter javaWriter = new FileWriter(getJavaFile(destinationSrcDir, modelPackage, enumName))) {
javaWriter.write(
"package "+modelPackage+";\n\n"+
createEnum("", javaDocComment, deprecated, enumName, maxIndex, enumValues, false)
createEnum(javaDocComment, deprecated, enumName,
maxIndex, enumValues, false)
);
}
}

/**
* Generate code for a enum
*
* @param indent extra indent spaces beyond the default 4
* @param javaDocComment either enum javadoc comment or empty string
* @param deprecated either @deprecated string or empty string
* @param enumName the name for enum
Expand All @@ -87,114 +88,121 @@
* @param addUnknown when true we add an enum value for one of
* @return string code for enum
*/
static String createEnum(String indent, String javaDocComment, String deprecated, String enumName,
static String createEnum(String javaDocComment, String deprecated, String enumName,
int maxIndex, Map<Integer, EnumValue> enumValues, boolean addUnknown) {
final List<String> enumValuesCode = new ArrayList<>(maxIndex);
if (addUnknown) {
enumValuesCode.add(FIELD_INDENT+"""
/**
* Enum value for a unset OneOf, to avoid null OneOfs
*/
UNSET(-1, "UNSET")"""
.replaceAll("\n","\n"+FIELD_INDENT));
enumValuesCode.add(
"""
/**
* Enum value for a unset OneOf, to avoid null OneOfs
*/
UNSET(-1, "UNSET")""");
}
for (int i = 0; i <= maxIndex; i++) {
final EnumValue enumValue = enumValues.get(i);
if (enumValue != null) {
final String cleanedEnumComment = FIELD_INDENT + "/** \n"
+ FIELD_INDENT+" * "
+ enumValue.javaDoc.replaceAll("\n[\t\s]*","\n"+FIELD_INDENT+" * ") // clean up doc indenting
+ "\n"
+ FIELD_INDENT + " */\n";
final String deprecatedText = enumValue.deprecated ? FIELD_INDENT+"@Deprecated\n" : "";
final String cleanedEnumComment =
"""
/**$enumJavadoc
*/
"""
.replace("$enumJavadoc", enumValue.javaDoc);
final String deprecatedText = enumValue.deprecated ? "@Deprecated\n" : "";
enumValuesCode.add(
cleanedEnumComment
+ deprecatedText+FIELD_INDENT+camelToUpperSnake(enumValue.name)+"("+i+", \""+enumValue.name+"\")");
+ deprecatedText+ camelToUpperSnake(enumValue.name) +
"("+i+", \""+enumValue.name+"\")");
}
}
return """
$javaDocComment
$deprecated public enum $enumName implements com.hedera.pbj.runtime.EnumWithProtoMetadata{
$deprecated$public enum $enumName
implements com.hedera.pbj.runtime.EnumWithProtoMetadata {
$enumValues;
/** The field ordinal in protobuf for this type */
private final int protoOrdinal;
/** The original field name in protobuf for this type */
private final String protoName;
/**
* OneOf Type Enum Constructor
*
* @param protoOrdinal The oneof field ordinal in protobuf for this type
* @param protoName The original field name in protobuf for this type
*/
$enumName(final int protoOrdinal, String protoName) {
this.protoOrdinal = protoOrdinal;
this.protoName = protoName;
}
/**
* Get the oneof field ordinal in protobuf for this type
*
* @return The oneof field ordinal in protobuf for this type
*/
public int protoOrdinal() {
return protoOrdinal;
}
/**
* Get the original field name in protobuf for this type
*
* @return The original field name in protobuf for this type
*/
public String protoName() {
return protoName;
}
/**
* Get enum from protobuf ordinal
*
* @param ordinal the protobuf ordinal number
* @return enum for matching ordinal
* @throws IllegalArgumentException if ordinal doesn't exist
*/
public static $enumName fromProtobufOrdinal(int ordinal) {
return switch(ordinal) {
/** The field ordinal in protobuf for this type */
private final int protoOrdinal;
/** The original field name in protobuf for this type */
private final String protoName;
/**
* OneOf Type Enum Constructor
*
* @param protoOrdinal The oneof field ordinal in protobuf for this type
* @param protoName The original field name in protobuf for this type
*/
$enumName(final int protoOrdinal, String protoName) {
this.protoOrdinal = protoOrdinal;
this.protoName = protoName;
}
/**
* Get the oneof field ordinal in protobuf for this type
*
* @return The oneof field ordinal in protobuf for this type
*/
public int protoOrdinal() {
return protoOrdinal;
}
/**
* Get the original field name in protobuf for this type
*
* @return The original field name in protobuf for this type
*/
public String protoName() {
return protoName;
}
/**
* Get enum from protobuf ordinal
*
* @param ordinal the protobuf ordinal number
* @return enum for matching ordinal
* @throws IllegalArgumentException if ordinal doesn't exist
*/
public static $enumName fromProtobufOrdinal(int ordinal) {
return switch(ordinal) {
$caseStatements
default -> throw new IllegalArgumentException("Unknown protobuf ordinal "+ordinal);
};
}
/**
* Get enum from string name, supports the enum or protobuf format name
*
* @param name the enum or protobuf format name
* @return enum for matching name
*/
public static $enumName fromString(String name) {
return switch(name) {
default -> throw new IllegalArgumentException("Unknown protobuf ordinal "+ordinal);
};
}
/**
* Get enum from string name, supports the enum or protobuf format name
*
* @param name the enum or protobuf format name
* @return enum for matching name
*/
public static $enumName fromString(String name) {
return switch(name) {
$fromStringCaseStatements
default -> throw new IllegalArgumentException("Unknown token kyc status "+name);
};
}
default -> throw new IllegalArgumentException("Unknown token kyc status "+name);
};
}
}
"""
.replace("$javaDocComment", javaDocComment)
.replace("$deprecated", deprecated)
.replace("$deprecated$", deprecated)
.replace("$enumName", enumName)
.replace("$enumValues", String.join(",\n\n", enumValuesCode))
.replace("$caseStatements", enumValues.entrySet().stream().map((entry) -> " case " + entry.getKey() + " -> " +
camelToUpperSnake(entry.getValue().name) + ";").collect(Collectors.joining("\n")))
.replace("$enumValues", String.join(",\n\n", enumValuesCode).indent(DEFAULT_INDENT))
.replace("$caseStatements", enumValues.entrySet()
.stream()
.map((entry) -> "case %s -> %s;".formatted(entry.getKey(), camelToUpperSnake(entry.getValue().name))

Check notice on line 193 in pbj-core/pbj-compiler/src/main/java/com/hedera/pbj/compiler/impl/generators/EnumGenerator.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

pbj-core/pbj-compiler/src/main/java/com/hedera/pbj/compiler/impl/generators/EnumGenerator.java#L193

Line is longer than 120 characters (found 148).
.indent(DEFAULT_INDENT * 3))
.collect(Collectors.joining("\n")))
.replace("$fromStringCaseStatements", enumValues.values().stream().map(enumValue -> {
if (camelToUpperSnake(enumValue.name).equals(enumValue.name)) {
return " case \"" + enumValue.name + "\" -> " +
camelToUpperSnake(enumValue.name) + ";";
return "case \"%s\" -> %s;"
.formatted(enumValue.name, camelToUpperSnake(enumValue.name))

Check notice on line 199 in pbj-core/pbj-compiler/src/main/java/com/hedera/pbj/compiler/impl/generators/EnumGenerator.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

pbj-core/pbj-compiler/src/main/java/com/hedera/pbj/compiler/impl/generators/EnumGenerator.java#L199

Line is longer than 120 characters (found 125).
.indent(DEFAULT_INDENT * 3);
} else {
return " case \"" + camelToUpperSnake(enumValue.name) + "\", \"" + enumValue.name + "\" -> " +
camelToUpperSnake(enumValue.name) + ";";
return "case \"%s\", \"%s\" -> %s;"
.formatted(enumValue.name, camelToUpperSnake(enumValue.name), camelToUpperSnake(enumValue.name))

Check notice on line 203 in pbj-core/pbj-compiler/src/main/java/com/hedera/pbj/compiler/impl/generators/EnumGenerator.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

pbj-core/pbj-compiler/src/main/java/com/hedera/pbj/compiler/impl/generators/EnumGenerator.java#L203

Line is longer than 120 characters (found 160).
.indent(DEFAULT_INDENT * 3);
}
}).collect(Collectors.joining("\n")))
.replaceAll("\n", "\n" + indent);
}).collect(Collectors.joining("\n")));
}
}
Loading