Skip to content

Commit 94a9e99

Browse files
committed
feat: read/write PhoneNumber and GeoCoordinates data
1 parent 1e37d7f commit 94a9e99

8 files changed

Lines changed: 103 additions & 10 deletions

File tree

src/it/java/io/weaviate/integration/DataITest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import io.weaviate.ConcurrentTest;
1515
import io.weaviate.client6.v1.api.WeaviateApiException;
1616
import io.weaviate.client6.v1.api.WeaviateClient;
17+
import io.weaviate.client6.v1.api.collections.GeoCoordinates;
1718
import io.weaviate.client6.v1.api.collections.PhoneNumber;
1819
import io.weaviate.client6.v1.api.collections.Property;
1920
import io.weaviate.client6.v1.api.collections.ReferenceProperty;
@@ -461,6 +462,7 @@ public void testDataTypes() throws IOException {
461462
Map.entry("prop_uuid_array", List.of(uuid, uuid)),
462463
Map.entry("prop_text_array", List.of("a", "b", "c")),
463464
Map.entry("prop_phone_number", PhoneNumber.international("+380 95 1433336")),
465+
Map.entry("prop_geo_coordinates", new GeoCoordinates(1f, 2f)),
464466
Map.entry("prop_object", Map.of("marco", "polo")),
465467
Map.entry("prop_object_array", List.of(Map.of("marco", "polo"))));
466468

@@ -472,8 +474,10 @@ public void testDataTypes() throws IOException {
472474
Assertions.assertThat(got).get()
473475
.extracting(WeaviateObject::properties)
474476
.asInstanceOf(InstanceOfAssertFactories.map(String.class, Object.class))
475-
.containsAllEntriesOf(want);
476-
477+
// Most of PhoneNumber fields are only present on read and are null on write.
478+
.usingRecursiveComparison()
479+
.withComparatorForType(ORMITest::comparePhoneNumbers, PhoneNumber.class)
480+
.isEqualTo(want);
477481
}
478482

479483
record Address(

src/it/java/io/weaviate/integration/ORMITest.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import io.weaviate.ConcurrentTest;
1616
import io.weaviate.client6.v1.api.WeaviateClient;
1717
import io.weaviate.client6.v1.api.collections.CollectionConfig;
18+
import io.weaviate.client6.v1.api.collections.GeoCoordinates;
19+
import io.weaviate.client6.v1.api.collections.PhoneNumber;
1820
import io.weaviate.client6.v1.api.collections.WeaviateObject;
1921
import io.weaviate.client6.v1.api.collections.annotations.Collection;
2022
import io.weaviate.client6.v1.api.collections.annotations.Property;
@@ -79,7 +81,10 @@ static record Thing(
7981
Boolean booleanBoxed,
8082
boolean[] booleanArray,
8183
Boolean[] booleanBoxedArray,
82-
List<Boolean> booleanBoxedList) {
84+
List<Boolean> booleanBoxedList,
85+
86+
PhoneNumber phoneNumber,
87+
GeoCoordinates geoCoordinates) {
8388
}
8489

8590
@BeforeClass
@@ -150,14 +155,18 @@ public void test_createCollection() throws Exception {
150155
Map.entry("booleanBoxed", "boolean"),
151156
Map.entry("booleanArray", "boolean[]"),
152157
Map.entry("booleanBoxedArray", "boolean[]"),
153-
Map.entry("booleanBoxedList", "boolean[]"));
158+
Map.entry("booleanBoxedList", "boolean[]"),
159+
160+
Map.entry("phoneNumber", "phoneNumber"),
161+
Map.entry("geoCoordinates", "geoCoordinates"));
154162
}
155163

156-
private final RecursiveComparisonConfiguration COMPARISON_CONFIG = RecursiveComparisonConfiguration.builder()
164+
private static final RecursiveComparisonConfiguration COMPARISON_CONFIG = RecursiveComparisonConfiguration.builder()
157165
// Assertj is having a really bad time comparing List<Float>,
158166
// so we'll just always return true here.
159167
.withComparatorForFields((a, b) -> 0, "floatBoxedList")
160168
.withComparatorForType((a, b) -> Double.compare(a.doubleValue(), b.doubleValue()), Number.class)
169+
.withComparatorForType(ORMITest::comparePhoneNumbers, PhoneNumber.class)
161170
.build();
162171

163172
@Test
@@ -219,7 +228,10 @@ public void test_insertAndQuery() throws Exception {
219228
boolean_,
220229
new boolean[] { boolean_ },
221230
new Boolean[] { boolean_ },
222-
List.of(boolean_));
231+
List.of(boolean_),
232+
233+
PhoneNumber.international("+380 95 1433336"),
234+
new GeoCoordinates(1f, 2f));
223235

224236
var things = client.collections.use(Thing.class);
225237

@@ -294,7 +306,10 @@ public void test_insertManyAndQuery() throws Exception {
294306
boolean_,
295307
new boolean[] { boolean_ },
296308
new Boolean[] { boolean_ },
297-
List.of(boolean_));
309+
List.of(boolean_),
310+
311+
PhoneNumber.international("+380 95 1433336"),
312+
new GeoCoordinates(1f, 2f));
298313

299314
var things = client.collections.use(Thing.class);
300315

@@ -351,7 +366,7 @@ public void test_partialScan() throws IOException {
351366
.returns(null, Song::monthlyListeners);
352367
}
353368

354-
@Test
355-
public void test_nestedProperties() throws IOException {
369+
static int comparePhoneNumbers(PhoneNumber phone1, PhoneNumber phone2) {
370+
return phone1.rawInput().compareTo(phone2.rawInput());
356371
}
357372
}

src/main/java/io/weaviate/client6/v1/api/collections/data/InsertManyRequest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
import java.util.UUID;
99

1010
import io.weaviate.client6.v1.api.collections.CollectionHandleDefaults;
11+
import io.weaviate.client6.v1.api.collections.GeoCoordinates;
1112
import io.weaviate.client6.v1.api.collections.ObjectMetadata;
13+
import io.weaviate.client6.v1.api.collections.PhoneNumber;
1214
import io.weaviate.client6.v1.api.collections.WeaviateObject;
1315
import io.weaviate.client6.v1.internal.MapUtil;
1416
import io.weaviate.client6.v1.internal.grpc.ByteStringUtil;
@@ -181,6 +183,23 @@ private static com.google.protobuf.Value marshalValue(Object value) {
181183
protoValue.setBoolValue(v.booleanValue());
182184
} else if (value instanceof Number v) {
183185
protoValue.setNumberValue(v.doubleValue());
186+
} else if (value instanceof PhoneNumber phone) {
187+
var phoneProto = com.google.protobuf.Struct.newBuilder();
188+
if (phone.rawInput() != null) {
189+
var input = com.google.protobuf.Value.newBuilder().setStringValue(phone.rawInput());
190+
phoneProto.putFields("input", input.build());
191+
}
192+
if (phone.defaultCountry() != null) {
193+
var defaultCountry = com.google.protobuf.Value.newBuilder().setStringValue(phone.defaultCountry());
194+
phoneProto.putFields("defaultCountry", defaultCountry.build());
195+
}
196+
protoValue.setStructValue(phoneProto);
197+
} else if (value instanceof GeoCoordinates geo) {
198+
var latitude = com.google.protobuf.Value.newBuilder().setNumberValue(geo.latitude());
199+
var longitude = com.google.protobuf.Value.newBuilder().setNumberValue(geo.longitude());
200+
protoValue.setStructValue(com.google.protobuf.Struct.newBuilder()
201+
.putFields("latitude", latitude.build())
202+
.putFields("longitude", longitude.build()));
184203
} else if (value instanceof List<?> v) {
185204
protoValue.setListValue(
186205
com.google.protobuf.ListValue.newBuilder()

src/main/java/io/weaviate/client6/v1/api/collections/query/QueryResponse.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
import java.util.UUID;
77
import java.util.stream.Stream;
88

9+
import io.weaviate.client6.v1.api.collections.GeoCoordinates;
910
import io.weaviate.client6.v1.api.collections.ObjectMetadata;
11+
import io.weaviate.client6.v1.api.collections.PhoneNumber;
1012
import io.weaviate.client6.v1.api.collections.Vectors;
1113
import io.weaviate.client6.v1.api.collections.WeaviateObject;
1214
import io.weaviate.client6.v1.internal.DateUtil;
@@ -159,6 +161,21 @@ static <PropertiesT> void setProperty(String property, WeaviateProtoProperties.V
159161
builder.setOffsetDateTime(property, DateUtil.fromISO8601(value.getDateValue()));
160162
} else if (value.hasUuidValue()) {
161163
builder.setUuid(property, UUID.fromString(value.getUuidValue()));
164+
} else if (value.hasPhoneValue()) {
165+
var phone = value.getPhoneValue();
166+
builder.setPhoneNumber(property, new PhoneNumber(
167+
phone.getInput(),
168+
phone.getDefaultCountry(),
169+
Long.valueOf(phone.getCountryCode()).intValue(),
170+
phone.getInternationalFormatted(),
171+
Long.valueOf(phone.getNational()).intValue(),
172+
phone.getNationalFormatted(),
173+
phone.getValid()));
174+
} else if (value.hasGeoValue()) {
175+
var geo = value.getGeoValue();
176+
builder.setGeoCoordinates(property, new GeoCoordinates(
177+
geo.getLatitude(),
178+
geo.getLongitude()));
162179
} else if (value.hasListValue()) {
163180
var list = value.getListValue();
164181
if (list.hasTextValues()) {

src/main/java/io/weaviate/client6/v1/internal/orm/MapBuilder.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import java.util.Map;
77
import java.util.UUID;
88

9+
import io.weaviate.client6.v1.api.collections.GeoCoordinates;
10+
import io.weaviate.client6.v1.api.collections.PhoneNumber;
11+
912
public class MapBuilder implements PropertiesBuilder<Map<String, Object>> {
1013
private final Map<String, Object> properties = new HashMap<>();
1114

@@ -89,8 +92,18 @@ public void setNestedObjectArray(String property, List<? extends Object> value)
8992
properties.put(property, value);
9093
}
9194

95+
@Override
96+
public void setPhoneNumber(String property, PhoneNumber value) {
97+
properties.put(property, value);
98+
}
99+
100+
@Override
101+
public void setGeoCoordinates(String property, GeoCoordinates value) {
102+
properties.put(property, value);
103+
}
104+
92105
@Override
93106
public Map<String, Object> build() {
94-
return properties;
107+
return new HashMap<>(properties);
95108
}
96109
}

src/main/java/io/weaviate/client6/v1/internal/orm/PojoBuilder.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
import org.apache.commons.lang3.ArrayUtils;
1212

13+
import io.weaviate.client6.v1.api.collections.GeoCoordinates;
14+
import io.weaviate.client6.v1.api.collections.PhoneNumber;
15+
1316
final class PojoBuilder<PropertiesT extends Record> implements PropertiesBuilder<PropertiesT> {
1417
private static final Map<Class<?>, Object> PRIMITIVE_DEFAULTS;
1518

@@ -237,6 +240,16 @@ public void setNestedObjectArray(String property, List<? extends Object> value)
237240
throw new UnsupportedOperationException("Unimplemented method 'setNestedObjectArray'");
238241
}
239242

243+
@Override
244+
public void setPhoneNumber(String propertyName, PhoneNumber value) {
245+
setValue(propertyName, value);
246+
}
247+
248+
@Override
249+
public void setGeoCoordinates(String propertyName, GeoCoordinates value) {
250+
setValue(propertyName, value);
251+
}
252+
240253
@Override
241254
public PropertiesT build() {
242255
Object[] args = ctorArgs.values().stream().map(Arg::value).toArray();

src/main/java/io/weaviate/client6/v1/internal/orm/PojoDescriptor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import com.google.gson.reflect.TypeToken;
1717

1818
import io.weaviate.client6.v1.api.collections.CollectionConfig;
19+
import io.weaviate.client6.v1.api.collections.GeoCoordinates;
20+
import io.weaviate.client6.v1.api.collections.PhoneNumber;
1921
import io.weaviate.client6.v1.api.collections.Property;
2022
import io.weaviate.client6.v1.api.collections.annotations.Collection;
2123
import io.weaviate.client6.v1.internal.ObjectBuilder;
@@ -65,6 +67,9 @@ final class PojoDescriptor<T extends Record> implements CollectionDescriptor<T>
6567
put(Double[].class, Property::numberArray);
6668

6769
put(Map.class, Property::object);
70+
71+
put(PhoneNumber.class, Property::phoneNumber);
72+
put(GeoCoordinates.class, Property::geoCoordinates);
6873
}
6974
};
7075
CTORS = Collections.unmodifiableMap(ctors);

src/main/java/io/weaviate/client6/v1/internal/orm/PropertiesBuilder.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import java.util.List;
55
import java.util.UUID;
66

7+
import io.weaviate.client6.v1.api.collections.GeoCoordinates;
8+
import io.weaviate.client6.v1.api.collections.PhoneNumber;
9+
710
public interface PropertiesBuilder<T> {
811
void setNull(String property);
912

@@ -37,5 +40,9 @@ public interface PropertiesBuilder<T> {
3740

3841
void setNestedObjectArray(String property, List<? extends Object> value);
3942

43+
void setPhoneNumber(String property, PhoneNumber value);
44+
45+
void setGeoCoordinates(String property, GeoCoordinates value);
46+
4047
T build();
4148
}

0 commit comments

Comments
 (0)