Skip to content

Commit

Permalink
Use printesoi/xml-go instead of m29h/xml for better xml encoding
Browse files Browse the repository at this point in the history
- Use github.com/printesoi/xml-go instead of github.com/m29h/xml for
  better Exclusive XML C14.
- Add XML and XMLIndent helper methods for Invoice
- Use Go 1.21, 1.22
- Show tests, coverage badges in README
  • Loading branch information
printesoi committed Mar 18, 2024
1 parent 3ddabb4 commit 0bfdcdf
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 28 deletions.
9 changes: 7 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ jobs:
strategy:
matrix:
go-version:
- '1.20.x'
- '1.21.x'
- '1.22.x'

steps:
- uses: actions/checkout@v4
Expand All @@ -22,4 +23,8 @@ jobs:
with:
go-version: ${{ matrix.go-version }}
- name: Coverage
run: go test -v ./...
run: go test -v -coverprofile=profile.cov ./...

- uses: shogo82148/actions-goveralls@v1
with:
path-to-profile: profile.cov
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# e-factura-go #
# e-factura-go [![Tests](https://github.com/printesoi/e-factura-go/actions/workflows/test.yml/badge.svg)](https://github.com/printesoi/e-factura-go/actions/workflows/test.yml) [![Coverage Status](https://coveralls.io/repos/github/printesoi/e-factura-go/badge.svg)](https://coveralls.io/github/printesoi/e-factura-go) [![Go Report Card](https://goreportcard.com/badge/github.com/printesoi/e-factura-go)](https://goreportcard.com/report/github.com/printesoi/e-factura-go)#

Package efactura provides a client for using the ANAF e-factura API.

Expand Down Expand Up @@ -208,11 +208,29 @@ if validateRes.IsOk() {
}
```
## Generating an Invoice
## Generating an Invoice ##
TODO: See TestInvoiceBuilder() from builders_test.go for an example of using
InvoiceBuilder for creating an Invoice.
## Getting the raw XML of the invoice ##
In case you need to get the XML encoding of the invoice (eg. you need to store
it somewhere before the upload):
```go
var invoice Invoice
// Build invoice (manually, or with the InvoiceBuilder)

xmlData, err := invoice.XML()
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]
## Tasks ##
- [ ] Implement all business terms.
Expand Down
1 change: 1 addition & 0 deletions builders.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,7 @@ func (b *InvoiceBuilder) Build() (retInvoice Invoice, ok bool) {
CurrencyID: b.documentCurrencyID,
}

invoice.Prefill()
retInvoice, ok = invoice, true
return
}
Expand Down
1 change: 0 additions & 1 deletion builders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,6 @@ func TestInvoiceBuilder(t *testing.T) {
}
{
// A.1.8 Exemplul 7 (Cota normală de TVA cu linii scutite de TVA)

buildInvoice := func(documentCurrencyID CurrencyCodeType) (Invoice, bool) {
var lines []InvoiceLine

Expand Down
2 changes: 1 addition & 1 deletion decimal.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package efactura

import (
"github.com/m29h/xml"
"github.com/printesoi/xml-go"
"github.com/shopspring/decimal"
)

Expand Down
9 changes: 7 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
module github.com/printesoi/e-factura-go

go 1.20
go 1.21.1

toolchain go1.22.1

require (
github.com/alexsergivan/transliterator v1.0.0
github.com/m29h/xml v1.0.1
github.com/printesoi/xml-go v1.0.0
github.com/shopspring/decimal v1.3.1
github.com/stretchr/testify v1.9.0
golang.org/x/oauth2 v0.18.0
Expand All @@ -13,8 +15,11 @@ require (
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.8.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
23 changes: 20 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
github.com/alexsergivan/transliterator v1.0.0 h1:SAA+fkGZKLnak47h8Dr6829IE2kpSZR2Y3yTd69cIwY=
github.com/alexsergivan/transliterator v1.0.0/go.mod h1:0IrumukulURJ4PD0z6UcdJKP2job1DYDhnHAP5y+5pE=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -9,10 +10,23 @@ github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/m29h/xml v1.0.1 h1:wxI7sO1mKYK21yS2PVadbfzv6h6coPmCejkLUJQhXBs=
github.com/m29h/xml v1.0.1/go.mod h1:Hd3L/i0B+cbJzU1XNlUteLW0bfshencMbKLwmEJ/VG8=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/printesoi/xml-go v1.0.0 h1:EBqfWNM/oXulKe25iBJNgtBeShRRCXNjkRNPSRzfH5w=
github.com/printesoi/xml-go v1.0.0/go.mod h1:+LM4KmCtr2ZLvs92Or/Dy+eZqsZ0aPES8lrGc/EU6JY=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down Expand Up @@ -52,8 +66,11 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2 changes: 1 addition & 1 deletion helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"regexp"
"strconv"

"github.com/m29h/xml"
"github.com/printesoi/xml-go"
)

// This is a copy of the drainBody from src/net/http/httputil/dump.go
Expand Down
35 changes: 30 additions & 5 deletions invoice.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
package efactura

import (
"bytes"
"fmt"

"github.com/m29h/xml"
"github.com/printesoi/xml-go"
)

// Invoice is the object that represents an e-factura invoice. The invoice
Expand Down Expand Up @@ -206,12 +207,36 @@ func (iv *Invoice) Prefill() {
}
}

func (iv Invoice) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
// this is just to strip away methods like MarshalXML
type invoice Invoice
// XML returns the XML encoding of the Invoice
func (iv Invoice) XML() ([]byte, error) {
return iv.XMLIndent("", "")
}

// XMLIndent returns the XML encoding of the Invoice with the given prefix and
// indent.
func (iv Invoice) XMLIndent(prefix, indent string) ([]byte, error) {
var b bytes.Buffer
if _, err := b.WriteString(xml.Header); err != nil {
return nil, err
}

enc := xml.NewEncoder(&b)
enc.AddNamespaceBinding(XMLNSUBLcac, "cac")
enc.AddSkipNamespaceAttrForPrefix(XMLNSUBLcac, "cac")
enc.AddNamespaceBinding(XMLNSUBLcbc, "cbc")
enc.AddSkipNamespaceAttrForPrefix(XMLNSUBLcbc, "cbc")
enc.Indent(prefix, indent)

iv.Prefill()
return e.EncodeElement(invoice(iv), start)
if err := enc.Encode(iv); err != nil {
enc.Close()
return nil, err
}
if err := enc.Close(); err != nil {
return nil, err
}

return b.Bytes(), nil
}

type InvoiceBillingReference struct {
Expand Down
2 changes: 1 addition & 1 deletion rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"strconv"
"strings"

"github.com/m29h/xml"
"github.com/printesoi/xml-go"
)

type (
Expand Down
2 changes: 1 addition & 1 deletion xml_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package efactura
import (
"time"

"github.com/m29h/xml"
"github.com/printesoi/xml-go"
"github.com/shopspring/decimal"
)

Expand Down
9 changes: 0 additions & 9 deletions xmlns.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@

package efactura

import (
xml "github.com/m29h/xml"
)

// Constants for namespaces and versions
const (
XMLNSInvoice2 = "urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
Expand All @@ -31,8 +27,3 @@ const (

XMLNSMsgErrorV1 = "mfp:anaf:dgti:efactura:mesajEroriFactuta:v1"
)

func init() {
xml.NameSpaceBinding.Add(XMLNSUBLcac, "cac")
xml.NameSpaceBinding.Add(XMLNSUBLcbc, "cbc")
}

0 comments on commit 0bfdcdf

Please sign in to comment.