You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@felix.apache.org by "David Bosschaert (JIRA)" <ji...@apache.org> on 2017/08/16 15:27:00 UTC

[jira] [Commented] (FELIX-5678) Allow merging of objects

    [ https://issues.apache.org/jira/browse/FELIX-5678?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16128952#comment-16128952 ] 

David Bosschaert commented on FELIX-5678:
-----------------------------------------

Hi [~dleangen], so you really want to convert two objects into one :)

While the converter API isn't really designed for this, you could achieve it with a custom rule, where the rule is specifically created for this conversion with the second object as context provided to the rule, i.e.
{code}
Foo f = ...;
ConverterBuilder builder = converter.newConverterBuilder();
builder.rule(new TypeRule<>(Map.class, Foo.class, {v -> merge(f, v)});
Converter c = builder.build();

Foo f2 = c.convert(m).to(Foo.class); // f is already passed to the rule
{code}
Maybe not the most elegant, but it might work?

> Allow merging of objects
> ------------------------
>
>                 Key: FELIX-5678
>                 URL: https://issues.apache.org/jira/browse/FELIX-5678
>             Project: Felix
>          Issue Type: Bug
>          Components: Converter
>            Reporter: David Leangen
>
> Given a typed object O1 and a "partial" representation of an object O2 (for instance in the form of a Map), allow O2 to be merged into O1.
> Example:
> {code}
> public class Foo {
>   public String a;
>   public String b;
>   public String c;
> }
> Foo f = new Foo();
> a = "Eh!";
> b = "Be cool.";
> c = "See you later?";
> Map<String, String> m = new Map<>();
> m.put("b", "Be there or be square");
> Foo f2 = Converter.convert(f).merge(m);
> {code}
> I am sure there are many ways to skin this cat.
> If the Converter API cannot be changed, what would be the best way to tackle this problem?
> (In the meantime, while awaiting comments form [~bosschaert], I'll try to run a few experiments to see if I can come up with something reasonable.)



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)