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) 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) + } +}