From f65dc3191edb4b2b00d385f0575916a159918ab1 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 23 Nov 2026 03:02:24 +0330 Subject: [PATCH 1/2] Keep number types in JSON to YAML conversion json.Number values were emitted as quoted YAML strings (1 -> "1"), so a JSON -> YAML -> JSON round trip silently changed types. Also fix misleading error messages and a duplicated port-check error. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0183H1i14AVAdwx5KFXBDo7Q --- main.go | 34 ++++++++++++++++++++++++++++++---- main_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 7c3b7d8..e9a3387 100644 --- a/main.go +++ b/main.go @@ -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: @@ -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": @@ -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": @@ -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") diff --git a/main_test.go b/main_test.go index 0bb9fc3..b5b134a 100644 --- a/main_test.go +++ b/main_test.go @@ -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) + } +} From 1330c3c4984214cae4f71173b0d8feae8136bd7c Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 23 Nov 2026 03:18:51 +0330 Subject: [PATCH 2/2] CI: build with current stable Go Go 1.22 binaries lack the LC_UUID load command that current macOS dyld requires, so the macOS test job aborted before running tests (also failing on the base branch). go.mod still declares 1.22 as the minimum language version. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0183H1i14AVAdwx5KFXBDo7Q --- .github/workflows/release.yml | 2 +- .github/workflows/test.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 932452e..af69f7f 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -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 diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b26b366..042ac1f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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)