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

Avoid invalidating Gson type adapter cache when creating Mongo Repository #1406

Merged
merged 3 commits into from Oct 18, 2022

Conversation

jmoghisi
Copy link
Contributor

Avoid invalidating Gson type adapter cache when creating Mongo Repository.

We only register the delegatingTypeAdapterFactory if Gson does not have TypeAdapters registered for all Bson related types

* @return {@code gson} configured with delegating type adapter factory.
*/
public static Gson newGsonWithDelegatingTypeAdapterFactory(Gson gson) {
if (hasBsonTypeAdapters(gson)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@asereda-gs not too happy with this approach but could not identify a better way to detect if these type adapters were already registered. an alternative I considered was to add another builder method to RepositorySetup that allows providing a preconfigured Gson instance. please let me know your thoughts.

@elucash
Copy link
Member

elucash commented Oct 4, 2022

recovering from Ian storm, will get back in a couple of days, sorry (if interested my zip code is 33947)

@talios
Copy link

talios commented Oct 4, 2022 via email

@elucash
Copy link
Member

elucash commented Oct 4, 2022

Thank you Mark @talios for support! We were lucky! No comments
IMG_2982
IMG_2988
IMG_2981

@elucash
Copy link
Member

elucash commented Oct 4, 2022

only minor damage, happy to survive this shit ))

@elucash
Copy link
Member

elucash commented Oct 11, 2022

@jmoghisi I've reviewed the PR, and I think I've got the idea. The mongo module is in maintenance mode mostly, newer stuff went into criteria module, including MongoDB backend implementation. I'm ok with these changes overall, but want to minimize the surface of changes. I would propose to remove the changes (and cleanup other corresponding methods) in RepositorySetup.Builder.gson( and instead create a new method RepositorySetup.Builder.preconfiguredGson( where we will take Gson instance as is, essentially what if (hasBsonTypeAdapters(gson)) {... branch is doing. In light of this, it's even possible to get away without this new method, because we already can pass Builder codecRegistry(CodecRegistry registry) which is just 2 calls away from preconfiguredGson via CodecRegistries.fromRegistries(GsonCodecs.codecRegistryFromGson(.

So in the dry down we could have just some new codecs registered, some changes to util, constants, and tests. Plus, if you really wish – add RepositorySetup.Builder.preconfiguredGson( or leave it out in favor of calling RepositorySetup.Builder.codecRegistry and construct gson/codecs manually.

Minor stuff - if possible change line continuation indent to 4 spaces instead of 8 (even if we had it accidental 8 previously). The regular indent is 2 which is correct. Also make ); trail on the previous line, without line break

  smth = wrappingCall(new Something.Something()
      .lineContinuation()  // <-- +4 continuation indent
      .anotherLine());  // <-- no newline before closing );

@asereda-gs
Copy link
Member

hi @jmoghisi, thanks for this PR.

I tend to agree with Eugene. Maybe modify RepositorySetup to provide "raw" Gson or have a boolean flag to indicate not to enhance existing gson instance.

Also we you can't leverage Builder.codecRegistry() for your usecase ?

@jmoghisi
Copy link
Contributor Author

Thanks you both for the feedback. I've removed the changes to Builder#gson.

Builder#codecRegistry should be sufficient but I just needed to made GsonNamingStrategy public.

* @param gson preconfigured instance
* @return {@code gson} configured with delegating type adapter factory.
*/
public static Gson newGsonWithDelegatingTypeAdapterFactory(Gson gson) {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe call it newGsonWithMongoSupport , withBsonSupport(Gson) , addBsonSupport(Gson) or includeBson etc.

WithDelegatingTypeAdapterFactory doesn't fully convey the meaning of this method.

Copy link
Member

@elucash elucash Oct 13, 2022

Choose a reason for hiding this comment

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

right, it's more important what the purpose of the action is (adds bson support) than in which way it's technically achieved (by delegating type adapter factory)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

addressed by renaming it to newGsonWithBsonSupport

@@ -128,7 +127,7 @@ public String translateName(Member member) {
class GsonNamingStrategy implements FieldNamingStrategy {
Copy link
Member

Choose a reason for hiding this comment

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

I would move this class one level up it is already inside RepositorySetup.FieldNamingStrategy.

On the other hand, FieldNamingStrategy is already Single Abstract Method (SMA) interface so having a lambda on the top of Gson instance should be trivial to write for clients of this API.

Up to you ...

Copy link
Member

@elucash elucash Oct 13, 2022

Choose a reason for hiding this comment

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

I've been thinking about it for a short while. Seems that we better have something like GsonNamingStrategy public (or private with public factory method namingStrategyFrom(Gson)) because it's kinda documents that it's possible and should be used sometimes. It is easy to implement with a lambda, but people might have no clue that they ought to do that and will do smth hardcoded.

Copy link
Member

Choose a reason for hiding this comment

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

Perhaps GsonNamingStrategy warrants its own file and be public or, at least, gsonNamingStrategy factory method (also public).

Not 100% sure if it should be inside bson4gson package / GsonCodes class.

Copy link
Member

@elucash elucash Oct 13, 2022

Choose a reason for hiding this comment

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

Maybe, but given that we think Criteria is the newer implementation and to minimize impact of changes (as it is the goal of some changes asked for this PR) let it be just public GsonNamingStrategy, just can be moved out from interface and be directly nested in RepositorySetup as RepositorySetup.GsonNamingStrategy. I think this will be minimal and cute enough.
P.S. I'll answer on other comments tomorrow morning.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

made it public visibility and moved to RepositorySetup

Copy link
Member

@asereda-gs asereda-gs left a comment

Choose a reason for hiding this comment

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

Overall looks fine to me.

Just small comments before merging.

@@ -22,7 +25,7 @@ public void reflectiveTypeAdapter() {
}

@Test
public void dateCodec() throws IOException {
public void typeAdapterFromCodec() throws IOException {
Copy link
Member

Choose a reason for hiding this comment

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

I think keeping the old name (dateCodec) makes more sense

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I went with typeAdapterFromDateCodec

@@ -36,4 +39,16 @@ public void dateCodec() throws IOException {
check(doc.get("$date").getBsonType()).is(BsonType.DATE_TIME);
check(doc.get("$date").asDateTime().getValue()).is(date.getTime());
}

@Test
public void newGsonWithDelegatingTypeAdapterFactory() {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe call it gsonWithBsonSupport

Copy link
Contributor Author

Choose a reason for hiding this comment

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

addressed

@elucash
Copy link
Member

elucash commented Oct 18, 2022

Thank you! I think this is good and useful! I'm merging this. The release will be when I'll have my broadband internet restored, should be soon, but no ETA

@elucash
Copy link
Member

elucash commented Oct 18, 2022

I'll also squash this (I'm forgetting sometimes to squash other PRs, sorry), so you'll see one commit in the master.

@elucash elucash merged commit c3344b5 into immutables:master Oct 18, 2022
@jmoghisi
Copy link
Contributor Author

Thanks Eugene, appreciate all the feedback as well.

@jmoghisi jmoghisi deleted the gson_reuse branch October 18, 2022 04:25
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

4 participants