Remove lossy query-parameter (qs) parse to Bool. We really can't now if "TRUE" was meant as a string or a boolean until we look at the data-type.

The `qs::Value::Double(_)` is technically lossy as well put one step at a time.
This commit is contained in:
Sebastian Jeltsch
2025-10-09 13:25:23 +02:00
parent c42d35ca5d
commit 4cfffe1901
4 changed files with 2 additions and 24 deletions

View File

@@ -37,7 +37,6 @@ pub(crate) fn qs_value_to_sql(value: trailbase_qs::Value) -> rusqlite::types::Va
}
QsValue::Integer(i) => Value::Integer(i),
QsValue::Double(d) => Value::Real(d),
QsValue::Bool(b) => Value::Integer(if b { 1 } else { 0 }),
};
}
@@ -64,13 +63,6 @@ pub(crate) fn qs_value_to_sql_with_constraints(
QsValue::String(s) => Ok(Value::Text(s)),
QsValue::Integer(i) => Ok(Value::Text(i.to_string())),
QsValue::Double(d) => Ok(Value::Text(d.to_string())),
// TODO: This is broken because we don't preserve the user's original input. We parse and
// best-effort unparse.
QsValue::Bool(b) => Ok(Value::Text(if b {
"true".to_string()
} else {
"false".to_string()
})),
},
ColumnDataType::Integer | ColumnDataType::Int => match value {
QsValue::Integer(i) => Ok(Value::Integer(i)),

View File

@@ -129,7 +129,7 @@ where
serde_value::Value::U32(i) => Ok(Value::Integer(i as i64)),
serde_value::Value::U16(i) => Ok(Value::Integer(i as i64)),
serde_value::Value::U8(i) => Ok(Value::Integer(i as i64)),
serde_value::Value::Bool(b) => Ok(Value::Bool(b)),
serde_value::Value::Bool(b) => Ok(Value::Integer(if b { 1 } else { 0 })),
_ => Err(Error::invalid_type(
unexpected(&value),
&"trailbase_qs::Value, i.e. string, integer, double or bool",

View File

@@ -19,7 +19,6 @@ where
let value = Value::deserialize(deserializer)?;
match value {
Value::String(ref str) => {
// NOTE: Unlike for Value, we also parse '1' and '0'.
match str.as_str() {
"TRUE" | "true" | "1" => {
return Ok(Some(true));

View File

@@ -7,22 +7,10 @@ pub enum Value {
String(String),
Integer(i64),
Double(f64),
Bool(bool),
}
impl Value {
pub(crate) fn unparse(value: String) -> Self {
// NOTE: We don't parse '1' and '0'. since we would prefer to parse those as integers.
match value.as_str() {
"TRUE" | "true" => {
return Value::Bool(true);
}
"FALSE" | "false" => {
return Value::Bool(false);
}
_ => {}
};
return if let Ok(i) = i64::from_str(&value) {
Value::Integer(i)
} else if let Ok(d) = f64::from_str(&value) {
@@ -39,7 +27,6 @@ impl std::fmt::Display for Value {
Self::String(s) => s.fmt(f),
Self::Integer(i) => i.fmt(f),
Self::Double(d) => d.fmt(f),
Self::Bool(b) => b.fmt(f),
};
}
}
@@ -83,7 +70,7 @@ mod tests {
ValueOrComposite::Value(ColumnOpValue {
column: "col0".to_string(),
op: CompareOp::NotEqual,
value: Value::Bool(true),
value: Value::String("TRUE".to_string()),
})
);