59 lines
1.0 KiB
Go
59 lines
1.0 KiB
Go
// Command basic demonstrates decoding and encoding a TOML document with
|
|
// interpres.
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
|
|
"sourcedock.dev/petrbalvin/interpres"
|
|
)
|
|
|
|
const document = `
|
|
title = "interpres demo"
|
|
|
|
[server]
|
|
host = "127.0.0.1"
|
|
port = 9090
|
|
|
|
[[users]]
|
|
name = "petr"
|
|
admin = true
|
|
|
|
[[users]]
|
|
name = "guest"
|
|
admin = false
|
|
`
|
|
|
|
// Config mirrors the document above.
|
|
type Config struct {
|
|
Title string `toml:"title"`
|
|
Server struct {
|
|
Host string `toml:"host"`
|
|
Port int `toml:"port"`
|
|
} `toml:"server"`
|
|
Users []struct {
|
|
Name string `toml:"name"`
|
|
Admin bool `toml:"admin"`
|
|
} `toml:"users"`
|
|
}
|
|
|
|
func main() {
|
|
var cfg Config
|
|
if err := interpres.Unmarshal([]byte(document), &cfg); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
fmt.Printf("title: %s\n", cfg.Title)
|
|
fmt.Printf("server: %s:%d\n", cfg.Server.Host, cfg.Server.Port)
|
|
for _, u := range cfg.Users {
|
|
fmt.Printf("user: %-6s admin=%t\n", u.Name, u.Admin)
|
|
}
|
|
|
|
out, err := interpres.Marshal(cfg)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
fmt.Printf("\n--- marshal ---\n%s", out)
|
|
}
|