Skip to content
This repository has been archived by the owner on Feb 3, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1816 from holochain/feature/hdk-prelude
Browse files Browse the repository at this point in the history
Feature/hdk prelude
  • Loading branch information
willemolding authored Oct 31, 2019
2 parents a751100 + b766cb4 commit cac905c
Show file tree
Hide file tree
Showing 52 changed files with 196 additions and 339 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG-UNRELEASED.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

### Added

* Allows the HC CLI to generate zomes from template repos. This will by default use the default holochain template repos (holochain/rust-zome-template and holochain/rust-proc-zome-template) but can also point to custom templates in remote repos or locally (e.g. `hc generate zomes/my_zome https://github.com/org/some-custom-zome-template`). [#1565](https://github.com/holochain/holochain-rust/pull/1565)
* Adds option `--property` to `hc hash` that sets DNA properties for hash calculation. [#1807](https://github.com/holochain/holochain-rust/pull/1807)
* Adds a prelude module to the HDK. Adding the statement `use hdk::prelude::*` should be enough for 90% of zome development [#1816](https://github.com/holochain/holochain-rust/pull/1816)

### Changed

### Deprecated
Expand Down
5 changes: 3 additions & 2 deletions app_spec/zomes/blog/code/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
name = "blog"
version = "0.0.35-alpha7"
authors = ["Holochain Core Dev Team <devcore@holochain.org>"]
edition = "2018"

[dependencies]
hdk = { path = "../../../../crates/hdk" }
serde = "=1.0.89"
serde_json = { version = "=1.0.39", features = ["preserve_order"] }
hdk = { path = "../../../../crates/hdk" }
holochain_json_derive = "=0.0.17"
serde_derive = "=1.0.89"
boolinator = "=2.4.0"

[lib]
path = "src/lib.rs"
crate-type = ["cdylib"]
31 changes: 7 additions & 24 deletions app_spec/zomes/blog/code/src/blog.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,16 @@
use hdk::prelude::*;
use hdk::{
self,
error::{ZomeApiError, ZomeApiResult},
holochain_persistence_api::{
cas::content::Address,
},
holochain_json_api::{
json::JsonString,
error::JsonError
},
serde_json::json,
holochain_core_types::{
dna::capabilities::CapabilityRequest,
entry::{cap_entries::CapabilityType, entry_type::EntryType, Entry},
error::HolochainError,
entry::{cap_entries::CapabilityType},
signature::{Provenance, Signature},
link::LinkMatch,
},
holochain_wasm_utils::api_serialization::{
commit_entry::CommitEntryOptions,
get_entry::{
EntryHistory, GetEntryOptions, GetEntryResult, GetEntryResultType, StatusRequestKind,
},
get_links::{GetLinksOptions, GetLinksResult},
QueryArgsOptions, QueryResult,
},
AGENT_ADDRESS, AGENT_ID_STR, CAPABILITY_REQ, DNA_ADDRESS, DNA_NAME, PROPERTIES, PUBLIC_TOKEN
};

use memo::Memo;
use post::Post;
use crate::memo::Memo;
use crate::post::Post;
use std::{
collections::BTreeMap,
convert::{TryFrom, TryInto},
Expand Down Expand Up @@ -567,9 +550,9 @@ pub fn handle_get_post_bridged(post_address: Address) -> ZomeApiResult<Option<En
#[cfg(test)]
pub mod tests {

use blog::{check_sum_args, post_entry, SumInput};
use crate::blog::{check_sum_args, post_entry, SumInput};
use hdk::holochain_core_types::entry::{entry_type::AppEntryType, AppEntryValue, Entry};
use post::Post;
use crate::post::Post;

#[test]
fn check_sum_args_test() {
Expand Down
28 changes: 8 additions & 20 deletions app_spec/zomes/blog/code/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,18 @@
#![warn(unused_extern_crates)]
#[macro_use]
extern crate hdk;
#[macro_use]
extern crate serde_derive;
extern crate boolinator;
#[macro_use]
extern crate serde_json;
#[macro_use]
extern crate holochain_json_derive;

pub mod blog;
pub mod memo;
pub mod post;

use blog::Env;
use hdk::prelude::*;
use hdk::{
error::ZomeApiResult,
holochain_core_types::{entry::Entry, signature::Provenance},
holochain_json_api::{error::JsonError, json::JsonString},
holochain_persistence_api::cas::content::Address,
holochain_core_types::{signature::Provenance},
holochain_wasm_utils::api_serialization::{
get_entry::{EntryHistory, GetEntryResult},
get_links::GetLinksResult,
},
};

mod blog;
mod memo;
mod post;

use blog::Env;

define_zome! {

entries: [
Expand Down
10 changes: 1 addition & 9 deletions app_spec/zomes/blog/code/src/memo.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
/// This file holds everything that represents the "memo" entry type.
/// a Memo is essentially a private post that should never be publically
/// published on the dht.
use hdk::entry_definition::ValidatingEntryType;
use hdk::{
holochain_core_types::{
dna::entry_types::Sharing
},
holochain_json_api::{
error::JsonError, json::JsonString,
}
};
use hdk::prelude::*;

/// We declare the structure of our entry type with this Rust struct.
/// It will be checked automatically by the macro below, similar
Expand Down
11 changes: 2 additions & 9 deletions app_spec/zomes/blog/code/src/post.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
use boolinator::Boolinator;
use hdk::entry_definition::ValidatingEntryType;
/// This file holds everything that represents the "post" entry type.
use hdk::holochain_core_types::{
dna::entry_types::Sharing,
validation::EntryValidationData,
};
use hdk::holochain_json_api::{
error::JsonError, json::JsonString,
};
use hdk::prelude::*;

use boolinator::Boolinator;

/// We declare the structure of our entry type with this Rust struct.
/// It will be checked automatically by the macro below, similar
Expand Down
2 changes: 1 addition & 1 deletion app_spec/zomes/converse/code/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ authors = ["Julian Laubstein <contact@julianlaubstein.de>"]
edition = "2018"

[dependencies]
hdk = { path = "../../../../crates/hdk" }
serde = "=1.0.89"
serde_json = "=1.0.39"
serde_derive = "=1.0.89"
hdk = { path = "../../../../crates/hdk" }

[lib]
path = "src/lib.rs"
Expand Down
11 changes: 1 addition & 10 deletions app_spec/zomes/converse/code/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
#[macro_use]
extern crate hdk;
#[macro_use]
extern crate serde_derive;

use hdk::prelude::*;
use hdk::{
error::ZomeApiResult,
holochain_core_types::{
signature::{Provenance, Signature},
},
holochain_json_api::{
error::JsonError,
json::JsonString,
},
holochain_wasm_utils::api_serialization::keystore::KeyType,
};

Expand Down
3 changes: 1 addition & 2 deletions app_spec/zomes/simple/code/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ authors = ["Ashanti Mutinta <Ashantimutinta@gmail.com>"]
edition = "2018"

[dependencies]
hdk = { path = "../../../../crates/hdk" }
serde = "=1.0.89"
serde_json = { version = "=1.0.39", features = ["preserve_order"] }
serde_derive = "=1.0.89"
hdk = { path = "../../../../crates/hdk" }
holochain_json_derive = "=0.0.17"

[lib]
path = "src/lib.rs"
Expand Down
43 changes: 6 additions & 37 deletions app_spec/zomes/simple/code/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,10 @@
#![warn(unused_extern_crates)]
#[macro_use]
extern crate hdk;
#[macro_use]
extern crate serde_derive;
#[macro_use]
extern crate holochain_json_derive;

use hdk::{
entry_definition::ValidatingEntryType,
error::ZomeApiResult,
};
use hdk::holochain_core_types::{
dna::entry_types::Sharing,
entry::Entry,
link::LinkMatch,
agent::AgentId,
validation::EntryValidationData,
};
use hdk::holochain_persistence_api::{
cas::content::Address,
hash::HashString
};
use hdk::holochain_json_api::{
json::JsonString,
error::JsonError
};


use hdk::holochain_wasm_utils::api_serialization::get_links::{GetLinksResult,LinksStatusRequestKind,GetLinksOptions,GetLinksResultCount};


use hdk::prelude::*;
// see https://developer.holochain.org/api/latest/hdk/ for info on using the hdk library

// This is a sample zome that defines an entry type "MyEntry" that can be committed to the
// agent's chain via the exposed function create_my_entry

#[derive(Serialize, Deserialize, Debug, DefaultJson,Clone)]
#[derive(Serialize, Deserialize, Debug, DefaultJson, Clone)]
pub struct Simple {
content: String,
}
Expand All @@ -59,25 +28,25 @@ pub fn handle_create_anchor() -> ZomeApiResult<Address> {

pub fn handle_create_my_link(base: Address,target : String) -> ZomeApiResult<()> {
let address = hdk::commit_entry(&simple_entry(target))?;
hdk::link_entries(&base, &HashString::from(address), "authored_simple_posts", "tag")?;
hdk::link_entries(&base, &Address::from(address), "authored_simple_posts", "tag")?;
Ok(())
}

pub fn handle_create_my_link_with_tag(base: Address,target : String, tag : String) -> ZomeApiResult<()> {
let address = hdk::commit_entry(&simple_entry(target))?;
hdk::link_entries(&base, &HashString::from(address), "authored_simple_posts", &tag)?;
hdk::link_entries(&base, &Address::from(address), "authored_simple_posts", &tag)?;
Ok(())
}

pub fn handle_delete_my_link(base: Address,target : String) -> ZomeApiResult<()> {
let address = hdk::entry_address(&simple_entry(target))?;
hdk::remove_link(&base, &HashString::from(address), "authored_simple_posts","tag")?;
hdk::remove_link(&base, &Address::from(address), "authored_simple_posts","tag")?;
Ok(())
}

pub fn handle_delete_my_link_with_tag(base: Address,target : String,tag:String) -> ZomeApiResult<()> {
let address = hdk::entry_address(&simple_entry(target))?;
hdk::remove_link(&base, &HashString::from(address), "authored_simple_posts",&tag)?;
hdk::remove_link(&base, &Address::from(address), "authored_simple_posts",&tag)?;
Ok(())
}

Expand Down
3 changes: 2 additions & 1 deletion app_spec/zomes/summer/code/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
name = "summer"
version = "0.0.35-alpha7"
authors = ["Holochain Core Dev Team <devcore@holochain.org>"]
edition = "2018"

[dependencies]
hdk = { path = "../../../../crates/hdk" }
serde = "=1.0.89"
serde_json = { version = "=1.0.39", features = ["preserve_order"] }
hdk = { path = "../../../../crates/hdk" }
serde_derive = "=1.0.89"
boolinator = "=2.4.0"

Expand Down
13 changes: 2 additions & 11 deletions app_spec/zomes/summer/code/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
#[macro_use]
extern crate hdk;
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
extern crate boolinator;
use hdk::holochain_json_api::error::JsonError;
use hdk::holochain_json_api::json::JsonString;
use hdk::error::ZomeApiResult;
use hdk::prelude::*;

fn handle_sum(num1: u32, num2: u32) -> ZomeApiResult<u32> {
Ok(num1 + num2)
Expand Down Expand Up @@ -41,7 +32,7 @@ define_zome! {
#[cfg(test)]
mod tests {

use handle_sum;
use super::handle_sum;

#[test]
pub fn handle_sum_test() {
Expand Down
1 change: 1 addition & 0 deletions app_spec_proc_macro/zomes/blog/code/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "blog"
version = "0.0.35-alpha7"
authors = ["Holochain Core Dev Team <devcore@holochain.org>"]
edition = "2018"

[dependencies]
serde = "=1.0.89"
Expand Down
37 changes: 8 additions & 29 deletions app_spec_proc_macro/zomes/blog/code/src/blog.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,16 @@
use hdk::prelude::*;
use hdk::{
self,
error::{ZomeApiError, ZomeApiResult},
serde_json::json,
holochain_core_types::{
dna::capabilities::CapabilityRequest,
entry::{cap_entries::CapabilityType, entry_type::EntryType, Entry},
error::HolochainError,
entry::{cap_entries::CapabilityType},
signature::{Provenance, Signature},
link::LinkMatch,
},
holochain_persistence_api::{
cas::content::Address,
},
holochain_json_api::{
error::JsonError,
json::JsonString,
},
holochain_wasm_utils::api_serialization::{
commit_entry::CommitEntryOptions,
get_entry::{
EntryHistory, GetEntryOptions, GetEntryResult, GetEntryResultType, StatusRequestKind,
},
get_links::{GetLinksOptions, GetLinksResult},
QueryArgsOptions, QueryResult,
},
AGENT_ADDRESS, AGENT_ID_STR, CAPABILITY_REQ, DNA_ADDRESS, DNA_NAME, PROPERTIES, PUBLIC_TOKEN,
AGENT_ADDRESS, AGENT_ID_STR, CAPABILITY_REQ, DNA_ADDRESS, DNA_NAME, PROPERTIES, PUBLIC_TOKEN
};

use memo::Memo;
use post::Post;
use crate::memo::Memo;
use crate::post::Post;
use std::{
collections::BTreeMap,
convert::{TryFrom, TryInto},
Expand Down Expand Up @@ -65,10 +48,6 @@ pub fn handle_show_env() -> ZomeApiResult<Env> {
})
}

pub fn handle_get_test_properties() -> ZomeApiResult<JsonString> {
hdk::property("test_property")
}

pub fn handle_get_sources(address: Address) -> ZomeApiResult<Vec<Address>> {
if let GetEntryResultType::Single(result) = hdk::get_entry_result(
&address,
Expand Down Expand Up @@ -563,9 +542,9 @@ pub fn handle_get_post_bridged(post_address: Address) -> ZomeApiResult<Option<En
#[cfg(test)]
pub mod tests {

use blog::{check_sum_args, post_entry, SumInput};
use crate::blog::{check_sum_args, post_entry, SumInput};
use hdk::holochain_core_types::entry::{entry_type::AppEntryType, AppEntryValue, Entry};
use post::Post;
use crate::post::Post;

#[test]
fn check_sum_args_test() {
Expand Down
Loading

0 comments on commit cac905c

Please sign in to comment.