acoustic-carpenter-78188
02/27/2023, 10:11 PMSdkType<T> with low overhead
Type
☐ Bug Fix
☑︎ Feature
☐ Plugin
Are all requirements met?
☑︎ Code completed
☐ Smoke tested
☑︎ Unit tests added
☐ Code documentation added
☐ Any pending items have an associated Issue
Complete description
Currently the way to create custom SdkType<T> in Java is using JacksonSdkType. For example
If a developer wants to define a task equivalent to (String str) -> str.toUpperCase() currently he/she needs to write it as
@AutoService(SdkRunnableTask.class)
public class ToUpperCaseTask extends SdkRunnableTask<ToUpperCaseTask.Input, ToUpperCaseTask.Output> {
public ToUpperCaseTask() {
super(JacksonSdkType.of(Input.class), JacksonSdkType.of(Output.class));
}
@AutoValue
public abstract static class Input {
public abstract String in();
}
@AutoValue
public abstract static class Output {
public abstract String out();
public static Output create(String out) {
return new AutoValue_ToUpperCaseTask_Output(out);
}
}
@Override
public Output run(Input input) {
return Output.create(input.toUpperCase()));
}
}
For such trivial task, defining the AutoValues to define the types takes the majority of the lines.
With this PR the developer could write the task this way instead
@AutoService(SdkRunnableTask.class)
public class ToUpperCaseTask extends SdkRunnableTask<String, String> {
public ToUpperCaseTask() {
super(SdkTypes.ofPrimitive("in", String.class), SdkTypes.ofPrimitive("out", String.class));
}
@Override
public String run(String input) {
return input.toUpperCase();
}
}
Defining the types for the task now takes one line only and the run is simplified too as no need to create or access AutoValues
This PR supports creating simplified `SdkType`s for the following types:
• SdkTypes.ofPrimitive("in", Long.class) for SdkType<Long>
• SdkTypes.ofPrimitive("in", Double.class) for SdkType<Double>
• SdkTypes.ofPrimitive("in", String.class) for SdkType<String>
• SdkTypes.ofPrimitive("in", Boolean.class) for SdkType<Boolean>
• SdkTypes.ofPrimitive("in", Instant.class) for SdkType<Instant>
• SdkTypes.ofPrimitive("in", Duration.class) for SdkType<Duration>
Also support for collection and map for the the same primitive types:
• SdkTypes.ofCollection("in", Long.class) for SdkType<List<Long>>
• SdkTypes.ofMap("in", Long.class) for SdkType<Map<String, Long>>
And finally support for struts, collection of structs and map of struts:
• SdkTypes.ofStruct("in", JacksonSdkType.of(Input.class)) for SdkType<Input>
• SdkTypes.ofCollection("in", JacksonSdkType.of(Input.class)) for SdkType<List<Input>>
• SdkTypes.ofMap("in", JacksonSdkType.of(Input.class)) for SdkType<Map<String, Input>>
flyteorg/flytekit-java
✅ All checks have passed
2/2 successful checksacoustic-carpenter-78188
02/27/2023, 10:11 PM