-
Notifications
You must be signed in to change notification settings - Fork 311
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
feat: add support for ARRAY<JSON> type in Spring Data Spanner #1157
Conversation
…ccessor. This is to avoid conflict of class A and List<A> annotated with Json at same time.
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.
Looks amazing. I only have a few optional nits.
.../google/cloud/spring/data/spanner/core/convert/ConverterAwareMappingSpannerEntityWriter.java
Outdated
Show resolved
Hide resolved
...-spanner/src/main/java/com/google/cloud/spring/data/spanner/core/convert/StructAccessor.java
Outdated
Show resolved
Hide resolved
private <T> List<T> getListJsonValue(int colIndex, Class<T> colType) { | ||
if (this.struct.getColumnType(colIndex).getCode() != Code.ARRAY) { | ||
throw new SpannerDataException(EXCEPTION_NARRATIVE_COL_NOT_ARRAY + colIndex); | ||
} | ||
List<String> jsonStringList = this.struct.getJsonList(colIndex); | ||
List<T> result = new ArrayList<>(); | ||
jsonStringList.forEach(item -> | ||
result.add(gson.fromJson(item, colType))); | ||
return result; | ||
} |
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.
You might be able to reuse the same code for the two getListJsonValue
definitions by passing struct -> struct.getJsonList(colIndex);
and struct->struct.getJsonList(colName)
as lambdas.
@Query( | ||
"SELECT additionalDetails from :com.google.cloud.spring.data.spanner.test.domain.Trade:" | ||
+ " where trader_id = @trader_id") | ||
List<List<Details>> getAdditionalDetailsById(@Param("trader_id") String traderId); |
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.
Very cool that this works.
Kudos, SonarCloud Quality Gate passed! |
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.
Looks good to me.
QQ> I couldn't see a test for the behaviour around a malformed array of Json. Would there be any behavioural difference in what was happening prior to this PR (say it used to throw something like an UnimplementedException) vs after this PR (say it throws something like InvalidDataException)?
@@ -184,7 +215,8 @@ <T> T getSingleJsonValue(String colName, Class<T> colType) { | |||
return gson.fromJson(jsonString, colType); | |||
} | |||
|
|||
public <T> T getSingleJsonValue(int colIndex, Class<T> colType) { | |||
//TODO: change this to private in next major release |
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.
Is it because users will be using getJsonValue
directly for single json value and array of json, instead of using separate methods getSingleJsonValue
and getListJsonValue
?
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, getJsonValue
is public and getSingleJsonValue
and getListJsonValue
does not need to be exposed. In fact, this method is mostly for internal usage. Users don’t need to interact with neither of these methods, as shown in the samples, user only need to add annotation of the dedicated field and use save()/find() as usual. But this method needs to be public because it is used outside of its package.
@ansh0l To your question: About malformed array of Json: User is expected to interact with entities (data model), with |
…CloudPlatform#1157) support ARRAY spanner type. read: StructAccessor.java and StructPropertyValueProvider.java write: ConverterAwareMappingSpannerEntityWriter.java sql query when return type is ARRAY field. SqlSpannerQuery.java and SpannerPersistentEntityImpl.java test and sample closes GoogleCloudPlatform#651.
support ARRAY spanner type.
StructAccessor.java
andStructPropertyValueProvider.java
ConverterAwareMappingSpannerEntityWriter.java
SqlSpannerQuery.java
andSpannerPersistentEntityImpl.java
closes #651.