Skip to content

Commit

Permalink
Renaming components -> services (#287)
Browse files Browse the repository at this point in the history
  • Loading branch information
slinkydeveloper authored Apr 18, 2024
1 parent 18538d6 commit 5bb5164
Show file tree
Hide file tree
Showing 61 changed files with 627 additions and 656 deletions.
2 changes: 1 addition & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ You can modify the class to run setting `-PmainClass=<FQCN>`, for example, in or
./gradlew :examples:run -PmainClass=my.restate.sdk.examples.CounterKtKt
```

## Invoking the counter bindableComponent
## Invoking the Counter

If you want to invoke the counter virtual object via curl:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public Handler validateAndBuild() {
if (handlerNameLowercase.startsWith("restate")
|| handlerNameLowercase.startsWith("openapi")) {
throw new IllegalArgumentException(
"A component name cannot start with `restate` or `openapi`");
"A service name cannot start with `restate` or `openapi`");
}

return new Handler(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,31 @@
// https://github.com/restatedev/sdk-java/blob/main/LICENSE
package dev.restate.sdk.gen.model;

import dev.restate.sdk.common.ComponentType;
import dev.restate.sdk.common.ServiceType;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

public class Component {
public class Service {

private final CharSequence targetPkg;
private final CharSequence targetFqcn;
private final String componentName;
private final ComponentType componentType;
private final String serviceName;
private final ServiceType serviceType;
private final List<Handler> handlers;

public Component(
public Service(
CharSequence targetPkg,
CharSequence targetFqcn,
String componentName,
ComponentType componentType,
String serviceName,
ServiceType serviceType,
List<Handler> handlers) {
this.targetPkg = targetPkg;
this.targetFqcn = targetFqcn;
this.componentName = componentName;
this.serviceName = serviceName;

this.componentType = componentType;
this.serviceType = serviceType;
this.handlers = handlers;
}

Expand All @@ -44,23 +44,23 @@ public CharSequence getTargetFqcn() {
return this.targetFqcn;
}

public String getFullyQualifiedComponentName() {
return this.componentName;
public String getFullyQualifiedServiceName() {
return this.serviceName;
}

public String getSimpleComponentName() {
return this.componentName.substring(this.componentName.lastIndexOf('.') + 1);
public String getSimpleServiceName() {
return this.serviceName.substring(this.serviceName.lastIndexOf('.') + 1);
}

public CharSequence getGeneratedClassFqcnPrefix() {
if (this.targetPkg == null || this.targetPkg.length() == 0) {
return getSimpleComponentName();
return getSimpleServiceName();
}
return this.targetPkg + "." + getSimpleComponentName();
return this.targetPkg + "." + getSimpleServiceName();
}

public ComponentType getComponentType() {
return componentType;
public ServiceType getServiceType() {
return serviceType;
}

public List<Handler> getMethods() {
Expand All @@ -74,8 +74,8 @@ public static Builder builder() {
public static class Builder {
private CharSequence targetPkg;
private CharSequence targetFqcn;
private String componentName;
private ComponentType componentType;
private String serviceName;
private ServiceType serviceType;
private final List<Handler> handlers = new ArrayList<>();

public Builder withTargetPkg(CharSequence targetPkg) {
Expand All @@ -88,13 +88,13 @@ public Builder withTargetFqcn(CharSequence targetFqcn) {
return this;
}

public Builder withComponentName(String componentName) {
this.componentName = componentName;
public Builder withServiceName(String serviceName) {
this.serviceName = serviceName;
return this;
}

public Builder withComponentType(ComponentType componentType) {
this.componentType = componentType;
public Builder withServiceType(ServiceType serviceType) {
this.serviceType = serviceType;
return this;
}

Expand All @@ -116,39 +116,39 @@ public CharSequence getTargetFqcn() {
return targetFqcn;
}

public String getComponentName() {
return componentName;
public String getServiceName() {
return serviceName;
}

public ComponentType getComponentType() {
return componentType;
public ServiceType getServiceType() {
return serviceType;
}

public List<Handler> getHandlers() {
return handlers;
}

public Component validateAndBuild() {
String componentNameLowercase = componentName.toLowerCase();
if (componentNameLowercase.startsWith("restate")
|| componentNameLowercase.startsWith("openapi")) {
public Service validateAndBuild() {
String serviceNameLowercase = serviceName.toLowerCase();
if (serviceNameLowercase.startsWith("restate")
|| serviceNameLowercase.startsWith("openapi")) {
throw new IllegalArgumentException(
"A component name cannot start with `restate` or `openapi`");
"A service name cannot start with `restate` or `openapi`");
}

if (componentType.equals(ComponentType.WORKFLOW)) {
if (serviceType.equals(ServiceType.WORKFLOW)) {
if (handlers.stream().filter(m -> m.getHandlerType().equals(HandlerType.WORKFLOW)).count()
!= 1) {
throw new IllegalArgumentException(
"Workflow services must have exactly one method annotated as @Workflow");
}
}

return new Component(
return new Service(
Objects.requireNonNull(targetPkg),
Objects.requireNonNull(targetFqcn),
Objects.requireNonNull(componentName),
Objects.requireNonNull(componentType),
Objects.requireNonNull(serviceName),
Objects.requireNonNull(serviceType),
handlers);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
import com.github.jknack.handlebars.context.FieldValueResolver;
import com.github.jknack.handlebars.helper.StringHelpers;
import com.github.jknack.handlebars.io.TemplateLoader;
import dev.restate.sdk.common.ComponentType;
import dev.restate.sdk.common.ServiceType;
import dev.restate.sdk.common.function.ThrowingFunction;
import dev.restate.sdk.gen.model.Component;
import dev.restate.sdk.gen.model.Handler;
import dev.restate.sdk.gen.model.HandlerType;
import dev.restate.sdk.gen.model.Service;
import java.io.IOException;
import java.io.Writer;
import java.util.List;
Expand All @@ -29,13 +29,13 @@
public class HandlebarsTemplateEngine {

private final String baseTemplateName;
private final Map<ComponentType, Template> templates;
private final Map<ServiceType, Template> templates;
private final Set<String> handlerNamesToPrefix;

public HandlebarsTemplateEngine(
String baseTemplateName,
TemplateLoader templateLoader,
Map<ComponentType, String> templates,
Map<ServiceType, String> templates,
Set<String> handlerNamesToPrefix) {
this.baseTemplateName = baseTemplateName;
this.handlerNamesToPrefix = handlerNamesToPrefix;
Expand All @@ -62,16 +62,16 @@ public HandlebarsTemplateEngine(
}));
}

public void generate(ThrowingFunction<String, Writer> createFile, Component component)
public void generate(ThrowingFunction<String, Writer> createFile, Service service)
throws Throwable {
String fileName = component.getGeneratedClassFqcnPrefix() + this.baseTemplateName;
String fileName = service.getGeneratedClassFqcnPrefix() + this.baseTemplateName;
try (Writer out = createFile.apply(fileName)) {
this.templates
.get(component.getComponentType())
.get(service.getServiceType())
.apply(
Context.newBuilder(
new ComponentTemplateModel(
component, this.baseTemplateName, this.handlerNamesToPrefix))
new ServiceTemplateModel(
service, this.baseTemplateName, this.handlerNamesToPrefix))
.resolver(FieldValueResolver.INSTANCE)
.build(),
out);
Expand All @@ -80,30 +80,30 @@ public void generate(ThrowingFunction<String, Writer> createFile, Component comp

// --- classes to interact with the handlebars template

static class ComponentTemplateModel {
static class ServiceTemplateModel {
public final String originalClassPkg;
public final String originalClassFqcn;
public final String generatedClassSimpleNamePrefix;
public final String generatedClassSimpleName;
public final String componentName;
public final String componentType;
public final String serviceName;
public final String serviceType;
public final boolean isWorkflow;
public final boolean isObject;
public final boolean isService;
public final List<HandlerTemplateModel> handlers;

private ComponentTemplateModel(
Component inner, String baseTemplateName, Set<String> handlerNamesToPrefix) {
private ServiceTemplateModel(
Service inner, String baseTemplateName, Set<String> handlerNamesToPrefix) {
this.originalClassPkg = inner.getTargetPkg().toString();
this.originalClassFqcn = inner.getTargetFqcn().toString();
this.generatedClassSimpleNamePrefix = inner.getSimpleComponentName();
this.generatedClassSimpleNamePrefix = inner.getSimpleServiceName();
this.generatedClassSimpleName = this.generatedClassSimpleNamePrefix + baseTemplateName;
this.componentName = inner.getFullyQualifiedComponentName();
this.serviceName = inner.getFullyQualifiedServiceName();

this.componentType = inner.getComponentType().toString();
this.isWorkflow = inner.getComponentType() == ComponentType.WORKFLOW;
this.isObject = inner.getComponentType() == ComponentType.VIRTUAL_OBJECT;
this.isService = inner.getComponentType() == ComponentType.SERVICE;
this.serviceType = inner.getServiceType().toString();
this.isWorkflow = inner.getServiceType() == ServiceType.WORKFLOW;
this.isObject = inner.getServiceType() == ServiceType.VIRTUAL_OBJECT;
this.isService = inner.getServiceType() == ServiceType.SERVICE;

this.handlers =
inner.getMethods().stream()
Expand Down
Loading

0 comments on commit 5bb5164

Please sign in to comment.