Skip to content

Commit

Permalink
Expand benchmarks, example (#331)
Browse files Browse the repository at this point in the history
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
arp242 authored Nov 19, 2021
1 parent 9be4ccf commit 9865bee
Show file tree
Hide file tree
Showing 13 changed files with 361 additions and 220 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,4 @@ type clients struct {
Note that a case insensitive match will be tried if an exact match can't be
found.

A working example of the above can be found in `_examples/example.{go,toml}`.
A working example of the above can be found in `_example/example.{go,toml}`.
112 changes: 112 additions & 0 deletions _example/example.go
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)
}
}
53 changes: 53 additions & 0 deletions _example/example.toml
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"
61 changes: 0 additions & 61 deletions _examples/example.go

This file was deleted.

35 changes: 0 additions & 35 deletions _examples/example.toml

This file was deleted.

22 changes: 0 additions & 22 deletions _examples/hard.toml

This file was deleted.

4 changes: 0 additions & 4 deletions _examples/implicit.toml

This file was deleted.

6 changes: 0 additions & 6 deletions _examples/invalid-apples.toml

This file was deleted.

35 changes: 0 additions & 35 deletions _examples/invalid.toml

This file was deleted.

5 changes: 0 additions & 5 deletions _examples/readme1.toml

This file was deleted.

1 change: 0 additions & 1 deletion _examples/readme2.toml

This file was deleted.

Loading

0 comments on commit 9865bee

Please sign in to comment.