From 7aadbb6ae9ef65041c5b62cc6d65b54e3af98c49 Mon Sep 17 00:00:00 2001 From: Jackson Tian Date: Thu, 4 Jan 2024 16:13:55 +0800 Subject: [PATCH] lib: use json unmarshal instead of jmespath --- meta/product.go | 2 +- meta/repository.go | 44 +++++++++++++++++++++++++++++++++++--------- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/meta/product.go b/meta/product.go index 2bf079cbb..e479f168a 100644 --- a/meta/product.go +++ b/meta/product.go @@ -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{ diff --git a/meta/repository.go b/meta/repository.go index 9323f9449..71ae3d93e 100644 --- a/meta/repository.go +++ b/meta/repository.go @@ -19,8 +19,6 @@ import ( "strings" _ "embed" - - jmespath "github.com/jmespath/go-jmespath" ) type Repository struct { @@ -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 }