5
5
"fmt"
6
6
"regexp"
7
7
"slices"
8
+ "strings"
8
9
"sync"
9
10
10
11
"github.com/go-viper/mapstructure/v2"
@@ -153,7 +154,19 @@ func EncodeExtendedMap(extension Extension, data map[string]any) error {
153
154
return encoder .Decode (extension )
154
155
}
155
156
156
- func DecodeExtendedMap (extension Extension , data map [string ]any ) error {
157
+ func DecodeExtendedMap (extension Extension , data map [string ]any , prefix string ) error {
158
+ if prefix != "" {
159
+ applies := false
160
+ for k := range data {
161
+ if strings .HasPrefix (k , prefix + ":" ) {
162
+ applies = true
163
+ break
164
+ }
165
+ }
166
+ if ! applies {
167
+ return ErrExtensionDoesNotApply
168
+ }
169
+ }
157
170
decoder , err := mapstructure .NewDecoder (& mapstructure.DecoderConfig {
158
171
TagName : "json" ,
159
172
Result : extension ,
@@ -208,15 +221,16 @@ func decodeExtendedAssets(data map[string]any, assets map[string]*Asset, extensi
208
221
}
209
222
210
223
for key , asset := range assets {
224
+ assetMap , ok := assetsMap [key ].(map [string ]any )
225
+ if ! ok {
226
+ return fmt .Errorf ("unexpected type for %q asset: %T" , key , assetsMap [key ])
227
+ }
228
+
211
229
for _ , uri := range extensionUris {
212
230
extension := GetAssetExtension (uri )
213
231
if extension == nil {
214
232
continue
215
233
}
216
- assetMap , ok := assetsMap [key ].(map [string ]any )
217
- if ! ok {
218
- return fmt .Errorf ("unexpected type for %q asset: %t" , key , assetsMap [key ])
219
- }
220
234
if err := extension .Decode (assetMap ); err != nil {
221
235
if errors .Is (err , ErrExtensionDoesNotApply ) {
222
236
continue
@@ -225,8 +239,52 @@ func decodeExtendedAssets(data map[string]any, assets map[string]*Asset, extensi
225
239
}
226
240
asset .Extensions = append (asset .Extensions , extension )
227
241
}
242
+
243
+ if err := decodeExtendedBands (assetMap , asset .Bands , extensionUris ); err != nil {
244
+ return err
245
+ }
246
+ }
247
+
248
+ return nil
249
+ }
250
+
251
+ func decodeExtendedBands (assetData map [string ]any , bands []* Band , extensionUris []string ) error {
252
+ bandsValue , ok := assetData ["bands" ]
253
+ if ! ok {
254
+ return nil
228
255
}
229
256
257
+ bandsSlice , ok := bandsValue .([]any )
258
+ if ! ok {
259
+ return fmt .Errorf ("unexpected type for bands: %T" , bandsValue )
260
+ }
261
+
262
+ if len (bandsSlice ) != len (bands ) {
263
+ return fmt .Errorf ("band length mismatch: got %d but expected %d" , len (bandsSlice ), len (bands ))
264
+ }
265
+
266
+ for i , band := range bands {
267
+ for _ , uri := range extensionUris {
268
+ extension := GetBandExtension (uri )
269
+ if extension == nil {
270
+ continue
271
+ }
272
+
273
+ bandMap , ok := bandsSlice [i ].(map [string ]any )
274
+ if ! ok {
275
+ return fmt .Errorf ("unexpected type for band %d: %T" , i , bandsSlice [i ])
276
+ }
277
+
278
+ if err := extension .Decode (bandMap ); err != nil {
279
+ if errors .Is (err , ErrExtensionDoesNotApply ) {
280
+ continue
281
+ }
282
+ return fmt .Errorf ("decoding error for %s: %w" , uri , err )
283
+ }
284
+
285
+ band .Extensions = append (band .Extensions , extension )
286
+ }
287
+ }
230
288
return nil
231
289
}
232
290
0 commit comments