[][src]Macro hdk::entry

macro_rules! entry {
    (
        name: $name:expr,
        description: $properties:expr,
        sharing: $sharing:expr,
       // $(native_type: $native_type:ty,)*

        validation_package: || $package_creator:expr,
        validation: | $validation_data:ident : hdk::EntryValidationData<$native_type:ty> | $entry_validation:expr

        $(
            ,
            links : [
                $( $link_expr:expr ),*
            ]
        )*

    ) => { ... };
}

The entry macro is a helper for creating ValidatingEntryType definitions for use within the define_zome macro. It has 7 component parts:

  1. name: name is simply the descriptive name of the entry type, such as "post", or "user". It is what must be given as the entry_type_name argument when calling commit_entry and the other data read/write functions.
  2. description: description is something that is primarily for human readers of your code, just describe this entry type
  3. sharing: sharing defines what distribution over the DHT, or not, occurs with entries of this type, possible values are defined in the Sharing enum
  4. native_type: native_type references a given Rust struct, which provides a clear schema for entries of this type.
  5. validation_package: validation_package is a special identifier, which declares which data is required from peers when attempting to validate entries of this type. Possible values are found within ValidationPackageDefinition
  6. validation: validation is a callback function which will be called any time that a (DHT) node processes or stores this entry, triggered through actions such as commit_entry, update_entry, remove_entry. It always expects two arguments, the first of which is the entry attempting to be validated, the second is the validation context, which offers a variety of metadata useful for validation. See ValidationData for more details.
  7. links: links is a vector of link definitions represented by ValidatingLinkDefinition. Links can be defined with the link! macro or, more concise, with either the to! or from! macro, to define an association pointing from this entry type to another, or one that points back from the other entry type to this one. See link!, to! and from! for more details.

Examples

The following is a standalone Rust file that exports a function which can be called to get a ValidatingEntryType of a "post".



#[derive(Serialize, Deserialize, Debug, DefaultJson,Clone)]
pub struct Post {
    content: String,
    date_created: String,
}

pub fn definition() -> ValidatingEntryType {
    entry!(
        name: "post",
        description: "a short social media style sharing of content",
        sharing: Sharing::Public,

        validation_package: || {
            hdk::ValidationPackageDefinition::ChainFull
        },

        validation: |validation_data: hdk::EntryValidationData<Post>| {
             match validation_data
             {
             EntryValidationData::Create{entry:test_entry,validation_data:_} =>
             {


                       (test_entry.content != "FAIL")
                       .ok_or_else(|| "FAIL content is not allowed".to_string())
               }
               _ =>
                {
                     Err("Failed to validate with wrong entry type".to_string())
               }
        }},

        links: [
            to!(
                "post",
                link_type: "comments",

                validation_package: || {
                    hdk::ValidationPackageDefinition::ChainFull
                },

                validation: | _validation_data: hdk::LinkValidationData| {
                    Ok(())
                }
            )
        ]
    )
}