-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #153 from planetlabs/extensions
Reworked extension handling
- Loading branch information
Showing
18 changed files
with
1,339 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,10 +15,10 @@ jobs: | |
- uses: actions/checkout@v4 | ||
- uses: actions/[email protected] | ||
with: | ||
go-version: '1.20' | ||
go-version: '1.21' | ||
- uses: golangci/golangci-lint-action@v2 | ||
with: | ||
version: v1.51.2 | ||
version: v1.54.1 | ||
args: "--out-${NO_FUTURE}format colored-line-number" | ||
|
||
test: | ||
|
@@ -27,5 +27,5 @@ jobs: | |
- uses: actions/checkout@v4 | ||
- uses: actions/[email protected] | ||
with: | ||
go-version: '1.20' | ||
go-version: '1.21' | ||
- run: go test -v ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,53 @@ | ||
package stac | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"regexp" | ||
|
||
"github.com/mitchellh/mapstructure" | ||
) | ||
|
||
type Asset struct { | ||
Type string `json:"type,omitempty"` | ||
Href string `json:"href"` | ||
Title string `json:"title,omitempty"` | ||
Description string `json:"description,omitempty"` | ||
Created string `json:"created,omitempty"` | ||
Roles []string `json:"roles,omitempty"` | ||
Extensions []AssetExtension `json:"-"` | ||
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 _ json.Marshaler = (*Asset)(nil) | ||
var assetExtensions = newExtensionRegistry() | ||
|
||
type AssetExtension interface { | ||
Apply(*Asset) | ||
URI() string | ||
func RegisterAssetExtension(pattern *regexp.Regexp, provider ExtensionProvider) { | ||
assetExtensions.register(pattern, provider) | ||
} | ||
|
||
func (asset Asset) MarshalJSON() ([]byte, error) { | ||
assetMap := map[string]any{} | ||
decoder, decoderErr := mapstructure.NewDecoder(&mapstructure.DecoderConfig{ | ||
TagName: "json", | ||
Result: &assetMap, | ||
}) | ||
if decoderErr != nil { | ||
return nil, decoderErr | ||
} | ||
|
||
decodeErr := decoder.Decode(asset) | ||
if decodeErr != nil { | ||
return nil, decodeErr | ||
} | ||
func GetAssetExtension(uri string) Extension { | ||
return assetExtensions.get(uri) | ||
} | ||
|
||
for _, extension := range asset.Extensions { | ||
extension.Apply(&asset) | ||
if decodeErr := decoder.Decode(extension); decodeErr != nil { | ||
return nil, fmt.Errorf("trouble encoding JSON for %s asset: %w", extension.URI(), decodeErr) | ||
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 json.Marshal(assetMap) | ||
return assetsMap, extensionUris, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.