|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Splatgames.de Software and Contributors |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal |
| 6 | + * in the Software without restriction, including without limitation the rights |
| 7 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | + * copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in all |
| 12 | + * copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | + * SOFTWARE. |
| 21 | + */ |
| 22 | + |
| 23 | +package de.splatgames.aether.datafixers.functional.diagnostic; |
| 24 | + |
| 25 | +import de.splatgames.aether.datafixers.api.TypeReference; |
| 26 | +import de.splatgames.aether.datafixers.api.codec.Codec; |
| 27 | +import de.splatgames.aether.datafixers.api.dynamic.Dynamic; |
| 28 | +import de.splatgames.aether.datafixers.api.dynamic.DynamicOps; |
| 29 | +import de.splatgames.aether.datafixers.api.result.DataResult; |
| 30 | +import de.splatgames.aether.datafixers.api.type.SimpleType; |
| 31 | +import de.splatgames.aether.datafixers.api.type.Type; |
| 32 | +import de.splatgames.aether.datafixers.api.util.Pair; |
| 33 | +import org.jetbrains.annotations.NotNull; |
| 34 | + |
| 35 | +/** |
| 36 | + * Test support utilities for the field-aware diagnostics E2E tests. |
| 37 | + * |
| 38 | + * <p>The diagnostic tests need to register types in mock schemas that accept |
| 39 | + * arbitrary {@code Dynamic} values without forcing a strict codec round-trip. |
| 40 | + * The built-in {@link Type#STRING}, {@link Type#INT} etc. all use codecs that |
| 41 | + * expect their respective Java type — but the field-rewriting rules |
| 42 | + * ({@link de.splatgames.aether.datafixers.api.rewrite.Rules#renameField}, etc.) |
| 43 | + * operate on free-form maps, so we need a passthrough type whose codec just |
| 44 | + * stores and retrieves the {@code Dynamic} unchanged.</p> |
| 45 | + * |
| 46 | + * <p>This is the same pattern used by the example schemas |
| 47 | + * ({@code Schema100#dynamicPassthroughCodec}); we duplicate it here so the |
| 48 | + * functional tests do not depend on the examples module.</p> |
| 49 | + */ |
| 50 | +final class DiagnosticsTestSupport { |
| 51 | + |
| 52 | + private DiagnosticsTestSupport() { |
| 53 | + // Utility class |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Creates a {@link Type} that wraps a {@code Dynamic<?>} value with a |
| 58 | + * passthrough codec. |
| 59 | + * |
| 60 | + * <p>The codec preserves the {@code Dynamic} unchanged on both encode and |
| 61 | + * decode, allowing field-rewriting rules to operate directly on the value |
| 62 | + * without going through a typed Java representation.</p> |
| 63 | + * |
| 64 | + * @param reference the type reference, must not be {@code null} |
| 65 | + * @return a passthrough type |
| 66 | + */ |
| 67 | + @NotNull |
| 68 | + static Type<Dynamic<?>> passthroughType(@NotNull final TypeReference reference) { |
| 69 | + return new SimpleType<>(reference, dynamicPassthroughCodec()); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Creates a codec that treats {@code Dynamic<?>} as the value type and |
| 74 | + * passes it through unchanged in both directions. |
| 75 | + * |
| 76 | + * @return a passthrough codec |
| 77 | + */ |
| 78 | + @NotNull |
| 79 | + static Codec<Dynamic<?>> dynamicPassthroughCodec() { |
| 80 | + return new Codec<>() { |
| 81 | + @NotNull |
| 82 | + @Override |
| 83 | + public <T> DataResult<T> encode(@NotNull final Dynamic<?> input, |
| 84 | + @NotNull final DynamicOps<T> ops, |
| 85 | + @NotNull final T prefix) { |
| 86 | + @SuppressWarnings("unchecked") |
| 87 | + final Dynamic<Object> dynamicObj = (Dynamic<Object>) input; |
| 88 | + final Dynamic<T> converted = dynamicObj.convert(ops); |
| 89 | + return DataResult.success(converted.value()); |
| 90 | + } |
| 91 | + |
| 92 | + @NotNull |
| 93 | + @Override |
| 94 | + public <T> DataResult<Pair<Dynamic<?>, T>> decode(@NotNull final DynamicOps<T> ops, |
| 95 | + @NotNull final T input) { |
| 96 | + final Dynamic<T> dynamic = new Dynamic<>(ops, input); |
| 97 | + return DataResult.success(Pair.of(dynamic, ops.empty())); |
| 98 | + } |
| 99 | + }; |
| 100 | + } |
| 101 | +} |
0 commit comments