-
Notifications
You must be signed in to change notification settings - Fork 2
/
catalog.go
129 lines (108 loc) · 2.8 KB
/
catalog.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
package stac
import (
"encoding/json"
"fmt"
"regexp"
"github.com/go-viper/mapstructure/v2"
)
type Catalog struct {
Version string `json:"stac_version"`
Id string `json:"id"`
Title string `json:"title,omitempty"`
Description string `json:"description"`
Links []*Link `json:"links"`
ConformsTo []string `json:"conformsTo,omitempty"`
Extensions []Extension `json:"-"`
}
var (
_ json.Marshaler = (*Catalog)(nil)
_ json.Unmarshaler = (*Catalog)(nil)
)
var catalogExtensions = newExtensionRegistry()
func RegisterCatalogExtension(pattern *regexp.Regexp, provider ExtensionProvider) {
catalogExtensions.register(pattern, provider)
}
func GetCatalogExtension(uri string) Extension {
return catalogExtensions.get(uri)
}
func (catalog Catalog) MarshalJSON() ([]byte, error) {
catalogMap := map[string]any{
"type": "Catalog",
}
decoder, decoderErr := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
TagName: "json",
Result: &catalogMap,
})
if decoderErr != nil {
return nil, decoderErr
}
decodeErr := decoder.Decode(catalog)
if decodeErr != nil {
return nil, decodeErr
}
extensionUris := []string{}
lookup := map[string]bool{}
for _, extension := range catalog.Extensions {
if err := extension.Encode(catalogMap); err != nil {
return nil, err
}
uris, err := GetExtensionUris(catalogMap)
if err != nil {
return nil, err
}
uris = append(uris, extension.URI())
for _, uri := range uris {
if !lookup[uri] {
extensionUris = append(extensionUris, uri)
lookup[uri] = true
}
}
}
links, linkExtensionUris, err := EncodeLinks(catalog.Links)
if err != nil {
return nil, err
}
catalogMap["links"] = links
for _, uri := range linkExtensionUris {
if !lookup[uri] {
extensionUris = append(extensionUris, uri)
lookup[uri] = true
}
}
SetExtensionUris(catalogMap, extensionUris)
return json.Marshal(catalogMap)
}
func (catalog *Catalog) UnmarshalJSON(data []byte) error {
catalogMap := map[string]any{}
if err := json.Unmarshal(data, &catalogMap); err != nil {
return err
}
decoder, decoderErr := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
TagName: "json",
Result: catalog,
})
if decoderErr != nil {
return decoderErr
}
if err := decoder.Decode(catalogMap); err != nil {
return err
}
extensionUris, err := GetExtensionUris(catalogMap)
if err != nil {
return err
}
for _, uri := range extensionUris {
extension := GetCatalogExtension(uri)
if extension == nil {
continue
}
if err := extension.Decode(catalogMap); err != nil {
return fmt.Errorf("decoding error for %s: %w", uri, err)
}
catalog.Extensions = append(catalog.Extensions, extension)
}
if err := decodeExtendedLinks(catalogMap, catalog.Links, extensionUris); err != nil {
return err
}
return nil
}