Skip to content

Commit

Permalink
fix issues w/xml reader
Browse files Browse the repository at this point in the history
  • Loading branch information
dhubler committed Nov 23, 2023
1 parent 9dc8fe3 commit 3be8d98
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
32 changes: 31 additions & 1 deletion nodeutil/xml_rdr.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,17 @@ import (
"github.com/freeconf/yang/val"
)

func ReadXML(buf io.Reader) (*XmlNode, error) {
// ReadXMLDoc to read xml doc where root node is assumed to be the correct
// element.
//
// <my-module>
// <my-child>...
//
// module my-module {
// container my-child {...
//
// NewBrowser(myModule, ReadXMLDoc(myXmlDoc))
func ReadXMLDoc(buf io.Reader) (*XmlNode, error) {
dec := xml.NewDecoder(buf)
var n XmlNode
err := dec.Decode(&n)
Expand All @@ -21,6 +31,26 @@ func ReadXML(buf io.Reader) (*XmlNode, error) {
return &n, nil
}

// ReadXMLBlock to read xml doc where root node is assumed to be the correct
// element.
//
// <my-child>
// <my-item>...
//
// module my-module {
// container my-child {...
//
// b := NewBrowser(myModule, myApp)
// sel, _ := b.Root().Find("my-child")
// sel.UpsertFrom(ReadXMLBlock(myXmlDoc))
func ReadXMLBlock(buf io.Reader) (*XmlNode, error) {
n, err := ReadXMLDoc(buf)
if err != nil {
return nil, err
}
return &XmlNode{Nodes: []*XmlNode{n}}, nil
}

type XmlNode struct {
XMLName xml.Name
Attrs []xml.Attr `xml:"-"`
Expand Down
2 changes: 1 addition & 1 deletion nodeutil/xml_rdr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ module xml-test {
}

func readXml(data string) *XmlNode {
n, err := ReadXML(strings.NewReader(data))
n, err := ReadXMLDoc(strings.NewReader(data))
if err != nil {
panic(err)
}
Expand Down

0 comments on commit 3be8d98

Please sign in to comment.