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

flow and inline structure tags don't work together #1032

Open
palage4a opened this issue Apr 19, 2024 · 0 comments
Open

flow and inline structure tags don't work together #1032

palage4a opened this issue Apr 19, 2024 · 0 comments

Comments

@palage4a
Copy link

palage4a commented Apr 19, 2024

I am trying to inline structure with flow style serialization.
If i use flow and inline tags together, then only inline works, but flow doesn't.

As workaround of this situation i had to build yaml node by myself. For example:

func newFlowMapNode(p map[string]int) *yaml.Node {
	n := &yaml.Node{
		Kind:    yaml.MappingNode,
		Style:   yaml.FlowStyle,
		Content: []*yaml.Node{},
	}

	for k, v := range p {
		n.Content = append(n.Content, &yaml.Node{Kind: yaml.ScalarNode, Value: k})
		n.Content = append(n.Content, &yaml.Node{Kind: yaml.ScalarNode, Value: fmt.Sprintf("%d", v)})
	}

	return n
}

Then if i am trying to serialize this yaml.Node into string i can got desired result like {a: 1, b: 2}.

Repro test

package main

import (
	"testing"

	"gopkg.in/yaml.v3"
)

type InlineFlow struct {
	Map map[string]int `yaml:",inline,flow"`
}

type FlowInline struct {
	Map map[string]int `yaml:",flow,inline"`
}

func TestYamlFlowInline(t *testing.T) {
	data := map[string]int{"a": 1}

	a := &InlineFlow{Map: data}
	b := &FlowInline{Map: data}

	resA, err := yaml.Marshal(a)
	if err != nil {
		t.Error(err)
	}

	expected := "{a : 1}"

	if string(resA) != expected {
		t.Errorf("wrong result: expected: %s, actual: %s", expected, resA)
	}

	resB, err := yaml.Marshal(b)
	if err != nil {
		t.Error(err)
	}

	if string(resB) != "{a: 1}" {
		t.Errorf("wrong result: expected: %s, actual: %s", expected, resB)
	}

}

Thank you!

@palage4a palage4a changed the title Tags flow and inline don't work together flow and inline tags don't work together Apr 19, 2024
@palage4a palage4a changed the title flow and inline tags don't work together flow and inline structure tags don't work together Apr 19, 2024
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

1 participant