Skip to content

Commit

Permalink
feat: jsonpath format (#299)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrii-codefresh authored Mar 12, 2024
1 parent 2e245e3 commit b23aa3f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 12 deletions.
2 changes: 2 additions & 0 deletions changelog/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
### Features
- feat: using $ format for jsonPath expressions
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ require (
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.1.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/internal v1.0.0 // indirect
github.com/AzureAD/microsoft-authentication-library-for-go v0.5.2 // indirect
github.com/PaesslerAG/gval v1.0.0 // indirect
github.com/aws/aws-sdk-go-v2 v1.17.6 // indirect
github.com/aws/aws-sdk-go-v2/config v1.18.17 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.13.17 // indirect
Expand Down Expand Up @@ -157,6 +158,7 @@ require (
github.com/MakeNowJust/heredoc v0.0.0-20170808103936-bb23615498cd // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/PaesslerAG/jsonpath v0.1.1
github.com/PagerDuty/go-pagerduty v1.7.0 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230717121422-5aa5874ade95 // indirect
github.com/RocketChat/Rocket.Chat.Go.SDK v0.0.0-20210112200207-10ab4d695d60 // indirect
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,11 @@ github.com/Microsoft/hcsshim v0.8.22/go.mod h1:91uVCVzvX2QD16sMCenoxxXo6L1wJnLMX
github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46/go.mod h1:3wb06e3pkSAbeQ52E9H9iFoQsEEwGN64994WTCIhntQ=
github.com/NYTimes/gziphandler v1.1.1/go.mod h1:n/CVRwUEOgIxrgPvAQhUUr9oeUtvrhMomdKFjzJNB0c=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/PaesslerAG/gval v1.0.0 h1:GEKnRwkWDdf9dOmKcNrar9EA1bz1z9DqPIO1+iLzhd8=
github.com/PaesslerAG/gval v1.0.0/go.mod h1:y/nm5yEyTeX6av0OfKJNp9rBNj2XrGhAf5+v24IBN1I=
github.com/PaesslerAG/jsonpath v0.1.0/go.mod h1:4BzmtoM/PI8fPO4aQGIusjGxGir2BzcV0grWtFzq1Y8=
github.com/PaesslerAG/jsonpath v0.1.1 h1:c1/AToHQMVsduPAa4Vh6xp2U0evy4t8SWp8imEsylIk=
github.com/PaesslerAG/jsonpath v0.1.1/go.mod h1:lVboNxFGal/VwW6d9JzIy56bUsYAP6tH/x80vjnCseY=
github.com/PagerDuty/go-pagerduty v1.7.0 h1:S1NcMKECxT5hJwV4VT+QzeSsSiv4oWl1s2821dUqG/8=
github.com/PagerDuty/go-pagerduty v1.7.0/go.mod h1:PuFyJKRz1liIAH4h5KVXVD18Obpp1ZXRdxHvmGXooro=
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8/go.mod h1:I0gYDMZ6Z5GRU7l58bNFSkPTFN6Yl12dsUlAZ8xy98g=
Expand Down
55 changes: 43 additions & 12 deletions reposerver/repository/app_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
"os"
"path/filepath"
"reflect"
"strings"
"strconv"

"github.com/PaesslerAG/jsonpath"
"github.com/argoproj/argo-cd/v2/pkg/version_config_manager"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
"k8s.io/client-go/util/jsonpath"
)

type DependenciesMap struct {
Expand Down Expand Up @@ -42,7 +42,7 @@ func getVersionFromFile(appPath, jsonPathExpression string) (*string, error) {
if err := yaml.Unmarshal(content, &obj); err != nil {
return nil, err
}
// Convert YAML to Map[interface{}]interface{}
// Convert YAML to Map[string]interface{}
jsonObj, err = convertToJSONCompatible(obj)
if err != nil {
return nil, err
Expand All @@ -56,19 +56,20 @@ func getVersionFromFile(appPath, jsonPathExpression string) (*string, error) {
return nil, fmt.Errorf("Unsupported file format of %s", appPath)
}

jp := jsonpath.New("jsonpathParser")
jp.AllowMissingKeys(true)
if err := jp.Parse(jsonPathExpression); err != nil {
return nil, err
}

var buf strings.Builder
err = jp.Execute(&buf, jsonObj)
versionValue, err := jsonpath.Get(jsonPathExpression, jsonObj)
if err != nil {
return nil, err
}
appVersion, ok := versionValue.(string)
if !ok {
if versionValue == nil {
log.Info("Version value is not a string. Got: nil")
} else {
log.Infof("Version value is not a string. Got: %v", versionValue)
}
appVersion = ""
}

appVersion := buf.String()
log.Infof("Extracted appVersion: %s", appVersion)
return &appVersion, nil
}
Expand All @@ -82,6 +83,36 @@ func convertToJSONCompatible(i interface{}) (interface{}, error) {
if err := yaml.Unmarshal(data, &obj); err != nil {
return nil, err
}

return convertMapKeysToString(obj)
}

func convertMapKeysToString(obj interface{}) (interface{}, error) {
switch m := obj.(type) {
case map[interface{}]interface{}:
result := make(map[string]interface{})
for k, v := range m {
strKey, ok := k.(string)
if !ok {
return nil, fmt.Errorf("Non-string key found in map: %v", k)
}
result[strKey], _ = convertMapKeysToString(v)
}
return result, nil
case []interface{}:
for i, v := range m {
var err error
m[i], err = convertMapKeysToString(v)
if err != nil {
return nil, err
}
}
return m, nil
case float64:
obj = strconv.FormatFloat(m, 'f', -1, 64)
case int:
obj = strconv.Itoa(m)
}
return obj, nil
}

Expand Down

0 comments on commit b23aa3f

Please sign in to comment.