Skip to content

Commit

Permalink
Add UnmarshalInvoice helper and README fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
printesoi committed Mar 22, 2024
1 parent 87611e5 commit 3a64e24
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
34 changes: 28 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Package efactura provides a client for using the ANAF e-factura API.

## Installation ##

e-factura-go requires Go version >= 1.20. With Go installed:
e-factura-go requires Go version >= 1.21. With Go installed:

```bash
go get github.com/printesoi/e-factura-go
Expand Down Expand Up @@ -229,8 +229,30 @@ if err != nil {
}
```
To get the XML with indentation:
```go
xmlData, err := invoice.XMLIndent("", " ")
if err != nil {
// Handle error
}
```
**NOTE** Don't use the standard `encoding/xml` package for generating the XML
encoding, since it does not produce Canonical XML [XML-C14N]
encoding, since it does not produce Canonical XML [XML-C14N]!
## Unmarshal XML to invoice ##
```go
var invoice efactura.Invoice
if err := efactura.UnmarshalInvoice(data, &invoice); err != nil {
// Handle error
}
```
**NOTE** Only use efactura.UnmarshalInvoice, because `encoding/xml` package
cannot unmarshal a struct like efactura.Invoice due to namespace prefixes!
## Tasks ##
Expand Down Expand Up @@ -258,8 +280,8 @@ Pull requests are more than welcome :)
This library is distributed under the Apache License version 2.0 found in the
[LICENSE](./LICENSE) file.
## Support me ##
If you like this project and want to support me to make it better you can
## Commercial support ##
<a href="https://www.buymeacoffee.com/printesoi" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/default-blue.png" alt="Buy Me A Coffee" height="41" width="174"></a>
If you need help integrating this library in your software or you need
consulting services regarding e-factura APIs contact me (contact email in my
[Github profile](https://github.com/printesoi)).
8 changes: 8 additions & 0 deletions invoice.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,14 @@ func (iv Invoice) XMLIndent(prefix, indent string) ([]byte, error) {
return b.Bytes(), nil
}

// UnmarshalInvoice unmarshals an Invoice from XML data. Only use this method
// for unmarshaling an Invoice, since the standard encoding/xml cannot
// properly unmarshal a struct like Invoice due to namespace prefixes. This
// method does not check if the unmarshaled Invoice is valid.
func UnmarshalInvoice(xmlData []byte, invoice *Invoice) error {
return xml.Unmarshal(xmlData, invoice)
}

type InvoiceBillingReference struct {
InvoiceDocumentReference InvoiceDocumentReference `xml:"urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2 InvoiceDocumentReference"`
}
Expand Down

0 comments on commit 3a64e24

Please sign in to comment.