Item
A generic data type used to pass and retrieve your data from Reboot standard libraries.
For example, Item
is used to enqueue and dequeue your data from Queue
s.
It is not meant as a stand alone library, but rather a convenience type for marshalling and unmarshalling data.
There are three ways of formatting data for an Item
.
Value
represents a dynamically typed value which can be either null, a number, a string, a boolean, a recursive struct value, or a list of values. A producer of value is expected to set one of that variants, absence of any variant indicates an error. This type is meant to represent all possible JSON values.bytes
is used to store a string ofbytes
.Any
refers to theAny
protobuf and is the best choice if you are using protobufs to structure your data.
Imports
To use Item
s, import the library where you would like to use it.
- Python
- TypeScript
from reboot.std.item.v1.item import Item
import { Item } from "@reboot-dev/reboot-std/item/v1";
Since this is a data type that is not stored individually, you do not need a servicer for it.
Value
Value
is the recommended way to use Item
if you have structured data that is
not already a string of bytes
or a protobuf.
To marshal your data
- in Python, you can use helper methods from
reboot.protobuf
- in TypeScript, you can use @bufbuild/protobuf.
You'll need the additional import:
- Python
- TypeScript
from reboot.protobuf import (
as_bool,
as_dict,
as_int,
as_list,
as_str,
from_bool,
from_dict,
from_int,
from_list,
from_str,
)
import { Value } from "@bufbuild/protobuf";
Then you can use it to store and retrieve your data from Item
s as follows:
- Python
- TypeScript
item = Item(value=from_bool(True)),
print(as_bool(item.value))
item = Item(value=from_int(3)),
print(as_int(item.value))
item = Item(value=from_str("apple")),
print(as_str(item.value))
item = Item(value=from_list(["a", "b", "c"])),
print(as_list(item.value))
item = Item(value=from_dict({"details": "details-go-here"})),
print(as_dict(item.value))
const item = { value: Value.fromJson(true) };
console.log(item.value?.toJson());
const item = { value: Value.fromJson(1) };
console.log(item.value?.toJson());
const item = { value: Value.fromJson("apple") };
console.log(item.value?.toJson());
const item = { value: Value.fromJson(["a", "b", "c"]) };
console.log(item.value?.toJson());
const item = { value: Value.fromJson({ details: "details-go-here" }) };
console.log(item.value?.toJson());
bytes
If your data is formatted as a string of bytes
, you can directly store
that into an Item
.
- Python
- TypeScript
item = Item(bytes=b"some-bytes")
print(item.bytes)
const item = { bytes: new TextEncoder().encode("some-bytes") },
console.log(item.bytes);
Any
If you use protobufs to format your data, you can directly store that
into an Item
.
To pack and unpack from an Any
protobuf,
- in Python, you can use helper methods from
reboot.protobuf
- in TypeScript, you can use @bufbuild/protobuf v1.
- Python
- TypeScript
from reboot.protobuf import pack, unpack
message = SomeMessage(...)
item = Item(any=pack(message))
unpacked_message = unpack(item.any, SomeMessage)
import { Any, createRegistry } from "@bufbuild/protobuf";
const message = new SomeMessage({ ... });
const item = { any: Any.pack(message) };
const registry = createRegistry(SomeMessage);
const unpackedMessage = item.any.unpack(registry);