Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polimorphic adapters #258

Open
sky-pedrosaraiva opened this issue Jun 20, 2023 · 9 comments
Open

Polimorphic adapters #258

sky-pedrosaraiva opened this issue Jun 20, 2023 · 9 comments

Comments

@sky-pedrosaraiva
Copy link

sky-pedrosaraiva commented Jun 20, 2023

Hello, i'm trying to replace moshi with dsl-json.
In moshi is possible to implement adapters for a certain interface with multiple implementations:

PolymorphicJsonAdapterFactory.of(Dummy::class.java, "type")
        .withSubtype(SuperDummy::class.java, TypesEnum.SuperDummy.value)
        .withSubtype(MegaDummy::class.java, TypesEnum.MegaDummy.value)

This basically tells moshi to use the type field to map the correct implementation based on the value defined in a Enum.
I know that there is a descriminator but this doesnt allow mapping a custom value.

Is this approach possible to implement in dsl-json?

@zapov
Copy link
Member

zapov commented Jun 20, 2023

It would be helpful if you could provide more info, but you can pair discriminator with name to have a value for each subtype; which is I'm assuming what you want.

DSL-JSON does not support adapters for such configuration at the moment, although it should be relatively straightforward to implement it.

@sky-pedrosaraiva
Copy link
Author

sky-pedrosaraiva commented Jun 21, 2023

Yes, you are right. I tried that and now gives me this error:
com.dslplatform.json.ParsingException: Expecting "type" attribute as first element of mixin com.test.client.models.Dummy. Found: 'items' at position: 21, following: "group":{"dummy":[{", before: items":[{"classifica

Unfortunatly I can't assure that type will be the first item because it comes from external API. Is there a workaround for this?

@zapov
Copy link
Member

zapov commented Jun 21, 2023

Not really. The only workaround is you implement your own converter with this logic.
But to handle this in a general case it is not really performant and thus there is no plan to change how this works.

@sky-pedrosaraiva
Copy link
Author

I have another issue related to @JsonConverter.
My project consists in multiple gradle modules (i.e feat1 and feat2).
In feat1 I declare a converter like this and it works fine within classes annotated with @CompiledJson in feat1:

@JsonConverter(target = Instant::class)
public object InstantConverter {
    public fun read(reader: JsonReader<*>): Instant? {
        return if (reader.wasNull()) null else Instant.parse(reader.readSimpleString())
    }

    public fun write(writer: JsonWriter, value: Instant?) {
        if (value == null) {
            writer.writeNull()
        } else {
            writer.writeString(value.toString())
        }
    }
}

But if in feat2 there's a @CompiledJson with an Instant field, it fails to compile because of missing converter.
I've tried to annotate that field with @JsAttribute(converter = ...) but it still fails to compile.

Any ideas?

@zapov
Copy link
Member

zapov commented Jun 23, 2023

The only thing which comes to mind is some problem with incremental compilation.
So try to use whole project compilation and see if that helps.

@sky-pedrosaraiva
Copy link
Author

sky-pedrosaraiva commented Jun 26, 2023

Can't get it to work.
I made a simple app with two modules were this issue is present:
Repo

The converter is defined here:
Source

@zapov
Copy link
Member

zapov commented Jul 2, 2023

I tried looking into your project but compilation fails with

A problem occurred configuring root project 'test'.
> Could not resolve all files for configuration ':classpath'.
   > Could not resolve com.android.tools.build:gradle:8.1.0-rc01.
     Required by:
         project : > com.android.application:com.android.application.gradle.plugin:8.1.0-rc01

I dont have time to fix it or debug why its not compiling out of the box

@sky-pedrosaraiva
Copy link
Author

Please try this one:
Repo

@zapov
Copy link
Member

zapov commented Jul 3, 2023

Still doesn't work.
I had to change all kind of stuff to get it working but in the end it fails with

This version (1.3.2) of the Compose Compiler requires Kotlin version 1.7.20 but you appear to be using Kotlin version 1.8.20 which is not known to be compatible.  Please fix your configuration (or `suppressKotlinVersionCompatibilityCheck` but don't say I didn't warn you!).

Anyway... from what I've seen I dont see why you expect this to work at all. There is no dependency between the projects (lib1 and lib2)
It seems to me it should work as long as lib2 can see this setup from lib1 (or has its own).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants