-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
226 lines (200 loc) · 5.84 KB
/
parse.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package internal
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"sort"
"strings"
"github.com/imdario/mergo"
"github.com/invopop/gobl"
"github.com/invopop/gobl.cli/internal/iotools"
"github.com/invopop/gobl/schema"
"github.com/labstack/echo/v4"
"gopkg.in/yaml.v3"
)
// ParseOptions are the options used for parsing incoming GOBL data.
type ParseOptions struct {
Template io.Reader
Input io.Reader
DocType string
SetYAML map[string]string
SetString map[string]string
SetFile map[string]string
// When set to `true`, the parsed data is wrapped in an envelope (if needed).
Envelop bool
}
// decodeInto unmarshals in as YAML, then merges it into dest.
func decodeInto(ctx context.Context, dest *map[string]interface{}, in io.Reader) error {
var intermediate map[string]interface{}
dec := yaml.NewDecoder(iotools.CancelableReader(ctx, in))
if err := dec.Decode(&intermediate); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
if err := mergo.Merge(dest, intermediate, mergo.WithOverride); err != nil {
return echo.NewHTTPError(http.StatusUnprocessableEntity, err.Error())
}
return nil
}
func parseGOBLData(ctx context.Context, opts *ParseOptions) (interface{}, error) {
var intermediate map[string]interface{}
values, err := parseSets(opts)
if err != nil {
return nil, err
}
if opts.Template != nil {
if err = decodeInto(ctx, &intermediate, opts.Template); err != nil {
return nil, err
}
}
if err = decodeInto(ctx, &intermediate, opts.Input); err != nil {
return nil, err
}
if err = mergo.Merge(&intermediate, values, mergo.WithOverride); err != nil {
return nil, echo.NewHTTPError(http.StatusUnprocessableEntity, err.Error())
}
if opts.DocType != "" {
// We want to infer if the incoming data is an envelope, but don't want
// to call `gobl.Parse` just yet (because we want to first merge based
// on `opts.DocType`. Thus, we (somewhat hacky) infer based on the
// intermediate map ourselves.
schemaDataFunc := docSchemaData
if schema, ok := intermediate["$schema"].(string); ok && schema == string(gobl.EnvelopeSchema) {
schemaDataFunc = docInEnvelopeSchemaData
}
schema := FindType(opts.DocType)
if schema == "" {
return nil, echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("unrecognized doc type: %q", opts.DocType))
}
if err = mergo.Merge(&intermediate, schemaDataFunc(schema)); err != nil {
return nil, err
}
}
// Encode intermediate to JSON for usage with `gobl.Parse`.
intermediateJSON, err := json.Marshal(intermediate)
if err != nil {
return nil, echo.NewHTTPError(http.StatusUnprocessableEntity, err.Error())
}
// Parse the JSON encoded intermediate, so we can figure out if the incoming data
// is already an envelope.
obj, err := gobl.Parse(intermediateJSON)
if err != nil {
return nil, echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
// If the incoming data was parsed as an envelope, we can simply return
// without the need for wrapping.
env, isEnvelope := obj.(*gobl.Envelope)
if isEnvelope {
return env, nil
}
var doc *schema.Object
if d, ok := obj.(*schema.Object); ok {
doc = d
} else {
var err error
doc, err = schema.NewObject(obj)
if err != nil {
return nil, echo.NewHTTPError(http.StatusUnprocessableEntity, err.Error())
}
}
if !opts.Envelop {
return doc, nil
}
// Wrap the parsed document in an envelope.
// Note: We don't use `gobl.Envelop()`, because it (indirectly) calculates
// the document, which we don't want to do here. Any calculations should
// be incurred from the call site of this function.
env = gobl.NewEnvelope()
env.Document = doc
// Set envelope as draft, so it can be rebuilt over time, and eventually
// signed using the separate `sign` command.
env.Head.Draft = true
return env, nil
}
func docInEnvelopeSchemaData(schema schema.ID) map[string]interface{} {
return map[string]interface{}{
"doc": docSchemaData(schema),
}
}
func docSchemaData(schema schema.ID) map[string]interface{} {
return map[string]interface{}{
"$schema": schema,
}
}
func parseSets(opts *ParseOptions) (map[string]interface{}, error) {
values := map[string]interface{}{}
keys := make([]string, 0, len(opts.SetYAML))
for k := range opts.SetYAML {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
v := opts.SetYAML[k]
var parsed interface{}
if err := yaml.Unmarshal([]byte(v), &parsed); err != nil {
return nil, err
}
if err := setValue(&values, k, parsed); err != nil {
return nil, err
}
}
keys = make([]string, 0, len(opts.SetString))
for k := range opts.SetString {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
v := opts.SetString[k]
if err := setValue(&values, k, v); err != nil {
return nil, err
}
}
keys = make([]string, 0, len(opts.SetFile))
for k := range opts.SetFile {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
v := opts.SetFile[k]
f, err := os.Open(v)
if err != nil {
return nil, err
}
defer f.Close() // nolint:errcheck
dec := yaml.NewDecoder(f)
var val interface{}
if err := dec.Decode(&val); err != nil {
return nil, err
}
if err := setValue(&values, k, val); err != nil {
return nil, err
}
}
return values, nil
}
func setValue(values *map[string]interface{}, key string, value interface{}) error {
key = strings.ReplaceAll(key, `\.`, "\x00")
// If the key starts with '.', we treat that as the root of the
// target object
if key == "." {
return mergo.Merge(values, value, mergo.WithOverride)
}
if len(key) > 1 && key[0] == '.' {
key = key[1:]
}
for {
i := strings.LastIndex(key, ".")
if i == -1 {
break
}
value = map[string]interface{}{
strings.ReplaceAll(key[i+1:], "\x00", "."): value,
}
key = key[:i]
}
return mergo.Merge(values, map[string]interface{}{
strings.ReplaceAll(key, "\x00", "."): value,
}, mergo.WithOverride)
}