Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't auto-convert to bytes #11

Merged
merged 2 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions crates/cmds-pdg/src/gen_pdg_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,26 @@ async fn run(_: Context, input: Input) -> Result<Output, CommandError> {

Ok(Output { attributes })
}

#[cfg(test)]
mod tests {
use super::*;
use flow_lib::value;

#[tokio::test]
async fn test_generate() {
let output = build()
.unwrap()
.run(
<_>::default(),
value::map! {
"flag" => "base",
},
)
.await
.unwrap();
let attrs = &output["attributes"];
let pose = value::crud::get(attrs, &["Pose", "value"]).unwrap();
assert!(matches!(pose, flow_lib::Value::Array(_)));
}
}
7 changes: 4 additions & 3 deletions lib/flow-value/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ where
/// use solana_sdk::signature::Signature;
/// use flow_value::Value;
///
/// let val = flow_value::to_value(&Signature::new_unique()).unwrap();
/// assert!(matches!(val, Value::B64(_)));
/// let signature = Signature::new_unique();
/// let value = flow_value::to_value(&flow_value::Bytes(signature.as_ref())).unwrap();
/// assert_eq!(value, Value::B64(signature.into()));
/// ```
pub fn to_value<T>(t: &T) -> Result<Value, Error>
where
Expand Down Expand Up @@ -729,7 +730,7 @@ impl serde::de::Error for Error {
}

// default implementation of [u8] doesn't call serialize_bytes
pub(crate) struct Bytes<'a>(&'a [u8]);
pub struct Bytes<'a>(pub &'a [u8]);

impl<'a> serde::Serialize for Bytes<'a> {
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
Expand Down
14 changes: 7 additions & 7 deletions lib/flow-value/src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ impl serde::Serializer for Serializer {
type Ok = Value;
type Error = Error;

type SerializeSeq = SerializeSeq;
type SerializeTuple = SerializeSeq;
type SerializeTupleStruct = SerializeSeq;
type SerializeSeq = SerializeSeqNoBytes;
type SerializeTuple = SerializeSeqNoBytes;
type SerializeTupleStruct = SerializeSeqNoBytes;
type SerializeTupleVariant = SerializeTupleVariant;
type SerializeMap = SerializeMap;
type SerializeStruct = SerializeMap;
Expand Down Expand Up @@ -170,7 +170,7 @@ impl serde::Serializer for Serializer {
}

fn serialize_seq(self, _: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
Ok(SerializeSeq::new())
Ok(SerializeSeqNoBytes::default())
}

fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, Self::Error> {
Expand Down Expand Up @@ -308,11 +308,11 @@ mod tests {
assert_eq!(s(Option::<()>::None), Value::Null);
assert_eq!(s(()), Value::Null);
assert_eq!(s("end"), Value::String("end".to_owned()));
assert_eq!(s([1i32]), Value::Array(vec![Value::I64(1)]));
assert_eq!(
s((1i32, -2i32, "hello")),
Value::Array(vec![
// SerializerSeq does not retain the original type
Value::U64(1),
Value::I64(1),
Value::I64(-2),
Value::String("hello".to_owned())
])
Expand All @@ -323,7 +323,7 @@ mod tests {
s(HashMap::from([("a".to_owned(), -1i32)])),
Value::Map(Map::from([("a".to_owned(), Value::I64(-1))]))
);
assert_eq!(s([1u8; 32]), Value::B32([1; 32]));
// assert_eq!(s([1u8; 32]), Value::B32([1; 32]));
assert_eq!(s(crate::Bytes(&[2u8; 64])), Value::B64([2; 64]));
}

Expand Down
32 changes: 32 additions & 0 deletions lib/flow-value/src/ser/seq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,3 +312,35 @@ impl serde::Serializer for SerializeSeqNoBytes {
Err(Error::ExpectedArray)
}
}

impl serde::ser::SerializeTuple for SerializeSeqNoBytes {
type Ok = Value;
type Error = Error;

fn serialize_element<T>(&mut self, value: &T) -> Result<(), Error>
where
T: ?Sized + serde::Serialize,
{
serde::ser::SerializeSeq::serialize_element(self, value)
}

fn end(self) -> Result<Value, Error> {
serde::ser::SerializeSeq::end(self)
}
}

impl serde::ser::SerializeTupleStruct for SerializeSeqNoBytes {
type Ok = Value;
type Error = Error;

fn serialize_field<T>(&mut self, value: &T) -> Result<(), Error>
where
T: ?Sized + serde::Serialize,
{
serde::ser::SerializeSeq::serialize_element(self, value)
}

fn end(self) -> Result<Value, Error> {
serde::ser::SerializeSeq::end(self)
}
}
Loading