Skip to content

Commit

Permalink
rename jsontoml flag to -use-json-number
Browse files Browse the repository at this point in the history
  • Loading branch information
dangra committed Jan 24, 2024
1 parent 7b5aa9f commit 32c7de0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
6 changes: 4 additions & 2 deletions cmd/jsontoml/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ Reading from a file:
`

var (
useNumber = flag.Bool("use-number", false, "Tells the json decoder to unmarshal numbers into json.Number type instead of float64")
useJsonNumber bool
)

func main() {
flag.BoolVar(&useJsonNumber, "use-json-number", false, "unmarshal numbers into `json.Number` type instead of as `float64`")

p := cli.Program{
Usage: usage,
Fn: convert,
Expand All @@ -52,7 +54,7 @@ func convert(r io.Reader, w io.Writer) error {
d := json.NewDecoder(r)
e := toml.NewEncoder(w)

if useNumber != nil && *useNumber {
if useJsonNumber {
d.UseNumber()
e.SetMarshalJsonNumbers(true)
}
Expand Down
20 changes: 8 additions & 12 deletions cmd/jsontoml/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (

func TestConvert(t *testing.T) {
examples := []struct {
name string
input string
expected string
errors bool
useNumber bool
name string
input string
expected string
errors bool
useJsonNumber bool
}{
{
name: "valid json",
Expand All @@ -30,8 +30,8 @@ a = 42.0
`,
},
{
name: "use json number",
useNumber: true,
name: "use json number",
useJsonNumber: true,
input: `
{
"mytoml": {
Expand All @@ -49,13 +49,9 @@ a = 42
},
}

trueValue := true

for _, e := range examples {
b := new(bytes.Buffer)
if e.useNumber {
useNumber = &trueValue
}
useJsonNumber = e.useJsonNumber
err := convert(strings.NewReader(e.input), b)
if e.errors {
require.Error(t, err)
Expand Down

0 comments on commit 32c7de0

Please sign in to comment.