coerceVector function
Coerce a SQL value to a Vector. Accepts:
- a BLOB (
List<int>) in encodeVectorBlob layout, - a TEXT JSON-array literal, or
- an already-decoded Vector.
Returns null when v is null; throws FormatException on unusable
input so scalar functions surface the error to the caller.
Implementation
Vector? coerceVector(Object? v) {
if (v == null) return null;
if (v is Vector) return v;
if (v is List<int>) return decodeVectorBlob(v);
if (v is String) return parseVectorText(v);
if (v is List) {
// Generic Dart list (e.g. from JSON persistence round-trip).
final nums = <num>[];
for (final e in v) {
if (e is num) {
nums.add(e);
} else {
throw FormatException('vector element not numeric: $e');
}
}
return Vector.fromList(nums);
}
throw FormatException(
'cannot coerce ${v.runtimeType} to vector; expected BLOB, TEXT, or List',
);
}