Skip to content

Commit

Permalink
Merge pull request #179 from goccy/feature/fix-recursive-flow-style
Browse files Browse the repository at this point in the history
Fix flow style for composite type
  • Loading branch information
goccy authored Nov 13, 2020
2 parents b92080a + 7540551 commit bdb73b4
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
35 changes: 35 additions & 0 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,14 @@ func (n *MappingNode) Merge(target *MappingNode) {
}
}

// SetIsFlowStyle set value to IsFlowStyle field recursively.
func (n *MappingNode) SetIsFlowStyle(isFlow bool) {
n.IsFlowStyle = isFlow
for _, value := range n.Values {
value.SetIsFlowStyle(isFlow)
}
}

// Read implements (io.Reader).Read
func (n *MappingNode) Read(p []byte) (int, error) {
return readNode(p, n)
Expand Down Expand Up @@ -1060,6 +1068,18 @@ func (n *MappingValueNode) AddColumn(col int) {
}
}

// SetIsFlowStyle set value to IsFlowStyle field recursively.
func (n *MappingValueNode) SetIsFlowStyle(isFlow bool) {
switch value := n.Value.(type) {
case *MappingNode:
value.SetIsFlowStyle(isFlow)
case *MappingValueNode:
value.SetIsFlowStyle(isFlow)
case *SequenceNode:
value.SetIsFlowStyle(isFlow)
}
}

// String mapping value to text
func (n *MappingValueNode) String() string {
space := strings.Repeat(" ", n.Key.GetToken().Position.Column-1)
Expand Down Expand Up @@ -1150,6 +1170,21 @@ func (n *SequenceNode) Merge(target *SequenceNode) {
}
}

// SetIsFlowStyle set value to IsFlowStyle field recursively.
func (n *SequenceNode) SetIsFlowStyle(isFlow bool) {
n.IsFlowStyle = isFlow
for _, value := range n.Values {
switch value := value.(type) {
case *MappingNode:
value.SetIsFlowStyle(isFlow)
case *MappingValueNode:
value.SetIsFlowStyle(isFlow)
case *SequenceNode:
value.SetIsFlowStyle(isFlow)
}
}
}

// Read implements (io.Reader).Read
func (n *SequenceNode) Read(p []byte) (int, error) {
return readNode(p, n)
Expand Down
4 changes: 2 additions & 2 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -538,12 +538,12 @@ func (e *Encoder) encodeStruct(ctx context.Context, value reflect.Value, column
}
if m, ok := value.(*ast.MappingNode); ok {
if !e.isFlowStyle && structField.IsFlow {
m.IsFlowStyle = true
m.SetIsFlowStyle(true)
}
value.AddColumn(e.indent)
} else if s, ok := value.(*ast.SequenceNode); ok {
if !e.isFlowStyle && structField.IsFlow {
s.IsFlowStyle = true
s.SetIsFlowStyle(true)
}
}
key := e.encodeString(structField.RenderName, column)
Expand Down
20 changes: 20 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,26 @@ func TestEncoder_Flow(t *testing.T) {
}
}

func TestEncoder_FlowRecursive(t *testing.T) {
var v struct {
M map[string][]int `yaml:",flow"`
}
v.M = map[string][]int{
"test": []int{1, 2, 3},
}
var buf bytes.Buffer
if err := yaml.NewEncoder(&buf).Encode(v); err != nil {
t.Fatalf("%+v", err)
}
expect := `
m: {test: [1, 2, 3]}
`
actual := "\n" + buf.String()
if expect != actual {
t.Fatalf("flow style marshal error: expect=[%s] actual=[%s]", expect, actual)
}
}

func TestEncoder_JSON(t *testing.T) {
var buf bytes.Buffer
enc := yaml.NewEncoder(&buf, yaml.JSON())
Expand Down

0 comments on commit bdb73b4

Please sign in to comment.