github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/label/translator.go (about) 1 package label 2 3 import ( 4 "regexp" 5 6 "github.com/kyma-incubator/compass/components/director/pkg/apperrors" 7 ) 8 9 // ExtractValueFromJSONPath returns the value that is placed in the SQL/JSON path query 10 // 11 // For a given JSON path $[*] ? (@ == "dd") it extracts the actual value - dd 12 // For a given JSON path $[*] ? (@ == "dd" || @ == "ww") it extracts the actual values - dd and ww 13 func ExtractValueFromJSONPath(jpq string) ([]interface{}, error) { 14 re := regexp.MustCompile(`^\$\[\*\]\s*\?\s*\(\s*@\s*==\s*"(?P<value>[a-zA-Z0-9\-\_\s]+)"\s*|\|\|\s*@\s*==\s*"(?P<value>[a-zA-Z0-9\-\_\s]+)"`) 15 res := re.FindAllStringSubmatch(jpq, -1) 16 if res == nil { 17 return nil, apperrors.NewInternalError("value not found in the query parameter") 18 } 19 20 extractedValues := make([]interface{}, len(res)) 21 for idx, r := range res { 22 if idx == 0 { 23 extractedValues[idx] = r[1] 24 continue 25 } 26 27 extractedValues[idx] = r[len(r)-1] 28 } 29 30 return extractedValues, nil 31 }