encoding/yaml
Reading YAML by path, building and writing it, and converting it to JSON.
import encoding.yaml
YAML is the usual format for configuration files. This package converts YAML to JSON internally, which is what makes its reading functions simple and what lets it work alongside the json package.
The reading functions take a path: keys separated by dots, with a list index in square brackets, as in app.hosts[1]. A path that is not there gives an empty string or zero; to tell that apart from a real value, use Has.
Functions
Decode
func Decode(text: str, err &: encerr.EncodingError): value.ValueValue.import encoding.yaml
import encoding.value
import encoding.encerr
import str
func main:
mut members := Vector {} as Vector<str>
members.push(yaml.Member("name", "salam"))
members.push(yaml.MemberInt("port", 8080))
members.push(yaml.MemberBool("debug", false))
members.push(yaml.MemberFloat("ratio", 0.25))
mut hosts := Vector {} as Vector<str>
hosts.push(yaml.Str("a: b"))
members.push(yaml.MemberRaw("hosts", yaml.Array(hosts)))
object := yaml.Object(members)
println object
println yaml.Dump(object)
println str.Contains(yaml.DumpTabs(`{"a": {"b": 1}}`), "\tb: 1"), yaml.DumpWith(`{"a": [1]}`, " ")
println yaml.Escape("one: two")
println yaml.Parse("x: 1")
mut e := encerr.NoError()
v := yaml.Decode("city: Tehran\npopulation: 9000000\n", e)
println value.AsInt(value.ObjectGet(v, "population"))
println yaml.Encode(v)
endEncode
func Encode(v: value.Value): strValue into YAML.The example for this item is shown with the ones above: encoding-yaml-Build.salam
DumpWith
func DumpWith(text: str, unit: str): strThe example for this item is shown with the ones above: encoding-yaml-Build.salam
Dump
func Dump(text: str): strThe example for this item is shown with the ones above: encoding-yaml-Build.salam
DumpTabs
func DumpTabs(text: str): strDump, with tabs.The example for this item is shown with the ones above: encoding-yaml-Build.salam
Escape
func Escape(s: str): strThe example for this item is shown with the ones above: encoding-yaml-Build.salam
Str
func Str(value: str): strThe example for this item is shown with the ones above: encoding-yaml-Build.salam
Member
func Member(key: str, value: str): strThe example for this item is shown with the ones above: encoding-yaml-Build.salam
MemberInt
func MemberInt(key: str, value: i64): strThe example for this item is shown with the ones above: encoding-yaml-Build.salam
MemberBool
func MemberBool(key: str, value: bool): strThe example for this item is shown with the ones above: encoding-yaml-Build.salam
MemberFloat
func MemberFloat(key: str, value: f64): strThe example for this item is shown with the ones above: encoding-yaml-Build.salam
MemberRaw
func MemberRaw(key: str, raw: str): strThe example for this item is shown with the ones above: encoding-yaml-Build.salam
Object
func Object(members: Vector<str>): strDump turns into YAML.The example for this item is shown with the ones above: encoding-yaml-Build.salam
Array
func Array(items: Vector<str>): strThe example for this item is shown with the ones above: encoding-yaml-Build.salam
Get
func Get(text: str, path: str): strimport encoding.yaml
func main:
text := "app:\n name: salam\n port: 8080\n debug: true\n ratio: 0.5\n hosts:\n - a.example\n - b.example\n"
println yaml.Valid(text), yaml.Has(text, "app.name"), yaml.Has(text, "app.user")
println yaml.Get(text, "app.name"), yaml.GetInt(text, "app.port"), yaml.GetBool(text, "app.debug"), yaml.GetFloat(text, "app.ratio")
println yaml.TypeOf(text, "app.hosts"), yaml.Len(text, "app.hosts"), yaml.Get(text, "app.hosts[1]")
println yaml.Keys(text, "app")
mut required := Vector {} as Vector<str>
required.push("app")
println yaml.HasKeys(text, required)
required.push("db")
println yaml.HasKeys(text, required)
println yaml.ToJSON(text)
println yaml.SplitDocuments("a: 1\n---\nb: 2\n").len()
endHas
func Has(text: str, path: str): boolThe example for this item is shown with the ones above: encoding-yaml-Query.salam
GetInt
func GetInt(text: str, path: str): intThe example for this item is shown with the ones above: encoding-yaml-Query.salam
GetFloat
func GetFloat(text: str, path: str): f64The example for this item is shown with the ones above: encoding-yaml-Query.salam
GetBool
func GetBool(text: str, path: str): boolThe example for this item is shown with the ones above: encoding-yaml-Query.salam
TypeOf
func TypeOf(text: str, path: str): strmap, list, string, int, float, bool, null, or none when it is absent.The example for this item is shown with the ones above: encoding-yaml-Query.salam
Len
func Len(text: str, path: str): intThe example for this item is shown with the ones above: encoding-yaml-Query.salam
Keys
func Keys(text: str, path: str): Vector<str>The example for this item is shown with the ones above: encoding-yaml-Query.salam
HasKeys
func HasKeys(text: str, required: Vector<str>): boolThe example for this item is shown with the ones above: encoding-yaml-Query.salam
Parse
func Parse(text: str): strThe example for this item is shown with the ones above: encoding-yaml-Build.salam
ToJSON
func ToJSON(text: str): strThe example for this item is shown with the ones above: encoding-yaml-Query.salam
Valid
func Valid(text: str): boolThe example for this item is shown with the ones above: encoding-yaml-Query.salam
SplitDocuments
func SplitDocuments(text: str): Vector<str>---, into separate ones.The example for this item is shown with the ones above: encoding-yaml-Query.salam