addTursoReservedDatabase function
Adds logicalName to reservedDatabases of a turso.Functions.turso /
turso.Functions.tursoToken call call (whose argument is an object literal).
call(引数がオブジェクトリテラルのturso.Functions.turso /
turso.Functions.tursoToken呼び出し)のreservedDatabasesにlogicalNameを追加します。
Implementation
String addTursoReservedDatabase(String call, String logicalName) {
final existing =
RegExp(r"reservedDatabases\s*:\s*\[([^\]]*)\]").firstMatch(call);
if (existing != null) {
final names = RegExp(r'''["']([^"']+)["']''')
.allMatches(existing.group(1)!)
.map((match) => match.group(1)!)
.toList();
if (names.contains(logicalName)) {
return call;
}
final value = "reservedDatabases: ${jsonEncode([...names, logicalName])}";
return call.replaceRange(existing.start, existing.end, value);
}
if (RegExp(r"\breservedDatabases\b").hasMatch(call)) {
// A non-literal value is owned by the application.
return call;
}
final open = call.indexOf("(");
final objectStart = call.indexOf("{", open);
if (objectStart < 0) {
return call;
}
return call.replaceRange(objectStart + 1, objectStart + 1,
"\n reservedDatabases: ${jsonEncode([logicalName])},");
}