|
| 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