Skip to content

Commit

Permalink
misc tweaks (encoredev#1594)
Browse files Browse the repository at this point in the history
- runtimes/core: fix enum row parsing
- runtimes/js: fix date decoding in ToNapiValue
  • Loading branch information
eandre authored Nov 25, 2024
1 parent b23efc8 commit a1c5114
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
48 changes: 27 additions & 21 deletions runtimes/core/src/sqldb/val.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::Context;
use bytes::BytesMut;
use std::error::Error;
use tokio_postgres::types::{FromSql, IsNull, ToSql, Type};
use tokio_postgres::types::{FromSql, IsNull, Kind, ToSql, Type};
use uuid::Uuid;

#[derive(Debug)]
Expand Down Expand Up @@ -240,6 +240,7 @@ impl<'a> FromSql<'a> for RowValue {
let val: String = FromSql::from_sql(ty, raw)?;
Self::Json(serde_json::Value::String(val))
}

Type::INT2 => {
let val: i16 = FromSql::from_sql(ty, raw)?;
Self::Json(serde_json::Value::Number(serde_json::Number::from(val)))
Expand Down Expand Up @@ -303,7 +304,12 @@ impl<'a> FromSql<'a> for RowValue {
}

_ => {
return Err(format!("unsupported type: {:?}", ty).into());
if let Kind::Enum(_) = ty.kind() {
let val = std::str::from_utf8(raw)?;
Self::Json(serde_json::Value::String(val.to_string()))
} else {
return Err(format!("unsupported type: {:?}", ty).into());
}
}
})
}
Expand All @@ -313,25 +319,25 @@ impl<'a> FromSql<'a> for RowValue {
}

fn accepts(ty: &Type) -> bool {
matches!(
*ty,
match *ty {
Type::BOOL
| Type::BYTEA
| Type::TEXT
| Type::VARCHAR
| Type::INT2
| Type::INT4
| Type::INT8
| Type::OID
| Type::JSONB
| Type::JSON
| Type::UUID
| Type::FLOAT4
| Type::FLOAT8
| Type::TIMESTAMP
| Type::TIMESTAMPTZ
| Type::DATE
| Type::TIME
)
| Type::BYTEA
| Type::TEXT
| Type::VARCHAR
| Type::INT2
| Type::INT4
| Type::INT8
| Type::OID
| Type::JSONB
| Type::JSON
| Type::UUID
| Type::FLOAT4
| Type::FLOAT8
| Type::TIMESTAMP
| Type::TIMESTAMPTZ
| Type::DATE
| Type::TIME => true,
ref other => matches!(other.kind(), Kind::Enum(_)),
}
}
}
4 changes: 2 additions & 2 deletions runtimes/js/src/pvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl ToNapiValue for PVal {
PValue::Object(obj) => unsafe { ToNapiValue::to_napi_value(env, PVals(obj)) },
PValue::DateTime(dt) => {
let env2 = Env::from_raw(env);
let ts = dt.timestamp();
let ts = dt.timestamp_millis();
let dt = env2.create_date(ts as f64)?;
JsDate::to_napi_value(env, dt)
}
Expand Down Expand Up @@ -100,7 +100,7 @@ impl ToNapiValue for &PVal {
},
PValue::DateTime(dt) => {
let env2 = Env::from_raw(env);
let ts = dt.timestamp();
let ts = dt.timestamp_millis();
let dt = env2.create_date(ts as f64)?;
JsDate::to_napi_value(env, dt)
}
Expand Down

0 comments on commit a1c5114

Please sign in to comment.