record
fastn
supports record
types. These are also called struct
in some
languages.record
record
syntax:-- record person:
caption name:
integer age:
optional body bio:
person
. It has three
fields: name
, age
and bio
.-- record company:
caption name:
person list employees:
company
. It has two fields:
caption name
and list of employees
of person
type.record
with default values-- record person:
caption name: Undefined
integer age:
optional body bio: Not specified
;; << Alternative way >>
-- record person:
caption name: Undefined
integer age:
-- optional body person.bio:
No bio is specified for this person.
-- record company:
string name: FifthTry
-- person list company.employees:
-- person:
name: Arpita
age: 22
-- person:
name: Abrar
age: 24
-- end: company.employees
-- person john-snow: John Snow
age: 14
Here we have created a new variable of type person
, called it amitu
, and the
value of name
, since its declared as caption
in the record definition, is read
from the "caption" area, and age
is read from the "header".
bio
, since bio
is declared as optional body
,
so it's not a problem. Had it been just body
the above would not have been
valid.-- $john-snow.age: 15
-- person $john-snow: John Snow
$age: 14
Here we have used -- $john-snow.age: 15
to update a single field of a record.
-- record person:
caption name:
string list alias:
-- person $john-snow: John Snow
-- $john-snow.alias:
-- string: Aegon Targaryen
-- string: Lord Crow
-- string: The White Wolf
-- string: The Prince That Was Promised
-- end: $john-snow.alias
record
in fastn
is equivalent of a struct
in Rust..ftd
file from Rust you will have to first create a struct
in Rust that is compatible with our person
definition:#[derive(serde::Deserialize)]
struct Person {
name: String,
age: i32,
bio: Option<String>,
}
For each field in person
record, we have a corresponding field in our Person
struct
.
age
as i32, but it could have been any type that can be
deserialised from
JSON Number since
fastn
integer is converted to JSON Number
.