-
Notifications
You must be signed in to change notification settings - Fork 744
feat(sdk/java) enums #9856
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
feat(sdk/java) enums #9856
Changes from all commits
037704a
2fe48a9
0e27176
3a79ebe
ee8f3ce
edd54bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -288,6 +288,85 @@ func (JavaSuite) TestConstructor(_ context.Context, t *testctx.T) { | |
}) | ||
} | ||
|
||
func (JavaSuite) TestEnum(_ context.Context, t *testctx.T) { | ||
t.Run("can use an enum value", func(ctx context.Context, t *testctx.T) { | ||
c := connect(ctx, t) | ||
|
||
out, err := javaModule(t, c, "enums"). | ||
With(daggerCall("print", "--severity=LOW")). | ||
Stdout(ctx) | ||
|
||
require.NoError(t, err) | ||
require.Equal(t, "LOW", out) | ||
}) | ||
|
||
t.Run("can not use a value not defined in the enum", func(ctx context.Context, t *testctx.T) { | ||
c := connect(ctx, t) | ||
|
||
_, err := javaModule(t, c, "enums"). | ||
With(daggerCall("print", "--severity=FOO")). | ||
Stdout(ctx) | ||
|
||
require.Error(t, err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally, the error should include the list of possible values for the enums https://docs.dagger.io/api/enumerations/ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed, added the line after this one 👍 |
||
requireErrOut(t, err, "value should be one of LOW,MEDIUM,HIGH") | ||
}) | ||
|
||
t.Run("can return an enum value", func(ctx context.Context, t *testctx.T) { | ||
c := connect(ctx, t) | ||
|
||
out, err := javaModule(t, c, "enums"). | ||
With(daggerCall("from-string", "--severity=MEDIUM")). | ||
Stdout(ctx) | ||
|
||
require.NoError(t, err) | ||
require.Equal(t, "MEDIUM", out) | ||
}) | ||
|
||
t.Run("can return a list of enum values", func(ctx context.Context, t *testctx.T) { | ||
c := connect(ctx, t) | ||
|
||
out, err := javaModule(t, c, "enums"). | ||
With(daggerCall("get-severities-list")). | ||
Stdout(ctx) | ||
|
||
require.NoError(t, err) | ||
require.Equal(t, "LOW\nMEDIUM\nHIGH\n", out) | ||
}) | ||
|
||
t.Run("can return an array of enum values", func(ctx context.Context, t *testctx.T) { | ||
c := connect(ctx, t) | ||
|
||
out, err := javaModule(t, c, "enums"). | ||
With(daggerCall("get-severities-array")). | ||
Stdout(ctx) | ||
|
||
require.NoError(t, err) | ||
require.Equal(t, "LOW\nMEDIUM\nHIGH\n", out) | ||
}) | ||
|
||
t.Run("can read list of enum values", func(ctx context.Context, t *testctx.T) { | ||
c := connect(ctx, t) | ||
|
||
out, err := javaModule(t, c, "enums"). | ||
With(daggerCall("list-to-string", "--severities=MEDIUM,LOW")). | ||
Stdout(ctx) | ||
|
||
require.NoError(t, err) | ||
require.Equal(t, "MEDIUM,LOW", out) | ||
}) | ||
|
||
t.Run("can read array of enum values", func(ctx context.Context, t *testctx.T) { | ||
c := connect(ctx, t) | ||
|
||
out, err := javaModule(t, c, "enums"). | ||
With(daggerCall("array-to-string", "--severities=HIGH,LOW")). | ||
Stdout(ctx) | ||
|
||
require.NoError(t, err) | ||
require.Equal(t, "HIGH,LOW", out) | ||
}) | ||
} | ||
|
||
func javaModule(t *testctx.T, c *dagger.Client, moduleName string) *dagger.Container { | ||
helderco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
t.Helper() | ||
modSrc, err := filepath.Abs(filepath.Join("./testdata/modules/java", moduleName)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/target/generated-sources/** linguist-generated |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/target |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"name": "my-module", | ||
"engineVersion": "v0.16.4-010101000000-dev-8cdf75fba379", | ||
"sdk": { | ||
"source": "../../sdk/java" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>io.dagger.modules.construct</groupId> | ||
<artifactId>construct</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<name>construct</name> | ||
|
||
<properties> | ||
<maven.compiler.release>17</maven.compiler.release> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<dagger.module.deps>0.16.3-construct-module</dagger.module.deps> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.dagger</groupId> | ||
<artifactId>dagger-java-sdk</artifactId> | ||
<version>${dagger.module.deps}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.dagger</groupId> | ||
<artifactId>dagger-java-annotation-processor</artifactId> | ||
<version>${dagger.module.deps}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-simple</artifactId> | ||
<scope>runtime</scope> | ||
<version>2.0.16</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.eclipse</groupId> | ||
<artifactId>yasson</artifactId> | ||
<version>3.0.4</version> | ||
<scope>runtime</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.netty</groupId> | ||
<artifactId>netty-handler</artifactId> | ||
<version>4.1.118.Final</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>net.minidev</groupId> | ||
<artifactId>json-smart</artifactId> | ||
<version>2.5.2</version> | ||
<scope>runtime</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<pluginManagement> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-clean-plugin</artifactId> | ||
<version>3.4.0</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-resources-plugin</artifactId> | ||
<version>3.3.1</version> | ||
</plugin> | ||
<plugin> | ||
<!-- | ||
This plugin configuration is required by Dagger to generate the module. | ||
Please do not remove or modify it. | ||
--> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.13.0</version> | ||
<configuration> | ||
<verbose>false</verbose> | ||
<annotationProcessorPaths> | ||
<path> | ||
<groupId>io.dagger</groupId> | ||
<artifactId>dagger-java-annotation-processor</artifactId> | ||
<version>${dagger.module.deps}</version> | ||
</path> | ||
</annotationProcessorPaths> | ||
<annotationProcessors> | ||
<annotationProcessor>io.dagger.annotation.processor.DaggerModuleAnnotationProcessor</annotationProcessor> | ||
</annotationProcessors> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
<version>3.3.0</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-jar-plugin</artifactId> | ||
<version>3.4.2</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-install-plugin</artifactId> | ||
<version>3.1.2</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-deploy-plugin</artifactId> | ||
<version>3.1.2</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-site-plugin</artifactId> | ||
<version>3.12.1</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-project-info-reports-plugin</artifactId> | ||
<version>3.6.1</version> | ||
</plugin> | ||
</plugins> | ||
</pluginManagement> | ||
|
||
<plugins> | ||
<plugin> | ||
<!-- | ||
This plugin configuration helps VS Code editor (and others) to find | ||
the generated code created by `dagger init` and `dagger develop` and | ||
helps for code completion. | ||
--> | ||
<groupId>org.codehaus.mojo</groupId> | ||
<artifactId>build-helper-maven-plugin</artifactId> | ||
<version>3.3.0</version> | ||
<executions> | ||
<execution> | ||
<id>add-generated-sources</id> | ||
<goals> | ||
<goal>add-source</goal> | ||
</goals> | ||
<configuration> | ||
<sources> | ||
<source>${project.build.directory}/generated-sources/dagger-io</source> | ||
<source>${project.build.directory}/generated-sources/dagger-module</source> | ||
<source>${project.build.directory}/generated-sources/entrypoint</source> | ||
</sources> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
<plugin> | ||
<!-- | ||
This plugin configuration is required by Dagger to generate the module. | ||
Please do not remove or modify it. | ||
--> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-shade-plugin</artifactId> | ||
<version>3.5.0</version> | ||
<executions> | ||
<execution> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>shade</goal> | ||
</goals> | ||
<configuration> | ||
<transformers> | ||
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> | ||
<mainClass>io.dagger.gen.entrypoint.Entrypoint</mainClass> | ||
</transformer> | ||
</transformers> | ||
<outputDirectory>${project.build.outputDirectory}</outputDirectory> | ||
<finalName>${project.artifactId}-${project.version}</finalName> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package io.dagger.modules.mymodule; | ||
|
||
import io.dagger.module.annotation.Function; | ||
import io.dagger.module.annotation.Object; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Object | ||
public class MyModule { | ||
@Function | ||
public String print(Severity severity) { | ||
return severity.name(); | ||
} | ||
|
||
@Function | ||
public Severity fromString(String severity) { | ||
return Severity.valueOf(severity); | ||
} | ||
|
||
@Function | ||
public List<Severity> getSeveritiesList() { | ||
return Arrays.asList(Severity.values()); | ||
} | ||
|
||
@Function | ||
public Severity[] getSeveritiesArray() { | ||
return Severity.values(); | ||
} | ||
|
||
@Function | ||
public String listToString(List<Severity> severities) { | ||
return severities.stream().map(Severity::name).collect(Collectors.joining(",")); | ||
} | ||
|
||
@Function | ||
public String arrayToString(Severity[] severities) { | ||
return Arrays.stream(severities).map(Severity::name).collect(Collectors.joining(",")); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package io.dagger.modules.mymodule; | ||
|
||
import io.dagger.module.annotation.Enum; | ||
|
||
@Enum | ||
public enum Severity { | ||
LOW, | ||
MEDIUM, | ||
HIGH | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package io.dagger.modules.mymodule; | ||
|
||
import static io.dagger.client.Dagger.dag; | ||
|
||
import io.dagger.client.DaggerQueryException; | ||
import io.dagger.module.annotation.Function; | ||
import io.dagger.module.annotation.Object; | ||
import java.util.List; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
@Object | ||
public class MyModule { | ||
@Function | ||
public String scan(String ref, Severity severity) | ||
throws ExecutionException, DaggerQueryException, InterruptedException { | ||
var ctr = dag().container().from(ref); | ||
|
||
return dag() | ||
.container() | ||
.from("aquasec/trivy:0.50.4") | ||
.withMountedFile("/mnt/ctr.tar", ctr.asTarball()) | ||
.withMountedCache("/root/.cache", dag().cacheVolume("trivy-cache")) | ||
.withExec( | ||
List.of( | ||
"trivy", | ||
"image", | ||
"--format=json", | ||
"--no-progress", | ||
"--exit-code=1", | ||
"--vuln-type=os,library", | ||
"--severity=" + severity.name(), | ||
"--show-suppressed", | ||
"--input=/mnt/ctr.tar")) | ||
.stdout(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package io.dagger.modules.mymodule; | ||
|
||
import io.dagger.module.annotation.Enum; | ||
|
||
@Enum | ||
public enum Severity { | ||
UNKNOWN, | ||
LOW, | ||
MEDIUM, | ||
HIGH, | ||
CRITICAL | ||
} |
Uh oh!
There was an error while loading. Please reload this page.