125 lines
4.6 KiB
Rust
125 lines
4.6 KiB
Rust
use base64::engine::general_purpose::STANDARD;
|
|
use base64::Engine;
|
|
use chrono::{NaiveDateTime, Timelike};
|
|
use chrono_tz::Tz;
|
|
use serde_json::{json, Number, Value};
|
|
use taos::taos_query::common::Timestamp;
|
|
use taos::BorrowedValue;
|
|
|
|
pub fn borrowed_value_to_json(value: BorrowedValue<'_>, timezone: Option<Tz>) -> Value {
|
|
match value {
|
|
BorrowedValue::Null(_) => Value::Null,
|
|
BorrowedValue::Bool(value) => Value::Bool(value),
|
|
BorrowedValue::TinyInt(value) => json!(value),
|
|
BorrowedValue::SmallInt(value) => json!(value),
|
|
BorrowedValue::Int(value) => json!(value),
|
|
BorrowedValue::BigInt(value) => json!(value),
|
|
BorrowedValue::UTinyInt(value) => json!(value),
|
|
BorrowedValue::USmallInt(value) => json!(value),
|
|
BorrowedValue::UInt(value) => json!(value),
|
|
BorrowedValue::UBigInt(value) => json!(value),
|
|
BorrowedValue::Float(value) => finite_number(value as f64),
|
|
BorrowedValue::Double(value) => finite_number(value),
|
|
BorrowedValue::VarChar(value) => Value::String(value.to_string()),
|
|
BorrowedValue::NChar(value) => Value::String(value.into_owned()),
|
|
BorrowedValue::Timestamp(value) => Value::String(format_timestamp(value, timezone)),
|
|
BorrowedValue::Json(value) => serde_json::from_slice(&value)
|
|
.unwrap_or_else(|_| Value::String(String::from_utf8_lossy(&value).into_owned())),
|
|
BorrowedValue::VarBinary(value) | BorrowedValue::Blob(value) | BorrowedValue::MediumBlob(value) => {
|
|
binary_json(&value)
|
|
}
|
|
BorrowedValue::Geometry(value) => geometry_json(&value),
|
|
BorrowedValue::Decimal(value) => Value::String(value.to_string()),
|
|
BorrowedValue::Decimal64(value) => Value::String(value.to_string()),
|
|
}
|
|
}
|
|
|
|
fn finite_number(value: f64) -> Value {
|
|
Number::from_f64(value).map(Value::Number).unwrap_or_else(|| Value::String(value.to_string()))
|
|
}
|
|
|
|
fn binary_json(value: &[u8]) -> Value {
|
|
json!({ "$binary": STANDARD.encode(value) })
|
|
}
|
|
|
|
fn geometry_json(value: &[u8]) -> Value {
|
|
if let Ok(text) = std::str::from_utf8(value) {
|
|
let trimmed = text.trim();
|
|
if looks_like_wkt(trimmed) {
|
|
return Value::String(trimmed.to_string());
|
|
}
|
|
}
|
|
binary_json(value)
|
|
}
|
|
|
|
fn looks_like_wkt(value: &str) -> bool {
|
|
let upper = value.to_ascii_uppercase();
|
|
["POINT", "LINESTRING", "POLYGON", "MULTIPOINT", "MULTILINESTRING", "MULTIPOLYGON", "GEOMETRYCOLLECTION"]
|
|
.iter()
|
|
.any(|prefix| upper.starts_with(prefix))
|
|
}
|
|
|
|
fn format_timestamp(timestamp: Timestamp, timezone: Option<Tz>) -> String {
|
|
let datetime = match timezone {
|
|
Some(timezone) => timestamp.to_datetime_with_custom_tz(&timezone).naive_local(),
|
|
None => timestamp.to_naive_datetime(),
|
|
};
|
|
format_naive_datetime(datetime, timestamp)
|
|
}
|
|
|
|
fn format_naive_datetime(datetime: NaiveDateTime, timestamp: Timestamp) -> String {
|
|
let digits = match timestamp {
|
|
Timestamp::Milliseconds(_) => 3,
|
|
Timestamp::Microseconds(_) => 6,
|
|
Timestamp::Nanoseconds(_) => 9,
|
|
};
|
|
let mut fraction = format!("{:09}", datetime.nanosecond());
|
|
fraction.truncate(digits);
|
|
while fraction.len() > 3 && fraction.ends_with('0') {
|
|
fraction.pop();
|
|
}
|
|
format!("{}.{}", datetime.format("%Y-%m-%d %H:%M:%S"), fraction)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use taos::Ty;
|
|
|
|
#[test]
|
|
fn preserves_timestamp_precision_with_at_least_milliseconds() {
|
|
assert_eq!(
|
|
borrowed_value_to_json(
|
|
BorrowedValue::Timestamp(Timestamp::Milliseconds(1_704_067_200_123)),
|
|
Some(chrono_tz::UTC)
|
|
),
|
|
Value::String("2024-01-01 00:00:00.123".into())
|
|
);
|
|
assert_eq!(
|
|
borrowed_value_to_json(
|
|
BorrowedValue::Timestamp(Timestamp::Microseconds(1_704_067_200_123_400)),
|
|
Some(chrono_tz::UTC)
|
|
),
|
|
Value::String("2024-01-01 00:00:00.1234".into())
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn keeps_text_and_binary_values_distinct() {
|
|
assert_eq!(borrowed_value_to_json(BorrowedValue::VarChar("hello"), None), json!("hello"));
|
|
assert_eq!(
|
|
borrowed_value_to_json(BorrowedValue::VarBinary(vec![0, 255].into()), None),
|
|
json!({"$binary": "AP8="})
|
|
);
|
|
assert_eq!(borrowed_value_to_json(BorrowedValue::Null(Ty::Int), None), Value::Null);
|
|
}
|
|
|
|
#[test]
|
|
fn returns_wkt_geometry_as_text() {
|
|
assert_eq!(
|
|
borrowed_value_to_json(BorrowedValue::Geometry(b"POINT (1 2)".as_slice().into()), None),
|
|
json!("POINT (1 2)")
|
|
);
|
|
}
|
|
}
|