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

Comparator reference #6

Merged
merged 1 commit into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package org.sudu.protogen.generator.message;

import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.FieldSpec;
import com.squareup.javapoet.ParameterSpec;
import com.squareup.javapoet.TypeSpec;
import com.squareup.javapoet.*;
import org.jetbrains.annotations.NotNull;
import org.sudu.protogen.generator.GenerationContext;
import org.sudu.protogen.generator.field.FieldGenerator;
Expand Down Expand Up @@ -42,6 +39,9 @@ public TypeSpec generate() {
TypeSpec.Builder typeBuilder = getRecordBuilder(fields);

boolean annotateNotNull = msgDescriptor.getContainingFile().doUseNullabilityAnnotation(false);
msgDescriptor.getComparatorReference().ifPresent(
comparator -> addComparable(typeBuilder, comparator)
);

return typeBuilder
.multiLineRecord(true)
Expand All @@ -52,6 +52,21 @@ public TypeSpec generate() {
.build();
}

private void addComparable(TypeSpec.Builder typeBuilder, String comparator) {
ClassName type = generatedType();
typeBuilder.addSuperinterface(ParameterizedTypeName.get(ClassName.get(Comparable.class), type));
ParameterSpec methodParameter = ParameterSpec.builder(type, "rhs").build();
typeBuilder.addMethod(
MethodSpec.methodBuilder("compareTo")
.addModifiers(Modifier.PUBLIC)
.returns(TypeName.INT)
.addParameter(methodParameter)
.addAnnotation(ClassName.get(Override.class))
.addStatement("return $L.compare(this, $N)", comparator, methodParameter)
.build()
);
}

private TypeSpec.Builder getRecordBuilder(List<FieldSpec> fields) {
List<ParameterSpec> parameters = fields.stream()
.map(Poem::fieldToParameter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@ public final boolean isUnfolded() {
*/
public abstract boolean isMap();

public abstract Optional<String> getComparatorReference();

protected abstract Optional<Boolean> getUnfoldOption();
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,9 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(descriptor);
}

@Override
public Optional<String> getComparatorReference() {
return Options.wrapExtension(descriptor.getOptions(), protogen.Options.messageComparator);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ public void setUnfoldOption(Optional<Boolean> unfoldOption) {
this.unfoldOption = unfoldOption;
}

@Override
public Optional<String> getComparatorReference() {
return Optional.empty();
}

@Override
public String toString() {
return "MessageMock{" +
Expand Down
5 changes: 5 additions & 0 deletions options/src/main/proto/protogen/options.proto
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ extend google.protobuf.MessageOptions {
* Ex: option (protogen.custom_class) = "org.sudu.api.computeengine.BatchInfo";
*/
string custom_class = 5104;
/*
* Allows to implement Comparable using specified comparator.
* Ex: option (protogen.message_comparator) = "org.sudu.api.console.types.ConsoleComparators.INSTANCE_ID";
*/
string message_comparator = 5105;
}

extend google.protobuf.EnumOptions {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.sudu.protogen.test.comparatorRef;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class ComparatorReferenceTest {

@Test
public void Person_should_compare_correctly() {
Person anton20 = new Person("Anton", 20);
Person anton25 = new Person("Anton", 25);
Person lena20 = new Person("Lena", 20);

Assertions.assertTrue(anton20.compareTo(anton25) < 0);
Assertions.assertTrue(anton20.compareTo(anton20) == 0);
Assertions.assertTrue(lena20.compareTo(anton25) > 0);
Assertions.assertTrue(lena20.compareTo(anton20) > 0);
Assertions.assertTrue(anton20.compareTo(lena20) < 0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.sudu.protogen.test.comparatorRef;

import java.util.Comparator;

public class TestComparators {

public static final Comparator<Person> PERSON_COMPARATOR = Comparator.comparing(Person::name).thenComparing(Person::age);
}
16 changes: 16 additions & 0 deletions tests/src/test/proto/comparatorRef.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
syntax = "proto3";

package org.sudu.protogen.test.comparatorRef;

import "google/protobuf/empty.proto";
import "protogen/options.proto";

option java_multiple_files = true;
option (.protogen.enable) = true;

message GrpcPerson {
string name = 1;
int32 age = 2;

option (.protogen.message_comparator) = "org.sudu.protogen.test.comparatorRef.TestComparators.PERSON_COMPARATOR";
}