From 63cbe18d0357ae718d31484b991e991273c6caef Mon Sep 17 00:00:00 2001 From: Maurice Makaay Date: Thu, 27 Jun 2019 07:36:24 +0000 Subject: [PATCH] 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) --- README.md | 9 ++++ cmd/toml-test-decoder/main.go | 91 +++++++++++++++++++++++++++++++++++ go.mod | 4 +- go.sum | 2 - 4 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 README.md create mode 100644 cmd/toml-test-decoder/main.go diff --git a/README.md b/README.md new file mode 100644 index 0000000..7538dc8 --- /dev/null +++ b/README.md @@ -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) diff --git a/cmd/toml-test-decoder/main.go b/cmd/toml-test-decoder/main.go new file mode 100644 index 0000000..9393fc7 --- /dev/null +++ b/cmd/toml-test-decoder/main.go @@ -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, + } +} diff --git a/go.mod b/go.mod index 17e29c9..df906fa 100644 --- a/go.mod +++ b/go.mod @@ -2,6 +2,8 @@ module git.makaay.nl/mauricem/go-toml 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 diff --git a/go.sum b/go.sum index fc70406..3bfbda3 100644 --- a/go.sum +++ b/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/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=