Holochain Forum

List entries with adresses?

I’d like to list entries along with their adresses, is there a utility function for doing that? Or can someone point me to an example? (My Rust knowledge is unfortunately … very basic)

hdk::api::get_links produces something like this:

  Ok: {
    links: [
      {
        address: 'QmZNaXogq8jaCGmRvuGNAeowqDXgQPhMiyQcxEVzZR4e6z',
        ...
      },
      {
        address: 'QmQ95DgYyXqVto9fe9sBx95cwxHWgyx34UxYZ3baH9n7Ar',
        ...
      },
    ]
  }
}

hdk::utils::get_links_and_load_type produces something like this:

{
  Ok: [
    {
      name: 'Foot',
      some_data: 'Sits at the end of the leg',
    },
    {
      name: 'Hand',
      some_data: 'Useful for picking up stuff',
    },
  ]
}

I’m interested in something like this:

{
  Ok: [
    {
      address: 'QmZNaXogq8jaCGmRvuGNAeowqDXgQPhMiyQcxEVzZR4e6z',
      name: 'Foot',
      some_data: 'Sits at the end of the leg',
    },
    {
      address: 'QmQ95DgYyXqVto9fe9sBx95cwxHWgyx34UxYZ3baH9n7Ar',
      name: 'Hand',
      some_data: 'Useful for picking up stuff',
    },
  ]
}

:pisces::pisces::pisces:

Hello Kristofer! Nice to see you here in the forum; I’ve seen you around (are you part of the Currency Design Workshop? or you a Twitter acquaintance?)

Wow, you know, there doesn’t seem to be an option for that. Fortunately, though, you can derive an address from an entry, so if you’re using get_links_and_load() you can iterate over the result like so:

fn get_body_parts(a: Address) -> ZomeApiResult<Vec<BodyPartWithAddress>> {
    let body_parts = hdk::get_links_and_load_type<BodyPart>(a)?;
    let body_parts_with_addresses = body_parts.map(|result| BodyPartWithAddress{ address: result.address(), name: result.name, some_data: result.some_data });
    Ok(body_parts_with_addresses)
}

(assuming that you have a BodyPartWithAddress struct defined somewhere in your code that can be converted to JSON)

1 Like

Great, thanks, that’s just what I was looking for. [team poke] A generic function similar to this sounds like a potential future addition to hdk::utils.

And no, not the currency design workshop. Twitter, yes. I work with @ViktorZaunders and Food Shift. We have done a lot of exploring and modelling together for Shiro and did some Holochain testing a while back.

Now with the alpha net coming online, the Holoports shipping and everything, I’m eager to start prototyping again. Will hopefully have some simple little thing running in a week or two that I would love to try all the way in Holoscape.

3 Likes