Skip to content

Commit

Permalink
fix: avoid unnecessary nesting when generating yaml
Browse files Browse the repository at this point in the history
Also add more example snippets that can be used for testing
  • Loading branch information
micovery committed Apr 10, 2024
1 parent a71749d commit d9f56f5
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 2 deletions.
2 changes: 1 addition & 1 deletion examples/snippets/check-quota.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
<SyncIntervalInSeconds>20</SyncIntervalInSeconds>
<SyncMessageCount>5</SyncMessageCount>
</AsynchronousConfiguration>
</Quota>
</Quota>
38 changes: 38 additions & 0 deletions examples/snippets/raise-fault.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<RaiseFault async="false" continueOnError="true" enabled="true" name="RF-Example" >
<DisplayName>RF-Example</DisplayName>
<FaultResponse>
<AssignVariable >
<Name>flow.var</Name>
<Value>123</Value>
</AssignVariable>
<Add >
<Headers>
<Header name="user-agent" >example</Header>
</Headers>
</Add>
<Copy source="request" >
<Headers>
<Header name="header-name" ></Header>
</Headers>
<StatusCode>304</StatusCode>
</Copy>
<Remove >
<Headers>
<Header name="sample-header" ></Header>
</Headers>
</Remove>
<Set >
<Headers>
<Header name="user-agent" >{request.header.user-agent}</Header>
</Headers>
<Payload contentType="application/json" >{"name":"foo", "type":"bar"}</Payload>
</Set>
<Set >
<ReasonPhrase>Server Error</ReasonPhrase>
<StatusCode>500</StatusCode>
</Set>
</FaultResponse>
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
<ShortFaultReason>false</ShortFaultReason>
</RaiseFault>
38 changes: 38 additions & 0 deletions examples/snippets/raise-fault.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
RaiseFault:
.async: false
.continueOnError: true
.enabled: true
.name: RF-Example
DisplayName: RF-Example
FaultResponse:
- AssignVariable:
Name: flow.var
Value: 123
- Add:
Headers:
Header:
.name: user-agent
.@: example
- Copy:
.source: request
Headers:
Header:
.name: header-name
StatusCode: 304
- Remove:
Headers:
Header:
.name: sample-header
- Set:
Headers:
Header:
.name: user-agent
.@: '{request.header.user-agent}'
Payload:
.contentType: application/json
.@: '{"name":"foo", "type":"bar"}'
- Set:
ReasonPhrase: Server Error
StatusCode: 500
IgnoreUnresolvedVariables: true
ShortFaultReason: false
21 changes: 20 additions & 1 deletion pkg/utils/xml.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/go-errors/errors"
"gopkg.in/yaml.v3"
"io"
"slices"
"strings"
)

Expand Down Expand Up @@ -209,7 +210,7 @@ func XML2YAMLRecursive(ele *etree.Element) (key *yaml.Node, value *yaml.Node, er
}

if !allUnique {
//it's a sequence, parent has no attributes
//it's a sequence
sequence := &yaml.Node{Kind: yaml.SequenceNode}
sequence.Content = []*yaml.Node{}

Expand All @@ -222,15 +223,33 @@ func XML2YAMLRecursive(ele *etree.Element) (key *yaml.Node, value *yaml.Node, er
}

if nodeVal == children {
// parent has no attributes
nodeVal = sequence
} else {
// parent has
children.Kind = sequence.Kind
children.Content = sequence.Content
}

return nodeKey, nodeVal, nil
}

childrenIndex := slices.IndexFunc(nodeVal.Content, func(e *yaml.Node) bool {
return e.Value == ".@"
})

if childrenIndex > 0 && children != nodeVal &&
children.Kind == yaml.MappingNode &&
nodeVal.Kind == yaml.MappingNode &&
allUnique {
//remove unnecessary nesting
newContent := append([]*yaml.Node{}, nodeVal.Content[0:childrenIndex]...)
newContent = append(newContent, nodeVal.Content[childrenIndex+2:]...)
newContent = append(newContent, children.Content...)
nodeVal.Content = newContent
return nodeKey, nodeVal, nil
}

return nodeKey, nodeVal, nil

}

0 comments on commit d9f56f5

Please # to comment.