commoble.databuddy.codec

## Class VariatingCodec<K,V>

- java.lang.Object
- - com.mojang.serialization.CompressorHolder
  - - com.mojang.serialization.MapCodec<com.mojang.datafixers.util.Pair<K,V>>
    - - commoble.databuddy.codec.VariatingCodec<K,V>

All Implemented Interfaces:
  com.mojang.serialization.Compressable, com.mojang.serialization.Keyable, com.mojang.serialization.MapDecoder<com.mojang.datafixers.util.Pair<K,V>>, com.mojang.serialization.MapEncoder<com.mojang.datafixers.util.Pair<K,V>>

    public class VariatingCodec<K,V>
    extends com.mojang.serialization.MapCodec<com.mojang.datafixers.util.Pair<K,V>>

Like KeyDispatchCodec, but subtly different. KeyDispatchCodec is difficult to work with when working with subtypes of a thing that doesn't store its own key, VariatingCodec explicitly supports such a scheme. In any case, VariatingCodec allows us to make codecs that can decode data of the form

     {
            "type": "some:type",
            "some_field_of_subtype": "data",
            "some_other_field": "data"
     }

where the fields beyond "type" depend on which subtype we are decoding to. This implementation requires that subtype codecs define their own fields; A single-value subtype codec can be represented with e.g.

     Codec<Integer> VALUE_CODEC = RecordCodecBuilder.create(instance -> instance.group(
    				Codec.INT.optionalFieldOf("value", 0).forGetter(x -> x)
    			).apply(instance, x -> Integer.valueOf(x)))

If we assigned this codec to a "constant" key, a json with this codec could look like (for example)

     "some_variant_type_field":
     {
            "type": "constant",
            "value": 24
     }

A singleton-like or fieldless codec can be represented with e.g.

     Codec<Integer> THOUSAND_CODEC = Codec.unit(1000);

A json using this codec could look like

     "some_variant_type_field":
     {
            "type": "always_thousand"
     }

For singleton codecs, the below form is equivalent to the above:

     "some_variant_type_field": "always_thousand"

### Nested Class Summary

Nested Classes
`protected static class` `VariatingCodec.NoopWrapperCodec<K,V>`

### Nested classes/interfaces inherited from class com.mojang.serialization.MapCodec

`com.mojang.serialization.MapCodec.MapCodecCodec<A>, com.mojang.serialization.MapCodec.ResultFunction<A>`

### Nested classes/interfaces inherited from interface com.mojang.serialization.MapDecoder

`com.mojang.serialization.MapDecoder.Implementation<A>`

### Nested classes/interfaces inherited from interface com.mojang.serialization.MapEncoder

`com.mojang.serialization.MapEncoder.Implementation<A>`

### Constructor Summary

Constructors
`protected` `VariatingCodec(java.lang.String typeKey, com.mojang.serialization.Codec<K> keyCodec, java.util.function.Function<K,? extends com.mojang.serialization.DataResult<? extends com.mojang.serialization.Decoder<? extends V>>> decoder, java.util.function.Function<K,? extends com.mojang.serialization.DataResult<? extends com.mojang.serialization.Encoder<V>>> encoder)`

### Method Summary

All Methods Static Methods Instance Methods Concrete Methods
`<T> com.mojang.serialization.DataResult<com.mojang.datafixers.util.Pair<K,V>>` `decode(com.mojang.serialization.DynamicOps<T> ops, com.mojang.serialization.MapLike<T> input)`
`<T> com.mojang.serialization.RecordBuilder<T>` `encode(com.mojang.datafixers.util.Pair<K,V> input, com.mojang.serialization.DynamicOps<T> ops, com.mojang.serialization.RecordBuilder<T> prefix)`
`<T> java.util.stream.Stream<T>` `keys(com.mojang.serialization.DynamicOps<T> ops)`
`static <K,V> com.mojang.serialization.Codec<com.mojang.datafixers.util.Pair<K,V>>` `makeMapBackedVariatingCodec(java.lang.String typeKey, com.mojang.serialization.Codec<K> keyCodec, java.util.function.Supplier<java.util.Map<K,com.mojang.serialization.Codec<V>>> codecMapGetter)` Make a variating codec backed by a key-codec map.
`static <K,V> com.mojang.serialization.Codec<com.mojang.datafixers.util.Pair<K,V>>` `makeVariatingCodec(java.lang.String typeKey, com.mojang.serialization.Codec<K> keyCodec, java.util.function.Function<K,? extends com.mojang.serialization.DataResult<? extends com.mojang.serialization.Codec<? extends V>>> codecFactory)` Make a variating codec with a general-purpose subtype getter
`java.lang.String` `toString()`

### Methods inherited from class com.mojang.serialization.MapCodec

`codec, dependent, deprecated, fieldOf, flatXmap, forGetter, mapResult, of, of, orElse, orElse, orElse, orElseGet, orElseGet, orElseGet, setPartial, stable, unit, unit, withLifecycle, xmap`

### Methods inherited from class com.mojang.serialization.CompressorHolder

`compressor`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

### Methods inherited from interface com.mojang.serialization.MapDecoder

`ap, compressedDecode, compressor, decoder, flatMap, map`

### Methods inherited from interface com.mojang.serialization.MapEncoder

`comap, compressedBuilder, compressor, encoder, flatComap, makeCompressedBuilder`

### Methods inherited from interface com.mojang.serialization.Keyable

`forStrings`

### Constructor Detail

#### VariatingCodec

    protected VariatingCodec(java.lang.String typeKey,
                             com.mojang.serialization.Codec<K> keyCodec,
                             java.util.function.Function<K,? extends com.mojang.serialization.DataResult<? extends com.mojang.serialization.Decoder<? extends V>>> decoder,
                             java.util.function.Function<K,? extends com.mojang.serialization.DataResult<? extends com.mojang.serialization.Encoder<V>>> encoder)

### Method Detail

#### makeMapBackedVariatingCodec

    public static <K,V> com.mojang.serialization.Codec<com.mojang.datafixers.util.Pair<K,V>> makeMapBackedVariatingCodec(java.lang.String typeKey,
                                                                                                                         com.mojang.serialization.Codec<K> keyCodec,
                                                                                                                         java.util.function.Supplier<java.util.Map<K,com.mojang.serialization.Codec<V>>> codecMapGetter)

Make a variating codec backed by a key-codec map. Helpful for registries-of-codecs.

Type Parameters:
  `K` - The type of the subtype key, e.g. String or ResourceLocation
  `V` - The type of the thing we are having subtypes of
Parameters:
  `typeKey` - The string-name of the field we are getting the type key from, e.g. "type"
  `keyCodec` - The codec for the typekey field, e.g. Codec.STRING or ResourceLocation.CODEC
  `codecMapGetter` - A getter for a map of codecs
Returns:
  a codec that uses the "type" field in an object to get a codec from the given map to determine what to decode the rest of the object as

#### makeVariatingCodec

    public static <K,V> com.mojang.serialization.Codec<com.mojang.datafixers.util.Pair<K,V>> makeVariatingCodec(java.lang.String typeKey,
                                                                                                                com.mojang.serialization.Codec<K> keyCodec,
                                                                                                                java.util.function.Function<K,? extends com.mojang.serialization.DataResult<? extends com.mojang.serialization.Codec<? extends V>>> codecFactory)

Make a variating codec with a general-purpose subtype getter

Type Parameters:
  `K` - The type of the subtype key, e.g. String or ResourceLocation
  `V` - The type of the thing we are having subtypes of
Parameters:
  `typeKey` - The string-name of the field we are getting the type key from, e.g. "type"
  `keyCodec` - The codec for the typekey field, e.g. Codec.STRING or ResourceLocation.CODEC
  `codecFactory` - A function to convert keys to subtype codecs. May return an error-holding-result if no subtype codec exists for the given key
Returns:
  a codec that uses the "type" field in an object to decide what codec to decode the rest of the object as

#### decode

    public <T> com.mojang.serialization.DataResult<com.mojang.datafixers.util.Pair<K,V>> decode(com.mojang.serialization.DynamicOps<T> ops,
                                                                                                com.mojang.serialization.MapLike<T> input)

#### encode

    public <T> com.mojang.serialization.RecordBuilder<T> encode(com.mojang.datafixers.util.Pair<K,V> input,
                                                                com.mojang.serialization.DynamicOps<T> ops,
                                                                com.mojang.serialization.RecordBuilder<T> prefix)

#### keys

    public <T> java.util.stream.Stream<T> keys(com.mojang.serialization.DynamicOps<T> ops)

Specified by:
  `keys` in interface `com.mojang.serialization.Keyable`
Specified by:
  `keys` in class `com.mojang.serialization.MapCodec<com.mojang.datafixers.util.Pair<K,V>>`

#### toString

    public java.lang.String toString()

Overrides:
  `toString` in class `java.lang.Object`