-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.go
65 lines (52 loc) · 1.68 KB
/
build.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
package internal
import (
"context"
"net/http"
"github.com/invopop/gobl"
"github.com/labstack/echo/v4"
)
type BuildOptions struct {
*ParseOptions
// When set to a non-nil value, the returned data is wrapped in an envelope (if needed)
// with its `draft` property set to true or false.
Draft *bool
}
// Build builds and validates GOBL data.
func Build(ctx context.Context, opts *BuildOptions) (interface{}, error) {
obj, err := parseGOBLData(ctx, opts.ParseOptions)
if err != nil {
return nil, err
}
if env, ok := obj.(*gobl.Envelope); ok {
// Signed documents should be regarded as immutable.
// Attempting to build an already signed document returns an error.
if len(env.Signatures) > 0 {
return nil, echo.NewHTTPError(http.StatusConflict, "document has already been signed")
}
if opts.Draft != nil {
env.Head.Draft = *opts.Draft
}
if err := env.Calculate(); err != nil {
return nil, echo.NewHTTPError(http.StatusUnprocessableEntity, err.Error())
}
if err := env.Validate(); err != nil {
return nil, echo.NewHTTPError(http.StatusUnprocessableEntity, err.Error())
}
return env, nil
}
if doc, ok := obj.(*gobl.Document); ok {
if opts.Draft != nil {
return nil, echo.NewHTTPError(http.StatusUnprocessableEntity, "cannot set draft status on non-envelope document")
}
if c, ok := doc.Instance().(gobl.Calculable); ok {
if err := c.Calculate(); err != nil {
return nil, echo.NewHTTPError(http.StatusUnprocessableEntity, err.Error())
}
}
if err := doc.Validate(); err != nil {
return nil, echo.NewHTTPError(http.StatusUnprocessableEntity, err.Error())
}
return doc, nil
}
panic("parsed data must be either an envelope or a document")
}