diff options
author | M. J. Fromberger <michael.j.fromberger@gmail.com> | 2019-06-09 08:31:03 -0700 |
---|---|---|
committer | M. J. Fromberger <michael.j.fromberger@gmail.com> | 2019-06-09 08:50:11 -0700 |
commit | 0d0bcc7333490eafad1440e9e37d4a497a47c971 (patch) | |
tree | b2a33fc0f6f3918f5009c16742fdad95acdc1871 | |
parent | f3b5c20cd9ea888707a2312497a8ab3b6593bb61 (diff) | |
download | go-creachadair-stringset-0d0bcc7333490eafad1440e9e37d4a497a47c971.tar.gz |
makeset: Use TOML instead of JSON for config files.
The JSON format is nice for the program, but is tedious for a human reader to
inspect and maintain due to the restrictive quoting and bracketing rules.
Switch to using TOML instead.
-rw-r--r-- | README.md | 4 | ||||
-rw-r--r-- | go.mod | 2 | ||||
-rw-r--r-- | go.sum | 2 | ||||
-rw-r--r-- | makeset/makeset.go | 29 |
4 files changed, 22 insertions, 15 deletions
@@ -12,4 +12,6 @@ around Go's built-in map type. The `stringset` package is generated by the `makeset` program. To modify the package, edit `makeset/makeset.go` and then run: - $ go run makeset/makeset.go -config makeset/stringset.json -output . +```shell +go run makeset/makeset.go -config makeset/stringset.toml -output . +``` @@ -1,3 +1,5 @@ module bitbucket.org/creachadair/stringset go 1.12 + +require github.com/BurntSushi/toml v0.3.1 @@ -0,0 +1,2 @@ +github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= diff --git a/makeset/makeset.go b/makeset/makeset.go index 6275b6a..939d6c4 100644 --- a/makeset/makeset.go +++ b/makeset/makeset.go @@ -1,15 +1,14 @@ // Program makeset generates source code for a set package. The type of the -// elements of the set is determined by a JSON configuration stored either in a +// elements of the set is determined by a TOML configuration stored either in a // file (named by the -config flag) or read from standard input. // // Usage: -// go run makeset.go -output $DIR -config config.json +// go run makeset.go -output $DIR -config config.toml // package main import ( "bytes" - "encoding/json" "errors" "flag" "fmt" @@ -21,27 +20,29 @@ import ( "sort" "strings" "text/template" + + "github.com/BurntSushi/toml" ) // A Config describes the nature of the set to be constructed. type Config struct { // A human-readable description of the set this config defines. // This is ignored by the code generator, but may serve as documentation. - Desc string `json:"desc,omitempty"` + Desc string // The name of the resulting set package, e.g., "intset" (required). - Package string `json:"package"` + Package string // The name of the type contained in the set, e.g., "int" (required). - Type string `json:"type"` + Type string // The spelling of the zero value for the set type, e.g., "0" (required). - Zero string `json:"zero"` + Zero string // If set, a type definition is added to the package mapping Type to this // structure, e.g., "struct { ... }". You may prefix Decl with "=" to // generate a type alias (this requires Go ≥ 1.9). - Decl string `json:"decl,omitempty"` + Decl string // If set, the body of a function with signature func(x, y Type) bool // reporting whether x is less than y. @@ -51,23 +52,23 @@ type Config struct { // return x[1] < y[1] // } // return x[0] < y[0] - Less string `json:"less,omitempty"` + Less string // If set, the body of a function with signature func(x Type) string that // converts x to a human-readable string. // // For example: // return strconv.Itoa(x) - ToString string `json:"toString,omitempty"` + ToString string // If set, additional packages to import in the generated code. - Imports []string `json:"imports,omitempty"` + Imports []string // If set, additional packages to import in the test. - TestImports []string `json:"testImports,omitempty"` + TestImports []string // If true, include transformations, e.g., Map, Partition, Each. - Transforms bool `json:"transforms,omitempty"` + Transforms bool // A list of exactly ten ordered test values used for the construction of // unit tests. If omitted, unit tests are not generated. @@ -108,7 +109,7 @@ func readConfig(path string) (*Config, error) { return nil, err } var c Config - if err := json.Unmarshal(data, &c); err != nil { + if err := toml.Unmarshal(data, &c); err != nil { return nil, err } |