-
-
Notifications
You must be signed in to change notification settings - Fork 26
Feature/support null as default value (primitive types) #114
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
base: RxJava1.x
Are you sure you want to change the base?
Feature/support null as default value (primitive types) #114
Conversation
…hen getting a primitive type.
… the readability and comprehension of the change of first commit.
Codecov Report
@@ Coverage Diff @@
## RxJava1.x #114 +/- ##
=============================================
- Coverage 98.11% 98.07% -0.04%
=============================================
Files 5 5
Lines 159 156 -3
Branches 11 10 -1
=============================================
- Hits 156 153 -3
Misses 2 2
Partials 1 1
Continue to review full report at Codecov.
|
Hi,
Thanks for your contribution.
I'll review that.
07.07.2017 8:11 PM "LeonardooBorges" <notifications@github.com> napisał(a):
… #50 <#50>
Passing null as default value in get gives a NullPointerException when
the key is already signed for primitive types. From my study of the code I
see that it was passing the defValue (object of primitive) to
SharedPreferences but it only accepts primitive types. This way sending a
null gives the exception. I saw your comment in the 50 issue, but I think
that sending null as default value make sense sometimes, even if using the
primitive objects.
I also added some tests, you can run them in your version to see the
problem by yourself.
You can see just the first commit
<LeonardooBorges@2bb0108>
also, it already solves the issue, but the second one improves the
readability and comprehension of the changes.
------------------------------
You can view, comment on, or merge this pull request online at:
#114
Commit Summary
- Add some tests and changes to support sending null as default value
when getting a primitive type.
- Changes to verify the existence of the key before accessor,
improving the readability and comprehension of the change of first commit.
File Changes
- *M* build.gradle
<https://github.com/pwittchen/prefser/pull/114/files#diff-0> (2)
- *M* library/src/main/java/com/github/pwittchen/prefser/
library/Accessor.java
<https://github.com/pwittchen/prefser/pull/114/files#diff-1> (2)
- *M* library/src/main/java/com/github/pwittchen/prefser/library/
PreferencesAccessorsProvider.java
<https://github.com/pwittchen/prefser/pull/114/files#diff-2> (24)
- *M* library/src/main/java/com/github/pwittchen/prefser/
library/Prefser.java
<https://github.com/pwittchen/prefser/pull/114/files#diff-3> (18)
- *M* library/src/test/java/com/github/pwittchen/prefser/
library/PrefserTest.java
<https://github.com/pwittchen/prefser/pull/114/files#diff-4> (175)
Patch Links:
- https://github.com/pwittchen/prefser/pull/114.patch
- https://github.com/pwittchen/prefser/pull/114.diff
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#114>, or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAqcF38d15AHwy7AbEbh1ZteGGv_TLcJks5sLnSCgaJpZM4ORQ2f>
.
|
assertThat(givenValue).isEqualTo(readValue); | ||
} | ||
|
||
// alteracoes comeca aqui |
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.
Sorry for that comment, after your review I can delete it.
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.
This comment should be deleted.
// then | ||
assertThat(value).isNull(); | ||
// CustomClass givenObject = new CustomClass(23, "someText"); | ||
// CustomClass defaultObject = new CustomClass(67, "defaultText"); |
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.
Also here.
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.
Commented code should be removed
// then | ||
assertThat(value).isNull(); | ||
// CustomClass givenObject = new CustomClass(23, "someText"); | ||
// CustomClass defaultObject = new CustomClass(67, "defaultText"); |
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.
Commented code should be removed
assertThat(givenValue).isEqualTo(readValue); | ||
} | ||
|
||
// alteracoes comeca aqui |
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.
This comment should be deleted.
@@ -48,7 +48,7 @@ private void createAccessors() { | |||
private void createBooleanAccessor() { | |||
accessors.put(Boolean.class, new Accessor<Boolean>() { | |||
@Override public Boolean get(String key, Boolean defaultValue) { | |||
return preferences.getBoolean(key, defaultValue); | |||
return preferences.getBoolean(key, (defaultValue == null) ? false : defaultValue); |
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.
In this use case, we are defining default behavior for the user in the case of passing null
. We don't know if a user wants true
or false
, but we're forcing using one of these. The same rule applies to all of the changes below.
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.
No, we are not defining default behavior for the user, because if the user sends null in defValue, it would already return before in Prefser class (and it would return null).
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.
Why would user pass the null
? Why should the user expect false
in the case of passing null
, but not true
? How could he or she guess such behavior just by looking at the library API without browsing source code or debugging?
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.
The user will not get true
or false
if he/she passing null
, the user will get null
. Look the other comment I mention you, we are returning defValue before that check.
if (!contains(key)) {
return defaultValue;
}
Line 250 of Prefser.
@@ -60,7 +60,7 @@ private void createBooleanAccessor() { | |||
private void createFloatAccessor() { | |||
accessors.put(Float.class, new Accessor<Float>() { | |||
@Override public Float get(String key, Float defaultValue) { | |||
return preferences.getFloat(key, defaultValue); | |||
return preferences.getFloat(key, (defaultValue == null) ? 0f : defaultValue); |
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.
Same issue as above.
@@ -72,7 +72,7 @@ private void createFloatAccessor() { | |||
private void createIntegerAccessor() { | |||
accessors.put(Integer.class, new Accessor<Integer>() { | |||
@Override public Integer get(String key, Integer defaultValue) { | |||
return preferences.getInt(key, defaultValue); | |||
return preferences.getInt(key, (defaultValue == null) ? 0 : defaultValue); |
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.
Same issue as above.
@@ -84,7 +84,7 @@ private void createIntegerAccessor() { | |||
private void createLongAccessor() { | |||
accessors.put(Long.class, new Accessor<Long>() { | |||
@Override public Long get(String key, Long defaultValue) { | |||
return preferences.getLong(key, defaultValue); | |||
return preferences.getLong(key, (defaultValue == null) ? 0L : defaultValue); |
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.
Same issue as above.
@@ -16,7 +16,7 @@ | |||
package com.github.pwittchen.prefser.library; | |||
|
|||
interface Accessor<T> { | |||
T get(String key, T defaultValue); | |||
T get(String key); |
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'd leave default value as a parameter inside the Accessor
interface
Hello @LeonardooBorges. I really appreciate your contribution, but I'd generally avoid forcing default behavior for accessing data. I think, in the case of this library, the user should explicitly define such behavior. Default Android SharedPreferences mechanism has almost the same API and behavior. Moreover, you wrote:
Can you provide any reasonable examples for that? I think passing PS. It's easier to review code when it's squashed into the single commit within the single PR. You can take a look at this tool: http://rebaseandsqua.sh/. |
@@ -253,20 +247,20 @@ public boolean contains(String key) { | |||
Preconditions.checkNotNull(key, KEY_IS_NULL); | |||
Preconditions.checkNotNull(typeTokenOfT, TYPE_TOKEN_OF_T_IS_NULL); | |||
|
|||
if (!contains(key)) { | |||
return defaultValue; |
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.
@pwittchen here we are returning the defValue, so if the execution reaches there (return preferences.getBoolean(key, (defaultValue == null) ? false : defaultValue);
) SharedPreferences will contain the key and defValue will not be used.
Hello @pwittchen it is me that should be thanking you for what you did and share, many projects in the company I work uses your lib and we are thankful. I already fork and I'm using the changes I have done and as it works fine I decide to open this PR to contribute. I have some points to Prefser accept the null as defValue.
The changes I did increases the Prefser scope to accept more values, so I cannot see a reason to not accept that. Please lets continue the discussion if you not agree. |
Ok, I see your point :). In the case of Java generics, we're not able to return |
Thanks for your time, if you could share your solution later I will appreciate to see. 😄 |
Passing null as default value in
get
gives aNullPointerException
when the key is already signed for primitive types. From my study of the code I see that it was passing the defValue (object of primitive) to SharedPreferences but it only accepts primitive types. This way sending a null gives the exception. I saw your comment in the #50 issue, but I think that sending null as default value make sense sometimes, even if using the primitive objects.I also added some tests, you can run them in your version to see the problem by yourself.
You can see just the first commit also, it already solves the issue, but the second one improves the readability and comprehension of the changes.