Coder Social home page Coder Social logo

failed to #getValue for crates and Cannot cast me.paradis.smashcratess.crates.actions.MessageAction to java.util.List about okaeri-configs HOT 3 CLOSED

Paradis4432 avatar Paradis4432 commented on June 1, 2024
failed to #getValue for crates and Cannot cast me.paradis.smashcratess.crates.actions.MessageAction to java.util.List

from okaeri-configs.

Comments (3)

Paradis4432 avatar Paradis4432 commented on June 1, 2024

would it be possible to create one serializer and make the different action handlers (like MessageAction) override this method to aovid having a single class for each action handler (MessageActionSerializer)

from okaeri-configs.

dasavick avatar dasavick commented on June 1, 2024

Your example is missing the config POJO itself, which probably reveals some unsuitable field type, but my best guess is that you are attempting to create a collection where the contents are all concrete implementations of a single interface.

After the object is transformed into the YAML data using okaeri-configs, the information about the type is lost. The only source of the type truth is the config definition class itself. This means that while it may be possible to serialize such collection, it will not be possible to deserialize it without including additional data, such as field 'type' with an enum or enum-like type.

For one of the solutions, you would need to create a field List<MyInterfaceType>, register a serializer for only the MyInterfaceType, and then implement MyInterfaceType serializer to pass the data to specific serializers or deserialize it in place, all while routing based on the previously mentioned type field.

Example of such a solution, where the type field is represented as action:

public class MyActionSerializer implements ObjectSerializer<MyAction> {

    @Override
    public boolean supports(@NonNull Class<? super MyAction> type) {
        return MyAction.class.isAssignableFrom(type);
    }

    @Override
    public void serialize(@NonNull MyAction action, @NonNull SerializationData data, @NonNull GenericsDeclaration generics) {
        data.add("action", action.getAction());
        switch (action.getAction()) {
            case ACTION_ONE -> {
                data.add("field1", ((MyActionOne) action).getFieldA1(), SomeType1.class);
                data.addCollection("field2", ((MyActionOne) action).getFieldA2(), SomeType2.class);
            }
            case ACTION_TWO -> {
                data.add("field1", ((MyActionTwo) action).getFieldB1(), SomeType3.class);
            }
        }
    }

    @Override
    public MyAction deserialize(@NonNull DeserializationData data, @NonNull GenericsDeclaration generics) {
        MyActionType action = data.get("action", MyActionType.class);
        return (switch (action) {
            case ACTION_ONE -> new MyActionOne(
                data.get("field", SomeType1.class),
                data.getAsList("field2", SomeType2.class)
            );
            case ACTION_TWO -> new MyActionTwo(
                data.get("field1", SomeType3.class)
            );
            case NOOP -> new NoopAction();
        });
    }
    
    /** FIXME: remove if using auto-registration with ConfigSerializable, the better way to serialize first-party types
    public static class Serdes implements OkaeriSerdesPack {

        @Override
        public void register(@NonNull SerdesRegistry registry) {
            registry.register(new MyActionSerializer());
        }
    }
    **/
}
@Data
public abstract class MyAction implements ConfigSerializable {

    private static final MyActionSerializer MY_ACTION_SERIALIZER= new MyActionSerializer();
    private final MyActionType action;

    public abstract void execute(@NonNull MyContext context);

    @Override
    public void serialize(@NonNull SerializationData data, @NonNull GenericsDeclaration generics) {
        MY_ACTION_SERIALIZER.serialize(this, data, generics);
    }

    public static MyAction deserialize(@NonNull DeserializationData data, @NonNull GenericsDeclaration generics) {
        return MY_ACTION_SERIALIZER.deserialize(data, generics);
    }
}
@Data
public class MyObject extends OkaeriConfig {
    // ...
    private List<MyAction> actions = new ArrayList<>();
    // ...
}

from okaeri-configs.

Paradis4432 avatar Paradis4432 commented on June 1, 2024

what a beautiful person you are!

worked like a charm thank you

for those in the future this is the updated code:
https://paste.md-5.net/safusuhebi.java

from okaeri-configs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.