@@ -2,8 +2,10 @@ package stac_test
2
2
3
3
import (
4
4
"encoding/json"
5
+ "regexp"
5
6
"testing"
6
7
8
+ "github.com/mitchellh/mapstructure"
7
9
"github.com/planetlabs/go-stac"
8
10
"github.com/stretchr/testify/assert"
9
11
"github.com/stretchr/testify/require"
@@ -38,3 +40,135 @@ func TestCatalogMarshal(t *testing.T) {
38
40
39
41
assert .JSONEq (t , expected , string (data ))
40
42
}
43
+
44
+ const (
45
+ extensionAlias = "test-catalog-extension"
46
+ extensionUri = "https://example.com/test-catalog-extension/v1.0.0/schema.json"
47
+ extensionPattern = `https://example.com/test-catalog-extension/v1\..*/schema.json`
48
+ )
49
+
50
+ type CatalogExtension struct {
51
+ RequiredNum float64 `json:"required_num"`
52
+ OptionalBool * bool `json:"optional_bool,omitempty"`
53
+ }
54
+
55
+ var _ stac.Extension = (* CatalogExtension )(nil )
56
+
57
+ func (* CatalogExtension ) URI () string {
58
+ return extensionUri
59
+ }
60
+
61
+ func (e * CatalogExtension ) Encode (catalogMap map [string ]any ) error {
62
+ extendedProps := map [string ]any {}
63
+ encoder , err := mapstructure .NewDecoder (& mapstructure.DecoderConfig {
64
+ TagName : "json" ,
65
+ Result : & extendedProps ,
66
+ })
67
+ if err != nil {
68
+ return err
69
+ }
70
+ if err := encoder .Decode (e ); err != nil {
71
+ return err
72
+ }
73
+ catalogMap [extensionAlias ] = extendedProps
74
+ return nil
75
+ }
76
+
77
+ func (e * CatalogExtension ) Decode (catalogMap map [string ]any ) error {
78
+ extendedProps , present := catalogMap [extensionAlias ]
79
+ if ! present {
80
+ return nil
81
+ }
82
+
83
+ decoder , err := mapstructure .NewDecoder (& mapstructure.DecoderConfig {
84
+ TagName : "json" ,
85
+ Result : e ,
86
+ })
87
+ if err != nil {
88
+ return err
89
+ }
90
+ return decoder .Decode (extendedProps )
91
+ }
92
+
93
+ func TestExtendedCatalogMarshal (t * testing.T ) {
94
+ stac .RegisterCatalogExtension (
95
+ regexp .MustCompile (extensionPattern ),
96
+ func () stac.Extension {
97
+ return & CatalogExtension {}
98
+ },
99
+ )
100
+
101
+ catalog := & stac.Catalog {
102
+ Description : "Test catalog with extension" ,
103
+ Id : "catalog-id" ,
104
+ Extensions : []stac.Extension {
105
+ & CatalogExtension {
106
+ RequiredNum : 42 ,
107
+ },
108
+ },
109
+ Links : []* stac.Link {},
110
+ Version : "1.2.3" ,
111
+ }
112
+
113
+ data , err := json .Marshal (catalog )
114
+ require .NoError (t , err )
115
+
116
+ expected := `{
117
+ "type": "Catalog",
118
+ "description": "Test catalog with extension",
119
+ "id": "catalog-id",
120
+ "test-catalog-extension": {
121
+ "required_num": 42
122
+ },
123
+ "links": [],
124
+ "stac_extensions": [
125
+ "https://example.com/test-catalog-extension/v1.0.0/schema.json"
126
+ ],
127
+ "stac_version": "1.2.3"
128
+ }`
129
+
130
+ assert .JSONEq (t , expected , string (data ))
131
+ }
132
+
133
+ func TestExtendedCatalogUnmarshal (t * testing.T ) {
134
+ stac .RegisterCatalogExtension (
135
+ regexp .MustCompile (extensionPattern ),
136
+ func () stac.Extension {
137
+ return & CatalogExtension {}
138
+ },
139
+ )
140
+
141
+ data := []byte (`{
142
+ "type": "Catalog",
143
+ "description": "Test catalog with extension",
144
+ "id": "catalog-id",
145
+ "test-catalog-extension": {
146
+ "required_num": 100,
147
+ "optional_bool": true
148
+ },
149
+ "links": [],
150
+ "stac_extensions": [
151
+ "https://example.com/test-catalog-extension/v1.0.0/schema.json"
152
+ ],
153
+ "stac_version": "1.2.3"
154
+ }` )
155
+
156
+ catalog := & stac.Catalog {}
157
+ require .NoError (t , json .Unmarshal (data , catalog ))
158
+
159
+ b := true
160
+ expected := & stac.Catalog {
161
+ Description : "Test catalog with extension" ,
162
+ Id : "catalog-id" ,
163
+ Extensions : []stac.Extension {
164
+ & CatalogExtension {
165
+ RequiredNum : 100 ,
166
+ OptionalBool : & b ,
167
+ },
168
+ },
169
+ Links : []* stac.Link {},
170
+ Version : "1.2.3" ,
171
+ }
172
+
173
+ assert .Equal (t , expected , catalog )
174
+ }
0 commit comments