Skip to content

Commit

Permalink
lib: use json unmarshal instead of jmespath
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonTian authored and yndu13 committed Jan 4, 2024
1 parent 6b5e976 commit 7aadbb6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
2 changes: 1 addition & 1 deletion meta/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (a *Product) GetEndpoint(region string, client *sdk.Client) (string, error)
}

func (a *Product) TryGetEndpoints(region string, client *sdk.Client) (endpoint string, lcEndpoint string) {
endpoint, _ = a.RegionalEndpoints[region]
endpoint = a.RegionalEndpoints[region]

if a.LocationServiceCode != "" {
rp := endpoints.ResolveParam{
Expand Down
44 changes: 35 additions & 9 deletions meta/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import (
"strings"

_ "embed"

jmespath "github.com/jmespath/go-jmespath"
)

type Repository struct {
Expand Down Expand Up @@ -79,16 +77,44 @@ func (a *Repository) GetApi(productCode string, version string, apiName string)
//go:embed versions.json
var versions []byte

// [
// {
// "code": "aegis",
// "styles": [
// {
// "style": "RPC",
// "version": "2016-11-11"
// }
// ]
// }
// ]

type ProductStyle struct {
Code string `json:"code"`
Styles []Style `json:"styles"`
}

type Style struct {
Style string `json:"style"`
Version string `json:"version"`
}

func (a *Repository) GetStyle(productCode, version string) (string, bool) {
v := new(interface{})
err := json.Unmarshal(versions, v)
productStyles := new([]ProductStyle)
err := json.Unmarshal(versions, &productStyles)
if err != nil {
return "", false
}
styles, _ := jmespath.Search("[?code=='"+productCode+"'].styles[]", *v)
style, _ := jmespath.Search("[?version=='"+version+"'].style", styles)
if len(style.([]interface{})) == 0 {
return "", false

for _, p := range *productStyles {
if p.Code == productCode {
for _, s := range p.Styles {
if s.Version == version {
return s.Style, true
}
}
}
}
return style.([]interface{})[0].(string), true

return "", false
}

0 comments on commit 7aadbb6

Please sign in to comment.