Skip to content

Commit

Permalink
use latest record-builder release
Browse files Browse the repository at this point in the history
  • Loading branch information
sebhoss committed Jan 10, 2023
1 parent dfd7191 commit 9a127c7
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 19 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,18 @@ String image = devcontainer.image();
The `Devcontainer` class is annotated with [record-builder](https://github.com/Randgalt/record-builder) annotations which allow you to create new `Devcontainer` instances like this:

```java
Devcontainer devcontainer = DevcontainerBuilder.builder()
Devcontainer devcontainer = Devcontainer.builder()
.image("docker.io/someuser/someimage:someversion")
.create()
```

All records in this project support withers and return a new instance of themselves using the new value:

```java
Devcontainer devcontainer = ...
Devcontainer changed = devcontainer.withImage("quay.io/other/image:version");
```

### Integration

In order to use this library in your project, declare the following dependency:
Expand Down Expand Up @@ -76,4 +83,4 @@ PERFORMANCE OF THIS SOFTWARE.
- https://github.com/metio/devcontainer.java
- https://gitlab.com/metio.wtf/devcontainer.java
- https://bitbucket.org/metio-wtf/devcontainer.java
- https://codeberg.org/metio.wtf/devcontainer.java
- https://codeberg.org/metio.wtf/devcontainer.java
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
<dependency>
<groupId>io.soabase.record-builder</groupId>
<artifactId>record-builder-core</artifactId>
<version>34</version>
<version>35</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand All @@ -102,7 +102,7 @@
<path>
<groupId>io.soabase.record-builder</groupId>
<artifactId>record-builder-processor</artifactId>
<version>34</version>
<version>35</version>
</path>
</annotationProcessorPaths>
</configuration>
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/wtf/metio/devcontainer/Build.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@
* string vs array properties.
*/
@RecordBuilder
@RecordBuilder.Options(buildMethodName = "create", enableWither = false)
@RecordBuilder.Options(buildMethodName = "create")
public record Build(
String dockerfile,
String context,
Map<String, String> args,
String target,
List<String> cacheFrom) {
List<String> cacheFrom) implements BuildBuilder.With {

public static BuildBuilder builder() {
return BuildBuilder.builder();
}

}
8 changes: 6 additions & 2 deletions src/main/java/wtf/metio/devcontainer/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@
* reference</a>
*/
@RecordBuilder
@RecordBuilder.Options(buildMethodName = "create", enableWither = false)
@RecordBuilder.Options(buildMethodName = "create")
@JsonDeserialize(using = CommandDeserializer.class)
public record Command(
String string,
List<String> array,
Map<String, Command> object) {
Map<String, Command> object) implements CommandBuilder.With {

public static CommandBuilder builder() {
return CommandBuilder.builder();
}

}
8 changes: 6 additions & 2 deletions src/main/java/wtf/metio/devcontainer/Devcontainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@
* @see <a href="https://code.visualstudio.com/docs/remote/devcontainerjson-reference">devcontainer reference</a>
*/
@RecordBuilder
@RecordBuilder.Options(buildMethodName = "create", enableWither = false)
@RecordBuilder.Options(buildMethodName = "create")
public record Devcontainer(
String name,
List<String> forwardPorts,
Expand Down Expand Up @@ -220,7 +220,7 @@ public record Devcontainer(
Command postStartCommand,
Command postAttachCommand,
WaitFor waitFor,
HostRequirements hostRequirements) {
HostRequirements hostRequirements) implements DevcontainerBuilder.With {

public static Devcontainer parse(final Path devcontainer) throws IOException {
return parse(devcontainer, defaultObjectMapper());
Expand Down Expand Up @@ -253,4 +253,8 @@ public static ObjectMapper defaultObjectMapper() {
return mapper;
}

public static DevcontainerBuilder builder() {
return DevcontainerBuilder.builder();
}

}
8 changes: 6 additions & 2 deletions src/main/java/wtf/metio/devcontainer/HostRequirements.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@
* "hostRequirements": {"storage": "32gb"}
*/
@RecordBuilder
@RecordBuilder.Options(buildMethodName = "create", enableWither = false)
@RecordBuilder.Options(buildMethodName = "create")
public record HostRequirements(
Integer cpus,
String memory,
String storage) {
String storage) implements HostRequirementsBuilder.With {

public static HostRequirementsBuilder builder() {
return HostRequirementsBuilder.builder();
}

}
8 changes: 6 additions & 2 deletions src/main/java/wtf/metio/devcontainer/PortAttribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,16 @@
* @see <a href="https://containers.dev/implementors/json_reference/#port-attributes">schema reference</a>
*/
@RecordBuilder
@RecordBuilder.Options(buildMethodName = "create", enableWither = false)
@RecordBuilder.Options(buildMethodName = "create")
public record PortAttribute(
String label,
Protocol protocol,
OnAutoForward onAutoForward,
Boolean requireLocalPort,
Boolean elevateIfNeeded) {
Boolean elevateIfNeeded) implements PortAttributeBuilder.With {

public static PortAttributeBuilder builder() {
return PortAttributeBuilder.builder();
}

}
93 changes: 88 additions & 5 deletions src/test/java/wtf/metio/devcontainer/DevcontainerBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.Map;

class DevcontainerBuilderTest {

@Test
void createInstanceWithBuilder() {
final var devcontainer = DevcontainerBuilder.builder()
final var devcontainer = Devcontainer.builder()
.name("test")
.create();

Expand All @@ -20,14 +22,95 @@ void createInstanceWithBuilder() {

@Test
void createNestedBuildComponent() {
final var devcontainer = DevcontainerBuilder.builder()
.name("test")
.build(BuildBuilder.builder().context(".").dockerfile("Containerfile").create())
.create();
final var devcontainer = Devcontainer.builder()
.name("test")
.build(Build.builder().context(".").dockerfile("Containerfile").create())
.create();

Assertions.assertEquals("test", devcontainer.name());
Assertions.assertEquals(".", devcontainer.build().context());
Assertions.assertEquals("Containerfile", devcontainer.build().dockerfile());
}

@Test
void createNestedCommandComponent() {
final var devcontainer = Devcontainer.builder()
.name("test")
.initializeCommand(Command.builder().string("ps -aux").create())
.create();

Assertions.assertEquals("test", devcontainer.name());
Assertions.assertEquals("ps -aux", devcontainer.initializeCommand().string());
}

@Test
void createNestedHostRequirementsComponent() {
final var devcontainer = Devcontainer.builder()
.name("test")
.hostRequirements(HostRequirements.builder().cpus(3).create())
.create();

Assertions.assertEquals("test", devcontainer.name());
Assertions.assertEquals(3, devcontainer.hostRequirements().cpus());
}

@Test
void createNestedPortAttributeComponent() {
final var devcontainer = Devcontainer.builder()
.name("test")
.portsAttributes(Map.of("http", PortAttribute.builder().elevateIfNeeded(true).create()))
.create();

Assertions.assertEquals("test", devcontainer.name());
Assertions.assertNotNull(devcontainer.portsAttributes().get("http"));
Assertions.assertEquals(true, devcontainer.portsAttributes().get("http").elevateIfNeeded());
}

@Test
void adjustDevcontainerWithWither() {
final var original = Devcontainer.builder()
.name("test")
.create();
final var changed = original.withName("new name");

Assertions.assertEquals("test", original.name());
Assertions.assertEquals("new name", changed.name());
}

@Test
void adjustBuildWithWither() {
final var original = Build.builder().dockerfile("Containerfile").create();
final var changed = original.withDockerfile("Dockerfile");

Assertions.assertEquals("Containerfile", original.dockerfile());
Assertions.assertEquals("Dockerfile", changed.dockerfile());
}

@Test
void adjustCommandWithWither() {
final var original = Command.builder().string("ps -aux").create();
final var changed = original.withString("htop");

Assertions.assertEquals("ps -aux", original.string());
Assertions.assertEquals("htop", changed.string());
}

@Test
void adjustHostRequirementsWithWither() {
final var original = HostRequirements.builder().cpus(3).create();
final var changed = original.withCpus(5);

Assertions.assertEquals(3, original.cpus());
Assertions.assertEquals(5, changed.cpus());
}

@Test
void adjustHostPortAttributeWithWither() {
final var original = PortAttribute.builder().elevateIfNeeded(true).create();
final var changed = original.withElevateIfNeeded(false);

Assertions.assertEquals(true, original.elevateIfNeeded());
Assertions.assertEquals(false, changed.elevateIfNeeded());
}

}

0 comments on commit 9a127c7

Please sign in to comment.