Skip to content

Commit

Permalink
Format binary data as hex in sql/satn
Browse files Browse the repository at this point in the history
  • Loading branch information
mamcx committed Nov 3, 2023
1 parent 593702c commit 4679945
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 11 deletions.
46 changes: 46 additions & 0 deletions crates/core/src/control_db/tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::str::FromStr;

use crate::messages::control_db::HostType;
use once_cell::sync::Lazy;
use spacetimedb_lib::error::ResultTest;
use spacetimedb_lib::Hash;
use tempfile::TempDir;

use super::*;
Expand Down Expand Up @@ -82,3 +85,46 @@ fn test_domain() -> anyhow::Result<()> {

Ok(())
}

#[test]
fn test_decode() -> ResultTest<()> {
let path = TempDir::with_prefix("decode")?;

let cdb = ControlDb::at(path)?;

let id = cdb.alloc_spacetime_identity()?;

let db = Database {
id: 0,
address: Default::default(),
identity: id,
host_type: HostType::Wasmer,
num_replicas: 0,
program_bytes_address: Hash::ZERO,
publisher_address: Some(Address::zero()),
};

let id = cdb.insert_database(db.clone())?;

let dbs = cdb.get_databases()?;

assert_eq!(dbs.len(), 1);
assert_eq!(dbs[0].id, id);

let mut new_database_instance = DatabaseInstance {
id: 0,
database_id: 1,
node_id: 0,
leader: true,
};

let id = cdb.insert_database_instance(new_database_instance.clone())?;
new_database_instance.id = id;

let dbs = cdb.get_database_instances()?;

assert_eq!(dbs.len(), 1);
assert_eq!(dbs[0].id, id);

Ok(())
}
32 changes: 31 additions & 1 deletion crates/lib/tests/serde.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use spacetimedb_lib::de::serde::SerdeDeserializer;
use spacetimedb_lib::de::DeserializeSeed;
use spacetimedb_lib::{AlgebraicType, ProductType, ProductTypeElement, ProductValue, SumType};
use spacetimedb_lib::{AlgebraicType, Identity, ProductType, ProductTypeElement, ProductValue, SumType};
use spacetimedb_sats::algebraic_value::de::ValueDeserializer;
use spacetimedb_sats::algebraic_value::ser::ValueSerializer;
use spacetimedb_sats::ser::Serialize;
use spacetimedb_sats::{satn::Satn, SumTypeVariant, Typespace, WithTypespace};

macro_rules! de_json_snapshot {
Expand All @@ -15,6 +18,33 @@ macro_rules! de_json_snapshot {
};
}

#[derive(
Debug,
PartialEq,
spacetimedb_sats::de::Deserialize,
spacetimedb_sats::ser::Serialize,
serde::Serialize,
serde::Deserialize,
)]
struct Sample {
identity: Identity,
}

#[test]
fn test_roundtrip() {
let original = Sample {
identity: Identity::__dummy(),
};

let s = original.serialize(ValueSerializer).unwrap();
let result: Sample = spacetimedb_sats::de::Deserialize::deserialize(ValueDeserializer::new(s)).unwrap();
assert_eq!(&original, &result);

let s = serde_json::ser::to_string(&original).unwrap();
let result: Sample = serde_json::from_str(&s).unwrap();
assert_eq!(&original, &result);
}

#[test]
fn test_json_mappings() {
let schema = tuple([
Expand Down
2 changes: 1 addition & 1 deletion crates/lib/tests/snapshots/serde__json_mappings-2.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ expression: "de_json({\n \"foo\": 5654,\n \"bar\": [1, 15, 44],\n \"baz
---
(
foo = 5654,
bar = [1, 15, 44],
bar = 010f2c,
baz = [
"it's 🥶°C",
],
Expand Down
4 changes: 2 additions & 2 deletions crates/lib/tests/snapshots/serde__json_mappings.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ expression: "de_json({\n \"foo\": 42,\n \"bar\": \"404040FFFF0A48656C6C6F\
---
(
foo = 42,
bar = [64, 64, 64, 255, 255, 10, 72, 101, 108, 108, 111],
bar = 404040ffff0a48656c6c6f,
baz = [
"heyyyyyy",
"hooo",
],
quux = (
Hash = [84, 163, 230, 210, 176, 149, 157, 234, 172, 241, 2, 41, 43, 28, 189, 111, 203, 184, 207, 35, 127, 115, 48, 110, 39, 237, 130, 195, 21, 56, 120, 170],
Hash = 54a3e6d2b0959deaacf102292b1cbd6fcbb8cf237f73306e27ed82c3153878aa,
),
and_peggy = (
some = 3.141592653589793,
Expand Down
4 changes: 2 additions & 2 deletions crates/sats/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ description = "Spacetime Algebraic Type Notation"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
serde = ["dep:serde", "hex"]
serde = ["dep:serde"]

[dependencies]
spacetimedb-bindings-macro = { path = "../bindings-macro", version = "0.7.2" }
Expand All @@ -18,7 +18,7 @@ arrayvec.workspace = true
decorum.workspace = true
derive_more.workspace = true
enum-as-inner.workspace = true
hex = { workspace = true, optional = true }
hex.workspace = true
itertools.workspace = true
nonempty.workspace = true
serde = { workspace = true, optional = true }
Expand Down
2 changes: 1 addition & 1 deletion crates/sats/src/algebraic_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ mod tests {
let array = AlgebraicType::array(AlgebraicType::U8);
let value = AlgebraicValue::Array([3u8].into());
let typespace = Typespace::new(vec![]);
assert_eq!(in_space(&typespace, &array, &value).to_satn(), "[3]");
assert_eq!(in_space(&typespace, &array, &value).to_satn(), "03");
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion crates/sats/src/satn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ impl<'a, 'b> ser::Serializer for SatnFormatter<'a, 'b> {
}

fn serialize_bytes(mut self, v: &[u8]) -> Result<Self::Ok, Self::Error> {
write!(self, "{:?}", v)
write!(self, "{}", hex::encode(v))
}

fn serialize_array(mut self, _len: usize) -> Result<Self::SerializeArray, Self::Error> {
Expand Down
6 changes: 3 additions & 3 deletions test/tests/sql-format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,6 @@ run_test cargo run call "$ADDRESS" test
run_test cargo run sql "$ADDRESS" "SELECT * FROM BuiltIn"

[ "$(cat "$TEST_OUT" | tail -n 3)" == \
' a_b | a_i8 | a_i16 | a_i32 | a_i64 | a_i128 | a_u8 | a_u16 | a_u32 | a_u64 | a_u128 | a_f32 | a_f64 | a_str | a_bytes | a_tuple '$'\n'\
'------+------+-------+--------+----------+---------------+------+-------+-------+----------+---------------+-----------+--------------------+---------------------+-----------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------'$'\n'\
' true | -25 | -3224 | -23443 | -2344353 | -234434897853 | 105 | 1050 | 83892 | 48937498 | 4378528978889 | 594806.56 | -3454353.345389043 | This is spacetimedb | [1, 2, 3, 4, 5, 6, 7] | (a_b = true, a_i8 = -25, a_i16 = -3224, a_i32 = -23443, a_i64 = -2344353, a_i128 = -234434897853, a_u8 = 105, a_u16 = 1050, a_u32 = 83892, a_u64 = 48937498, a_u128 = 4378528978889, a_f32 = 594806.56, a_f64 = -3454353.345389043, a_str = "This is spacetimedb", a_bytes = [1, 2, 3, 4, 5, 6, 7]) ' ]
' a_b | a_i8 | a_i16 | a_i32 | a_i64 | a_i128 | a_u8 | a_u16 | a_u32 | a_u64 | a_u128 | a_f32 | a_f64 | a_str | a_bytes | a_tuple '$'\n'\
'------+------+-------+--------+----------+---------------+------+-------+-------+----------+---------------+-----------+--------------------+---------------------+----------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------'$'\n'\
' true | -25 | -3224 | -23443 | -2344353 | -234434897853 | 105 | 1050 | 83892 | 48937498 | 4378528978889 | 594806.56 | -3454353.345389043 | This is spacetimedb | 01020304050607 | (a_b = true, a_i8 = -25, a_i16 = -3224, a_i32 = -23443, a_i64 = -2344353, a_i128 = -234434897853, a_u8 = 105, a_u16 = 1050, a_u32 = 83892, a_u64 = 48937498, a_u128 = 4378528978889, a_f32 = 594806.56, a_f64 = -3454353.345389043, a_str = "This is spacetimedb", a_bytes = 01020304050607) ' ]

0 comments on commit 4679945

Please sign in to comment.