From 85d05d4ec4650c5645e72eaa10b110b192b5b12e Mon Sep 17 00:00:00 2001 From: Paul Bellamy Date: Fri, 20 Jan 2023 17:06:29 +0000 Subject: [PATCH 1/5] Nulls should be parsed as ScStatic::Void, not ScObject(None) --- cmd/soroban-cli/src/strval.rs | 3 +-- cmd/soroban-cli/tests/it/arg_parsing.rs | 9 ++++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/cmd/soroban-cli/src/strval.rs b/cmd/soroban-cli/src/strval.rs index 8b7129172..683e7fe32 100644 --- a/cmd/soroban-cli/src/strval.rs +++ b/cmd/soroban-cli/src/strval.rs @@ -145,8 +145,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() diff --git a/cmd/soroban-cli/tests/it/arg_parsing.rs b/cmd/soroban-cli/tests/it/arg_parsing.rs index 64300588e..18c355c85 100644 --- a/cmd/soroban-cli/tests/it/arg_parsing.rs +++ b/cmd/soroban-cli/tests/it/arg_parsing.rs @@ -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::{ScVal, ScStatic, ScSpecTypeDef, ScSpecTypeOption, ScSpecTypeUdt}; #[test] fn parse_bool() { @@ -11,6 +11,13 @@ 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; From ecbcc42e95acd66e50161e4d7d19257a37202a39 Mon Sep 17 00:00:00 2001 From: Paul Bellamy Date: Fri, 20 Jan 2023 18:09:01 +0000 Subject: [PATCH 2/5] Rustfmt --- cmd/soroban-cli/tests/it/arg_parsing.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/cmd/soroban-cli/tests/it/arg_parsing.rs b/cmd/soroban-cli/tests/it/arg_parsing.rs index 18c355c85..694cab71d 100644 --- a/cmd/soroban-cli/tests/it/arg_parsing.rs +++ b/cmd/soroban-cli/tests/it/arg_parsing.rs @@ -1,7 +1,7 @@ use crate::util::CUSTOM_TYPES; use serde_json::json; use soroban_cli::strval::{self, Spec}; -use soroban_env_host::xdr::{ScVal, ScStatic, ScSpecTypeDef, ScSpecTypeOption, ScSpecTypeUdt}; +use soroban_env_host::xdr::{ScSpecTypeDef, ScSpecTypeOption, ScSpecTypeUdt, ScStatic, ScVal}; #[test] fn parse_bool() { @@ -13,7 +13,13 @@ 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(); + 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)) } From 6efd39b60d1c483c89fee798038a40d5759e95ae Mon Sep 17 00:00:00 2001 From: Paul Bellamy Date: Fri, 20 Jan 2023 18:32:00 +0000 Subject: [PATCH 3/5] Support passing optional strings/bools/etc with no quotations --- cmd/soroban-cli/src/strval.rs | 11 ++++++++--- cmd/soroban-cli/tests/it/arg_parsing.rs | 26 +++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/cmd/soroban-cli/src/strval.rs b/cmd/soroban-cli/src/strval.rs index 683e7fe32..d73ca3451 100644 --- a/cmd/soroban-cli/src/strval.rs +++ b/cmd/soroban-cli/src/strval.rs @@ -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; @@ -87,6 +87,11 @@ impl Spec { /// Might return errors #[allow(clippy::wrong_self_convention)] pub fn from_string(&self, s: &str, t: &ScSpecTypeDef) -> Result { + if let ScSpecTypeDef::Option(b) = t { + 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 { diff --git a/cmd/soroban-cli/tests/it/arg_parsing.rs b/cmd/soroban-cli/tests/it/arg_parsing.rs index 694cab71d..a759e98ac 100644 --- a/cmd/soroban-cli/tests/it/arg_parsing.rs +++ b/cmd/soroban-cli/tests/it/arg_parsing.rs @@ -76,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 { From 9da43b0dbfa4b507b3a385b10eaa34090d2ee49f Mon Sep 17 00:00:00 2001 From: Paul Bellamy Date: Fri, 20 Jan 2023 18:33:22 +0000 Subject: [PATCH 4/5] Clippy --- cmd/soroban-cli/tests/it/arg_parsing.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/cmd/soroban-cli/tests/it/arg_parsing.rs b/cmd/soroban-cli/tests/it/arg_parsing.rs index a759e98ac..a2851f595 100644 --- a/cmd/soroban-cli/tests/it/arg_parsing.rs +++ b/cmd/soroban-cli/tests/it/arg_parsing.rs @@ -20,8 +20,8 @@ fn parse_null() { })), ) .unwrap(); - println!("{:#?}", parsed); - assert!(parsed == ScVal::Static(ScStatic::Void)) + println!("{parsed:#?}"); + assert!(parsed == ScVal::Static(ScStatic::Void)); } #[test] @@ -85,8 +85,8 @@ fn parse_optional_symbol_with_no_quotation_marks() { })), ) .unwrap(); - println!("{:#?}", parsed); - assert!(parsed == ScVal::Symbol("hello".try_into().unwrap())) + println!("{parsed:#?}"); + assert!(parsed == ScVal::Symbol("hello".try_into().unwrap())); } #[test] @@ -98,8 +98,8 @@ fn parse_optional_bool_with_no_quotation_marks() { })), ) .unwrap(); - println!("{:#?}", parsed); - assert!(parsed == ScVal::Static(ScStatic::True)) + println!("{parsed:#?}"); + assert!(parsed == ScVal::Static(ScStatic::True)); } #[test] From 8fc7add993b2b9e0dedb549764420d89b8d262d7 Mon Sep 17 00:00:00 2001 From: Paul Bellamy Date: Fri, 20 Jan 2023 19:13:13 +0000 Subject: [PATCH 5/5] Trying to get null parsing right --- cmd/soroban-cli/src/strval.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cmd/soroban-cli/src/strval.rs b/cmd/soroban-cli/src/strval.rs index d73ca3451..d7123ee65 100644 --- a/cmd/soroban-cli/src/strval.rs +++ b/cmd/soroban-cli/src/strval.rs @@ -88,6 +88,9 @@ impl Spec { #[allow(clippy::wrong_self_convention)] pub fn from_string(&self, s: &str, t: &ScSpecTypeDef) -> Result { 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);