From 5113f2157c5aa257ad7ebf555cb6a551701d169a Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Mon, 10 Aug 2026 10:19:49 +0100 Subject: Add generic struct example without any outputs --- .../zwobble/example/types/data/GenericNative.java | 3 + .../example/types/data/HobgoblinNativeData.java | 13 + .../org/zwobble/example/types/data/Native.java | 3 + .../data/transient0/HobgoblinTransient0Data.java | 581 +++++++++++++++++++++ .../zwobble/example/types/transient0/Decoder.java | 13 + .../zwobble/example/types/transient0/Encoder.java | 13 + .../transient0/HobgoblinTransient0Builtins.java | 82 +++ .../HobgoblinTransient0TaggedSharedValue.java | 22 + .../java/org/zwobble/example/Transient0Tests.java | 392 ++++++++++++++ 9 files changed, 1122 insertions(+) create mode 100644 examples/16-transient-0/output/java/src/main/java/org/zwobble/example/types/data/GenericNative.java create mode 100644 examples/16-transient-0/output/java/src/main/java/org/zwobble/example/types/data/HobgoblinNativeData.java create mode 100644 examples/16-transient-0/output/java/src/main/java/org/zwobble/example/types/data/Native.java create mode 100644 examples/16-transient-0/output/java/src/main/java/org/zwobble/example/types/data/transient0/HobgoblinTransient0Data.java create mode 100644 examples/16-transient-0/output/java/src/main/java/org/zwobble/example/types/transient0/Decoder.java create mode 100644 examples/16-transient-0/output/java/src/main/java/org/zwobble/example/types/transient0/Encoder.java create mode 100644 examples/16-transient-0/output/java/src/main/java/org/zwobble/example/types/transient0/HobgoblinTransient0Builtins.java create mode 100644 examples/16-transient-0/output/java/src/main/java/org/zwobble/example/types/transient0/HobgoblinTransient0TaggedSharedValue.java create mode 100644 examples/16-transient-0/output/java/src/test/java/org/zwobble/example/Transient0Tests.java (limited to 'examples/16-transient-0/output/java/src') diff --git a/examples/16-transient-0/output/java/src/main/java/org/zwobble/example/types/data/GenericNative.java b/examples/16-transient-0/output/java/src/main/java/org/zwobble/example/types/data/GenericNative.java new file mode 100644 index 0000000..cfd16d5 --- /dev/null +++ b/examples/16-transient-0/output/java/src/main/java/org/zwobble/example/types/data/GenericNative.java @@ -0,0 +1,3 @@ +package org.zwobble.example.types.data; + +public record GenericNative(A a, B b) {} diff --git a/examples/16-transient-0/output/java/src/main/java/org/zwobble/example/types/data/HobgoblinNativeData.java b/examples/16-transient-0/output/java/src/main/java/org/zwobble/example/types/data/HobgoblinNativeData.java new file mode 100644 index 0000000..32796b5 --- /dev/null +++ b/examples/16-transient-0/output/java/src/main/java/org/zwobble/example/types/data/HobgoblinNativeData.java @@ -0,0 +1,13 @@ +package org.zwobble.example.types.data; + +public class HobgoblinNativeData { + private HobgoblinNativeData() {} + + public static Native arbitraryNative() { + return new Native(0, 0); + } + + public static GenericNative arbitraryGenericNative() { + throw new RuntimeException("TODO"); + } +} diff --git a/examples/16-transient-0/output/java/src/main/java/org/zwobble/example/types/data/Native.java b/examples/16-transient-0/output/java/src/main/java/org/zwobble/example/types/data/Native.java new file mode 100644 index 0000000..366dd74 --- /dev/null +++ b/examples/16-transient-0/output/java/src/main/java/org/zwobble/example/types/data/Native.java @@ -0,0 +1,3 @@ +package org.zwobble.example.types.data; + +public record Native(int a, int b) {} diff --git a/examples/16-transient-0/output/java/src/main/java/org/zwobble/example/types/data/transient0/HobgoblinTransient0Data.java b/examples/16-transient-0/output/java/src/main/java/org/zwobble/example/types/data/transient0/HobgoblinTransient0Data.java new file mode 100644 index 0000000..1eea54c --- /dev/null +++ b/examples/16-transient-0/output/java/src/main/java/org/zwobble/example/types/data/transient0/HobgoblinTransient0Data.java @@ -0,0 +1,581 @@ +// Generated by hobgoblin. + +package org.zwobble.example.types.data.transient0; + +// Custom area start: org.zwobble.example.types.data.transient0.HobgoblinTransient0Data imports +import java.io.InputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.util.List; +import java.util.Map; +import org.zwobble.example.types.data.GenericNative; +import org.zwobble.example.types.data.Native; +import org.zwobble.example.types.transient0.Decoder; +import org.zwobble.example.types.transient0.Encoder; +import org.zwobble.example.types.transient0.HobgoblinTransient0Builtins; +import org.zwobble.example.types.transient0.HobgoblinTransient0TaggedSharedValue; +// Custom area end: org.zwobble.example.types.data.transient0.HobgoblinTransient0Data imports + +public final class HobgoblinTransient0Data { + public static void encodeStructSingleton(org.zwobble.example.types.data.StructSingleton value, java.io.OutputStream outputStream, java.util.Map sharedValues) throws java.io.IOException { + } + + public static org.zwobble.example.types.data.StructSingleton decodeStructSingleton(java.io.InputStream inputStream, java.util.List sharedValues) throws java.io.IOException { + return org.zwobble.example.types.data.StructSingleton.INSTANCE; + } + + public static void encodeStructWithBool(org.zwobble.example.types.data.StructWithBool value, java.io.OutputStream outputStream, java.util.Map sharedValues) throws java.io.IOException { + org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.encodeBool(value.a(), outputStream, sharedValues); + org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.encodeBool(value.b(), outputStream, sharedValues); + } + + public static org.zwobble.example.types.data.StructWithBool decodeStructWithBool(java.io.InputStream inputStream, java.util.List sharedValues) throws java.io.IOException { + var a = org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.decodeBool(inputStream, sharedValues); + var b = org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.decodeBool(inputStream, sharedValues); + return new org.zwobble.example.types.data.StructWithBool(a, b); + } + + public static void encodeStructWithInt8(org.zwobble.example.types.data.StructWithInt8 value, java.io.OutputStream outputStream, java.util.Map sharedValues) throws java.io.IOException { + org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.encodeInt8(value.a(), outputStream, sharedValues); + org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.encodeInt8(value.b(), outputStream, sharedValues); + } + + public static org.zwobble.example.types.data.StructWithInt8 decodeStructWithInt8(java.io.InputStream inputStream, java.util.List sharedValues) throws java.io.IOException { + var a = org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.decodeInt8(inputStream, sharedValues); + var b = org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.decodeInt8(inputStream, sharedValues); + return new org.zwobble.example.types.data.StructWithInt8(a, b); + } + + public static void encodeStructWithInt32(org.zwobble.example.types.data.StructWithInt32 value, java.io.OutputStream outputStream, java.util.Map sharedValues) throws java.io.IOException { + org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.encodeInt32(value.a(), outputStream, sharedValues); + org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.encodeInt32(value.b(), outputStream, sharedValues); + } + + public static org.zwobble.example.types.data.StructWithInt32 decodeStructWithInt32(java.io.InputStream inputStream, java.util.List sharedValues) throws java.io.IOException { + var a = org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.decodeInt32(inputStream, sharedValues); + var b = org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.decodeInt32(inputStream, sharedValues); + return new org.zwobble.example.types.data.StructWithInt32(a, b); + } + + public static void encodeStructWithInt64(org.zwobble.example.types.data.StructWithInt64 value, java.io.OutputStream outputStream, java.util.Map sharedValues) throws java.io.IOException { + org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.encodeInt64(value.a(), outputStream, sharedValues); + org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.encodeInt64(value.b(), outputStream, sharedValues); + } + + public static org.zwobble.example.types.data.StructWithInt64 decodeStructWithInt64(java.io.InputStream inputStream, java.util.List sharedValues) throws java.io.IOException { + var a = org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.decodeInt64(inputStream, sharedValues); + var b = org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.decodeInt64(inputStream, sharedValues); + return new org.zwobble.example.types.data.StructWithInt64(a, b); + } + + public static void encodeStructWithString(org.zwobble.example.types.data.StructWithString value, java.io.OutputStream outputStream, java.util.Map sharedValues) throws java.io.IOException { + org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.encodeString(value.a(), outputStream, sharedValues); + org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.encodeString(value.b(), outputStream, sharedValues); + } + + public static org.zwobble.example.types.data.StructWithString decodeStructWithString(java.io.InputStream inputStream, java.util.List sharedValues) throws java.io.IOException { + var a = org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.decodeString(inputStream, sharedValues); + var b = org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.decodeString(inputStream, sharedValues); + return new org.zwobble.example.types.data.StructWithString(a, b); + } + + public static void encodeStructWithStruct(org.zwobble.example.types.data.StructWithStruct value, java.io.OutputStream outputStream, java.util.Map sharedValues) throws java.io.IOException { + org.zwobble.example.types.data.transient0.HobgoblinTransient0Data.encodeStructWithInt32(value.inner(), outputStream, sharedValues); + org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.encodeInt32(value.c(), outputStream, sharedValues); + } + + public static org.zwobble.example.types.data.StructWithStruct decodeStructWithStruct(java.io.InputStream inputStream, java.util.List sharedValues) throws java.io.IOException { + var inner = org.zwobble.example.types.data.transient0.HobgoblinTransient0Data.decodeStructWithInt32(inputStream, sharedValues); + var c = org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.decodeInt32(inputStream, sharedValues); + return new org.zwobble.example.types.data.StructWithStruct(inner, c); + } + + public static void encodeStructWithList(org.zwobble.example.types.data.StructWithList value, java.io.OutputStream outputStream, java.util.Map sharedValues) throws java.io.IOException { + org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.encodeInt64((long) value.a().size(), outputStream, sharedValues); + for (var element : value.a()) { + org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.encodeInt64(element, outputStream, sharedValues); + } + org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.encodeInt64((long) value.b().size(), outputStream, sharedValues); + for (var element : value.b()) { + org.zwobble.example.types.data.transient0.HobgoblinTransient0Data.encodeStructWithInt32(element, outputStream, sharedValues); + } + } + + public static org.zwobble.example.types.data.StructWithList decodeStructWithList(java.io.InputStream inputStream, java.util.List sharedValues) throws java.io.IOException { + var a$size = org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.decodeInt64(inputStream, sharedValues); + if (a$size > java.lang.Integer.MAX_VALUE) { + throw new java.io.IOException("Lists longer than Integer.MAX_VALUE are not supported"); + } + var a = new java.util.ArrayList((int) a$size); + for (long a$index = 0; a$index < a$size; a$index++) { + var a$element = org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.decodeInt64(inputStream, sharedValues); + a.add(a$element); + } + var b$size = org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.decodeInt64(inputStream, sharedValues); + if (b$size > java.lang.Integer.MAX_VALUE) { + throw new java.io.IOException("Lists longer than Integer.MAX_VALUE are not supported"); + } + var b = new java.util.ArrayList((int) b$size); + for (long b$index = 0; b$index < b$size; b$index++) { + var b$element = org.zwobble.example.types.data.transient0.HobgoblinTransient0Data.decodeStructWithInt32(inputStream, sharedValues); + b.add(b$element); + } + return new org.zwobble.example.types.data.StructWithList(a, b); + } + + public static void encodeStructWithOption(org.zwobble.example.types.data.StructWithOption value, java.io.OutputStream outputStream, java.util.Map sharedValues) throws java.io.IOException { + org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.encodeBool(value.a().isPresent(), outputStream, sharedValues); + if (value.a().isPresent()) { + org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.encodeInt64(value.a().get(), outputStream, sharedValues); + } + org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.encodeBool(value.b().isPresent(), outputStream, sharedValues); + if (value.b().isPresent()) { + org.zwobble.example.types.data.transient0.HobgoblinTransient0Data.encodeStructWithInt32(value.b().get(), outputStream, sharedValues); + } + } + + public static org.zwobble.example.types.data.StructWithOption decodeStructWithOption(java.io.InputStream inputStream, java.util.List sharedValues) throws java.io.IOException { + var a$isPresent = org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.decodeBool(inputStream, sharedValues); + java.util.Optional a; + if (a$isPresent) { + var a$element = org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.decodeInt64(inputStream, sharedValues); + a = java.util.Optional.of(a$element); + } else { + a = java.util.Optional.empty(); + } + var b$isPresent = org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.decodeBool(inputStream, sharedValues); + java.util.Optional b; + if (b$isPresent) { + var b$element = org.zwobble.example.types.data.transient0.HobgoblinTransient0Data.decodeStructWithInt32(inputStream, sharedValues); + b = java.util.Optional.of(b$element); + } else { + b = java.util.Optional.empty(); + } + return new org.zwobble.example.types.data.StructWithOption(a, b); + } + + public static void encodeEnumWithVariants(org.zwobble.example.types.data.EnumWithVariants value, java.io.OutputStream outputStream, java.util.Map sharedValues) throws java.io.IOException { + org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.encodeInt32(value.ordinal(), outputStream, sharedValues); + } + + private static final org.zwobble.example.types.data.EnumWithVariants[] EnumWithVariants$values = org.zwobble.example.types.data.EnumWithVariants.values(); + + public static org.zwobble.example.types.data.EnumWithVariants decodeEnumWithVariants(java.io.InputStream inputStream, java.util.List sharedValues) throws java.io.IOException { + var ordinal = org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.decodeInt32(inputStream, sharedValues); + return EnumWithVariants$values[ordinal]; + } + + public static void encodeStructWithEnum(org.zwobble.example.types.data.StructWithEnum value, java.io.OutputStream outputStream, java.util.Map sharedValues) throws java.io.IOException { + org.zwobble.example.types.data.transient0.HobgoblinTransient0Data.encodeEnumWithVariants(value.inner(), outputStream, sharedValues); + org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.encodeInt32(value.c(), outputStream, sharedValues); + } + + public static org.zwobble.example.types.data.StructWithEnum decodeStructWithEnum(java.io.InputStream inputStream, java.util.List sharedValues) throws java.io.IOException { + var inner = org.zwobble.example.types.data.transient0.HobgoblinTransient0Data.decodeEnumWithVariants(inputStream, sharedValues); + var c = org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.decodeInt32(inputStream, sharedValues); + return new org.zwobble.example.types.data.StructWithEnum(inner, c); + } + + public static void encodeSumWithVariants(org.zwobble.example.types.data.SumWithVariants value, java.io.OutputStream outputStream, java.util.Map sharedValues) throws java.io.IOException { + switch (value) { + case org.zwobble.example.types.data.VariantOne value$0 -> { + org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.encodeInt32(0, outputStream, sharedValues); + org.zwobble.example.types.data.transient0.HobgoblinTransient0Data.encodeVariantOne(value$0, outputStream, sharedValues); + } + case org.zwobble.example.types.data.VariantTwo value$1 -> { + org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.encodeInt32(1, outputStream, sharedValues); + org.zwobble.example.types.data.transient0.HobgoblinTransient0Data.encodeVariantTwo(value$1, outputStream, sharedValues); + } + } + } + + public static org.zwobble.example.types.data.SumWithVariants decodeSumWithVariants(java.io.InputStream inputStream, java.util.List sharedValues) throws java.io.IOException { + var variantTag = org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.decodeInt32(inputStream, sharedValues); + switch (variantTag) { + case 0 -> { + var value$0 = org.zwobble.example.types.data.transient0.HobgoblinTransient0Data.decodeVariantOne(inputStream, sharedValues); + return value$0; + } + case 1 -> { + var value$1 = org.zwobble.example.types.data.transient0.HobgoblinTransient0Data.decodeVariantTwo(inputStream, sharedValues); + return value$1; + } + default -> { + throw new java.lang.RuntimeException("unexpected data.SumWithVariants variant tag: " + variantTag); + } + } + } + + public static void encodeVariantOne(org.zwobble.example.types.data.VariantOne value, java.io.OutputStream outputStream, java.util.Map sharedValues) throws java.io.IOException { + org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.encodeInt32(value.a(), outputStream, sharedValues); + } + + public static org.zwobble.example.types.data.VariantOne decodeVariantOne(java.io.InputStream inputStream, java.util.List sharedValues) throws java.io.IOException { + var a = org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.decodeInt32(inputStream, sharedValues); + return new org.zwobble.example.types.data.VariantOne(a); + } + + public static void encodeVariantTwo(org.zwobble.example.types.data.VariantTwo value, java.io.OutputStream outputStream, java.util.Map sharedValues) throws java.io.IOException { + org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.encodeInt64(value.a(), outputStream, sharedValues); + } + + public static org.zwobble.example.types.data.VariantTwo decodeVariantTwo(java.io.InputStream inputStream, java.util.List sharedValues) throws java.io.IOException { + var a = org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.decodeInt64(inputStream, sharedValues); + return new org.zwobble.example.types.data.VariantTwo(a); + } + + public static void encodeSumWithBoxedVariants(org.zwobble.example.types.data.SumWithBoxedVariants value, java.io.OutputStream outputStream, java.util.Map sharedValues) throws java.io.IOException { + switch (value) { + case org.zwobble.example.types.data.VariantOne value$0 -> { + org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.encodeInt32(0, outputStream, sharedValues); + org.zwobble.example.types.data.transient0.HobgoblinTransient0Data.encodeVariantOne(value$0, outputStream, sharedValues); + } + case org.zwobble.example.types.data.VariantTwo value$1 -> { + org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.encodeInt32(1, outputStream, sharedValues); + org.zwobble.example.types.data.transient0.HobgoblinTransient0Data.encodeVariantTwo(value$1, outputStream, sharedValues); + } + } + } + + public static org.zwobble.example.types.data.SumWithBoxedVariants decodeSumWithBoxedVariants(java.io.InputStream inputStream, java.util.List sharedValues) throws java.io.IOException { + var variantTag = org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.decodeInt32(inputStream, sharedValues); + switch (variantTag) { + case 0 -> { + var value$0 = org.zwobble.example.types.data.transient0.HobgoblinTransient0Data.decodeVariantOne(inputStream, sharedValues); + return value$0; + } + case 1 -> { + var value$1 = org.zwobble.example.types.data.transient0.HobgoblinTransient0Data.decodeVariantTwo(inputStream, sharedValues); + return value$1; + } + default -> { + throw new java.lang.RuntimeException("unexpected data.SumWithBoxedVariants variant tag: " + variantTag); + } + } + } + + public static void encodeStructWithSum(org.zwobble.example.types.data.StructWithSum value, java.io.OutputStream outputStream, java.util.Map sharedValues) throws java.io.IOException { + org.zwobble.example.types.data.transient0.HobgoblinTransient0Data.encodeSumWithVariants(value.inner(), outputStream, sharedValues); + org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.encodeInt32(value.b(), outputStream, sharedValues); + } + + public static org.zwobble.example.types.data.StructWithSum decodeStructWithSum(java.io.InputStream inputStream, java.util.List sharedValues) throws java.io.IOException { + var inner = org.zwobble.example.types.data.transient0.HobgoblinTransient0Data.decodeSumWithVariants(inputStream, sharedValues); + var b = org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.decodeInt32(inputStream, sharedValues); + return new org.zwobble.example.types.data.StructWithSum(inner, b); + } + + public static void encodeStructWithNative(org.zwobble.example.types.data.StructWithNative value, java.io.OutputStream outputStream, java.util.Map sharedValues) throws java.io.IOException { + org.zwobble.example.types.data.transient0.HobgoblinTransient0Data.encodeNative(value.a(), outputStream, sharedValues); + } + + public static org.zwobble.example.types.data.StructWithNative decodeStructWithNative(java.io.InputStream inputStream, java.util.List sharedValues) throws java.io.IOException { + var a = org.zwobble.example.types.data.transient0.HobgoblinTransient0Data.decodeNative(inputStream, sharedValues); + return new org.zwobble.example.types.data.StructWithNative(a); + } + + public static void encodeStructWithGenericNative(org.zwobble.example.types.data.StructWithGenericNative value, java.io.OutputStream outputStream, java.util.Map sharedValues) throws java.io.IOException { + org.zwobble.example.types.data.transient0.HobgoblinTransient0Data.encodeGenericNative(value.a(), outputStream, sharedValues, org.zwobble.example.types.transient0.HobgoblinTransient0Builtins::encodeInt32, org.zwobble.example.types.transient0.HobgoblinTransient0Builtins::encodeString); + org.zwobble.example.types.data.transient0.HobgoblinTransient0Data.encodeGenericNative(value.b(), outputStream, sharedValues, org.zwobble.example.types.data.transient0.HobgoblinTransient0Data::encode$constructedType$0, org.zwobble.example.types.data.transient0.HobgoblinTransient0Data::encode$constructedType$1); + } + + public static org.zwobble.example.types.data.StructWithGenericNative decodeStructWithGenericNative(java.io.InputStream inputStream, java.util.List sharedValues) throws java.io.IOException { + var a = org.zwobble.example.types.data.transient0.HobgoblinTransient0Data.decodeGenericNative(inputStream, sharedValues, org.zwobble.example.types.transient0.HobgoblinTransient0Builtins::decodeInt32, org.zwobble.example.types.transient0.HobgoblinTransient0Builtins::decodeString); + var b = org.zwobble.example.types.data.transient0.HobgoblinTransient0Data.decodeGenericNative(inputStream, sharedValues, org.zwobble.example.types.data.transient0.HobgoblinTransient0Data::decode$constructedType$0, org.zwobble.example.types.data.transient0.HobgoblinTransient0Data::decode$constructedType$1); + return new org.zwobble.example.types.data.StructWithGenericNative(a, b); + } + + public static void encodeStructWithBox(org.zwobble.example.types.data.StructWithBox value, java.io.OutputStream outputStream, java.util.Map sharedValues) throws java.io.IOException { + org.zwobble.example.types.data.transient0.HobgoblinTransient0Data.encodeStructWithInt32(value.inner(), outputStream, sharedValues); + } + + public static org.zwobble.example.types.data.StructWithBox decodeStructWithBox(java.io.InputStream inputStream, java.util.List sharedValues) throws java.io.IOException { + var inner = org.zwobble.example.types.data.transient0.HobgoblinTransient0Data.decodeStructWithInt32(inputStream, sharedValues); + return new org.zwobble.example.types.data.StructWithBox(inner); + } + + public static void encodeStructWithListOfShared(org.zwobble.example.types.data.StructWithListOfShared value, java.io.OutputStream outputStream, java.util.Map sharedValues) throws java.io.IOException { + org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.encodeInt64((long) value.inner().size(), outputStream, sharedValues); + for (var element : value.inner()) { + { + var existingId = sharedValues.get(new org.zwobble.example.types.transient0.HobgoblinTransient0TaggedSharedValue(0, element)); + if (existingId != null) { + org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.encodeInt64(existingId, outputStream, sharedValues); + } else { + org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.encodeInt64(sharedValues.size(), outputStream, sharedValues); + sharedValues.put(new org.zwobble.example.types.transient0.HobgoblinTransient0TaggedSharedValue(0, element), (long) sharedValues.size()); + org.zwobble.example.types.data.transient0.HobgoblinTransient0Data.encodeStructWithInt32(element, outputStream, sharedValues); + } + } + } + } + + public static org.zwobble.example.types.data.StructWithListOfShared decodeStructWithListOfShared(java.io.InputStream inputStream, java.util.List sharedValues) throws java.io.IOException { + var inner$size = org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.decodeInt64(inputStream, sharedValues); + if (inner$size > java.lang.Integer.MAX_VALUE) { + throw new java.io.IOException("Lists longer than Integer.MAX_VALUE are not supported"); + } + var inner = new java.util.ArrayList((int) inner$size); + for (long inner$index = 0; inner$index < inner$size; inner$index++) { + var inner$element$id = org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.decodeInt64(inputStream, sharedValues); + if (inner$element$id > java.lang.Integer.MAX_VALUE) { + throw new java.io.IOException("Shared value IDs greater than Integer.MAX_VALUE are not supported"); + } + org.zwobble.example.types.data.StructWithInt32 inner$element; + if (inner$element$id < sharedValues.size()) { + inner$element = switch (sharedValues.get((int) inner$element$id)) { + case org.zwobble.example.types.data.StructWithInt32 inner$element$decodedValue -> { + yield inner$element$decodedValue; + } + default -> { + throw new java.io.IOException("Shared value was of the wrong type"); + } + }; + } else { + var inner$element$decodedValue = org.zwobble.example.types.data.transient0.HobgoblinTransient0Data.decodeStructWithInt32(inputStream, sharedValues); + sharedValues.add(inner$element$decodedValue); + inner$element = inner$element$decodedValue; + } + inner.add(inner$element); + } + return new org.zwobble.example.types.data.StructWithListOfShared(inner); + } + + public static void encodeStructWithDifferentSharedTypes(org.zwobble.example.types.data.StructWithDifferentSharedTypes value, java.io.OutputStream outputStream, java.util.Map sharedValues) throws java.io.IOException { + { + var existingId = sharedValues.get(new org.zwobble.example.types.transient0.HobgoblinTransient0TaggedSharedValue(0, value.a())); + if (existingId != null) { + org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.encodeInt64(existingId, outputStream, sharedValues); + } else { + org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.encodeInt64(sharedValues.size(), outputStream, sharedValues); + sharedValues.put(new org.zwobble.example.types.transient0.HobgoblinTransient0TaggedSharedValue(0, value.a()), (long) sharedValues.size()); + org.zwobble.example.types.data.transient0.HobgoblinTransient0Data.encodeStructWithInt32(value.a(), outputStream, sharedValues); + } + } + { + var existingId = sharedValues.get(new org.zwobble.example.types.transient0.HobgoblinTransient0TaggedSharedValue(1, value.b())); + if (existingId != null) { + org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.encodeInt64(existingId, outputStream, sharedValues); + } else { + org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.encodeInt64(sharedValues.size(), outputStream, sharedValues); + sharedValues.put(new org.zwobble.example.types.transient0.HobgoblinTransient0TaggedSharedValue(1, value.b()), (long) sharedValues.size()); + org.zwobble.example.types.data.transient0.HobgoblinTransient0Data.encodeStructWithInt64(value.b(), outputStream, sharedValues); + } + } + { + var existingId = sharedValues.get(new org.zwobble.example.types.transient0.HobgoblinTransient0TaggedSharedValue(0, value.c())); + if (existingId != null) { + org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.encodeInt64(existingId, outputStream, sharedValues); + } else { + org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.encodeInt64(sharedValues.size(), outputStream, sharedValues); + sharedValues.put(new org.zwobble.example.types.transient0.HobgoblinTransient0TaggedSharedValue(0, value.c()), (long) sharedValues.size()); + org.zwobble.example.types.data.transient0.HobgoblinTransient0Data.encodeStructWithInt32(value.c(), outputStream, sharedValues); + } + } + } + + public static org.zwobble.example.types.data.StructWithDifferentSharedTypes decodeStructWithDifferentSharedTypes(java.io.InputStream inputStream, java.util.List sharedValues) throws java.io.IOException { + var a$id = org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.decodeInt64(inputStream, sharedValues); + if (a$id > java.lang.Integer.MAX_VALUE) { + throw new java.io.IOException("Shared value IDs greater than Integer.MAX_VALUE are not supported"); + } + org.zwobble.example.types.data.StructWithInt32 a; + if (a$id < sharedValues.size()) { + a = switch (sharedValues.get((int) a$id)) { + case org.zwobble.example.types.data.StructWithInt32 a$decodedValue -> { + yield a$decodedValue; + } + default -> { + throw new java.io.IOException("Shared value was of the wrong type"); + } + }; + } else { + var a$decodedValue = org.zwobble.example.types.data.transient0.HobgoblinTransient0Data.decodeStructWithInt32(inputStream, sharedValues); + sharedValues.add(a$decodedValue); + a = a$decodedValue; + } + var b$id = org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.decodeInt64(inputStream, sharedValues); + if (b$id > java.lang.Integer.MAX_VALUE) { + throw new java.io.IOException("Shared value IDs greater than Integer.MAX_VALUE are not supported"); + } + org.zwobble.example.types.data.StructWithInt64 b; + if (b$id < sharedValues.size()) { + b = switch (sharedValues.get((int) b$id)) { + case org.zwobble.example.types.data.StructWithInt64 b$decodedValue -> { + yield b$decodedValue; + } + default -> { + throw new java.io.IOException("Shared value was of the wrong type"); + } + }; + } else { + var b$decodedValue = org.zwobble.example.types.data.transient0.HobgoblinTransient0Data.decodeStructWithInt64(inputStream, sharedValues); + sharedValues.add(b$decodedValue); + b = b$decodedValue; + } + var c$id = org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.decodeInt64(inputStream, sharedValues); + if (c$id > java.lang.Integer.MAX_VALUE) { + throw new java.io.IOException("Shared value IDs greater than Integer.MAX_VALUE are not supported"); + } + org.zwobble.example.types.data.StructWithInt32 c; + if (c$id < sharedValues.size()) { + c = switch (sharedValues.get((int) c$id)) { + case org.zwobble.example.types.data.StructWithInt32 c$decodedValue -> { + yield c$decodedValue; + } + default -> { + throw new java.io.IOException("Shared value was of the wrong type"); + } + }; + } else { + var c$decodedValue = org.zwobble.example.types.data.transient0.HobgoblinTransient0Data.decodeStructWithInt32(inputStream, sharedValues); + sharedValues.add(c$decodedValue); + c = c$decodedValue; + } + return new org.zwobble.example.types.data.StructWithDifferentSharedTypes(a, b, c); + } + + public static void encodeStructWithSharedSumAndVariant(org.zwobble.example.types.data.StructWithSharedSumAndVariant value, java.io.OutputStream outputStream, java.util.Map sharedValues) throws java.io.IOException { + { + var existingId = sharedValues.get(new org.zwobble.example.types.transient0.HobgoblinTransient0TaggedSharedValue(2, value.a())); + if (existingId != null) { + org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.encodeInt64(existingId, outputStream, sharedValues); + } else { + org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.encodeInt64(sharedValues.size(), outputStream, sharedValues); + sharedValues.put(new org.zwobble.example.types.transient0.HobgoblinTransient0TaggedSharedValue(2, value.a()), (long) sharedValues.size()); + org.zwobble.example.types.data.transient0.HobgoblinTransient0Data.encodeSumWithVariants(value.a(), outputStream, sharedValues); + } + } + { + var existingId = sharedValues.get(new org.zwobble.example.types.transient0.HobgoblinTransient0TaggedSharedValue(3, value.b())); + if (existingId != null) { + org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.encodeInt64(existingId, outputStream, sharedValues); + } else { + org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.encodeInt64(sharedValues.size(), outputStream, sharedValues); + sharedValues.put(new org.zwobble.example.types.transient0.HobgoblinTransient0TaggedSharedValue(3, value.b()), (long) sharedValues.size()); + org.zwobble.example.types.data.transient0.HobgoblinTransient0Data.encodeVariantOne(value.b(), outputStream, sharedValues); + } + } + } + + public static org.zwobble.example.types.data.StructWithSharedSumAndVariant decodeStructWithSharedSumAndVariant(java.io.InputStream inputStream, java.util.List sharedValues) throws java.io.IOException { + var a$id = org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.decodeInt64(inputStream, sharedValues); + if (a$id > java.lang.Integer.MAX_VALUE) { + throw new java.io.IOException("Shared value IDs greater than Integer.MAX_VALUE are not supported"); + } + org.zwobble.example.types.data.SumWithVariants a; + if (a$id < sharedValues.size()) { + a = switch (sharedValues.get((int) a$id)) { + case org.zwobble.example.types.data.SumWithVariants a$decodedValue -> { + yield a$decodedValue; + } + default -> { + throw new java.io.IOException("Shared value was of the wrong type"); + } + }; + } else { + var a$decodedValue = org.zwobble.example.types.data.transient0.HobgoblinTransient0Data.decodeSumWithVariants(inputStream, sharedValues); + sharedValues.add(a$decodedValue); + a = a$decodedValue; + } + var b$id = org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.decodeInt64(inputStream, sharedValues); + if (b$id > java.lang.Integer.MAX_VALUE) { + throw new java.io.IOException("Shared value IDs greater than Integer.MAX_VALUE are not supported"); + } + org.zwobble.example.types.data.VariantOne b; + if (b$id < sharedValues.size()) { + b = switch (sharedValues.get((int) b$id)) { + case org.zwobble.example.types.data.VariantOne b$decodedValue -> { + yield b$decodedValue; + } + default -> { + throw new java.io.IOException("Shared value was of the wrong type"); + } + }; + } else { + var b$decodedValue = org.zwobble.example.types.data.transient0.HobgoblinTransient0Data.decodeVariantOne(inputStream, sharedValues); + sharedValues.add(b$decodedValue); + b = b$decodedValue; + } + return new org.zwobble.example.types.data.StructWithSharedSumAndVariant(a, b); + } + + private static void encode$constructedType$0(java.util.Optional value, java.io.OutputStream outputStream, java.util.Map sharedValues) throws java.io.IOException { + org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.encodeBool(value.isPresent(), outputStream, sharedValues); + if (value.isPresent()) { + org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.encodeString(value.get(), outputStream, sharedValues); + } + } + + private static java.util.Optional decode$constructedType$0(java.io.InputStream inputStream, java.util.List sharedValues) throws java.io.IOException { + var value$isPresent = org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.decodeBool(inputStream, sharedValues); + java.util.Optional value; + if (value$isPresent) { + var value$element = org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.decodeString(inputStream, sharedValues); + value = java.util.Optional.of(value$element); + } else { + value = java.util.Optional.empty(); + } + return value; + } + + private static void encode$constructedType$1(org.zwobble.example.types.data.GenericNative, org.zwobble.example.types.data.GenericNative> value, java.io.OutputStream outputStream, java.util.Map sharedValues) throws java.io.IOException { + org.zwobble.example.types.data.transient0.HobgoblinTransient0Data.encodeGenericNative(value, outputStream, sharedValues, org.zwobble.example.types.data.transient0.HobgoblinTransient0Data::encode$constructedType$0, org.zwobble.example.types.data.transient0.HobgoblinTransient0Data::encode$constructedType$2); + } + + private static org.zwobble.example.types.data.GenericNative, org.zwobble.example.types.data.GenericNative> decode$constructedType$1(java.io.InputStream inputStream, java.util.List sharedValues) throws java.io.IOException { + var value = org.zwobble.example.types.data.transient0.HobgoblinTransient0Data.decodeGenericNative(inputStream, sharedValues, org.zwobble.example.types.data.transient0.HobgoblinTransient0Data::decode$constructedType$0, org.zwobble.example.types.data.transient0.HobgoblinTransient0Data::decode$constructedType$2); + return value; + } + + private static void encode$constructedType$2(org.zwobble.example.types.data.GenericNative value, java.io.OutputStream outputStream, java.util.Map sharedValues) throws java.io.IOException { + org.zwobble.example.types.data.transient0.HobgoblinTransient0Data.encodeGenericNative(value, outputStream, sharedValues, org.zwobble.example.types.transient0.HobgoblinTransient0Builtins::encodeString, org.zwobble.example.types.transient0.HobgoblinTransient0Builtins::encodeString); + } + + private static org.zwobble.example.types.data.GenericNative decode$constructedType$2(java.io.InputStream inputStream, java.util.List sharedValues) throws java.io.IOException { + var value = org.zwobble.example.types.data.transient0.HobgoblinTransient0Data.decodeGenericNative(inputStream, sharedValues, org.zwobble.example.types.transient0.HobgoblinTransient0Builtins::decodeString, org.zwobble.example.types.transient0.HobgoblinTransient0Builtins::decodeString); + return value; + } + + // Custom area start: org.zwobble.example.types.data.transient0.HobgoblinTransient0Data body + public static void encodeNative( + Native value, + OutputStream outputStream, + Map sharedValues + ) throws IOException { + HobgoblinTransient0Builtins.encodeInt32(value.a(), outputStream, sharedValues); + HobgoblinTransient0Builtins.encodeInt32(value.b(), outputStream, sharedValues); + } + + public static Native decodeNative( + InputStream inputStream, + List sharedValues + ) throws IOException { + var a = HobgoblinTransient0Builtins.decodeInt32(inputStream, sharedValues); + var b = HobgoblinTransient0Builtins.decodeInt32(inputStream, sharedValues); + return new Native(a, b); + } + + public static void encodeGenericNative( + GenericNative value, + OutputStream outputStream, + Map sharedValues, + Encoder encoderA, + Encoder encoderB + ) throws IOException { + encoderA.encode(value.a(), outputStream, sharedValues); + encoderB.encode(value.b(), outputStream, sharedValues); + } + + public static GenericNative decodeGenericNative( + InputStream inputStream, + List sharedValues, + Decoder decoderA, + Decoder decoderB + ) throws IOException { + var a = decoderA.decode(inputStream, sharedValues); + var b = decoderB.decode(inputStream, sharedValues); + return new GenericNative(a, b); + } + // Custom area end: org.zwobble.example.types.data.transient0.HobgoblinTransient0Data body +} diff --git a/examples/16-transient-0/output/java/src/main/java/org/zwobble/example/types/transient0/Decoder.java b/examples/16-transient-0/output/java/src/main/java/org/zwobble/example/types/transient0/Decoder.java new file mode 100644 index 0000000..ad1a6d0 --- /dev/null +++ b/examples/16-transient-0/output/java/src/main/java/org/zwobble/example/types/transient0/Decoder.java @@ -0,0 +1,13 @@ +// Generated by hobgoblin. + +package org.zwobble.example.types.transient0; + +// Custom area start: org.zwobble.example.types.transient0.Decoder imports +// Custom area end: org.zwobble.example.types.transient0.Decoder imports + +public interface Decoder { + public T decode(java.io.InputStream inputStream, java.util.List sharedValues) throws java.io.IOException; + + // Custom area start: org.zwobble.example.types.transient0.Decoder body + // Custom area end: org.zwobble.example.types.transient0.Decoder body +} diff --git a/examples/16-transient-0/output/java/src/main/java/org/zwobble/example/types/transient0/Encoder.java b/examples/16-transient-0/output/java/src/main/java/org/zwobble/example/types/transient0/Encoder.java new file mode 100644 index 0000000..2ba43a6 --- /dev/null +++ b/examples/16-transient-0/output/java/src/main/java/org/zwobble/example/types/transient0/Encoder.java @@ -0,0 +1,13 @@ +// Generated by hobgoblin. + +package org.zwobble.example.types.transient0; + +// Custom area start: org.zwobble.example.types.transient0.Encoder imports +// Custom area end: org.zwobble.example.types.transient0.Encoder imports + +public interface Encoder { + public void encode(T value, java.io.OutputStream outputStream, java.util.Map sharedValues) throws java.io.IOException; + + // Custom area start: org.zwobble.example.types.transient0.Encoder body + // Custom area end: org.zwobble.example.types.transient0.Encoder body +} diff --git a/examples/16-transient-0/output/java/src/main/java/org/zwobble/example/types/transient0/HobgoblinTransient0Builtins.java b/examples/16-transient-0/output/java/src/main/java/org/zwobble/example/types/transient0/HobgoblinTransient0Builtins.java new file mode 100644 index 0000000..293aee7 --- /dev/null +++ b/examples/16-transient-0/output/java/src/main/java/org/zwobble/example/types/transient0/HobgoblinTransient0Builtins.java @@ -0,0 +1,82 @@ +// Generated by hobgoblin. + +package org.zwobble.example.types.transient0; + +// Custom area start: org.zwobble.example.types.transient0.HobgoblinTransient0Builtins imports +// Custom area end: org.zwobble.example.types.transient0.HobgoblinTransient0Builtins imports + +public final class HobgoblinTransient0Builtins { + public static void encodeBool(boolean value, java.io.OutputStream outputStream, java.util.Map sharedValues) throws java.io.IOException { + outputStream.write(value ? 1 : 0); + } + + public static boolean decodeBool(java.io.InputStream inputStream, java.util.List sharedValues) throws java.io.IOException { + return inputStream.read() != 0; + } + + public static void encodeInt8(byte value, java.io.OutputStream outputStream, java.util.Map sharedValues) throws java.io.IOException { + outputStream.write(value); + } + + public static byte decodeInt8(java.io.InputStream inputStream, java.util.List sharedValues) throws java.io.IOException { + return (byte) inputStream.read(); + } + + public static void encodeInt32(int value, java.io.OutputStream outputStream, java.util.Map sharedValues) throws java.io.IOException { + outputStream.write(value >>> 0 & 255); + outputStream.write(value >>> 8 & 255); + outputStream.write(value >>> 16 & 255); + outputStream.write(value >>> 24 & 255); + } + + public static int decodeInt32(java.io.InputStream inputStream, java.util.List sharedValues) throws java.io.IOException { + int value = 0; + value = value | inputStream.read() << 0; + value = value | inputStream.read() << 8; + value = value | inputStream.read() << 16; + value = value | inputStream.read() << 24; + return value; + } + + public static void encodeInt64(long value, java.io.OutputStream outputStream, java.util.Map sharedValues) throws java.io.IOException { + outputStream.write((int) (value >>> 0 & 255)); + outputStream.write((int) (value >>> 8 & 255)); + outputStream.write((int) (value >>> 16 & 255)); + outputStream.write((int) (value >>> 24 & 255)); + outputStream.write((int) (value >>> 32 & 255)); + outputStream.write((int) (value >>> 40 & 255)); + outputStream.write((int) (value >>> 48 & 255)); + outputStream.write((int) (value >>> 56 & 255)); + } + + public static long decodeInt64(java.io.InputStream inputStream, java.util.List sharedValues) throws java.io.IOException { + long value = 0; + value = value | inputStream.read() << 0; + value = value | inputStream.read() << 8; + value = value | inputStream.read() << 16; + value = value | inputStream.read() << 24; + value = value | inputStream.read() << 32; + value = value | inputStream.read() << 40; + value = value | inputStream.read() << 48; + value = value | inputStream.read() << 56; + return value; + } + + public static void encodeString(String value, java.io.OutputStream outputStream, java.util.Map sharedValues) throws java.io.IOException { + var bytes = value.getBytes(java.nio.charset.StandardCharsets.UTF_8); + org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.encodeInt64((long) bytes.length, outputStream, sharedValues); + outputStream.write(bytes); + } + + public static String decodeString(java.io.InputStream inputStream, java.util.List sharedValues) throws java.io.IOException { + var bytesLength = org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.decodeInt64(inputStream, sharedValues); + if (bytesLength > java.lang.Integer.MAX_VALUE) { + throw new java.io.IOException("Strings longer than Integer.MAX_VALUE are not supported"); + } + var bytes = inputStream.readNBytes((int) bytesLength); + return new String(bytes, java.nio.charset.StandardCharsets.UTF_8); + } + + // Custom area start: org.zwobble.example.types.transient0.HobgoblinTransient0Builtins body + // Custom area end: org.zwobble.example.types.transient0.HobgoblinTransient0Builtins body +} diff --git a/examples/16-transient-0/output/java/src/main/java/org/zwobble/example/types/transient0/HobgoblinTransient0TaggedSharedValue.java b/examples/16-transient-0/output/java/src/main/java/org/zwobble/example/types/transient0/HobgoblinTransient0TaggedSharedValue.java new file mode 100644 index 0000000..6c5b520 --- /dev/null +++ b/examples/16-transient-0/output/java/src/main/java/org/zwobble/example/types/transient0/HobgoblinTransient0TaggedSharedValue.java @@ -0,0 +1,22 @@ +// Generated by hobgoblin. + +package org.zwobble.example.types.transient0; + +// Custom area start: org.zwobble.example.types.transient0.HobgoblinTransient0TaggedSharedValue imports +// Custom area end: org.zwobble.example.types.transient0.HobgoblinTransient0TaggedSharedValue imports + +public record HobgoblinTransient0TaggedSharedValue( + long typeId, + java.lang.Object value +) { + public boolean equals(java.lang.Object other) { + return other instanceof org.zwobble.example.types.transient0.HobgoblinTransient0TaggedSharedValue otherSharedValue && this.typeId == otherSharedValue.typeId && this.value == otherSharedValue.value; + } + + public int hashCode() { + return 37 * (37 * 17 + java.lang.Long.hashCode(this.typeId)) + java.lang.System.identityHashCode(this.value); + } + + // Custom area start: org.zwobble.example.types.transient0.HobgoblinTransient0TaggedSharedValue body + // Custom area end: org.zwobble.example.types.transient0.HobgoblinTransient0TaggedSharedValue body +} diff --git a/examples/16-transient-0/output/java/src/test/java/org/zwobble/example/Transient0Tests.java b/examples/16-transient-0/output/java/src/test/java/org/zwobble/example/Transient0Tests.java new file mode 100644 index 0000000..ba43aed --- /dev/null +++ b/examples/16-transient-0/output/java/src/test/java/org/zwobble/example/Transient0Tests.java @@ -0,0 +1,392 @@ +package org.zwobble.example; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.zwobble.example.types.data.EnumWithVariants; +import org.zwobble.example.types.data.GenericNative; +import org.zwobble.example.types.data.Native; +import org.zwobble.example.types.data.StructSingleton; +import org.zwobble.example.types.data.StructWithBool; +import org.zwobble.example.types.data.StructWithBox; +import org.zwobble.example.types.data.StructWithDifferentSharedTypes; +import org.zwobble.example.types.data.StructWithEnum; +import org.zwobble.example.types.data.StructWithGenericNative; +import org.zwobble.example.types.data.StructWithInt8; +import org.zwobble.example.types.data.StructWithInt32; +import org.zwobble.example.types.data.StructWithInt64; +import org.zwobble.example.types.data.StructWithList; +import org.zwobble.example.types.data.StructWithListOfShared; +import org.zwobble.example.types.data.StructWithNative; +import org.zwobble.example.types.data.StructWithOption; +import org.zwobble.example.types.data.StructWithSharedSumAndVariant; +import org.zwobble.example.types.data.StructWithString; +import org.zwobble.example.types.data.StructWithStruct; +import org.zwobble.example.types.data.StructWithSum; +import org.zwobble.example.types.data.VariantOne; +import org.zwobble.example.types.data.VariantTwo; +import org.zwobble.example.types.data.transient0.HobgoblinTransient0Data; +import org.zwobble.example.types.transient0.HobgoblinTransient0TaggedSharedValue; + +public class Transient0Tests { + @Test + public void structSingleton() throws IOException { + var value = StructSingleton.INSTANCE; + + assertRoundTripEncoding( + "StructSingleton", + value, + HobgoblinTransient0Data::encodeStructSingleton, + HobgoblinTransient0Data::decodeStructSingleton + ); + } + + @Test + public void structWithBool() throws IOException { + var value = new StructWithBool(true, false); + + assertRoundTripEncoding( + "StructWithBool", + value, + HobgoblinTransient0Data::encodeStructWithBool, + HobgoblinTransient0Data::decodeStructWithBool + ); + } + + @Test + public void structWithInt8() throws IOException { + var value = new StructWithInt8((byte)10, (byte)25); + + assertRoundTripEncoding( + "StructWithInt8", + value, + HobgoblinTransient0Data::encodeStructWithInt8, + HobgoblinTransient0Data::decodeStructWithInt8 + ); + } + + @Test + public void structWithInt32() throws IOException { + var value = new StructWithInt32(10, 25); + + assertRoundTripEncoding( + "StructWithInt32", + value, + HobgoblinTransient0Data::encodeStructWithInt32, + HobgoblinTransient0Data::decodeStructWithInt32 + ); + } + + @Test + public void structWithInt64() throws IOException { + var value = new StructWithInt64(10, 25); + + assertRoundTripEncoding( + "StructWithInt64", + value, + HobgoblinTransient0Data::encodeStructWithInt64, + HobgoblinTransient0Data::decodeStructWithInt64 + ); + } + + @Test + public void structWithString() throws IOException { + var value = new StructWithString("abc", "def"); + + assertRoundTripEncoding( + "StructWithString", + value, + HobgoblinTransient0Data::encodeStructWithString, + HobgoblinTransient0Data::decodeStructWithString + ); + } + + @Test + public void structWithStruct() throws IOException { + var value = new StructWithStruct(new StructWithInt32(10, 25), 42); + + assertRoundTripEncoding( + "StructWithStruct", + value, + HobgoblinTransient0Data::encodeStructWithStruct, + HobgoblinTransient0Data::decodeStructWithStruct + ); + } + + @Test + public void structWithList() throws IOException { + var value = new StructWithList( + List.of(10L, 25L), + List.of( + new StructWithInt32(42, 47), + new StructWithInt32(52, 57), + new StructWithInt32(62, 67) + ) + ); + + assertRoundTripEncoding( + "StructWithList", + value, + HobgoblinTransient0Data::encodeStructWithList, + HobgoblinTransient0Data::decodeStructWithList + ); + } + + @Test + public void structWithOptionNone() throws IOException { + var value = new StructWithOption( + Optional.empty(), + Optional.empty() + ); + + assertRoundTripEncoding( + "StructWithOption_None", + value, + HobgoblinTransient0Data::encodeStructWithOption, + HobgoblinTransient0Data::decodeStructWithOption + ); + } + + @Test + public void structWithOptionSome() throws IOException { + var value = new StructWithOption( + Optional.of(10L), + Optional.of(new StructWithInt32(42, 47)) + ); + + assertRoundTripEncoding( + "StructWithOption_Some", + value, + HobgoblinTransient0Data::encodeStructWithOption, + HobgoblinTransient0Data::decodeStructWithOption + ); + } + + @Test + public void enumWithVariants() throws IOException { + assertRoundTripEncoding( + "EnumWithVariants_zero", + EnumWithVariants.ZERO, + HobgoblinTransient0Data::encodeEnumWithVariants, + HobgoblinTransient0Data::decodeEnumWithVariants + ); + + assertRoundTripEncoding( + "EnumWithVariants_one", + EnumWithVariants.ONE, + HobgoblinTransient0Data::encodeEnumWithVariants, + HobgoblinTransient0Data::decodeEnumWithVariants + ); + + assertRoundTripEncoding( + "EnumWithVariants_two", + EnumWithVariants.TWO, + HobgoblinTransient0Data::encodeEnumWithVariants, + HobgoblinTransient0Data::decodeEnumWithVariants + ); + } + + @Test + public void structWithEnum() throws IOException { + var value = new StructWithEnum( + EnumWithVariants.ONE, + 42 + ); + + assertRoundTripEncoding( + "StructWithEnum", + value, + HobgoblinTransient0Data::encodeStructWithEnum, + HobgoblinTransient0Data::decodeStructWithEnum + ); + } + + @Test + public void sumWithVariants() throws IOException { + assertRoundTripEncoding( + "SumWithVariants_VariantOne", + new VariantOne(10), + HobgoblinTransient0Data::encodeSumWithVariants, + HobgoblinTransient0Data::decodeSumWithVariants + ); + + assertRoundTripEncoding( + "SumWithVariants_VariantTwo", + new VariantTwo(25), + HobgoblinTransient0Data::encodeSumWithVariants, + HobgoblinTransient0Data::decodeSumWithVariants + ); + } + + @Test + public void sumWithBoxedVariants() throws IOException { + assertRoundTripEncoding( + "SumWithBoxedVariants_VariantOne", + new VariantOne(10), + HobgoblinTransient0Data::encodeSumWithBoxedVariants, + HobgoblinTransient0Data::decodeSumWithBoxedVariants + ); + + assertRoundTripEncoding( + "SumWithBoxedVariants_VariantTwo", + new VariantTwo(25), + HobgoblinTransient0Data::encodeSumWithBoxedVariants, + HobgoblinTransient0Data::decodeSumWithBoxedVariants + ); + } + + @Test + public void structWithSum() throws IOException { + var value = new StructWithSum( + new VariantTwo(25), + 42 + ); + + assertRoundTripEncoding( + "StructWithSum", + value, + HobgoblinTransient0Data::encodeStructWithSum, + HobgoblinTransient0Data::decodeStructWithSum + ); + } + + @Test + public void structWithNative() throws IOException { + var value = new StructWithNative( + new Native(10, 25) + ); + + assertRoundTripEncoding( + "StructWithNative", + value, + HobgoblinTransient0Data::encodeStructWithNative, + HobgoblinTransient0Data::decodeStructWithNative + ); + } + + @Test + public void structWithGenericNative() throws IOException { + var value = new StructWithGenericNative( + new GenericNative(10, "x1"), + new GenericNative( + Optional.of("x2"), + new GenericNative( + Optional.of("x3"), + new GenericNative("x4", "x5") + ) + ) + ); + + assertRoundTripEncoding( + "StructWithGenericNative", + value, + HobgoblinTransient0Data::encodeStructWithGenericNative, + HobgoblinTransient0Data::decodeStructWithGenericNative + ); + } + + @Test + public void structWithBox() throws IOException { + var value = new StructWithBox( + new StructWithInt32(10, 25) + ); + + assertRoundTripEncoding( + "StructWithBox", + value, + HobgoblinTransient0Data::encodeStructWithBox, + HobgoblinTransient0Data::decodeStructWithBox + ); + } + + @Test + public void structWithListOfShared() throws IOException { + var a = new StructWithInt32(10, 25); + var b = new StructWithInt32(42, 47); + + var value = new StructWithListOfShared(List.of(a, a, b, a, b)); + + var decodedValue = assertRoundTripEncoding( + "StructWithListOfShared", + value, + HobgoblinTransient0Data::encodeStructWithListOfShared, + HobgoblinTransient0Data::decodeStructWithListOfShared + ); + Assertions.assertSame(decodedValue.inner().get(0), decodedValue.inner().get(1)); + Assertions.assertSame(decodedValue.inner().get(0), decodedValue.inner().get(3)); + Assertions.assertSame(decodedValue.inner().get(2), decodedValue.inner().get(4)); + } + + @Test + public void structWithDifferentSharedTypes() throws IOException { + var a = new StructWithInt32(10, 25); + var b = new StructWithInt64(42, 47); + var c = new StructWithInt32(52, 57); + + var value = new StructWithDifferentSharedTypes(a, b, c); + + assertRoundTripEncoding( + "StructWithDifferentSharedTypes", + value, + HobgoblinTransient0Data::encodeStructWithDifferentSharedTypes, + HobgoblinTransient0Data::decodeStructWithDifferentSharedTypes + ); + } + + @Test + public void structWithSharedSumAndVariant() throws IOException { + var a = new VariantOne(10); + + var value = new StructWithSharedSumAndVariant(a, a); + + assertRoundTripEncoding( + "StructWithSharedSumAndVariant", + value, + HobgoblinTransient0Data::encodeStructWithSharedSumAndVariant, + HobgoblinTransient0Data::decodeStructWithSharedSumAndVariant + ); + } + + private T assertRoundTripEncoding( + String name, + T value, + Encoder encoder, + Decoder decoder + ) throws IOException { + var outputStream = new ByteArrayOutputStream(); + encoder.encode(value, outputStream, new HashMap<>()); + var bytes = outputStream.toByteArray(); + + var outputDir = System.getenv("HOBGOBLIN_OUTPUT_DIR"); + var transient0OutputDir = Path.of(outputDir).resolve("transient-0"); + Files.createDirectories(transient0OutputDir); + var transient0OutputPath = transient0OutputDir.resolve(name); + Files.write(transient0OutputPath, bytes); + + var inputStream = new ByteArrayInputStream(bytes); + var decodedValue = decoder.decode(inputStream, new ArrayList<>()); + + Assertions.assertEquals(value, decodedValue); + + return decodedValue; + } + + private interface Encoder { + void encode(T value, OutputStream outputStream, Map sharedValues) throws IOException; + } + + private interface Decoder { + T decode(InputStream inputStream, List sharedValues) throws IOException; + } +} -- cgit v1.2.3