-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
`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
Showing
7 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |