Skip to content

any way to modify existing instance's fields rather than creating new instances? #361

Description

@portlek

node.get(XXXConfig.class, emptyInstance);
this one returns different instances every single time, is there any way to use existing instance to modify the fields only?

Activity

  1. changed the title [-]any way to modify existing instance's fields rather than creating new instances.[/-] [+]any way to modify existing instance's fields rather than creating new instances?[/+] on Dec 5, 2022
  2. Machine-Maker commented on Dec 5, 2022

    @Machine-Maker
    Contributor

    You can get a Mutable ObjectFactory which you can then load an existing instance from a configuration node.

    ObjectMapper.Factory factory = (ObjectMapper.Factory) Objects.requireNonNull(node.options().serializers().get(type));
    ObjectMapper.Mutable<T> mutable = (ObjectMapper.Mutable<T>) factory.get(type);
    mutable.load(instance, node);
  3. portlek commented on Dec 5, 2022

    @portlek
    ContributorAuthor

    is this a right approach?

    @SneakyThrows
    private void reload(final boolean ensureAsync) {
      if (ensureAsync) {
        PluginExceptions.ensureAsync();
      }
      synchronized (this.lock) {
        final var loader = Configurate.yaml(
          this.directory.resolve("messages.yaml")
        );
        final var node = loader.load();
        final var factory = (ObjectMapper.Factory) Objects.requireNonNull(node.options().serializers().get(AnnouncementMessages2.class));
        final var mutable = (ObjectMapper.Mutable<AnnouncementMessages2>) factory.get(AnnouncementMessages2.class);
        mutable.load(this, node);
        node.set(AnnouncementMessages2.class, this);
        loader.save(node);
      }
    }
  4. locked and limited conversation to collaborators on Dec 5, 2022
  5. converted this issue into a discussion #362 on Dec 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions