Skip to content

Commit

Permalink
jsontoml tool (#296)
Browse files Browse the repository at this point in the history
`jsontoml` is very similar to `tomljson`
It uses json.Unmarshal to convert read json to map and then
converts the map to tree using `toml.TreeFromMap`.
Then this tree is converted to toml using `tree.toTomlString()`
The numbers when taken as input from json get converted to float64
because of how `json.Unmarshal()` converts all json numbers to float.

Fixes #280
  • Loading branch information
gdhameeja authored and pelletier committed Sep 6, 2019
1 parent 3ded2e0 commit 4d5afd7
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ jobs:
test_name: "tomljson"
module: "github.com/pelletier/go-toml/cmd/tomljson"
allow_fail: <<parameters.allow_fail>>
- run_test:
test_name: "jsontoml"
module: "github.com/pelletier/go-toml/cmd/jsontoml"
allow_fail: <<parameters.allow_fail>>
- run_test:
test_name: "tomll"
module: "github.com/pelletier/go-toml/cmd/tomll"
Expand Down
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ script:
- if [ -n "$(go fmt ./...)" ]; then exit 1; fi
- go test github.com/pelletier/go-toml -race -coverprofile=coverage.txt -covermode=atomic
- go test github.com/pelletier/go-toml/cmd/tomljson
- go test github.com/pelletier/go-toml/cmd/jsontoml
- go test github.com/pelletier/go-toml/cmd/tomll
- go test github.com/pelletier/go-toml/query
- ./benchmark.sh $TRAVIS_BRANCH https://github.com/$TRAVIS_REPO_SLUG.git
Expand Down
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ RUN go install ./...
FROM scratch
COPY --from=builder /go/bin/tomll /usr/bin/tomll
COPY --from=builder /go/bin/tomljson /usr/bin/tomljson
COPY --from=builder /go/bin/jsontoml /usr/bin/jsontoml
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ Go-toml provides two handy command line tools:
go install github.com/pelletier/go-toml/cmd/tomljson
tomljson --help
```

* `jsontoml`: Reads a JSON file and outputs a TOML representation.

```
go install github.com/pelletier/go-toml/cmd/jsontoml
jsontoml --help
```

### Docker image

Expand Down
1 change: 1 addition & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ deploy: false
test_script:
- go test github.com/pelletier/go-toml
- go test github.com/pelletier/go-toml/cmd/tomljson
- go test github.com/pelletier/go-toml/cmd/jsontoml
- go test github.com/pelletier/go-toml/cmd/tomll
- go test github.com/pelletier/go-toml/query
81 changes: 81 additions & 0 deletions cmd/jsontoml/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Jsontoml reads JSON and converts to TOML.
//
// Usage:
// cat file.toml | jsontoml > file.json
// jsontoml file1.toml > file.json
package main

import (
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"os"

"github.com/pelletier/go-toml"
)

func main() {
flag.Usage = func() {
fmt.Fprintln(os.Stderr, "jsontoml can be used in two ways:")
fmt.Fprintln(os.Stderr, "Writing to STDIN and reading from STDOUT:")
fmt.Fprintln(os.Stderr, "")
fmt.Fprintln(os.Stderr, "")
fmt.Fprintln(os.Stderr, "Reading from a file name:")
fmt.Fprintln(os.Stderr, " tomljson file.toml")
}
flag.Parse()
os.Exit(processMain(flag.Args(), os.Stdin, os.Stdout, os.Stderr))
}

func processMain(files []string, defaultInput io.Reader, output io.Writer, errorOutput io.Writer) int {
// read from stdin and print to stdout
inputReader := defaultInput

if len(files) > 0 {
file, err := os.Open(files[0])
if err != nil {
printError(err, errorOutput)
}
inputReader = file
defer file.Close()
}
s, err := reader(inputReader)
if err != nil {
printError(err, errorOutput)
return -1
}
io.WriteString(output, s)
return 0
}

func printError(err error, output io.Writer) {
io.WriteString(output, err.Error()+"\n")
}

func reader(r io.Reader) (string, error) {
jsonMap := make(map[string]interface{})
jsonBytes, err := ioutil.ReadAll(r)
if err != nil {
return "", err
}
error := json.Unmarshal(jsonBytes, &jsonMap)
if error != nil {
return "", error
}

tree, err := toml.TreeFromMap(jsonMap)
if err != nil {
return "", err
}
return mapToTOML(tree)
}

func mapToTOML(t *toml.Tree) (string, error) {
tomlBytes, err := t.ToTomlString()
if err != nil {
return "", err
}
return string(tomlBytes[:]), nil
}
93 changes: 93 additions & 0 deletions cmd/jsontoml/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package main

import (
"bytes"
"io/ioutil"
"os"
"runtime"
"strings"
"testing"
)

func expectBufferEquality(t *testing.T, name string, buffer *bytes.Buffer, expected string) {
output := buffer.String()
if output != expected {
t.Errorf("incorrect %s: \n%sexpected %s: \n%s", name, output, name, expected)
t.Log([]rune(output))
t.Log([]rune(expected))
}
}

func expectProcessMainResults(t *testing.T, input string, args []string, exitCode int, expectedOutput string, expectedError string) {
inputReader := strings.NewReader(input)

outputBuffer := new(bytes.Buffer)
errorBuffer := new(bytes.Buffer)

returnCode := processMain(args, inputReader, outputBuffer, errorBuffer)

expectBufferEquality(t, "output", outputBuffer, expectedOutput)
expectBufferEquality(t, "error", errorBuffer, expectedError)

if returnCode != exitCode {
t.Error("incorrect return code:", returnCode, "expected", exitCode)
}
}

func TestProcessMainReadFromStdin(t *testing.T) {
expectedOutput := `
[mytoml]
a = 42.0
`
input := `{
"mytoml": {
"a": 42
}
}
`
expectedError := ``
expectedExitCode := 0

expectProcessMainResults(t, input, []string{}, expectedExitCode, expectedOutput, expectedError)
}

func TestProcessMainReadFromFile(t *testing.T) {
input := `{
"mytoml": {
"a": 42
}
}
`
tmpfile, err := ioutil.TempFile("", "example.json")
if err != nil {
t.Fatal(err)
}
if _, err := tmpfile.Write([]byte(input)); err != nil {
t.Fatal(err)
}

defer os.Remove(tmpfile.Name())

expectedOutput := `
[mytoml]
a = 42.0
`
expectedError := ``
expectedExitCode := 0

expectProcessMainResults(t, ``, []string{tmpfile.Name()}, expectedExitCode, expectedOutput, expectedError)
}

func TestProcessMainReadFromMissingFile(t *testing.T) {
var expectedError string
if runtime.GOOS == "windows" {
expectedError = `open /this/file/does/not/exist: The system cannot find the path specified.
`
} else {
expectedError = `open /this/file/does/not/exist: no such file or directory
invalid argument
`
}

expectProcessMainResults(t, ``, []string{"/this/file/does/not/exist"}, -1, ``, expectedError)
}

0 comments on commit 4d5afd7

Please sign in to comment.