Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: "1.22.x"
go-version: "stable"
cache: true
- name: Build
shell: bash
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: "1.22.x"
go-version: "stable"
cache: true
- name: Format check
run: test -z "$(gofmt -l .)" || (gofmt -l . && exit 1)
Expand Down
34 changes: 30 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,39 @@ func JSONToYAML(data []byte) ([]byte, error) {
}
return nil, fmt.Errorf("invalid JSON: %w", err)
}
output, err := yaml.Marshal(value)
output, err := yaml.Marshal(numbersToYAMLNodes(value))
if err != nil {
return nil, fmt.Errorf("convert JSON to YAML: %w", err)
}
return output, nil
}

// numbersToYAMLNodes replaces json.Number values with YAML scalar nodes tagged
// as !!int or !!float. Without this, yaml.v3 sees json.Number as a string and
// emits quoted values ("1"), which silently changes the data type.
func numbersToYAMLNodes(value any) any {
switch v := value.(type) {
case json.Number:
tag := "!!int"
if strings.ContainsAny(string(v), ".eE") {
tag = "!!float"
}
return &yaml.Node{Kind: yaml.ScalarNode, Tag: tag, Value: string(v)}
case map[string]any:
for key, item := range v {
v[key] = numbersToYAMLNodes(item)
}
return v
case []any:
for i, item := range v {
v[i] = numbersToYAMLNodes(item)
}
return v
default:
return value
}
}

func yamlNodeToJSONCompatible(node *yaml.Node) (any, error) {
switch node.Kind {
case yaml.DocumentNode:
Expand Down Expand Up @@ -257,7 +283,7 @@ func run(args []string) error {
return nil
case "json":
if len(args) < 2 {
return errors.New("json command requires format, to-yaml, or tojson")
return errors.New("json command requires format or to-yaml")
}
switch args[1] {
case "format":
Expand Down Expand Up @@ -285,7 +311,7 @@ func run(args []string) error {
}
case "yaml":
if len(args) < 2 {
return errors.New("yaml command requires to-json or tojson")
return errors.New("yaml command requires to-json")
}
switch args[1] {
case "to-json", "tojson", "2json":
Expand Down Expand Up @@ -379,7 +405,7 @@ func runPortCheck(args []string) error {
}
timeout := time.Duration(timeoutSeconds * float64(time.Second))
if err := CheckTCPPort(remaining[0], port, timeout); err != nil {
fmt.Fprintf(os.Stdout, "closed: %s\n", err)
fmt.Fprintln(os.Stdout, "closed")
return err
}
fmt.Fprintln(os.Stdout, "open")
Expand Down
24 changes: 24 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,27 @@ func TestCLIEndToEnd(t *testing.T) {
func TestMain(m *testing.M) {
os.Exit(m.Run())
}

func TestJSONToYAMLKeepsNumberTypes(t *testing.T) {
input := []byte(`{"int":1,"neg":-7,"float":1.5,"big":12345678901234567890,"nested":[3,{"x":4}],"str":"5"}`)
yamlOutput, err := JSONToYAML(input)
if err != nil {
t.Fatalf("JSONToYAML() error = %v", err)
}
for _, quoted := range []string{`"1"`, `"-7"`, `"1.5"`, `"12345678901234567890"`} {
if bytes.Contains(yamlOutput, []byte(quoted)) {
t.Fatalf("JSONToYAML() quoted a number %s:\n%s", quoted, yamlOutput)
}
}
if !bytes.Contains(yamlOutput, []byte(`str: "5"`)) {
t.Fatalf("JSONToYAML() lost the string type of \"5\":\n%s", yamlOutput)
}
back, err := YAMLToJSON(yamlOutput)
if err != nil {
t.Fatalf("YAMLToJSON() error = %v", err)
}
want, _ := FormatJSON(input)
if string(back) != string(want) {
t.Fatalf("round trip changed data:\n got %s\nwant %s", back, want)
}
}
Loading