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

Changed the field name to label #923

Merged
merged 5 commits into from
Dec 10, 2021
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
10 changes: 4 additions & 6 deletions crates/lang/codegen/src/generator/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ impl Metadata<'_> {
let ident = constructor.ident();
let args = constructor.inputs().map(Self::generate_dispatch_argument);
quote_spanned!(span=>
::ink_metadata::ConstructorSpec::from_name(::core::stringify!(#ident))
::ink_metadata::ConstructorSpec::from_label(::core::stringify!(#ident))
.selector([
#( #selector_bytes ),*
])
Expand Down Expand Up @@ -216,7 +216,7 @@ impl Metadata<'_> {
let args = message.inputs().map(Self::generate_dispatch_argument);
let ret_ty = Self::generate_return_type(message.output());
quote_spanned!(span =>
::ink_metadata::MessageSpec::from_name(::core::stringify!(#ident))
::ink_metadata::MessageSpec::from_label(::core::stringify!(#ident))
.selector([
#( #selector_bytes ),*
])
Expand Down Expand Up @@ -275,11 +275,9 @@ impl Metadata<'_> {
as ::ink_lang::reflect::TraitMessageInfo<#local_id>>::SELECTOR
}};
let ret_ty = Self::generate_return_type(message.output());
let label = [trait_ident.to_string(), message_ident.to_string()].join("::");
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you prefer this logic here or in from_trait_and_label on "spec" level?

What do you think about the idea that the namespace will affect the label too(not only selector)? If you agree then I will implement it like this https://github.com/paritytech/ink/blob/master/crates/lang/ir/src/ir/selector.rs#L108

Copy link
Collaborator

@cmichi cmichi Nov 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a good point, I think for consistency reasons it would be good.

What are you referring to with from_trait_and_label?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant, that I removed from_trait_and_label from spec and implement the logic of concatenating(joining of trait name and method name by "::") here, on generator level.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, got it. It's fine here.

Do you want to implement the namespace affecting the label as well? Could also be done as a follow-up.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can implement that in follow-up PR=) @Robbepop What do you think about the namespace affecting the label?

quote_spanned!(message_span=>
::ink_metadata::MessageSpec::from_trait_and_name(
::core::stringify!(#trait_ident),
::core::stringify!(#message_ident)
)
::ink_metadata::MessageSpec::from_label(#label)
.selector(#selector)
.args([
#( #message_args ),*
Expand Down
6 changes: 4 additions & 2 deletions crates/metadata/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,14 @@ pub enum MetadataVersioned {
/// Version 0 placeholder. Represents the original non-versioned metadata format.
V0(MetadataVersionDeprecated),
/// Version 1 of the contract metadata.
V1(InkProject),
V1(MetadataVersionDeprecated),
/// Version 2 of the contract metadata.
V2(InkProject),
}

impl From<InkProject> for MetadataVersioned {
fn from(ink_project: InkProject) -> Self {
MetadataVersioned::V1(ink_project)
MetadataVersioned::V2(ink_project)
}
}

Expand Down
143 changes: 50 additions & 93 deletions crates/metadata/src/specs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,10 @@ impl ContractSpec {
deserialize = "F::Type: DeserializeOwned, F::String: DeserializeOwned"
))]
pub struct ConstructorSpec<F: Form = MetaForm> {
/// The name of the message.
/// The label of the constructor.
///
/// In case of a trait provided constructor the trait name is prefixed.
pub name: Vec<F::String>,
/// In case of a trait provided constructor the label is prefixed with the trait label.
pub label: F::String,
/// The selector hash of the message.
pub selector: Selector,
/// The parameters of the deployment handler.
Expand All @@ -237,7 +237,7 @@ impl IntoPortable for ConstructorSpec {

fn into_portable(self, registry: &mut Registry) -> Self::Output {
ConstructorSpec {
name: registry.map_into_portable(self.name),
label: self.label.into_portable(registry),
selector: self.selector,
args: self
.args
Expand All @@ -253,11 +253,11 @@ impl<F> ConstructorSpec<F>
where
F: Form,
{
/// Returns the name of the message.
/// Returns the label of the constructor.
///
/// In case of a trait provided constructor the trait name is prefixed.
pub fn name(&self) -> &[F::String] {
&self.name
/// In case of a trait provided constructor the label is prefixed with the trait label.
pub fn label(&self) -> &F::String {
&self.label
}

/// Returns the selector hash of the message.
Expand Down Expand Up @@ -290,37 +290,19 @@ pub struct ConstructorSpecBuilder<Selector> {

impl ConstructorSpec {
/// Creates a new constructor spec builder.
fn from_name_segments<T>(
segments: T,
) -> ConstructorSpecBuilder<Missing<state::Selector>>
where
T: IntoIterator<Item = &'static str>,
{
pub fn from_label(
label: &'static str,
) -> ConstructorSpecBuilder<Missing<state::Selector>> {
ConstructorSpecBuilder {
spec: Self {
name: segments.into_iter().collect(),
label,
selector: Selector::default(),
args: Vec::new(),
docs: Vec::new(),
},
marker: PhantomData,
}
}

/// Creates a new constructor spec builder.
pub fn from_name(
name: &'static str,
) -> ConstructorSpecBuilder<Missing<state::Selector>> {
Self::from_name_segments([name])
}

/// Creates a new constructor spec builder for a trait provided constructor.
pub fn from_trait_and_name(
trait_name: &'static str,
constructor_name: &'static str,
) -> ConstructorSpecBuilder<Missing<state::Selector>> {
Self::from_name_segments([trait_name, constructor_name])
}
}

impl ConstructorSpecBuilder<Missing<state::Selector>> {
Expand Down Expand Up @@ -375,11 +357,11 @@ impl ConstructorSpecBuilder<state::Selector> {
))]
#[serde(rename_all = "camelCase")]
pub struct MessageSpec<F: Form = MetaForm> {
/// The name of the message and some optional prefixes.
/// The label of the message.
///
/// In case of trait provided messages and constructors the prefix
/// by convention in ink! is the name of the trait.
name: Vec<F::String>,
/// by convention in ink! is the label of the trait.
label: F::String,
/// The selector hash of the message.
selector: Selector,
/// If the message is allowed to mutate the contract state.
Expand Down Expand Up @@ -413,9 +395,9 @@ mod state {
}

impl MessageSpec {
/// Creates a new message spec from the given name segments.
fn from_name_segments(
segments: Vec<&'static str>,
/// Creates a new message spec builder.
pub fn from_label(
label: &'static str,
) -> MessageSpecBuilder<
Missing<state::Selector>,
Missing<state::Mutates>,
Expand All @@ -424,7 +406,7 @@ impl MessageSpec {
> {
MessageSpecBuilder {
spec: Self {
name: segments,
label,
selector: Selector::default(),
mutates: false,
payable: false,
Expand All @@ -435,43 +417,18 @@ impl MessageSpec {
marker: PhantomData,
}
}

/// Creates a new message spec builder.
pub fn from_name(
name: &'static str,
) -> MessageSpecBuilder<
Missing<state::Selector>,
Missing<state::Mutates>,
Missing<state::IsPayable>,
Missing<state::Returns>,
> {
Self::from_name_segments(vec![name])
}

/// Creates a new message spec builder for a trait provided message.
pub fn from_trait_and_name(
trait_name: &'static str,
message_name: &'static str,
) -> MessageSpecBuilder<
Missing<state::Selector>,
Missing<state::Mutates>,
Missing<state::IsPayable>,
Missing<state::Returns>,
> {
Self::from_name_segments(vec![trait_name, message_name])
}
}

impl<F> MessageSpec<F>
where
F: Form,
{
/// Returns the name of the message and some optional prefixes.
/// Returns the label of the message.
///
/// In case of trait provided messages and constructors the prefix
/// by convention in ink! is the name of the trait.
pub fn name(&self) -> &[F::String] {
&self.name
/// by convention in ink! is the label of the trait.
pub fn label(&self) -> &F::String {
&self.label
}

/// Returns the selector hash of the message.
Expand Down Expand Up @@ -617,7 +574,7 @@ impl IntoPortable for MessageSpec {

fn into_portable(self, registry: &mut Registry) -> Self::Output {
MessageSpec {
name: registry.map_into_portable(self.name),
label: self.label.into_portable(registry),
selector: self.selector,
mutates: self.mutates,
payable: self.payable,
Expand All @@ -639,8 +596,8 @@ impl IntoPortable for MessageSpec {
deserialize = "F::Type: DeserializeOwned, F::String: DeserializeOwned"
))]
pub struct EventSpec<F: Form = MetaForm> {
/// The name of the event.
name: F::String,
/// The label of the event.
label: F::String,
/// The event arguments.
args: Vec<EventParamSpec<F>>,
/// The event documentation.
Expand Down Expand Up @@ -686,7 +643,7 @@ impl IntoPortable for EventSpec {

fn into_portable(self, registry: &mut Registry) -> Self::Output {
EventSpec {
name: self.name.into_portable(registry),
label: self.label.into_portable(registry),
args: self
.args
.into_iter()
Expand All @@ -699,10 +656,10 @@ impl IntoPortable for EventSpec {

impl EventSpec {
/// Creates a new event specification builder.
pub fn new(name: &'static str) -> EventSpecBuilder {
pub fn new(label: &'static str) -> EventSpecBuilder {
EventSpecBuilder {
spec: Self {
name,
label,
args: Vec::new(),
docs: Vec::new(),
},
Expand All @@ -714,9 +671,9 @@ impl<F> EventSpec<F>
where
F: Form,
{
/// Returns the name of the event.
pub fn name(&self) -> &F::String {
&self.name
/// Returns the label of the event.
pub fn label(&self) -> &F::String {
&self.label
}

/// The event arguments.
Expand Down Expand Up @@ -892,15 +849,15 @@ where
}
}

/// Describes a pair of parameter name and type.
/// Describes a pair of parameter label and type.
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(bound(
serialize = "F::Type: Serialize, F::String: Serialize",
deserialize = "F::Type: DeserializeOwned, F::String: DeserializeOwned"
))]
pub struct EventParamSpec<F: Form = MetaForm> {
/// The name of the parameter.
name: F::String,
/// The label of the parameter.
label: F::String,
/// If the event parameter is indexed.
indexed: bool,
/// The type of the parameter.
Expand All @@ -915,7 +872,7 @@ impl IntoPortable for EventParamSpec {

fn into_portable(self, registry: &mut Registry) -> Self::Output {
EventParamSpec {
name: self.name.into_portable(registry),
label: self.label.into_portable(registry),
indexed: self.indexed,
ty: self.ty.into_portable(registry),
docs: registry.map_into_portable(self.docs),
Expand All @@ -925,10 +882,10 @@ impl IntoPortable for EventParamSpec {

impl EventParamSpec {
/// Creates a new event parameter specification builder.
pub fn new(name: &'static str) -> EventParamSpecBuilder {
pub fn new(label: &'static str) -> EventParamSpecBuilder {
EventParamSpecBuilder {
spec: Self {
name,
label,
// By default event parameters are not indexed.
indexed: false,
// We initialize every parameter type as `()`.
Expand All @@ -944,9 +901,9 @@ impl<F> EventParamSpec<F>
where
F: Form,
{
/// Returns the name of the parameter.
pub fn name(&self) -> &F::String {
&self.name
/// Returns the label of the parameter.
pub fn label(&self) -> &F::String {
&self.label
}

/// Returns true if the event parameter is indexed.
Expand Down Expand Up @@ -1060,15 +1017,15 @@ where
}
}

/// Describes a pair of parameter name and type.
/// Describes a pair of parameter label and type.
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(bound(
serialize = "F::Type: Serialize, F::String: Serialize",
deserialize = "F::Type: DeserializeOwned, F::String: DeserializeOwned"
))]
pub struct MessageParamSpec<F: Form = MetaForm> {
/// The name of the parameter.
name: F::String,
/// The label of the parameter.
label: F::String,
/// The type of the parameter.
#[serde(rename = "type")]
ty: TypeSpec<F>,
Expand All @@ -1079,18 +1036,18 @@ impl IntoPortable for MessageParamSpec {

fn into_portable(self, registry: &mut Registry) -> Self::Output {
MessageParamSpec {
name: self.name.into_portable(registry),
label: self.label.into_portable(registry),
ty: self.ty.into_portable(registry),
}
}
}

impl MessageParamSpec {
/// Constructs a new message parameter specification via builder.
pub fn new(name: &'static str) -> MessageParamSpecBuilder {
pub fn new(label: &'static str) -> MessageParamSpecBuilder {
MessageParamSpecBuilder {
spec: Self {
name,
label,
// Uses `()` type by default.
ty: TypeSpec::new::<()>(),
},
Expand All @@ -1102,9 +1059,9 @@ impl<F> MessageParamSpec<F>
where
F: Form,
{
/// Returns the name of the parameter.
pub fn name(&self) -> &F::String {
&self.name
/// Returns the label of the parameter.
pub fn label(&self) -> &F::String {
&self.label
}

/// Returns the type of the parameter.
Expand Down
Loading