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

Nulls should be parsed as ScStatic::Void, not ScObject(None) #343

Merged
merged 6 commits into from
Jan 23, 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
17 changes: 12 additions & 5 deletions cmd/soroban-cli/src/strval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use std::str::FromStr;

use soroban_env_host::xdr::{
AccountId, BytesM, Error as XdrError, PublicKey, ScMap, ScMapEntry, ScObject, ScSpecEntry,
ScSpecFunctionV0, ScSpecTypeDef, ScSpecTypeMap, ScSpecTypeTuple, ScSpecTypeUdt,
ScSpecUdtEnumV0, ScSpecUdtStructV0, ScSpecUdtUnionV0, ScStatic, ScVal, ScVec, StringM, Uint256,
VecM,
ScSpecFunctionV0, ScSpecTypeDef, ScSpecTypeMap, ScSpecTypeOption, ScSpecTypeTuple,
ScSpecTypeUdt, ScSpecUdtEnumV0, ScSpecUdtStructV0, ScSpecUdtUnionV0, ScStatic, ScVal, ScVec,
StringM, Uint256, VecM,
};

use stellar_strkey::ed25519;
Expand Down Expand Up @@ -87,6 +87,14 @@ impl Spec {
/// Might return errors
#[allow(clippy::wrong_self_convention)]
pub fn from_string(&self, s: &str, t: &ScSpecTypeDef) -> Result<ScVal, Error> {
if let ScSpecTypeDef::Option(b) = t {
if s == "null" {
return Ok(ScVal::Static(ScStatic::Void));
}
let ScSpecTypeOption { value_type } = b.as_ref().clone();
let v = value_type.as_ref().clone();
return self.from_string(s, &v);
}
// Parse as string and for special types assume Value::String
serde_json::from_str(s)
.or_else(|e| match t {
Expand Down Expand Up @@ -145,8 +153,7 @@ impl Spec {
(ScSpecTypeDef::Map(map), Value::Object(raw)) => self.parse_map(map, raw)?,

// Option parsing
// is null -> void the right thing here?
(ScSpecTypeDef::Option(_), Value::Null) => ScVal::Object(None),
(ScSpecTypeDef::Option(_), Value::Null) => ScVal::Static(ScStatic::Void),
(ScSpecTypeDef::Option(elem), v) => ScVal::Object(Some(
self.from_json(v, &elem.value_type)?
.try_into()
Expand Down
41 changes: 40 additions & 1 deletion cmd/soroban-cli/tests/it/arg_parsing.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::util::CUSTOM_TYPES;
use serde_json::json;
use soroban_cli::strval::{self, Spec};
use soroban_env_host::xdr::{ScSpecTypeDef, ScSpecTypeUdt};
use soroban_env_host::xdr::{ScSpecTypeDef, ScSpecTypeOption, ScSpecTypeUdt, ScStatic, ScVal};

#[test]
fn parse_bool() {
Expand All @@ -11,6 +11,19 @@ fn parse_bool() {
);
}

#[test]
fn parse_null() {
let parsed = strval::from_string_primitive(
"null",
&ScSpecTypeDef::Option(Box::new(ScSpecTypeOption {
value_type: Box::new(ScSpecTypeDef::Bool),
})),
)
.unwrap();
println!("{parsed:#?}");
assert!(parsed == ScVal::Static(ScStatic::Void));
}

#[test]
fn parse_u32() {
let u32_ = 42u32;
Expand Down Expand Up @@ -63,6 +76,32 @@ fn parse_symbol_with_no_quotation_marks() {
);
}

#[test]
fn parse_optional_symbol_with_no_quotation_marks() {
let parsed = strval::from_string_primitive(
"hello",
&ScSpecTypeDef::Option(Box::new(ScSpecTypeOption {
value_type: Box::new(ScSpecTypeDef::Symbol),
})),
)
.unwrap();
println!("{parsed:#?}");
assert!(parsed == ScVal::Symbol("hello".try_into().unwrap()));
}

#[test]
fn parse_optional_bool_with_no_quotation_marks() {
let parsed = strval::from_string_primitive(
"true",
&ScSpecTypeDef::Option(Box::new(ScSpecTypeOption {
value_type: Box::new(ScSpecTypeDef::Bool),
})),
)
.unwrap();
println!("{parsed:#?}");
assert!(parsed == ScVal::Static(ScStatic::True));
}

#[test]
fn parse_obj() {
let type_ = &ScSpecTypeDef::Udt(ScSpecTypeUdt {
Expand Down