|
22 | 22 | import static com.google.gcloud.datastore.DoubleValue.of; |
23 | 23 | import static com.google.gcloud.datastore.EntityValue.of; |
24 | 24 | import static com.google.gcloud.datastore.KeyValue.of; |
| 25 | +import static com.google.gcloud.datastore.LatLngValue.of; |
25 | 26 | import static com.google.gcloud.datastore.ListValue.of; |
26 | 27 | import static com.google.gcloud.datastore.LongValue.of; |
27 | 28 | import static com.google.gcloud.datastore.NullValue.of; |
28 | 29 | import static com.google.gcloud.datastore.StringValue.of; |
29 | 30 |
|
30 | | -import com.google.api.services.datastore.DatastoreV1; |
31 | 31 | import com.google.common.collect.ImmutableSortedMap; |
32 | 32 | import com.google.common.collect.Maps; |
33 | 33 | import com.google.protobuf.InvalidProtocolBufferException; |
|
49 | 49 | * @see <a href="https://cloud.google.com/datastore/docs/concepts/entities">Google Cloud Datastore |
50 | 50 | * Entities, Properties, and Keys</a> |
51 | 51 | */ |
52 | | -public abstract class BaseEntity<K extends IncompleteKey> extends Serializable<DatastoreV1.Entity> { |
| 52 | +public abstract class BaseEntity<K extends IncompleteKey> |
| 53 | + extends Serializable<com.google.datastore.v1beta3.Entity> { |
53 | 54 |
|
54 | 55 | private static final long serialVersionUID = 8175618724683792766L; |
55 | 56 |
|
@@ -91,10 +92,11 @@ private B self() { |
91 | 92 | } |
92 | 93 |
|
93 | 94 | @SuppressWarnings("unchecked") |
94 | | - B fill(DatastoreV1.Entity entityPb) { |
| 95 | + B fill(com.google.datastore.v1beta3.Entity entityPb) { |
95 | 96 | Map<String, Value<?>> copiedProperties = Maps.newHashMap(); |
96 | | - for (DatastoreV1.Property property : entityPb.getPropertyList()) { |
97 | | - copiedProperties.put(property.getName(), Value.fromPb(property.getValue())); |
| 97 | + for (Map.Entry<String, com.google.datastore.v1beta3.Value> entry : |
| 98 | + entityPb.getProperties().entrySet()) { |
| 99 | + copiedProperties.put(entry.getKey(), Value.fromPb(entry.getValue())); |
98 | 100 | } |
99 | 101 | properties(copiedProperties); |
100 | 102 | if (entityPb.hasKey()) { |
@@ -290,6 +292,36 @@ public B set(String name, DateTime first, DateTime second, DateTime... others) { |
290 | 292 | return self(); |
291 | 293 | } |
292 | 294 |
|
| 295 | + /** |
| 296 | + * Sets a property of type {@link LatLng}. |
| 297 | + * |
| 298 | + * @param name name of the property |
| 299 | + * @param value value associated with the property |
| 300 | + */ |
| 301 | + public B set(String name, LatLng value) { |
| 302 | + properties.put(name, of(value)); |
| 303 | + return self(); |
| 304 | + } |
| 305 | + |
| 306 | + /** |
| 307 | + * Sets a list property containing elements of type {@link LatLng}. |
| 308 | + * |
| 309 | + * @param name name of the property |
| 310 | + * @param first the first {@link LatLng} in the list |
| 311 | + * @param second the second {@link LatLng} in the list |
| 312 | + * @param others other {@link LatLng}s in the list |
| 313 | + */ |
| 314 | + public B set(String name, LatLng first, LatLng second, LatLng... others) { |
| 315 | + List<LatLngValue> values = new LinkedList<>(); |
| 316 | + values.add(of(first)); |
| 317 | + values.add(of(second)); |
| 318 | + for (LatLng other : others) { |
| 319 | + values.add(of(other)); |
| 320 | + } |
| 321 | + properties.put(name, of(values)); |
| 322 | + return self(); |
| 323 | + } |
| 324 | + |
293 | 325 | /** |
294 | 326 | * Sets a property of type {@link KeyValue}. |
295 | 327 | * |
@@ -545,6 +577,17 @@ public DateTime getDateTime(String name) { |
545 | 577 | return ((Value<DateTime>) getValue(name)).get(); |
546 | 578 | } |
547 | 579 |
|
| 580 | + /** |
| 581 | + * Returns the property value as a LatLng. |
| 582 | + * |
| 583 | + * @throws DatastoreException if not such property. |
| 584 | + * @throws ClassCastException if value is not a LatLng. |
| 585 | + */ |
| 586 | + @SuppressWarnings("unchecked") |
| 587 | + public LatLng getLatLng(String name) { |
| 588 | + return ((Value<LatLng>) getValue(name)).get(); |
| 589 | + } |
| 590 | + |
548 | 591 | /** |
549 | 592 | * Returns the property value as a Key. |
550 | 593 | * |
@@ -603,20 +646,19 @@ ImmutableSortedMap<String, Value<?>> properties() { |
603 | 646 | @Override |
604 | 647 | Object fromPb(byte[] bytesPb) throws InvalidProtocolBufferException { |
605 | 648 | Builder<?, ?> builder = emptyBuilder(); |
606 | | - builder.fill(DatastoreV1.Entity.parseFrom(bytesPb)); |
| 649 | + builder.fill(com.google.datastore.v1beta3.Entity.parseFrom(bytesPb)); |
607 | 650 | return builder.build(); |
608 | 651 | } |
609 | 652 |
|
610 | 653 | protected abstract Builder<?, ?> emptyBuilder(); |
611 | 654 |
|
612 | 655 | @Override |
613 | | - final DatastoreV1.Entity toPb() { |
614 | | - DatastoreV1.Entity.Builder entityPb = DatastoreV1.Entity.newBuilder(); |
| 656 | + final com.google.datastore.v1beta3.Entity toPb() { |
| 657 | + com.google.datastore.v1beta3.Entity.Builder entityPb = |
| 658 | + com.google.datastore.v1beta3.Entity.newBuilder(); |
| 659 | + Map<String, com.google.datastore.v1beta3.Value> propertiesPb = entityPb.getMutableProperties(); |
615 | 660 | for (Map.Entry<String, Value<?>> entry : properties.entrySet()) { |
616 | | - DatastoreV1.Property.Builder propertyPb = DatastoreV1.Property.newBuilder(); |
617 | | - propertyPb.setName(entry.getKey()); |
618 | | - propertyPb.setValue(entry.getValue().toPb()); |
619 | | - entityPb.addProperty(propertyPb.build()); |
| 661 | + propertiesPb.put(entry.getKey(), entry.getValue().toPb()); |
620 | 662 | } |
621 | 663 | if (key != null) { |
622 | 664 | entityPb.setKey(key.toPb()); |
|
0 commit comments