-
Notifications
You must be signed in to change notification settings - Fork 656
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
Add record field serialization #342
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7e37048
Add record field serialiation, record size estimation
BenSchwab 3eeb615
Change 'setField' in Record to builder pattern, as Record should only…
BenSchwab 129e41f
write better javadoc
BenSchwab e2ea512
keep provided add api to test config
BenSchwab a3debf9
fix checkystyle
BenSchwab 20425b8
remove record sizing
BenSchwab 11ee673
fix nits
BenSchwab 6f150e8
more semantic reader names
BenSchwab 6fc6ebc
fix checkstyle
BenSchwab File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
apollo-runtime/src/main/java/com/apollographql/android/cache/normalized/FieldsAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.apollographql.android.cache.normalized; | ||
|
||
import com.apollographql.android.impl.CacheJsonStreamReader; | ||
import com.squareup.moshi.JsonAdapter; | ||
import com.squareup.moshi.JsonReader; | ||
import com.squareup.moshi.JsonWriter; | ||
import com.squareup.moshi.Moshi; | ||
import com.squareup.moshi.Types; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.lang.reflect.Type; | ||
import java.math.BigDecimal; | ||
import java.util.Map; | ||
|
||
import okio.BufferedSource; | ||
import okio.Okio; | ||
|
||
import static com.apollographql.android.impl.ApolloReader.bufferedSourceJsonReader; | ||
import static com.apollographql.android.impl.ApolloReader.cacheJsonStreamReader; | ||
|
||
public final class FieldsAdapter { | ||
private final JsonAdapter<Map<String, Object>> serializationAdapter; | ||
|
||
private FieldsAdapter(Moshi moshi) { | ||
Type type = Types.newParameterizedType(Map.class, String.class, Object.class); | ||
serializationAdapter = moshi.adapter(type); | ||
} | ||
|
||
public static FieldsAdapter create() { | ||
Moshi moshi = new Moshi.Builder() | ||
.add(CacheReference.class, new CacheReferenceAdapter()) | ||
.add(BigDecimal.class, new BigDecimalAdapter()) | ||
.build(); | ||
return new FieldsAdapter(moshi); | ||
} | ||
|
||
public String toJson(Map<String, Object> fields) { | ||
return serializationAdapter.toJson(fields); | ||
} | ||
|
||
public Map<String, Object> from(BufferedSource bufferedFieldSource) throws IOException { | ||
final CacheJsonStreamReader cacheJsonStreamReader = | ||
cacheJsonStreamReader(bufferedSourceJsonReader(bufferedFieldSource)); | ||
return cacheJsonStreamReader.buffer(); | ||
} | ||
|
||
public Map<String, Object> from(String jsonFieldSource) throws IOException { | ||
final BufferedSource bufferSource = Okio.buffer(Okio.source(new ByteArrayInputStream(jsonFieldSource.getBytes()))); | ||
return from(bufferSource); | ||
} | ||
|
||
private static class CacheReferenceAdapter extends JsonAdapter<CacheReference> { | ||
|
||
@Override public CacheReference fromJson(JsonReader reader) throws IOException { | ||
throw new IllegalStateException(this.getClass().getName() + " should only be used for serialization."); | ||
} | ||
|
||
@Override public void toJson(JsonWriter writer, CacheReference value) throws IOException { | ||
writer.value(value.serialize()); | ||
} | ||
} | ||
|
||
private static class BigDecimalAdapter extends JsonAdapter<BigDecimal> { | ||
|
||
@Override public BigDecimal fromJson(JsonReader reader) throws IOException { | ||
throw new IllegalStateException(this.getClass().getName() + " should only be used for serialization."); | ||
} | ||
|
||
@Override public void toJson(JsonWriter writer, BigDecimal value) throws IOException { | ||
writer.value(value); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 0 additions & 36 deletions
36
...o-runtime/src/main/java/com/apollographql/android/cache/normalized/sql/FieldsAdapter.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
any reason for this change? I'd prefer to be able to use 1 moshi instance for whole app apollo or otherwise
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 might be missing something of how Moshi works, but it seems dangerous to register our BigDecimal converter on a global Moshi instance, in case there is an existing converter. (Currently, it seems we do not allow a user to pass in a Moshi instance to Apollo anyway).
We could register these convertors on the Apollo's Moshi instance in ApolloClient. But that Moshi instance seems primarily concerned with serializing variable objects, and initializing cache serializers there would take some thought of how to do it properly without spaghetti dependencies in the ApolloClient builder.
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.
My thinking was from a performance standup. Moshi caches adapters but it's on a per instance level. So say I have same date converter on app and apollo client, I'd prefer to not have to cache both. Currently our app works with 1 instance of gson. It doesn't have to be solved now. I can do a pr if it becomes an issue