updateD1VectorBinding function
環境別bindingとcronを追加する。既存cronは保持する。
Implementation
String updateD1VectorBinding(String source,
{required String binding, required String name}) {
final pattern = RegExp(r'"vectorize"\s*:\s*(\[[\s\S]*?\])');
final match = pattern.firstMatch(source);
final entries =
match == null ? <dynamic>[] : jsonDecode(match.group(1)!) as List;
entries.removeWhere((e) => e["binding"] == binding);
entries.add({"binding": binding, "index_name": name});
final property = '"vectorize":${jsonEncode(entries)}';
source = match == null
? source.replaceFirst("{", "{$property,")
: source.replaceRange(match.start, match.end, property);
final triggers = RegExp(r'"triggers"\s*:\s*(\{[^}]*\})').firstMatch(source);
final value = triggers == null
? <String, dynamic>{}
: Map<String, dynamic>.from(jsonDecode(triggers.group(1)!) as Map);
final crons = List<String>.from(value["crons"] as List? ?? []);
if (!crons.contains("* * * * *")) {
crons.add("* * * * *");
}
value["crons"] = crons;
final triggerProperty = '"triggers":${jsonEncode(value)}';
return triggers == null
? source.replaceFirst("{", "{$triggerProperty,")
: source.replaceRange(triggers.start, triggers.end, triggerProperty);
}