[−][src]Module no_proto::schema
Schemas are JSON used to declare & store the shape of buffer objects.
No Proto Schemas are JSON objects that describe how the data in a NP_ buffer is stored.
Every schema object has at least a "type" key that provides the kind of value stored at that part of the schema. Additional keys are dependent on the type of schema.
Schemas are validated and sanity checked by the NP_Factory struct upon creation. You cannot pass an invalid schema into a factory constructor and build/parse buffers with it.
If you're familiar with typescript, schemas can be described by this recursive interface:
interface NP_Schema {
type: string;
// used by list types
of?: NP_Schema
// used by map types
value?: NP_Schema
// used by tuple types
values?: NP_Schema[]
// used by table types
columns?: [string, NP_Schema][]
}
Schemas do not have to contain collections, for example a perfectly valid schema for just a string would be:
{
"type": "string"
}
Nesting is easy to perform. For example, this is a list of tables. Each table has two columns: id and title. Both columns are a string type.
{
"type": "list",
"of": {
"type": "table",
"columns": [
["id", {type: "string"}]
["title", {type: "string"}]
]
}
}
A list of strings is just as easy...
{
"type": "list",
"of": { type: "string" }
}
Each type has trade offs associated with it. The table and documentation below go into further detail.
Here is a table of supported types.
Type | Rust Type / Struct | Bytes (Size) | Limits / Notes |
---|---|---|---|
table | NP_Table | 4 bytes - ~4GB | Linked list with indexed keys that map against up to 255 named columns. |
list | NP_List | 8 bytes - ~4GB | Linked list with up to 65,535 items. |
map | NP_Map | 4 bytes - ~4GB | Linked list with Vec<u8> keys. |
tuple | NP_Tuple | 4 bytes - ~4GB | Static sized collection of specific values. |
any | NP_Any | 4 bytes - ~4GB | Generic type. |
string | String | 4 bytes - ~4GB | Utf-8 formatted string. |
bytes | NP_Bytes | 4 bytes - ~4GB | Arbitrary bytes. |
int8 | i8 | 1 byte | -127 to 127 |
int16 | i16 | 2 bytes | -32,768 to 32,768 |
int32 | i32 | 4 bytes | -2,147,483,648 to 2,147,483,648 |
int64 | i64 | 8 bytes | -9.22e18 to 9.22e18 |
uint8 | u8 | 1 byte | 0 - 255 |
uint16 | u16 | 2 bytes | 0 - 65,535 |
uint32 | u32 | 4 bytes | 0 - 4,294,967,295 |
uint64 | u64 | 8 bytes | 0 - 1.84e19 |
float | f32 | 4 bytes | -3.4e38 to 3.4e38 |
double | f64 | 8 bytes | -1.7e308 to 1.7e308 |
option | NP_Option | 1 byte | Up to 255 strings in schema. |
bool | bool | 1 byte | |
dec64 | NP_Dec | 9 bytes | Big Integer Decimal format. |
geo4 | NP_Geo | 4 bytes | 1.5km resolution (city) geographic coordinate |
geo8 | NP_Geo | 8 bytes | 16mm resolution (marble) geographic coordinate |
geo16 | NP_Geo | 16 bytes | 3.5nm resolution (flea) geographic coordinate |
tid | NP_TimeID | 16 bytes | 8 byte u64 for time with 8 bytes of random numbers. |
uuid | NP_UUID | 16 bytes | v4 UUID, 2e37 possible UUID v4s |
date | NP_Date | 8 bytes | Good to store unix epoch (in seconds) until the year 584,942,417,355 |