Skip to content

Commit

Permalink
fix: hopefully better force self close tags setup
Browse files Browse the repository at this point in the history
  • Loading branch information
carlmontanari committed Aug 12, 2023
1 parent f71219f commit 9d692c8
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 14 deletions.
2 changes: 1 addition & 1 deletion driver/netconf/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const (
// emptyTagPattern matches netconf empty tags to allow
// forcing of self-closing tags.
// See https://regex101.com/r/rmsS2E/3.
emptyTagPattern = `<([^>/]+?)(\s+[^>]+?)?>\s*</[\w-]+>`
emptyTagPattern = `<([^>/]+?)(\s+[^>]+?)?>\s*</([\w-]+)>`

defaultNamespace = "urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults"

Expand Down
50 changes: 50 additions & 0 deletions driver/netconf/message.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package netconf

import (
"bytes"
"encoding/xml"
"fmt"
)
Expand Down Expand Up @@ -47,3 +48,52 @@ func (m *message) serialize(v string, forceSelfClosingTags bool) (*serializedInp

return serialized, nil
}

// 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/>`.
//
// Ideally this functino would just be replaced with this: https://github.com/golang/go/issues/59710
// but for now this is more preferred than having either a different regex library imported, or
// importing @ECUST_XX proposed package. Historically, this was simply a regex.ReplaceAll but, there
// are/were issues with the pattern accidentally matching over already self closed tags inside
// other tags, for example:
//
// `<target><candidate xyz/></target>`
//
// Would end up like:
//
// `<target><candidate xyz//>
//
// Which is obviously not correct! This could be pretty easily solved in regex with backtracking/
// capture groups but since we don't get that in std library go regex we can instead just write some
// simple code to iterate over stuff and replace as needed after comparing the open/close tags that
// our pattern found actually do match.
func ForceSelfClosingTags(b []byte) []byte {
ncPatterns := getNetconfPatterns()

for _, sm := range ncPatterns.emptyTags.FindAllSubmatch(b, -1) {
fullMatch := sm[0]
openingTag := sm[1]
openingTagContents := sm[2]
closingTag := sm[3]

if !bytes.Equal(openingTag, closingTag) {
// we found a chunk that contains an already "self closed" tag, ignore this
continue
}

b = bytes.ReplaceAll(
b,
fullMatch,
[]byte(fmt.Sprintf("<%s%s/>", openingTag, openingTagContents)),
)
}

return b
}
13 changes: 0 additions & 13 deletions driver/netconf/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,6 @@ func (d *Driver) RPC(opts ...util.Option) (*response.NetconfResponse, error) {
return d.sendRPC(d.buildRPCElem(op.Filter), op)
}

// 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/>"))

return r
}

func (d *Driver) sendRPC(
m *message,
op *OperationOptions,
Expand Down
8 changes: 8 additions & 0 deletions driver/netconf/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ func TestForceSelfClosingTags(t *testing.T) {
got: []byte(`<running> </running>`),
want: []byte(`<running/>`),
},
"dont_replace_already_self_closed_tags": {
got: []byte(
`<target><candidate xmlns="urn:nokia.com:sros:ns:yang:sr:ietf-netconf-augments"/></target>`, //nolint: lll
),
want: []byte(
`<target><candidate xmlns="urn:nokia.com:sros:ns:yang:sr:ietf-netconf-augments"/></target>`, //nolint: lll
),
},
}

for name, tt := range tests {
Expand Down

0 comments on commit 9d692c8

Please sign in to comment.