-
Notifications
You must be signed in to change notification settings - Fork 2
/
catalog_test.go
174 lines (151 loc) · 3.68 KB
/
catalog_test.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
package stac_test
import (
"encoding/json"
"regexp"
"testing"
"github.com/go-viper/mapstructure/v2"
"github.com/planetlabs/go-stac"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCatalogMarshal(t *testing.T) {
catalog := &stac.Catalog{
Version: "1.0.0",
Id: "catalog-id",
Description: "Test Catalog",
Links: []*stac.Link{
{Href: "https://example.com/stac/catalog", Rel: "self", Type: "application/json"},
},
}
data, err := json.Marshal(catalog)
require.Nil(t, err)
expected := `{
"type": "Catalog",
"id": "catalog-id",
"description": "Test Catalog",
"links": [
{
"href": "https://example.com/stac/catalog",
"rel": "self",
"type": "application/json"
}
],
"stac_version": "1.0.0"
}`
assert.JSONEq(t, expected, string(data))
}
const (
extensionAlias = "test-catalog-extension"
extensionUri = "https://example.com/test-catalog-extension/v1.0.0/schema.json"
extensionPattern = `https://example.com/test-catalog-extension/v1\..*/schema.json`
)
type CatalogExtension struct {
RequiredNum float64 `json:"required_num"`
OptionalBool *bool `json:"optional_bool,omitempty"`
}
var _ stac.Extension = (*CatalogExtension)(nil)
func (*CatalogExtension) URI() string {
return extensionUri
}
func (e *CatalogExtension) Encode(catalogMap map[string]any) error {
extendedProps := map[string]any{}
encoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
TagName: "json",
Result: &extendedProps,
})
if err != nil {
return err
}
if err := encoder.Decode(e); err != nil {
return err
}
catalogMap[extensionAlias] = extendedProps
return nil
}
func (e *CatalogExtension) Decode(catalogMap map[string]any) error {
extendedProps, present := catalogMap[extensionAlias]
if !present {
return nil
}
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
TagName: "json",
Result: e,
})
if err != nil {
return err
}
return decoder.Decode(extendedProps)
}
func TestExtendedCatalogMarshal(t *testing.T) {
stac.RegisterCatalogExtension(
regexp.MustCompile(extensionPattern),
func() stac.Extension {
return &CatalogExtension{}
},
)
catalog := &stac.Catalog{
Description: "Test catalog with extension",
Id: "catalog-id",
Extensions: []stac.Extension{
&CatalogExtension{
RequiredNum: 42,
},
},
Links: []*stac.Link{},
Version: "1.2.3",
}
data, err := json.Marshal(catalog)
require.NoError(t, err)
expected := `{
"type": "Catalog",
"description": "Test catalog with extension",
"id": "catalog-id",
"test-catalog-extension": {
"required_num": 42
},
"links": [],
"stac_extensions": [
"https://example.com/test-catalog-extension/v1.0.0/schema.json"
],
"stac_version": "1.2.3"
}`
assert.JSONEq(t, expected, string(data))
}
func TestExtendedCatalogUnmarshal(t *testing.T) {
stac.RegisterCatalogExtension(
regexp.MustCompile(extensionPattern),
func() stac.Extension {
return &CatalogExtension{}
},
)
data := []byte(`{
"type": "Catalog",
"description": "Test catalog with extension",
"id": "catalog-id",
"test-catalog-extension": {
"required_num": 100,
"optional_bool": true
},
"links": [],
"stac_extensions": [
"https://example.com/test-catalog-extension/v1.0.0/schema.json"
],
"stac_version": "1.2.3"
}`)
catalog := &stac.Catalog{}
require.NoError(t, json.Unmarshal(data, catalog))
b := true
expected := &stac.Catalog{
Description: "Test catalog with extension",
Id: "catalog-id",
Extensions: []stac.Extension{
&CatalogExtension{
RequiredNum: 100,
OptionalBool: &b,
},
},
Links: []*stac.Link{},
Version: "1.2.3",
}
assert.Equal(t, expected, catalog)
}