0

I would get table entries by uuid. how can I convert string format 16cadf15-dda7-4be2-8026-c3c7875322d0 to an indexable eosio type ( uint64_t, uint128_t, uint256_t, double or long double).

Or even use as primary key if possible.

ACTION saveuuid(string uuid){
  my_table mytable(_self, _self.value);
  mytable.emplace(_self, [&]( auto& row ) {
    row.id = mytable.available_primary_key();
    row.uuid = uuid;
  });
}

TABLE mytable {
      int id;
      string uuid;
      uint128_t primary_key() const { return id; }
      uint128_t by_uuid() const { return uuid.value; } // cast here?

      EOSLIB_SERIALIZE( approvals, (id)(uuid));
    };

    typedef eosio::multi_index<"mytable"_n, mytable,
       eosio::indexed_by<"mytable"_n, eosio::const_mem_fun<poll, string, &poll::by_uuid>>
    > my_table;


TABLE mytable {
      int id;
      string uuid;
      uint64_t primary_key() const { return id; }
      uint128_t by_uuid() const { return uuid.value; }

      EOSLIB_SERIALIZE( approvals, (id)(uuid));
    };

    typedef eosio::multi_index<"mytable"_n, mytable,
       eosio::indexed_by<"mytable"_n, eosio::const_mem_fun<poll, string, &poll::by_uuid>>
    > my_table;

ACTION saveuuid(string uuid){
  my_table mytable(_self, _self.value);
  mytable.emplace(_self, [&]( auto& row ) {
    row.id = mytable.available_primary_key();
    row.uuid = uuid;  // how to cast ?
  });
}

1 Answer 1

2

You need to convert the uuid to a uint128_t and also store it as uint128_t to make it indexable so that you can use it as key.

You can find more info about uuids and how they can be converted here.

The following should work

string uuid = "16cadf15-dda7-4be2-8026-c3c7875322d";
std::replace( uuid.begin(), uuid.end(), '-', ''); // remove all '-'
uuid = "Ox"+uuid; // add "0x" hex-prefix to string
uint128_t indexable_uuid = std::strtoul(uuid, 0, 16);. // convert hex-string to uint128_t

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.