Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unwanted int to float conversion #427

Open
xsilas opened this issue Feb 19, 2024 · 3 comments
Open

Unwanted int to float conversion #427

xsilas opened this issue Feb 19, 2024 · 3 comments
Labels
question Further information is requested

Comments

@xsilas
Copy link

xsilas commented Feb 19, 2024

Describe the bug
Using the Marshal function on a interface{} converts an integer to a float value despite the gopkg yamlv3 (gopkg.in/yaml.v3) does not:
Port somehow turns into a float:

  • docker:
    file: Dockerfile
    platform:
    • arch: amd64
      os: linux
    • arch: arm64
      os: linux
      ingresses:
    • host: www
      name: website
      ports:
    • 8080.0
      resources: {}
      user: "101"

To Reproduce

yamlv3Bytes, err := yamlv3.Marshal(someInterface)
if err != nil {
return err
}
fmt.Println(string(yamlv3Bytes))

yamlBytes, err := yaml.Marshal(someInterface)
if err != nil {
return err
}
fmt.Println(string(yamlBytes))

Expected behavior
Port should remain an int like so:

  • docker:
    file: Dockerfile
    platform:
    - arch: amd64
    os: linux
    - arch: arm64
    os: linux
    ingresses:
    • host: www
      name: website
      ports:
    • 8080
      resources: {}
      user: "101"

Screenshots
Bildschirmfoto vom 2024-02-19 11-32-17

Version Variables

  • Go version: 1.22
  • go-yaml's Version: v1.11.3

Additional context
If theres a mistake / missunderstanding on my side please let me know

@xsilas xsilas added the bug Something isn't working label Feb 19, 2024
@goccy
Copy link
Owner

goccy commented Nov 6, 2024

@xsilas I think this depends on the type of someInterface. If the type of port in someInterface is float64 ( or float32 ), then the correct approach is to change the type to int rather than modifying the encoder.

@goccy goccy added question Further information is requested and removed bug Something isn't working labels Nov 6, 2024
@scriptory
Copy link

Encountered this issue when trying to display epoch times. This contends to be the worst way to display such an integer. Google's yaml Marshal also has the same behavior, but their json Marshal does not.

package main

import (
	"fmt"
	"time"
	"encoding/json"
	yaml "github.com/goccy/go-yaml"
)

func main() {

	elem := make(map[string]int64)
	elem["now"] = time.Now().Unix()

	y, err := yaml.Marshal(elem)
	if err != nil {
		panic(err)
	}
	fmt.Print("#1: This is fine: " + string(y))

	j, err := json.Marshal(elem)
	if err != nil {
		panic(err)
	}

	var whatever interface{}
	err = json.Unmarshal(j, &whatever)
	if err != nil {
		panic(err)
	}

	j2, err := json.Marshal(whatever)
	if err != nil {
		panic(err)
	}
	fmt.Print("#2: This is fine: " + string(j2) + "\n")

	y2, err := yaml.Marshal(whatever)
	if err != nil {
		panic(err)
	}

	fmt.Print("#3: This is fugly: " + string(y2))
}

% ./fugly
#1: This is fine: now: 1731255645
#2: This is fine: {"now":1731255645}
#3: This is fugly: now: 1.731255645e+09

@scriptory
Copy link

I submitted a PR to fix this behavior, but it ran into your test suite failing for such things as rendering 1.0 as 1. Which to me is correct behavior, and is indeed correct for JSON but others disagree for YAML. But with YAML claiming to be a superset of JSON, I don't see how the behavior should differ. So I closed the PR and will use my fork instead.

Here's the reverse issue on Google's library
go-yaml/yaml#671

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants