forked from invopop/gobl.fatturapa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverter.go
80 lines (66 loc) · 1.84 KB
/
converter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package fatturapa
import (
"bytes"
"encoding/json"
"io"
"github.com/invopop/gobl"
"github.com/invopop/xmldsig"
)
// Converter contains information related to the entity using this library
// to submit invoices to SDI.
type Converter struct {
Config *Config
}
// Transmitter contains information about the entity integrating directly
// with the SDI to submit and receive invoices
type Transmitter struct {
CountryCode string
TaxID string
}
// Config contains the configuration for the Converter
type Config struct {
Certificate *xmldsig.Certificate
WithTimestamp bool
Transmitter *Transmitter
}
// Option is a function that can be passed to NewConverter to configure it
type Option func(*Converter)
// WithTransmitterData will ensure the XML document contains the given transmitter data
func WithTransmitterData(transmitter *Transmitter) Option {
return func(c *Converter) {
c.Config.Transmitter = transmitter
}
}
// WithCertificate will ensure the XML document is signed with the given certificate
func WithCertificate(cert *xmldsig.Certificate) Option {
return func(c *Converter) {
c.Config.Certificate = cert
}
}
// WithTimestamp will ensure the XML document is timestamped
func WithTimestamp() Option {
return func(c *Converter) {
c.Config.WithTimestamp = true
}
}
// NewConverter returns a new GOBL to XML Converter with the given options
func NewConverter(opts ...Option) *Converter {
c := new(Converter)
c.Config = new(Config)
for _, opt := range opts {
opt(c)
}
return c
}
// UnmarshalGOBL converts the given JSON document to a GOBL Envelope
func UnmarshalGOBL(reader io.Reader) (*gobl.Envelope, error) {
buf := new(bytes.Buffer)
if _, err := buf.ReadFrom(reader); err != nil {
return nil, err
}
env := new(gobl.Envelope)
if err := json.Unmarshal(buf.Bytes(), env); err != nil {
return nil, err
}
return env, nil
}