fix: fix Polygon serialization ()

Co-authored-by: Tobie Morgan Hitchcock <tobie@surrealdb.com>
This commit is contained in:
David Bottiau 2024-06-06 13:37:14 +02:00 committed by GitHub
parent d38bf4e386
commit 57b488f7b2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 41 additions and 70 deletions
core/src
rpc/format/cbor
sql
syn/parser

View file

@ -227,7 +227,7 @@ impl TryFrom<Cbor> for Value {
_ => Err("Expected a CBOR array with Geometry Point values"),
},
TAG_GEOMETRY_POLYGON => match v.deref() {
Data::Array(v) if v.len() >= 2 => {
Data::Array(v) if !v.is_empty() => {
let lines = v
.iter()
.map(|v| match Value::try_from(Cbor(v.clone()))? {
@ -236,19 +236,20 @@ impl TryFrom<Cbor> for Value {
})
.collect::<Result<Vec<LineString>, &str>>()?;
let first = match lines.first() {
let exterior = match lines.first() {
Some(v) => v,
_ => return Err(
"Expected a CBOR array with at least two Geometry Line values",
"Expected a CBOR array with at least one Geometry Line values",
),
};
let interiors = Vec::from(&lines[1..]);
Ok(Value::Geometry(Geometry::Polygon(Polygon::new(
first.clone(),
Vec::from(&lines[1..]),
exterior.clone(),
interiors,
))))
}
_ => Err("Expected a CBOR array with at least two Geometry Line values"),
_ => Err("Expected a CBOR array with at least one Geometry Line values"),
},
TAG_GEOMETRY_MULTIPOINT => match v.deref() {
Data::Array(v) => {

View file

@ -440,32 +440,20 @@ impl fmt::Display for Geometry {
),
Self::Polygon(v) => write!(
f,
"{{ type: 'Polygon', coordinates: [[{}]{}] }}",
Fmt::comma_separated(v.exterior().points().map(|v| Fmt::new(v, |v, f| write!(
f,
"[{}, {}]",
v.x(),
v.y()
)))),
Fmt::new(v.interiors(), |interiors, f| {
match interiors.len() {
0 => Ok(()),
_ => write!(
"{{ type: 'Polygon', coordinates: [{}] }}",
Fmt::comma_separated(once(v.exterior()).chain(v.interiors()).map(|v| Fmt::new(
v,
|v, f| write!(
f,
"[{}]",
Fmt::comma_separated(v.points().map(|v| Fmt::new(v, |v, f| write!(
f,
", [{}]",
Fmt::comma_separated(interiors.iter().map(|i| Fmt::new(i, |i, f| {
write!(
f,
"[{}]",
Fmt::comma_separated(i.points().map(|v| Fmt::new(
v,
|v, f| write!(f, "[{}, {}]", v.x(), v.y())
)))
)
})))
),
}
})
"[{}, {}]",
v.x(),
v.y()
))))
)
)))
),
Self::MultiPoint(v) => {
write!(
@ -493,46 +481,28 @@ impl fmt::Display for Geometry {
))))
))))
),
Self::MultiPolygon(v) => write!(
f,
"{{ type: 'MultiPolygon', coordinates: [{}] }}",
Fmt::comma_separated(v.iter().map(|v| Fmt::new(v, |v, f| {
write!(
f,
"[[{}]{}]",
Fmt::comma_separated(
v.exterior().points().map(|v| Fmt::new(v, |v, f| write!(
f,
"[{}, {}]",
v.x(),
v.y()
)))
),
Fmt::new(v.interiors(), |interiors, f| {
match interiors.len() {
0 => Ok(()),
_ => write!(
Self::MultiPolygon(v) => {
write!(
f,
"{{ type: 'MultiPolygon', coordinates: [{}] }}",
Fmt::comma_separated(v.iter().map(|v| Fmt::new(v, |v, f| {
write!(
f,
"[{}]",
Fmt::comma_separated(once(v.exterior()).chain(v.interiors()).map(
|v| Fmt::new(v, |v, f| write!(
f,
", [{}]",
Fmt::comma_separated(interiors.iter().map(|i| Fmt::new(
i,
|i, f| {
write!(
f,
"[{}]",
Fmt::comma_separated(i.points().map(|v| Fmt::new(
v,
|v, f| write!(f, "[{}, {}]", v.x(), v.y())
)))
)
}
"[{}]",
Fmt::comma_separated(v.points().map(|v| Fmt::new(
v,
|v, f| write!(f, "[{}, {}]", v.x(), v.y())
)))
),
}
})
)
}))),
),
))
))
)
}))),
)
}
Self::Collection(v) => {
write!(
f,

View file

@ -772,6 +772,6 @@ mod tests {
}"#;
let out = Value::parse(sql);
assert!(matches!(out, Value::Geometry(_)));
assert_eq!("{ type: 'Polygon', coordinates: [[[-0.38314819, 51.37692386], [0.1785278, 51.37692386], [0.1785278, 51.6146057], [-0.38314819, 51.6146057], [-0.38314819, 51.37692386]], [[[-0.38314819, 51.37692386], [0.1785278, 51.37692386], [0.1785278, 51.6146057], [-0.38314819, 51.6146057], [-0.38314819, 51.37692386]]]] }", format!("{}", out));
assert_eq!("{ type: 'Polygon', coordinates: [[[-0.38314819, 51.37692386], [0.1785278, 51.37692386], [0.1785278, 51.6146057], [-0.38314819, 51.6146057], [-0.38314819, 51.37692386]], [[-0.38314819, 51.37692386], [0.1785278, 51.37692386], [0.1785278, 51.6146057], [-0.38314819, 51.6146057], [-0.38314819, 51.37692386]]] }", format!("{}", out));
}
}