Skip to content

Commit

Permalink
Fix #239, use simple class name as default name (#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
slinkydeveloper authored Mar 18, 2024
1 parent ac82682 commit d466b0d
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,9 @@ public Component fromTypeElement(TypeElement element) {
? virtualObjectAnnotation.name()
: workflowAnnotation.name();
if (componentName.isEmpty()) {
// Use FQCN
// With this logic we make sure we flatten subclasses names
String simpleComponentName =
targetFqcn.toString().substring(targetPkg.length()).replaceAll(Pattern.quote("."), "");
// Use simple class name, flattening subclasses names
componentName =
targetPkg.length() > 0 ? targetPkg + "." + simpleComponentName : simpleComponentName;
targetFqcn.toString().substring(targetPkg.length()).replaceAll(Pattern.quote("."), "");
}

// Compute handlers
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) 2023 - Restate Software, Inc., Restate GmbH
//
// This file is part of the Restate Java SDK,
// which is released under the MIT license.
//
// You can find a copy of the license in file LICENSE in the root
// directory of this repository or package, or at
// https://github.com/restatedev/sdk-java/blob/main/LICENSE
package dev.restate.sdk;

import dev.restate.sdk.annotation.Handler;
import dev.restate.sdk.annotation.Service;

@Service(name = "MyExplicitName")
public interface GreeterWithExplicitName {
@Handler
String greet(Context context, String request);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) 2023 - Restate Software, Inc., Restate GmbH
//
// This file is part of the Restate Java SDK,
// which is released under the MIT license.
//
// You can find a copy of the license in file LICENSE in the root
// directory of this repository or package, or at
// https://github.com/restatedev/sdk-java/blob/main/LICENSE
package dev.restate.sdk;

import dev.restate.sdk.annotation.Handler;
import dev.restate.sdk.annotation.Service;

@Service
public interface GreeterWithoutExplicitName {
@Handler
String greet(Context context, String request);
}
25 changes: 25 additions & 0 deletions sdk-api-gen/src/test/java/dev/restate/sdk/NameInferenceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) 2023 - Restate Software, Inc., Restate GmbH
//
// This file is part of the Restate Java SDK,
// which is released under the MIT license.
//
// You can find a copy of the license in file LICENSE in the root
// directory of this repository or package, or at
// https://github.com/restatedev/sdk-java/blob/main/LICENSE
package dev.restate.sdk;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

public class NameInferenceTest {

@Test
void expectedName() {
assertThat(CodegenTestServiceGreeterClient.COMPONENT_NAME)
.isEqualTo("CodegenTestServiceGreeter");
assertThat(GreeterWithoutExplicitNameClient.COMPONENT_NAME)
.isEqualTo("GreeterWithoutExplicitName");
assertThat(MyExplicitNameClient.COMPONENT_NAME).isEqualTo("MyExplicitName");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ internal class RestateHttpEndpointTest {
@Timeout(value = 1, timeUnit = TimeUnit.SECONDS)
@Test
fun endpointWithBlockingService(vertx: Vertx): Unit =
greetTest(vertx, BlockingGreeter::class.qualifiedName!!) { it.with(BlockingGreeter()) }
greetTest(vertx, BlockingGreeter::class.simpleName!!) { it.with(BlockingGreeter()) }

private fun greetTest(
vertx: Vertx,
Expand Down Expand Up @@ -169,7 +169,7 @@ internal class RestateHttpEndpointTest {
HttpMethod.POST,
endpointPort,
"localhost",
"/invoke/" + BlockingGreeter::class.java.canonicalName + "/unknownMethod")
"/invoke/" + BlockingGreeter::class.java.simpleName + "/unknownMethod")
.coAwait()

// Prepare request header
Expand Down Expand Up @@ -218,7 +218,7 @@ internal class RestateHttpEndpointTest {

assertThat(discoveryResponse.components)
.map<String> { it.fullyQualifiedComponentName }
.containsOnly(BlockingGreeter::class.java.canonicalName)
.containsOnly(BlockingGreeter::class.java.simpleName)
}

fun encode(msg: MessageLite): Buffer {
Expand Down

0 comments on commit d466b0d

Please sign in to comment.