whatcanGOwrong
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
)
|
||||
|
||||
type (
|
||||
example struct {
|
||||
Title string
|
||||
Desc string
|
||||
Integers []int
|
||||
Floats []float64
|
||||
Times []fmtTime
|
||||
Duration []time.Duration
|
||||
Distros []distro
|
||||
Servers map[string]server
|
||||
Characters map[string][]struct {
|
||||
Name string
|
||||
Rank string
|
||||
}
|
||||
}
|
||||
|
||||
server struct {
|
||||
IP string
|
||||
Hostname string
|
||||
Enabled bool
|
||||
}
|
||||
|
||||
distro struct {
|
||||
Name string
|
||||
Packages string
|
||||
}
|
||||
|
||||
fmtTime struct{ time.Time }
|
||||
)
|
||||
|
||||
func (t fmtTime) String() string {
|
||||
f := "2006-01-02 15:04:05.999999999"
|
||||
if t.Time.Hour() == 0 {
|
||||
f = "2006-01-02"
|
||||
}
|
||||
if t.Time.Year() == 0 {
|
||||
f = "15:04:05.999999999"
|
||||
}
|
||||
if t.Time.Location() == time.UTC {
|
||||
f += " UTC"
|
||||
} else {
|
||||
f += " -0700"
|
||||
}
|
||||
return t.Time.Format(`"` + f + `"`)
|
||||
}
|
||||
|
||||
func main() {
|
||||
f := "example.toml"
|
||||
if _, err := os.Stat(f); err != nil {
|
||||
f = "_example/example.toml"
|
||||
}
|
||||
|
||||
var config example
|
||||
meta, err := toml.DecodeFile(f, &config)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
indent := strings.Repeat(" ", 14)
|
||||
|
||||
fmt.Print("Decoded")
|
||||
typ, val := reflect.TypeOf(config), reflect.ValueOf(config)
|
||||
for i := 0; i < typ.NumField(); i++ {
|
||||
indent := indent
|
||||
if i == 0 {
|
||||
indent = strings.Repeat(" ", 7)
|
||||
}
|
||||
fmt.Printf("%s%-11s → %v\n", indent, typ.Field(i).Name, val.Field(i).Interface())
|
||||
}
|
||||
|
||||
fmt.Print("\nKeys")
|
||||
keys := meta.Keys()
|
||||
sort.Slice(keys, func(i, j int) bool { return keys[i].String() < keys[j].String() })
|
||||
for i, k := range keys {
|
||||
indent := indent
|
||||
if i == 0 {
|
||||
indent = strings.Repeat(" ", 10)
|
||||
}
|
||||
fmt.Printf("%s%-10s %s\n", indent, meta.Type(k...), k)
|
||||
}
|
||||
|
||||
fmt.Print("\nUndecoded")
|
||||
keys = meta.Undecoded()
|
||||
sort.Slice(keys, func(i, j int) bool { return keys[i].String() < keys[j].String() })
|
||||
for i, k := range keys {
|
||||
indent := indent
|
||||
if i == 0 {
|
||||
indent = strings.Repeat(" ", 5)
|
||||
}
|
||||
fmt.Printf("%s%-10s %s\n", indent, meta.Type(k...), k)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
# This is an example TOML document which shows most of its features.
|
||||
|
||||
# Simple key/value with a string.
|
||||
title = "TOML example \U0001F60A"
|
||||
|
||||
desc = """
|
||||
An example TOML document. \
|
||||
"""
|
||||
|
||||
# Array with integers and floats in the various allowed formats.
|
||||
integers = [42, 0x42, 0o42, 0b0110]
|
||||
floats = [1.42, 1e-02]
|
||||
|
||||
# Array with supported datetime formats.
|
||||
times = [
|
||||
2021-11-09T15:16:17+01:00, # datetime with timezone.
|
||||
2021-11-09T15:16:17Z, # UTC datetime.
|
||||
2021-11-09T15:16:17, # local datetime.
|
||||
2021-11-09, # local date.
|
||||
15:16:17, # local time.
|
||||
]
|
||||
|
||||
# Durations.
|
||||
duration = ["4m49s", "8m03s", "1231h15m55s"]
|
||||
|
||||
# Table with inline tables.
|
||||
distros = [
|
||||
{name = "Arch Linux", packages = "pacman"},
|
||||
{name = "Void Linux", packages = "xbps"},
|
||||
{name = "Debian", packages = "apt"},
|
||||
]
|
||||
|
||||
# Create new table; note the "servers" table is created implicitly.
|
||||
[servers.alpha]
|
||||
# You can indent as you please, tabs or spaces.
|
||||
ip = '10.0.0.1'
|
||||
hostname = 'server1'
|
||||
enabled = false
|
||||
[servers.beta]
|
||||
ip = '10.0.0.2'
|
||||
hostname = 'server2'
|
||||
enabled = true
|
||||
|
||||
# Start a new table array; note that the "characters" table is created implicitly.
|
||||
[[characters.star-trek]]
|
||||
name = "James Kirk"
|
||||
rank = "Captain"
|
||||
[[characters.star-trek]]
|
||||
name = "Spock"
|
||||
rank = "Science officer"
|
||||
|
||||
[undecoded] # To show the MetaData.Undecoded() feature.
|
||||
key = "This table intentionally left undecoded"
|
||||
Reference in New Issue
Block a user