Skip to content

Commit

Permalink
Support struct in Scala layer
Browse files Browse the repository at this point in the history
Signed-off-by: Hongxin Liang <[email protected]>
  • Loading branch information
honnix committed Oct 11, 2023
1 parent 4de3436 commit 87dd2fb
Show file tree
Hide file tree
Showing 18 changed files with 421 additions and 39 deletions.
9 changes: 9 additions & 0 deletions flytekit-examples-scala/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@
<artifactId>scala-reflect</artifactId>
<version>${scala213.version}</version>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-compiler</artifactId>
<version>${scala213.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand All @@ -62,6 +67,10 @@
<groupId>org.scala-lang</groupId>
<artifactId>scala-reflect</artifactId>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-compiler</artifactId>
</dependency>
<dependency>
<groupId>org.flyte</groupId>
<artifactId>flytekit-scala_2.13</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
org.flyte.examples.flytekitscala.FibonacciLaunchPlan
org.flyte.examples.flytekitscala.LaunchPlanRegistry
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ org.flyte.examples.flytekitscala.SumTask
org.flyte.examples.flytekitscala.GreetTask
org.flyte.examples.flytekitscala.AddQuestionTask
org.flyte.examples.flytekitscala.NoInputsTask
org.flyte.examples.flytekitscala.NestedIOTask
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
org.flyte.examples.flytekitscala.FibonacciWorkflow
org.flyte.examples.flytekitscala.WelcomeWorkflow
org.flyte.examples.flytekitscala.NestedIOWorkflow
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ import org.flyte.flytekit.{SdkLaunchPlan, SimpleSdkLaunchPlanRegistry}
import org.flyte.flytekitscala.SdkScalaType

case class FibonacciLaunchPlanInput(fib0: Long, fib1: Long)
case class NestedIOLaunchPlanInput(name: String, generic: Nested)

class FibonacciLaunchPlan extends SimpleSdkLaunchPlanRegistry {
class LaunchPlanRegistry extends SimpleSdkLaunchPlanRegistry {
// Register default launch plans for all workflows
registerDefaultLaunchPlans()

Expand Down Expand Up @@ -53,4 +54,33 @@ class FibonacciLaunchPlan extends SimpleSdkLaunchPlanRegistry {
.withDefaultInput("fib0", 0L)
.withDefaultInput("fib1", 1L)
)

registerLaunchPlan(
SdkLaunchPlan
.of(new NestedIOWorkflow)
.withName("NestedIOWorkflowLaunchPlan")
.withDefaultInput(
SdkScalaType[NestedIOLaunchPlanInput],
NestedIOLaunchPlanInput(
"yo",
Nested(
boolean = true,
1.toByte,
2.toShort,
3,
4L,
5.toFloat,
6.toDouble,
"hello",
List("1", "2"),
Map("1" -> "1", "2" -> "2"),
Some(false),
None,
Some(List("3", "4")),
Some(Map("3" -> "3", "4" -> "4")),
NestedNested(7.toDouble, NestedNestedNested("world"))
)
)
)
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2023 Flyte Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.flyte.examples.flytekitscala

import org.flyte.flytekit.{SdkBindingData, SdkRunnableTask, SdkTransform}
import org.flyte.flytekitscala.{
Description,
SdkBindingDataFactory,
SdkScalaType
}

case class NestedNestedNested(string: String)
case class NestedNested(double: Double, nested: NestedNestedNested)
case class Nested(
boolean: Boolean,
byte: Byte,
short: Short,
int: Int,
long: Long,
float: Float,
double: Double,
string: String,
list: List[String],
map: Map[String, String],
optBoolean: Option[Boolean],
optByte: Option[Byte],
optList: Option[List[String]],
optMap: Option[Map[String, String]],
nested: NestedNested
)
case class NestedIOTaskInput(
@Description("the name of the person to be greeted")
name: SdkBindingData[String],
@Description("a nested input")
generic: SdkBindingData[Nested]
)
case class NestedIOTaskOutput(
@Description("the name of the person to be greeted")
name: SdkBindingData[String],
@Description("a nested input")
generic: SdkBindingData[Nested]
)

/** Example Flyte task that takes a name as the input and outputs a simple
* greeting message.
*/
class NestedIOTask
extends SdkRunnableTask[
NestedIOTaskInput,
NestedIOTaskOutput
](
SdkScalaType[NestedIOTaskInput],
SdkScalaType[NestedIOTaskOutput]
) {

/** Defines task behavior. This task takes a name as the input, wraps it in a
* welcome message, and outputs the message.
*
* @param input
* the name of the person to be greeted
* @return
* the welcome message
*/
override def run(input: NestedIOTaskInput): NestedIOTaskOutput =
NestedIOTaskOutput(
input.name,
input.generic
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2020-2023 Flyte Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.flyte.examples.flytekitscala

import org.flyte.flytekitscala.{
SdkScalaType,
SdkScalaWorkflow,
SdkScalaWorkflowBuilder
}

class NestedIOWorkflow
extends SdkScalaWorkflow[NestedIOTaskInput, Unit](
SdkScalaType[NestedIOTaskInput],
SdkScalaType.unit
) {

override def expand(
builder: SdkScalaWorkflowBuilder,
input: NestedIOTaskInput
): Unit = {
builder.apply(new NestedIOTask(), input)
}
}
10 changes: 10 additions & 0 deletions flytekit-scala_2.12/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@
<artifactId>scala-reflect</artifactId>
<version>${scala212.version}</version>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-compiler</artifactId>
<version>${scala212.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down Expand Up @@ -78,6 +83,11 @@
<artifactId>scala-reflect</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-compiler</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
Expand Down
10 changes: 10 additions & 0 deletions flytekit-scala_2.13/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@
<artifactId>scala-library</artifactId>
<version>${scala213.version}</version>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-compiler</artifactId>
<version>${scala213.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down Expand Up @@ -78,6 +83,11 @@
<artifactId>scala-library</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-compiler</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
Expand Down
Loading

0 comments on commit 87dd2fb

Please sign in to comment.