-
-
Notifications
You must be signed in to change notification settings - Fork 527
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expand the benchmark a bit; use the toml-test files for this. Also revamp/expand the example, and benchmark this as well. Remove all the other examples as they're not really all that useful.
- Loading branch information
Showing
13 changed files
with
361 additions
and
220 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
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 []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 | ||
} | ||
|
||
duration struct{ time.Duration } | ||
fmtTime struct{ time.Time } | ||
) | ||
|
||
func (d *duration) UnmarshalText(text []byte) (err error) { | ||
d.Duration, err = time.ParseDuration(string(text)) | ||
return err | ||
} | ||
|
||
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
] | ||
|
||
# Custom Unmarshal. | ||
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" |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.