-
Notifications
You must be signed in to change notification settings - Fork 54
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
feat: ComponentMetadata
templating
#1027
Conversation
e5291d0
to
9df6689
Compare
ba8cc33
to
c73a3d2
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! Thank you! Not a full review yet, but I left some comments inline.
pub enum TemplateValue { | ||
Felt(Felt), | ||
Word(Word), | ||
Map(Vec<(Digest, Word)>), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: would Entries
be a better name here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of Map
? Sounds a bit more generic IMO but I can rename it if preferred
objects/src/accounts/package/mod.rs
Outdated
// Check for duplicates | ||
let mut seen = BTreeSet::new(); | ||
let duplicate = | ||
all_slots | ||
.iter() | ||
.find_map(|&slot| if !seen.insert(slot) { Some(slot) } else { None }); | ||
|
||
if let Some(dup) = duplicate { | ||
return Err(ComponentPackageError::DuplicateSlots(dup)); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check can be done inside the all_slots.windows(2)
. We should check that slots[1] == slots[0]
doesn't happen.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point! Changed.
objects/src/accounts/package/mod.rs
Outdated
#[error("error creating AccountComponent: {0}")] | ||
AccountComponentError(AccountError), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we use #[from]
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you mean #[source]
? I added #[from]
in the serde error and #[source]
here. I'd rather avoid automatically adding the conversion since there might be places where we benefit from double-checking the context
@@ -160,6 +164,59 @@ impl StorageEntry { | |||
StorageEntry::MultiSlot { .. } => &[], | |||
} | |||
} | |||
|
|||
/// Returns an iterator over all of the storage entries's template keys. | |||
// TODO: Should template keys be typed? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do we mean by "typed" here? Could keys be of different types other than strings?
EDIT: Oh, maybe we mean they key having the storage entry variant where it came from. I think this can be checked by the caller if necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was referring to whether we want keys to have what type of value they expect, basically.
} | ||
|
||
/// Returns `Ok(&Vec<(Digest, Word)>>` if the variant is `Map`, otherwise errors. | ||
pub fn as_map(&self) -> Result<&Vec<(Digest, Word)>, ComponentPackageError> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is not used, will this be used in a later PR?
objects/src/accounts/package/mod.rs
Outdated
/// they are not of a valid type) | ||
pub fn instantiate_component( | ||
&self, | ||
template_keys: &BTreeMap<String, TemplateValue>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
template_keys: &BTreeMap<String, TemplateValue>, | |
template_keys: &BTreeMap<TemplateKey, TemplateValue>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same in the try_into_...
methods that have this map as a parameter
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll probably leave refactoring initialization data for future PRs as explained here: #1027 (comment)
FeltRepresentation::Dynamic(template_key) => *template_values | ||
.get(template_key.key()) | ||
.ok_or_else(|| { | ||
ComponentPackageError::TemplateValueNotProvided( | ||
template_key.key().to_string(), | ||
) | ||
})? | ||
.as_felt()?, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this should be moved into a FeltRepresentation::try_into_felt(template_values)
method, so it is more in line with the other objects.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Factored out the code
// Attempt to deserialize as TemplateKey first | ||
if let Ok(tk) = TemplateKey::try_deserialize(value) { | ||
return Ok(WordRepresentation::Dynamic(tk)); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of trying one then the other, we could check the first two chars, if they are 0x
the we use the parse_hex_string_as_word
. This way the error message could be different in both cases because we know which variant we were expecting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While implementing this I realized it is still valuable to output valid formats for string-based values. For example: maybe someone writes "123" expecting decimals to work but then it won't match with the "0x" and so we would assume a dynamic value when that's not really the case.
Co-authored-by: Tomas Rodriguez Dala <[email protected]>
Sounds fair. @bobbinth if it's OK by you, I'll merge this and continue on the base branch |
No description provided.