Upgrade ghodss/yaml to use go-yaml v3#62
Conversation
This is a breaking change because: - Strict mode is now the default for yaml.v3 - String-valued boolean support has been dropped for YAML 1.2 spec compliance - Default indentation changes (we could set to previous value but since already breaking might make more sense to use base library default) As such I have appended the /v2 suffix to the github.com/ghodss/yaml package name. Signed-off-by: Silas Davis <silas@monax.io>
5f682c5 to
f34003f
Compare
|
Thanks for looking into this @silasdavis! |
|
is there any release date planned for this? |
|
I am also very interested in getting this merged, as it solves some serious issues in Helm. |
|
Just changing the module name doesn't seem right. We'd want a way to maintain both major branches. |
|
Like a top level |
|
I feel like it's traditional to have just the |
|
Said blog post actually lays out a pretty logical practice for multi-version module maintenance which doesn't require a long-term maintenance branch. |
|
I'm hoping to find some time over the holidays to take the work @silasdavis did and migrate it into the appropriately versioned module. |
|
I would to suggest making v2 a pre-release version in order to make more important breaking changes without need to make v3. import (
"bytes"
"fmt"
"strings"
"gopkg.in/yaml.v3"
)
// JSONToYAML transforms JSON to YAML keeping keys order.
func JSONToYAML(jsonData []byte) ([]byte, error) {
return NewMarshaler().JSONToYAML(jsonData)
}
func NewMarshaler(os ...MarshalOption) *Marshaler {
opts := &marshalOptions{
Intend: 4, // Default for yaml.v3 package.
}
for _, o := range os {
o(opts)
}
m := &Marshaler{}
m.enc = yaml.NewEncoder(&m.buf)
m.enc.SetIndent(opts.Intend)
return m
}
type Marshaler struct {
buf bytes.Buffer
enc *yaml.Encoder
}
func (m *Marshaler) JSONToYAML(jsonData []byte) ([]byte, error) {
n := &yaml.Node{}
err := yaml.Unmarshal(jsonData, n)
if err != nil {
return nil, err
}
jsonToYAMLFormat(n)
m.buf.Reset()
err = m.enc.Encode(n)
if err != nil {
return nil, fmt.Errorf("marshal formated: %w", err)
}
return m.buf.Bytes(), nil
}
type MarshalOption func(opts *marshalOptions)
type marshalOptions struct {
Intend int
}
func Indent(n int) MarshalOption { return func(opts *marshalOptions) {opts.Intend = n } }
func jsonToYAMLFormat(n *yaml.Node) {
if n == nil {
return
}
switch n.Kind {
case yaml.SequenceNode, yaml.MappingNode:
n.Style = yaml.LiteralStyle
case yaml.ScalarNode:
if n.Style == yaml.DoubleQuotedStyle {
n.Style = yaml.FlowStyle
if strings.Contains(n.Value, "\n") {
n.Style = yaml.LiteralStyle
}
}
}
for _, c := range n.Content {
jsonToYAMLFormat(c)
}
}Seems idea of formatting |
|
Any movement on this. Would love to get rid of yaml.v2 from Podman, but need this to get merged. |
|
@bradfitz Please merge this so we can remove yaml.v2 from vendored directories. |
Bump gopkg.in/yaml.v2 to v2.4.0: fixes inconsistent long lines wrapping
|
@skipor Thank you for this proof-of-concept! I opened a PR to this PR that does exactly this: silasdavis#1. |
This commit adds the `OrderedPropertyKeys` method to the
`openapi3.Schema`:
OrderedPropertyKeys returns the keys of the properties in the order
they were defined. This is useful for generating code that needs to
iterate over the properties in a consistent order. If the keys could
not be extracted for some reason, then this method automatically
sorts the keys to be deterministic.
This is done via a temporary fork of the YAML-to-JSON transformation library.
It will not be ready until these PRs are merged in this order:
- silasdavis/yaml#1
- ghodss/yaml#62
This commit adds the `OrderedPropertyKeys` method to the
`openapi3.Schema`:
OrderedPropertyKeys returns the keys of the properties in the order
they were defined. This is useful for generating code that needs to
iterate over the properties in a consistent order. If the keys could
not be extracted for some reason, then this method automatically
sorts the keys to be deterministic.
This is done via a temporary fork of the YAML-to-JSON transformation library.
It will not be ready until these PRs are merged in this order:
- silasdavis/yaml#1
- ghodss/yaml#62
This is a breaking change because:
compliance
already breaking might make more sense to use base library default)
As such I have appended the /v2 suffix to the github.com/ghodss/yaml
package name.
fixes #61
One major side-effect of this upgrade (and the one I am after) is that rountrip coding of YAML files does not incinerate comments.
Signed-off-by: Silas Davis silas@monax.io