near-runtime-ts icon indicating copy to clipboard operation
near-runtime-ts copied to clipboard

collections.Map should be able to store and return arbitrary data

Open behaviary opened this issue 5 years ago • 2 comments

Observe the following model:

export class Tiger {
  name: string;
}

If I want to store multiple lists of tigers in a persistent way, it makes sense to use a persistent map:

const tigers = collections.Map<string, Tiger[]>("t")

I'm assuming that I'll store a list of tigers for a user. This is a simplified version:

export function saveTheTigers(tigs:Tiger[]):string {
  let tigerOwner = context.sender; 
  tigers.set(tigerOwner, tigs);
  return "Tigers saved successfully";
}

export function getTheTigers(name:string):Tiger[] {
  let tigerOwner = context.sender;
  let tigs = tigers.get(counter);
  return tigs;
}

Expected behavior: Returns list of tigers on the frontend

Observed behavior: Screenshot 2019-07-29 17 58 33

This is because a reference to the array is returned, rather than the array itself. We should support the return of an array to the frontend client.

behaviary avatar Jul 30 '19 00:07 behaviary

Wrap tigers into a TigerList object and problem solved. It's an AssemblyScript issue, since it doesn't expose arrays basic types at the pre-compile time, so we can't build serializer and deserializer in assemblyscript without hacking the compiler.

evgenykuzyakov avatar Aug 08 '19 17:08 evgenykuzyakov

Wrap tigers into a TigerList object and problem solved. It's an AssemblyScript issue, since it doesn't expose arrays basic types at the pre-compile time, so we can't build serializer and deserializer in assemblyscript without hacking the compiler.

This is pretty rough. This must be true for any arbitrary data we'd like to store then?

behaviary avatar Aug 08 '19 21:08 behaviary