in functions/firebase/firestore-reactive/src/main/java/functions/FirebaseFirestoreReactive.java [54:96]
public void accept(String json, Context context) {
// Get the recently-written value
JsonObject body = gson.fromJson(json, JsonObject.class);
JsonObject tempJson = body.getAsJsonObject("value");
// Verify that value.fields.original.stringValue exists
String currentValue = null;
if (tempJson != null) {
tempJson = tempJson.getAsJsonObject("fields");
}
if (tempJson != null) {
tempJson = tempJson.getAsJsonObject("original");
}
if (tempJson != null && tempJson.has("stringValue")) {
currentValue = tempJson.get("stringValue").getAsString();
}
if (currentValue == null) {
throw new IllegalArgumentException("Malformed JSON: " + json);
}
// Convert recently-written value to ALL CAPS
String newValue = currentValue.toUpperCase(Locale.getDefault());
// Update Firestore DB with ALL CAPS value
Map<String, String> newFields = Map.of("original", newValue);
String affectedDoc = context.resource().split("/documents/")[1].replace("\"", "");
if (!currentValue.equals(newValue)) {
// The stored value needs to be updated
// Write the upper-cased value to Firestore
logger.info(String.format("Replacing value: %s --> %s", currentValue, newValue));
try {
FIRESTORE.document(affectedDoc).set(newFields, SetOptions.merge()).get();
} catch (ExecutionException | InterruptedException e) {
logger.log(Level.SEVERE, "Error updating Firestore document: " + e.getMessage(), e);
}
} else {
// The stored value is already upper-case, and doesn't need updating.
// (Don't perform a "second" write, since that could trigger an infinite loop.)
logger.info(String.format("Value is already upper-case."));
}
}