-
Notifications
You must be signed in to change notification settings - Fork 2
/
asset.go
53 lines (46 loc) · 1.38 KB
/
asset.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
package stac
import (
"regexp"
"github.com/go-viper/mapstructure/v2"
)
type Asset struct {
Type string `json:"type,omitempty"`
Href string `json:"href,omitempty"`
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
Created string `json:"created,omitempty"`
Roles []string `json:"roles,omitempty"`
Extensions []Extension `json:"-"`
}
var assetExtensions = newExtensionRegistry()
func RegisterAssetExtension(pattern *regexp.Regexp, provider ExtensionProvider) {
assetExtensions.register(pattern, provider)
}
func GetAssetExtension(uri string) Extension {
return assetExtensions.get(uri)
}
func EncodeAssets(assets map[string]*Asset) (map[string]any, []string, error) {
assetsMap := map[string]any{}
extensionUris := []string{}
for key, asset := range assets {
assetMap := map[string]any{}
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
TagName: "json",
Result: &assetMap,
})
if err != nil {
return nil, nil, err
}
if err := decoder.Decode(asset); err != nil {
return nil, nil, err
}
for _, extension := range asset.Extensions {
extensionUris = append(extensionUris, extension.URI())
if err := extension.Encode(assetMap); err != nil {
return nil, nil, err
}
}
assetsMap[key] = assetMap
}
return assetsMap, extensionUris, nil
}