Skip to content

Commit

Permalink
chore: make force self close tags func public, docstring
Browse files Browse the repository at this point in the history
  • Loading branch information
carlmontanari committed Jan 22, 2023
1 parent aace0ab commit 7a211e9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
9 changes: 7 additions & 2 deletions driver/netconf/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ func (d *Driver) RPC(opts ...util.Option) (*response.NetconfResponse, error) {
return d.sendRPC(d.buildRPCElem(op.Filter), op)
}

func forceSelfClosingTags(b []byte) []byte {
// ForceSelfClosingTags accepts a netconf looking xml byte slice and forces any "empty" tags (tags
// without attributes) to use self-closing tags. For example:
// `<running> </running>`
// Would be converted to:
// `<running/>`.
func ForceSelfClosingTags(b []byte) []byte {
ncPatterns := getNetconfPatterns()

r := ncPatterns.emptyTags.ReplaceAll(b, []byte("<$1$2/>"))
Expand All @@ -46,7 +51,7 @@ func (d *Driver) sendRPC(
if d.ForceSelfClosingTags {
d.Logger.Debug("ForceSelfClosingTags is true, enforcing...")

b = forceSelfClosingTags(b)
b = ForceSelfClosingTags(b)
}

d.Logger.Debugf("sending finalized rpc payload:\n%s", string(b))
Expand Down
6 changes: 4 additions & 2 deletions driver/netconf/rpc_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package netconf
package netconf_test

import (
"testing"

"github.com/scrapli/scrapligo/driver/netconf"

"github.com/google/go-cmp/cmp"
)

Expand Down Expand Up @@ -41,7 +43,7 @@ func TestForceSelfClosingTags(t *testing.T) {

for name, tt := range tests {
t.Run(name, func(t *testing.T) {
got := forceSelfClosingTags(tt.got)
got := netconf.ForceSelfClosingTags(tt.got)
if !cmp.Equal(got, tt.want) {
t.Fatalf(
"%s: actual and expected values do not match\nactual: %s\nexpected:%s",
Expand Down

0 comments on commit 7a211e9

Please sign in to comment.