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

Octal number marshalling #420

Open
ulyssessouza opened this issue Dec 18, 2018 · 8 comments
Open

Octal number marshalling #420

ulyssessouza opened this issue Dec 18, 2018 · 8 comments

Comments

@ulyssessouza
Copy link

Hello, I'm still not sure if it's a question or a feature request.
Is there is a way of marshalling an octal number?
Because when unmarshalling this :
number: 0660
I get:
number: 432

And if I implement Marshaler I get:
number: "0660" (the string "0660")
an not:
number: 0660 (the octal number)

When implementing jsonNumber I get the same

@niemeyer
Copy link
Contributor

It's a valid feature request. It should be easy to support that via something like an ",octal" field tag.

@ulyssessouza
Copy link
Author

@niemeyer The problem with an ",octal" field tag is that it's fixed.
At least in my case, the formatting will depend on the input, because my program's life cycle consists in unmarshal -> modify -> marshal. So the output format will depend on the input format.
The ideal would be using a "numeric struct" that has an integer and a string to map the value and format. Something like that.
WDYT?

@niemeyer
Copy link
Contributor

niemeyer commented Dec 18, 2018

I'm working on v3 at the moment, which will offer an intermediate state with significantly more control than what the package offers today. There's a quick draft of what it will look like here:

https://twitter.com/gniemeyer/status/1073638853225996288

With that you'll be able to marshal your value easily, with the exact formatting you want.

@ulyssessouza
Copy link
Author

Just for curiosity... (No pressure at all)
Is the v3 repository public?
Do you have any delivery estimation?

@niemeyer
Copy link
Contributor

I'll make it widely available soon. I just want to lock down on the compatibility breakages (MapSlice is coming out and be replaced by nodes, marshaling interfaces will also be based on Node, etc), and then will put it out for early testing.

@AvdN
Copy link

AvdN commented Feb 8, 2019

Finally another YAML library that recognises that comments warrant preserving in a human readable data format!

@shibe2
Copy link

shibe2 commented May 23, 2020

README.md says:

Octals encode and decode as 0777 per YAML 1.1, rather than 0o777 as specified in YAML 1.2, because most parsers still use the old format.

So, how do I encode integers to octal representation?

@shibe2
Copy link

shibe2 commented May 26, 2020

In the meantime, this is how you can encode with 0o prefix:

type OctalMode os.FileMode

func (self OctalMode) String() string {
	return "0o" + strconv.FormatUint(uint64(self), 8)
}

func (self OctalMode) MarshalYAML() (interface{}, error) {
	return &yaml.Node{
		Kind:  yaml.ScalarNode,
		Tag:   "!!int",
		Value: self.String(),
	}, nil
}

In fact, this way you can encode to any number format supported by decoder. One drawback to this approach is that you'll have to convert between the formatter type (here OctalMode) and the type you need (here os.FileMode). It would be easier if encoder had number format options.

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

No branches or pull requests

4 participants