Skip to content
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

Implement datastore Blob to byte[] conversion on read #729

Merged
merged 4 commits into from
Nov 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public <T> T convertOnRead(Object val, Class targetCollectionType, Class targetC
public <T> T convertOnRead(Object val, EmbeddedType embeddedType, TypeInformation targetTypeInformation) {
TypeInformation componentTypeInformation;
Class collectionType = null;
if (targetTypeInformation.isCollectionLike()) {
if (ValueUtil.isCollectionLike(targetTypeInformation.getType())) {
componentTypeInformation = targetTypeInformation.getComponentType();
collectionType = targetTypeInformation.getType();
}
Expand Down Expand Up @@ -155,6 +155,7 @@ private <T> T convertOnRead(Object val, EmbeddedType embeddedType,

if (ValueUtil.isCollectionLike(val.getClass())
&& targetCollectionType != null && targetComponentType != null) {
// Convert collection.
try {
List elements;
if (val.getClass().isArray()) {
Expand All @@ -175,6 +176,7 @@ private <T> T convertOnRead(Object val, EmbeddedType embeddedType,
throw new DatastoreDataException("Unable process elements of a collection", ex);
}
}
// Convert single value.
return (T) readConverter.apply(val, targetComponentType);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ public void setUp() {
public void readTest() {
byte[] bytes = { 1, 2, 3 };
Key otherKey = Key.newBuilder("testproject", "test_kind", "test_name").build();
Entity entity = getEntityBuilder()
// Datastore Entity from the backend / client library.
Entity datastoreEntity = getEntityBuilder()
.set("durationField", "PT24H")
.set("stringField", "string value")
.set("boolField", true)
Expand All @@ -116,21 +117,36 @@ public void readTest() {
.set("enumField", "WHITE")
.set("keyField", otherKey)
.build();
TestDatastoreItem item = ENTITY_CONVERTER.read(TestDatastoreItem.class, entity);

assertThat(item.getDurationField()).as("validate duration field").isEqualTo(Duration.ofDays(1));
assertThat(item.getStringField()).as("validate string field").isEqualTo("string value");
assertThat(item.getBoolField()).as("validate boolean field").isTrue();
assertThat(item.getDoubleField()).as("validate double field").isEqualTo(3.1415D);
assertThat(item.getLongField()).as("validate long field").isEqualTo(123L);
assertThat(item.getLatLngField()).as("validate latLng field")
// Plain Java Object that the user expects to operate on.
TestDatastoreItem userItem = ENTITY_CONVERTER.read(TestDatastoreItem.class, datastoreEntity);

assertThat(userItem.getDurationField()).as("validate duration field").isEqualTo(Duration.ofDays(1));
assertThat(userItem.getStringField()).as("validate string field").isEqualTo("string value");
assertThat(userItem.getBoolField()).as("validate boolean field").isTrue();
assertThat(userItem.getDoubleField()).as("validate double field").isEqualTo(3.1415D);
assertThat(userItem.getLongField()).as("validate long field").isEqualTo(123L);
assertThat(userItem.getLatLngField()).as("validate latLng field")
.isEqualTo(LatLng.of(10, 20));
assertThat(item.getTimestampField()).as("validate timestamp field")
assertThat(userItem.getTimestampField()).as("validate timestamp field")
.isEqualTo(Timestamp.ofTimeSecondsAndNanos(30, 40));
assertThat(item.getBlobField()).as("validate blob field").isEqualTo(Blob.copyFrom(bytes));
assertThat(item.getIntField()).as("validate int field").isEqualTo(99);
assertThat(item.getEnumField()).as("validate enum field").isEqualTo(TestDatastoreItem.Color.WHITE);
assertThat(item.getKeyField()).as("validate key field").isEqualTo(otherKey);
assertThat(userItem.getBlobField()).as("validate blob field").isEqualTo(Blob.copyFrom(bytes));
assertThat(userItem.getIntField()).as("validate int field").isEqualTo(99);
assertThat(userItem.getEnumField()).as("validate enum field").isEqualTo(TestDatastoreItem.Color.WHITE);
assertThat(userItem.getKeyField()).as("validate key field").isEqualTo(otherKey);
}

@Test
public void readTestByteArray() {
byte[] bytes = { 1, 2, 3 };

// Datastore Entity from the backend / client library.
Entity datastoreEntity = getEntityBuilder()
.set("byteArrayField", Blob.copyFrom(bytes))
.build();
// Plain Java Object that the user expects to operate on.
TestDatastoreItem userItem = ENTITY_CONVERTER.read(TestDatastoreItem.class, datastoreEntity);

assertThat(userItem.getByteArrayField()).as("validate byte array field").isEqualTo(bytes);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.example;

import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.Month;
import java.util.Arrays;
Expand Down Expand Up @@ -69,6 +70,7 @@ public CommandLineRunner commandLineRunner() {
new Album("b", LocalDate.of(2018, Month.FEBRUARY, 12)))));
Singer richardRoe = new Singer("singer3", "Richard", "Roe",
new HashSet<>(Arrays.asList(new Album("c", LocalDate.of(2000, Month.AUGUST, 31)))));
richardRoe.setMessage("Hello, dear fans!".getBytes(StandardCharsets.UTF_8));

this.singerRepository.saveAll(Arrays.asList(janeDoe, richardRoe));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public class Singer {

private Set<Album> albums;

private byte[] message;

public Singer() {
}

Expand Down Expand Up @@ -130,6 +132,14 @@ public void setAlbums(Set<Album> albums) {
this.albums = albums;
}

public byte[] getMessage() {
return message;
}

public void setMessage(byte[] message) {
this.message = message;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down Expand Up @@ -162,6 +172,7 @@ public String toString() {
+ ((this.personalInstruments == null) ? ""
: Strings.join(this.personalInstruments.stream()
.map(x -> x.getType()).collect(Collectors.toList()), ','))
+ ((this.message == null) ? "" : ", Message: " + new String(message))
+ '}';
}

Expand Down