[][src]Macro hdk::define_zome

macro_rules! define_zome {
    (
        entries : [
            $( $entry_expr:expr ),*
        ]

        init : || {
            $init_expr:expr
        }


        validate_agent: |$agent_validation_param:ident : EntryValidationData::<AgentId>| {
            $agent_validation_expr:expr
        }


        $(
            receive : |$receive_from:ident, $receive_param:ident| {
                $receive_expr:expr
            }
        )*

        functions : [
            $(
                        $zome_function_name:ident : {
                            inputs: | $( $input_param_name:ident : $input_param_type:ty ),* |,
                            outputs: | $( $output_param_name:ident : $output_param_type:ty ),* |,
                            handler: $handler_path:path
                        }
            )*
        ]

        traits : {
                $(
                    $trait:ident [
                        $($trait_fn:ident),*
                    ]
                )*
            }


    ) => { ... };
}

Every Zome must utilize the define_zome macro in the main library file in their Zome. The define_zome macro has 4 component parts:

  1. entries: an array of ValidatingEntryType as returned by using the entry macro
  2. init: init is a callback called by Holochain to every Zome implemented within a DNA. It gets called when a new agent is initializing an instance of the DNA for the first time, and should return Ok or an Err, depending on whether the agent can join the network or not.
  3. receive (optional): receive is a callback called by Holochain when another agent on a hApp has initiated a node-to-node direct message. That node-to-node message is initiated via the send function of the API, which is where you can read further about use of send and receive. receive is optional to include, based on whether you use send anywhere in the code.
  4. functions: functions declares all the zome's functions with their input/output signatures

Examples

use hdk::error::ZomeApiResult;
use holochain_core_types::{
    dna::entry_types::Sharing,
    validation::EntryValidationData
};
#[no_mangle]


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

fn handle_post_address(content: String) -> ZomeApiResult<Address> {
    let post_entry = Entry::App("post".into(), Post {
        content,
        date_created: "now".into(),
    }.into());

    hdk::entry_address(&post_entry)
}

define_zome! {
    entries: [
        entry!(
            name: "post",
            description: "",
            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())
               }
        }}

        )
    ]

    init: || {
        Ok(())
    }
     
    validate_agent: |validation_data : EntryValidationData::<AgentId>| {
        Ok(())
    }

    receive: |from, payload| {
      // just return what was received, but modified
      format!("Received: {} from {}", payload, from)
    }

    functions: [
            // the name of this function, "post_address" is the
            // one to give while performing a `call` method to this function.
            // the name of the handler function must be different than the
            // name of the Zome function.
            post_address: {
                inputs: |content: String|,
                outputs: |post: ZomeApiResult<Address>|,
                handler: handle_post_address
            }
    ]

    // trait named "hc_public" will grant public access to all its functions
    traits: {
        hc_public [post_address]
    }
}