Skip to content

Commit

Permalink
Make Invoice.XML(), Invoice.XMLIndent() return the xml header declara…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
printesoi committed Mar 26, 2024
1 parent 92151cb commit dbfb9ba
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
12 changes: 12 additions & 0 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,15 @@ func typeNameAddrPtr(v any) string {
}
return rt.Name()
}

func concatBytes(s ...[]byte) []byte {
n := 0
for _, v := range s {
n += len(v)
}
res := make([]byte, 0, n)
for _, v := range s {
res = append(res, v...)
}
return res
}
4 changes: 2 additions & 2 deletions invoice.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,14 @@ func (iv Invoice) MarshalXML(e *xml.Encoder, start xml.StartElement) error {

// XML returns the XML encoding of the Invoice
func (iv Invoice) XML() ([]byte, error) {
return MarshalXML(iv)
return MarshalXMLWithHeader(iv)
}

// XMLIndent works like XML, but each XML element begins on a new
// indented line that starts with prefix and is followed by one or more
// copies of indent according to the nesting depth.
func (iv Invoice) XMLIndent(prefix, indent string) ([]byte, error) {
return MarshalIndentXML(iv, prefix, indent)
return MarshalIndentXMLWithHeader(iv, prefix, indent)
}

// UnmarshalInvoice unmarshals an Invoice from XML data. Only use this method
Expand Down
25 changes: 23 additions & 2 deletions xml_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,18 +191,39 @@ func NewIDNode(id string) *IDNode {

// MarshalXML returns the XML encoding of v in Canonical XML form [XML-C14N].
// This method must be used for marshaling objects from this library, instead
// of encoding/xml.
// of encoding/xml. This method does NOT include the XML header declaration.
func MarshalXML(v any) ([]byte, error) {
return xml.Marshal(v)
}

// MarshalXMLWithHeader same as MarshalXML, but also add the XML header
// declaration.
func MarshalXMLWithHeader(v any) ([]byte, error) {
data, err := MarshalXML(v)
if err != nil {
return nil, err
}
return concatBytes([]byte(xml.Header), data), nil
}

// MarshalIndentXML works like MarshalXML, but each XML element begins on a new
// indented line that starts with prefix and is followed by one or more
// copies of indent according to the nesting depth.
// copies of indent according to the nesting depth. This method does NOT
// include the XML header declaration.
func MarshalIndentXML(v any, prefix, indent string) ([]byte, error) {
return xml.MarshalIndent(v, prefix, indent)
}

// MarshalIndentXMLWithHeader same as MarshalIndentXML, but also add the XML
// header declaration.
func MarshalIndentXMLWithHeader(v any, prefix, indent string) ([]byte, error) {
data, err := MarshalIndentXML(v, prefix, indent)
if err != nil {
return nil, err
}
return concatBytes([]byte(xml.Header), data), nil
}

// Unmarshal parses the XML-encoded data and stores the result in
// the value pointed to by v, which must be an arbitrary struct,
// slice, or string. Well-formed data that does not fit into v is
Expand Down

0 comments on commit dbfb9ba

Please sign in to comment.