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
Refactored client library code for Data Types for simplicity and code reuse #1
base: main
Are you sure you want to change the base?
Conversation
…-reuse. This refactoring is attempting the following benefits: 1. Code reuse for common value encoding conversions 2. Reducing the number of places when adding a new data-type, especially the type-related conversion logic. 3. Refactor the existing primitive types to use the new helper functions to simplify existing code.
These changes are great for a first pass. |
… set, setting query parameters and mutations
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.
One of the ideas was to centralize Type specific logic such that when we add a new type we would only need to touch one file, maybe two files. I see that if I add a new type I would still need to change: potentially Type.java, TypeHelper.java, Value.java and potentially ChecksumResultSet.java.
Could we see if we can centralize this further?
Perhaps all of the following could live in a TypeMapper of sorts:
- Code to primitive type
- Code to array type
- Supported primitive type classes
- Extracting value using code
- Extracting array using code
return date(); | ||
Code typeCode = Code.fromProto(proto.getCode(), proto.getTypeAnnotation()); | ||
if (isPrimitiveTypeCodeSupported(typeCode)) { | ||
System.out.format("Type from map: %s\n", CODE_TO_PRIMITIVE_TYPE.get(typeCode)); |
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.
Left over?
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.
Yes, thanks for catching. Will clean up
import com.google.cloud.spanner.Type.Code; | ||
import java.util.List; | ||
|
||
public final class TypeHelper { |
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.
nit: *Helper is too generic of a name, perhaps PrimitiveTypeMapper or something alike?
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 agree. Naming is hard! Will think of an alternative, otherwise Mapper sounds good.
@thiagotnunes I have named the types supported by these generic methods/interfaces as "Primitive" types, since I do not intend to handle complex types such as Struct or Protos. I am not sure if this is indicative though. Do comment if this sounds alright, or if you can think of alternatives
@charvisingla Yes, in the first parse I tried to just identify and reduce the duplication wherever I found common code fragments. Special cases are still left untouched. In a second parse I am now looking at these two as you suggested:
I had faced the following challenges in phase-1 while exploring ways to add new generic interfaces. Do share if you have any specific ideas or suggestions:
|
Yes, and also (6) Checksum calculation for each type. Attempting this now. |
Refactored client library code for Data Types for simplicity and code reuse
This refactoring is attempting the following benefits:
Reference - code change required for adding a new type:
Fixes - (Bug to be created) ☕️
[Note: This PR is raised on a fork of the java-spanner client repo. It is currently a prototype, and once things look promising a separate PR will be raised for merging this to the official repo.]