Skip to content

Commit a1a5767

Browse files
BenWhiteheadkolea2
authored andcommitted
---
yaml --- r: 33761 b: refs/heads/autosynth-redis c: 67072ee h: refs/heads/master i: 33759: 5a2f569
1 parent 5923407 commit a1a5767

4 files changed

Lines changed: 686 additions & 554 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ refs/heads/autosynth-iot: 044be280805a59e06d09658688c9ee474a9815ad
135135
refs/heads/autosynth-kms: d31449d6621a50fb16a4bef4f30f0f3051d27d7c
136136
refs/heads/autosynth-language: 6130869312f99a1e7d3aa0485759172a23333cc5
137137
refs/heads/autosynth-os-login: 49028d40ac477fca5f948cc5a3ce7422729fdb67
138-
refs/heads/autosynth-redis: 3569aeae1e40d6ea28781ee6f5fef70bc99a14ee
138+
refs/heads/autosynth-redis: 67072ee910739cdd772ebdeeaee7408611ae18a8
139139
refs/heads/autosynth-scheduler: 57f9fdb1e7de30c85f4ec7198931a07f50603e55
140140
refs/heads/autosynth-spanner: de02ca32edea133b68b51052e325359a3704b5d2
141141
refs/heads/autosynth-speech: 64692f6db11364f663921be02c08072b966b6e7b
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.firestore;
18+
19+
import com.google.cloud.Timestamp;
20+
import com.google.cloud.firestore.conformance.TestDefinition;
21+
import com.google.gson.Gson;
22+
import com.google.gson.reflect.TypeToken;
23+
import java.lang.reflect.Type;
24+
import java.util.ArrayList;
25+
import java.util.List;
26+
import java.util.Map;
27+
28+
final class ConformanceConversions {
29+
30+
private static final Gson gson = new Gson();
31+
32+
private ConformanceConversions() {}
33+
34+
/** Converts a Protobuf Precondition to its API counterpart. */
35+
static Precondition convertPrecondition(com.google.firestore.v1.Precondition precondition) {
36+
switch (precondition.getConditionTypeCase()) {
37+
case EXISTS:
38+
return Precondition.exists(precondition.getExists());
39+
case UPDATE_TIME:
40+
return Precondition.updatedAt(Timestamp.fromProto(precondition.getUpdateTime()));
41+
default:
42+
return Precondition.NONE;
43+
}
44+
}
45+
46+
/** Converts a list of Proto FieldPaths to its API counterpart. */
47+
static List<FieldPath> convertPaths(List<TestDefinition.FieldPath> fieldsList) {
48+
List<FieldPath> convertedPaths = new ArrayList<>();
49+
for (TestDefinition.FieldPath fieldPath : fieldsList) {
50+
convertedPaths.add(convertPath(fieldPath));
51+
}
52+
return convertedPaths;
53+
}
54+
55+
/** Converts a Proto FieldPath to its API counterpart. */
56+
static FieldPath convertPath(TestDefinition.FieldPath fieldPath) {
57+
return FieldPath.of(fieldPath.getFieldList().toArray(new String[0]));
58+
}
59+
60+
/** Converts a JSON string into a Java Map. */
61+
static Map<String, Object> convertInput(String jsonMap) {
62+
Type type = new TypeToken<Map<String, Object>>() {}.getType();
63+
Map<String, Object> parsedData = gson.fromJson(jsonMap, type);
64+
return convertMap(parsedData);
65+
}
66+
67+
/** Converts a list of Strings into a Java Object. Parses JSON when provided. */
68+
static List<Object> convertInput(List<String> jsonValues) {
69+
List<Object> result = new ArrayList<>();
70+
for (String input : jsonValues) {
71+
if (input.matches("^\\{.*}$")) {
72+
result.add(convertInput(input));
73+
} else {
74+
// We need to "fake" a proper JSON object to let GSON convert to native types.
75+
result.add(convertInput("{a:" + input + "}").get("a"));
76+
}
77+
}
78+
return result;
79+
}
80+
81+
/** Helper function to convert test values in a nested Map to Firestore API types. */
82+
private static Map<String, Object> convertMap(Map<String, Object> parsedData) {
83+
for (Map.Entry<String, Object> entry : parsedData.entrySet()) {
84+
parsedData.put(entry.getKey(), convertValue(entry.getValue()));
85+
}
86+
return parsedData;
87+
}
88+
89+
/**
90+
* Converts test values to Firestore API types. Replaces sentinel values with their FieldValue
91+
* constants.
92+
*/
93+
@SuppressWarnings("unchecked")
94+
private static Object convertValue(Object data) {
95+
if (data instanceof Map) {
96+
return convertMap((Map<String, Object>) data);
97+
} else if (data instanceof List) {
98+
return convertArray((List<Object>) data);
99+
} else if ("NaN".equals(data)) {
100+
return Double.NaN;
101+
} else if ("Delete".equals(data)) {
102+
return FieldValue.delete();
103+
} else if ("ServerTimestamp".equals(data)) {
104+
return FieldValue.serverTimestamp();
105+
} else if (data instanceof Double
106+
&& Double.compare((double) data, Math.floor((double) data)) == 0) {
107+
return (long) (double) data;
108+
} else {
109+
return data;
110+
}
111+
}
112+
113+
/** Helper function to convert test values in a list to Firestore API types. */
114+
@SuppressWarnings("unchecked")
115+
private static Object convertArray(List<Object> list) {
116+
if (!list.isEmpty() && list.get(0).equals("ArrayUnion")) {
117+
return FieldValue.arrayUnion(
118+
((List<Object>) convertArray(list.subList(1, list.size()))).toArray());
119+
} else if (!list.isEmpty() && list.get(0).equals("ArrayRemove")) {
120+
return FieldValue.arrayRemove(
121+
((List<Object>) convertArray(list.subList(1, list.size()))).toArray());
122+
} else {
123+
for (int i = 0; i < list.size(); ++i) {
124+
list.set(i, convertValue(list.get(i)));
125+
}
126+
return list;
127+
}
128+
}
129+
}

0 commit comments

Comments
 (0)