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

Kotlinx collection immutable #574

Closed
wants to merge 12 commits into from

Conversation

beigirad
Copy link

@beigirad beigirad commented Apr 3, 2024

Resolve issue #460 or 1739 of moshi

In following of #478, I implemented Collection, Set and did some improvements based on @ZacSweers comment:

  • support Sets
  • support Collection
  • simplify the API to just ImmutableCollectionsJsonAdapter.Factory, rather than one for each.

Originally posted by @ZacSweers in #478 (comment)

Copy link
Owner

@ZacSweers ZacSweers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The naming here is backward. We should focus on the parent type nature - Immutable. Let's rename all these from Persistent* -> Immutable*. Persistent collections are just an implementation of that, and not actually the focus here.

Similarly, we should be looking for the Immutable* types in the factories, as the current implementation only supports Persistent* subtypes.

Finally, let's add tests to cover both Immutable* and Persistent* types (the latter just to verify we're gracefully handling the concrete types).

gradle/libs.versions.toml Outdated Show resolved Hide resolved
moshi-adapters-kotlinx-immutable/README.md Outdated Show resolved Hide resolved
alias(libs.plugins.mavenPublish)
}

tasks.named<KotlinCompile>("compileTestKotlin") {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a DSL accessor for this

Suggested change
tasks.named<KotlinCompile>("compileTestKotlin") {
compileTestKotlin {

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ZacSweers
After committing this change, the project doesn't run tests. (I commit new changes without this change)

What do you think if I revert the changes?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry should be tasks.compileTestKotlin. That said, it would be helpful if you elaborated on what you mean by "doesn't run tests".

@beigirad beigirad force-pushed the kotlinx-collection-immutable branch from 3149b54 to 4b33f48 Compare April 15, 2024 12:03
beigirad and others added 4 commits April 15, 2024 15:34
Co-authored-by: Zac Sweers <pandanomic@gmail.com>
Co-authored-by: Zac Sweers <pandanomic@gmail.com>
@beigirad beigirad force-pushed the kotlinx-collection-immutable branch from 4b33f48 to 7203d59 Compare April 15, 2024 12:05
@beigirad beigirad requested a review from ZacSweers April 15, 2024 12:09
val objList: ImmutableList<SomeObject>, // or PersistentList<SomeObject>,
val objCollection: ImmutableCollection<SomeObject>, // or PersistentCollection<SomeObject>,
val objMap: ImmutableMap<String, SomeObject>, // or PersistentMap<String, SomeObject>,
val objectsSet: ImmutableMap<SomeObject>, // or PersistentMap<SomeObject>,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this correct?

Copy link
Author

@beigirad beigirad Apr 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying to minimize the model while it contain all types too.
What's your suggestion?
@ZacSweers

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am saying that ImmutableMap has two type parameters, it would be a syntax error to write ImmutableMap<SomeObject> (just one argument). Did you mean to write something else?

* val objList: ImmutableList<SomeObject>, // or PersistentList<SomeObject>,
* val objCollection: ImmutableCollection<SomeObject>, // or PersistentCollection<SomeObject>,
* val objMap: ImmutableMap<String, SomeObject>, // or PersistentMap<String, SomeObject>,
* val objectsSet: ImmutableMap<SomeObject>, // or PersistentMap<SomeObject>,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question about correctness here

}

override fun toJson(writer: JsonWriter, value: E?) {
writer.beginArray()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the value is null, we should emit a nullValue and return, not an empty array. That said, we also know this will never be null due to the nullSafe() use so let's just force it to non-null

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked moshi and it uses markNotNull

but it's internal and I used a simple requireNotNull() for that. Is it suit?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neither, I am referring to JsonWriter.nullValue()


override fun toJson(writer: JsonWriter, map: PersistentMap<K, V?>?) {
writer.beginObject()
map?.forEach { (key, value) ->
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here re: nullability

@beigirad beigirad requested a review from ZacSweers April 17, 2024 07:49
@ZacSweers
Copy link
Owner

Implemented in #586

@ZacSweers ZacSweers closed this May 10, 2024
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

Successfully merging this pull request may close these issues.

None yet

3 participants