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 Queues.
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.
Valuerepresents 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.bytesis used to store a string ofbytes.Anywraps a protobuf message, for code that already has one.
Imports
To use Items in Python, import the library where you would like to use it. In
Typescript, the type will be inferred.
- Python
- TypeScript
from reboot.std.item.v1.item import Item
// No import necessary.
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 for structured data:
anything you could express as JSON.
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 Items 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))
item = { value: Value.fromJson(true) };
console.log(item.value?.toJson());
item = { value: Value.fromJson(1) };
console.log(item.value?.toJson());
item = { value: Value.fromJson("apple") };
console.log(item.value?.toJson());
item = { value: Value.fromJson(["a", "b", "c"]) };
console.log(item.value?.toJson());
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 already have a protobuf message, you can store it directly in
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.
You'll need the additional import:
- Python
- TypeScript
from reboot.protobuf import pack, unpack
import { Any, createRegistry } from "@bufbuild/protobuf";
Then you can use it to store and retrieve your protobufs from Items as follows:
- Python
- TypeScript
item = Item(any=pack(message))
unpacked_message = unpack(item.any, SomeMessage)
const message = new SomeMessage(contents);
const item = { any: Any.pack(message) };
const registry = createRegistry(SomeMessage);
const unpackedMessage = item.any.unpack(registry) as SomeMessage;