From 3be8d985c9cb06c1ea99d81919ec865ae1845ec4 Mon Sep 17 00:00:00 2001 From: Douglas Hubler Date: Thu, 23 Nov 2023 05:32:35 -0500 Subject: [PATCH] fix issues w/xml reader --- nodeutil/xml_rdr.go | 32 +++++++++++++++++++++++++++++++- nodeutil/xml_rdr_test.go | 2 +- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/nodeutil/xml_rdr.go b/nodeutil/xml_rdr.go index 3c50210..4e3d75e 100644 --- a/nodeutil/xml_rdr.go +++ b/nodeutil/xml_rdr.go @@ -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. +// +// +// ... +// +// 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) @@ -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. +// +// +// ... +// +// 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:"-"` diff --git a/nodeutil/xml_rdr_test.go b/nodeutil/xml_rdr_test.go index d57e236..44e557a 100644 --- a/nodeutil/xml_rdr_test.go +++ b/nodeutil/xml_rdr_test.go @@ -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) }