Skip to content

Commit

Permalink
Tweaks for final edge cases. Added Conversion in model record constru…
Browse files Browse the repository at this point in the history
…ctors from OpenOf<Optioal.empty()> to OneOF.UNSET. Added work around for more cyclic dependency with Keys. Fixed field sorting in writers.
  • Loading branch information
jasperpotts committed Sep 8, 2022
1 parent 361b859 commit 19fd347
Show file tree
Hide file tree
Showing 6 changed files with 365 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,7 @@ private static void generateRecordFile(Protobuf3Parser.MessageDefContext msgDef,
""".formatted(javaRecordName,
fields.stream()
.filter(f -> f instanceof OneOfField || f.optional())
.map(f -> FIELD_INDENT+"""
if (%s == null) {
throw new NullPointerException("Parameter '%s' must be supplied and can not be null");
}""".formatted(f.nameCamelFirstLower(),f.nameCamelFirstLower()).replaceAll("\n","\n"+FIELD_INDENT))
.map(ModelGenerator::generateCostructorCode)
.collect(Collectors.joining("\n"))
).replaceAll("\n","\n"+FIELD_INDENT);
}
Expand Down Expand Up @@ -207,4 +204,32 @@ private static void generateRecordFile(Protobuf3Parser.MessageDefContext msgDef,
}
}

}
private static String generateCostructorCode(final Field f) {
StringBuilder sb = new StringBuilder(FIELD_INDENT+"""
if (%s == null) {
throw new NullPointerException("Parameter '%s' must be supplied and can not be null");
}""".formatted(f.nameCamelFirstLower(),f.nameCamelFirstLower()));
if (f instanceof OneOfField) {
final OneOfField oof = (OneOfField)f;
for (Field subField: oof.fields()) {
if(subField.optional()) {
sb.append("""
// handle special case where protobuf does not have destination between a OneOf with optional
// value of empty vs a unset OneOf.
if(%s.kind() == %sOneOfType.%s && ((Optional)%s.value()).isEmpty()) {
%s = new OneOf<>(%sOneOfType.UNSET, null);
}""".formatted(
f.nameCamelFirstLower(),
f.nameCamelFirstUpper(),
camelToUpperSnake(subField.name()),
f.nameCamelFirstLower(),
f.nameCamelFirstLower(),
f.nameCamelFirstUpper()
));
}
}
}
return sb.toString().replaceAll("\n","\n"+FIELD_INDENT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public FieldType type() {
*/
@Override
public int fieldNumber() {
return -1;
return fields.get(0).fieldNumber();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,8 @@ private static String generateTestData(String modelClassName, Field field, boole
if(subField instanceof SingleField) {
final String enumValueName = camelToUpperSnake(subField.name());
// special cases to break cyclic dependencies
if (!("THRESHOLD_KEY".equals(enumValueName) || "KEY_LIST".equals(enumValueName))) {
if (!("THRESHOLD_KEY".equals(enumValueName) || "KEY_LIST".equals(enumValueName)
|| "THRESHOLD_SIGNATURE".equals(enumValueName)|| "SIGNATURE_LIST".equals(enumValueName))) {
final String listStr;
if (subField.optional()) {
Field.FieldType convertedSubFieldType = getOptionalConvertedFieldType(subField);
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
version = 0.28.1-SNAPSHOT

# Need increased heap for running Gradle itself, or SonarQube will run the JVM out of metaspace
org.gradle.jvmargs = -Xmx2048m
org.gradle.jvmargs = -Xmx8192M

# Default JMH benchmark includes regex
includesRegex=.*
Expand Down
3 changes: 3 additions & 0 deletions modules/hedera-proto-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ dependencies {
testImplementation("com.hedera.hashgraph:hedera-protobuf-java-api:0.29.1")
}

tasks.withType<Test>().configureEach {
maxParallelForks = 4
}

// ===== PROTOBUF GENERATION ===========================================================================================

Expand Down
Loading

0 comments on commit 19fd347

Please sign in to comment.