forked from invopop/gobl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.go
More file actions
28 lines (23 loc) · 653 Bytes
/
parse.go
File metadata and controls
28 lines (23 loc) · 653 Bytes
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
package gobl
import (
"encoding/json"
"github.com/invopop/gobl/schema"
)
// Parse unmarshals the provided data and uses the schema ID
// to determine what type of object we're dealing with. As long as the
// provided data contains a schema registered in GOBL, a new
// object instance will be returned.
func Parse(data []byte) (interface{}, error) {
id, err := schema.Extract(data)
if err != nil {
return nil, ErrUnmarshal.WithCause(err)
}
if id == schema.UnknownID {
return nil, ErrUnknownSchema
}
obj := id.Interface()
if err := json.Unmarshal(data, obj); err != nil {
return nil, ErrUnmarshal.WithCause(err)
}
return obj, nil
}