Backup work, before switching from module development setup to standard GOPATH workspace setup (some tooling simply is not yet working with it, and I mainly miss refactoring a lot)
This commit is contained in:
parent
b95f255ae7
commit
63cbe18d03
|
@ -0,0 +1,9 @@
|
||||||
|
## TOML encoder/decoder for Go
|
||||||
|
|
||||||
|
TOML stands for Tom's Obvious, Minimal Language. This go package provides a
|
||||||
|
decoder and an encoder for TOML.
|
||||||
|
|
||||||
|
Specification: https://github.com/toml-lang/toml
|
||||||
|
|
||||||
|
Compatible with TOML version
|
||||||
|
[v0.5.0](https://github.com/toml-lang/toml/blob/master/versions/en/toml-v0.5.0.md)
|
|
@ -0,0 +1,91 @@
|
||||||
|
// Command toml-test-decoder satisfies BurntSushi's toml-test interface for testing
|
||||||
|
// TOML decoders. Namely, it accepts TOML on stdin and outputs JSON on stdout.
|
||||||
|
// See: https://github.com/BurntSushi/toml-test
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/mmakaay/toml"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
log.SetFlags(0)
|
||||||
|
|
||||||
|
flag.Usage = usage
|
||||||
|
flag.Parse()
|
||||||
|
}
|
||||||
|
|
||||||
|
func usage() {
|
||||||
|
log.Printf("Usage: %s < toml-file\n", path.Base(os.Args[0]))
|
||||||
|
flag.PrintDefaults()
|
||||||
|
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
if flag.NArg() != 0 {
|
||||||
|
flag.Usage()
|
||||||
|
}
|
||||||
|
|
||||||
|
var tmp interface{}
|
||||||
|
if _, err := toml.DecodeReader(os.Stdin, &tmp); err != nil {
|
||||||
|
log.Fatalf("Error decoding TOML: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
typedTmp := translate(tmp)
|
||||||
|
if err := json.NewEncoder(os.Stdout).Encode(typedTmp); err != nil {
|
||||||
|
log.Fatalf("Error encoding JSON: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func translate(tomlData interface{}) interface{} {
|
||||||
|
switch orig := tomlData.(type) {
|
||||||
|
case map[string]interface{}:
|
||||||
|
typed := make(map[string]interface{}, len(orig))
|
||||||
|
for k, v := range orig {
|
||||||
|
typed[k] = translate(v)
|
||||||
|
}
|
||||||
|
return typed
|
||||||
|
case []map[string]interface{}:
|
||||||
|
typed := make([]map[string]interface{}, len(orig))
|
||||||
|
for i, v := range orig {
|
||||||
|
typed[i] = translate(v).(map[string]interface{})
|
||||||
|
}
|
||||||
|
return typed
|
||||||
|
case []interface{}:
|
||||||
|
typed := make([]interface{}, len(orig))
|
||||||
|
for i, v := range orig {
|
||||||
|
typed[i] = translate(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
// We don't really need to tag arrays, but let's be future proof.
|
||||||
|
// (If TOML ever supports tuples, we'll need this.)
|
||||||
|
return tag("array", typed)
|
||||||
|
case time.Time:
|
||||||
|
return tag("datetime", orig.Format("2006-01-02T15:04:05Z"))
|
||||||
|
case bool:
|
||||||
|
return tag("bool", fmt.Sprintf("%v", orig))
|
||||||
|
case int64:
|
||||||
|
return tag("integer", fmt.Sprintf("%d", orig))
|
||||||
|
case float64:
|
||||||
|
return tag("float", fmt.Sprintf("%v", orig))
|
||||||
|
case string:
|
||||||
|
return tag("string", orig)
|
||||||
|
}
|
||||||
|
|
||||||
|
panic(fmt.Sprintf("Unknown type: %T", tomlData))
|
||||||
|
}
|
||||||
|
|
||||||
|
func tag(typeName string, data interface{}) map[string]interface{} {
|
||||||
|
return map[string]interface{}{
|
||||||
|
"type": typeName,
|
||||||
|
"value": data,
|
||||||
|
}
|
||||||
|
}
|
4
go.mod
4
go.mod
|
@ -2,6 +2,8 @@ module git.makaay.nl/mauricem/go-toml
|
||||||
|
|
||||||
go 1.12
|
go 1.12
|
||||||
|
|
||||||
require git.makaay.nl/mauricem/go-parsekit v0.0.0
|
require (
|
||||||
|
git.makaay.nl/mauricem/go-parsekit v0.0.0
|
||||||
|
)
|
||||||
|
|
||||||
replace git.makaay.nl/mauricem/go-parsekit v0.0.0 => ../go-parsekit
|
replace git.makaay.nl/mauricem/go-parsekit v0.0.0 => ../go-parsekit
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -1,4 +1,2 @@
|
||||||
git.makaay.nl/mauricem/go-parsekit v0.0.0-20190521150537-747456517939 h1:cMBHhfSJR2BZgVN7NmP+c2agNlXDef4Iz6+XQp5AqdU=
|
git.makaay.nl/mauricem/go-parsekit v0.0.0-20190521150537-747456517939 h1:cMBHhfSJR2BZgVN7NmP+c2agNlXDef4Iz6+XQp5AqdU=
|
||||||
git.makaay.nl/mauricem/go-parsekit v0.0.0-20190521150537-747456517939/go.mod h1:/mo+aM5Im5rkBqBvXTAsVR0//OfsAAiFyvuxxcxGGlU=
|
git.makaay.nl/mauricem/go-parsekit v0.0.0-20190521150537-747456517939/go.mod h1:/mo+aM5Im5rkBqBvXTAsVR0//OfsAAiFyvuxxcxGGlU=
|
||||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
|
||||||
github.com/BurntSushi/toml-test v0.1.0/go.mod h1:xhCXZ5sE5Y4uZrhYxoOUTfGFH+wVyuKlcOC371elHo8=
|
|
||||||
|
|
Loading…
Reference in New Issue