Skip to content

Allow the sealed class map of converters to have a default value#23

Open
gypsydave5 wants to merge 1 commit intouberto:mainfrom
gypsydave5:default-value-for-sealed-class-konvertor
Open

Allow the sealed class map of converters to have a default value#23
gypsydave5 wants to merge 1 commit intouberto:mainfrom
gypsydave5:default-value-for-sealed-class-konvertor

Conversation

@gypsydave5
Copy link
Contributor

No description provided.

@gypsydave5
Copy link
Contributor Author

When trying to lift a certain code base that I shall not name into using Kondor, I've had to do some wiggling to make it work. The VersionedConverter was one of them, this is another.

There's some JSON for a sealed class where the type isn't explicitly tagged. One of the properties is doing double duty: a particular string represents type A, and other values represent type B(value) - the property becomes a field for B.

As such I need to say, when getting the konverter, if this property is x, use konverter JA, for any other value, use konvertor JB.

We set up the mapping using a Map in JSealed. So I give the Map a defaultValue(JB). But...

Given a Map<K,V>
.get() takes a K?, and returns a V?.
.getValue() takes a K and returns a V or throws when not present.

.get() ignores the the defaultValue(). So use we should use getValue(). But sometimes the key we're using is null.

So we want the behaviour of get() with the default value of getValue(), but still handling the case where, when we have a null key we get a null value, and when the defaultValue hasn't been set, we still get a null back when we pass a missing key in.

Hence the totally insane dance you see in the code.

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.

1 participant