#How does registry dispatch work? Need help understanding.

1 messages · Page 1 of 1 (latest)

unreal light
#

I have this piece of code:

Codec<Bean> CODEC = BeanType.REGISTRY.getCodec().dispatch(Bean::type, BeanType::codec);

Where BeanType is defined as

record BeanType<T extends Bean>(Text name, Text desc, MapCodec<T> codec) {
    static final public Registry<BeanType<?>> REGISTRY = FabricRegistryBuilder
            .<BeanType<?>>createSimple(RegistryKey.ofRegistry(Identifier.of("beanmod", "beans")))
            .buildAndRegister();
}

As I understand it, and please correct me if I'm mistaken, upon serialization a Codec<Bean> will call Bean::type to get an associated entry from the REGISTRY using the BeanType<?> and call its codec to get the actual codec of the Bean, and upon deserialization will use stored Identifier to get the BeanType<?> and call its codec and use it to deserialize further.

Now the questions.

  1. How does BeanType.REGISTRY.getCodec() work by itself? What does it serialize a given BeanType<?> to? Straight up to its Identifier?
  2. I have a need to store both a list of BeanType Identifiers contained in the BeanType.REGISTRY as well as an "full" form of any Bean as associated by the BeanType<?>. How would I achieve that? I envision the resulting json to look like:
{
  "beans": [
    "beanmod:bean1", 
    "beanmod:bean2"
  ],
  "theOneStoredBean": {
    "type": "beanmod:bean1",
    "bean1field": "field1value"
  }
}
  1. How do I easily test codecs both ways?

Thank you very much for the answers in advance!

tame crypt
#
  1. I believe it just serializes to its identifier, yeah
  2. You'd made a class that has a List<BeanType> and a Bean and a codec for that. The codec would be made from a RecordCodecBuilder that uses xmap to go from an Identifier to a BeanType via the registry.
  3. Take a look at the docs for unit test. You can use Codec#encode/Codec#decode for testing that.

lmk if I missed something or wasn't clear enough :)

unreal light
tame crypt
#

Oh you can I forgot that existed :P