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
Conversation
e096e02
to
4a5e27c
Compare
* @return {@code gson} configured with delegating type adapter factory. | ||
*/ | ||
public static Gson newGsonWithDelegatingTypeAdapterFactory(Gson gson) { | ||
if (hasBsonTypeAdapters(gson)) { |
There was a problem hiding this comment.
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.
recovering from Ian storm, will get back in a couple of days, sorry (if interested my zip code is 33947) |
On 4/10/2022 at 9:05:58 PM, Eugene Lukash ***@***.***> wrote:
recovering from Ian storm, will get back in a couple of days, sorry
Off topic - but glad to hear your ok, and hope you weren’t too
displaced/affected - looks beyond nasty!
Mark
…--
"Great artists are extremely selfish and arrogant things" — Steven Wilson,
Porcupine Tree
|
Thank you Mark @talios for support! We were lucky! No comments |
only minor damage, happy to survive this shit )) |
@jmoghisi I've reviewed the PR, and I think I've got the idea. The 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 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 smth = wrappingCall(new Something.Something()
.lineContinuation() // <-- +4 continuation indent
.anotherLine()); // <-- no newline before closing ); |
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 |
Thanks you both for the feedback. I've removed the changes to
|
* @param gson preconfigured instance | ||
* @return {@code gson} configured with delegating type adapter factory. | ||
*/ | ||
public static Gson newGsonWithDelegatingTypeAdapterFactory(Gson gson) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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 ...
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this 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 { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe call it gsonWithBsonSupport
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
addressed
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 |
I'll also squash this (I'm forgetting sometimes to squash other PRs, sorry), so you'll see one commit in the master. |
Thanks Eugene, appreciate all the feedback as well. |
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