Skip to content

Commit

Permalink
👻 Refactor Label Manipulation (#268)
Browse files Browse the repository at this point in the history
Pull Request: Refactor Label Manipulation

Description:
Introducing the 'formatLabel' helper function to refactor and centralize
key-value label manipulation throughout the codebase. This change
addresses the issue of repeated label string manipulation, enhancing
code organization and maintainability.

Changes Made:

Added 'formatLabel' function to handle label key-value formatting.
Replaced instances of label construction with calls to 'formatLabel'.
Ensured consistent usage of 'formatLabel' for label manipulation.
Closes #262

---------

Signed-off-by: Ripul Handoo <[email protected]>
  • Loading branch information
RipulHandoo authored Jul 21, 2023
1 parent f5d14af commit b38354f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 41 deletions.
10 changes: 9 additions & 1 deletion engine/labels/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ type LabelSelector[T Labeled] struct {
language gval.Language
}

// Helper function to refactor key value label manipulation
func AsString(key, value string) string {
if value == "" {
return fmt.Sprintf("%s", key)
}
return fmt.Sprintf("%s=%s", key, value)
}

func (l *LabelSelector[T]) Matches(v T) (bool, error) {
ruleLabels, _ := ParseLabels(v.GetLabels())
expr := getBooleanExpression(l.expr, ruleLabels)
Expand Down Expand Up @@ -192,7 +200,7 @@ func getBooleanExpression(expr string, compareLabels map[string][]string) string
for _, exprLabelVal := range exprLabelVals {
toReplace := exprLabelKey
if exprLabelVal != "" {
toReplace = fmt.Sprintf("%s=%s", toReplace, exprLabelVal)
toReplace = AsString(toReplace, exprLabelVal)
}
if labelVals, ok := compareLabels[exprLabelKey]; !ok {
replaceMap[toReplace] = "false"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"path/filepath"
"strings"

"github.com/konveyor/analyzer-lsp/engine/labels"
"github.com/konveyor/analyzer-lsp/provider"
"go.lsp.dev/uri"
)
Expand Down Expand Up @@ -92,8 +93,8 @@ func parseGoDepString(dep string) (provider.Dep, error) {
d.Name = strings.TrimSpace(v[0])
d.Version = strings.TrimSpace(strings.ReplaceAll(v[1], "@", ""))
d.Labels = []string{
fmt.Sprintf("%v=%v", provider.DepSourceLabel, golangDownloadableDepSourceLabel),
fmt.Sprintf("%s=go", provider.DepLanguageLabel),
labels.AsString(provider.DepSourceLabel,golangDownloadableDepSourceLabel),
labels.AsString(provider.DepLanguageLabel,"go"),
}
return d, nil
}
Expand Down
9 changes: 5 additions & 4 deletions provider/internal/java/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"strings"

"github.com/antchfx/xmlquery"
"github.com/konveyor/analyzer-lsp/engine/labels"
"github.com/konveyor/analyzer-lsp/output/v1/konveyor"
"github.com/konveyor/analyzer-lsp/provider"
"go.lsp.dev/uri"
Expand Down Expand Up @@ -281,11 +282,11 @@ func (p *javaServiceClient) addDepLabels(depName string) []string {
}
// if open source label is not found, qualify the dep as being internal by default
if _, openSourceLabelFound :=
m[fmt.Sprintf("%s=%s", provider.DepSourceLabel, javaDepSourceOpenSource)]; !openSourceLabelFound {
m[labels.AsString(provider.DepSourceLabel, javaDepSourceOpenSource)]; !openSourceLabelFound {
s = append(s,
fmt.Sprintf("%s=%s", provider.DepSourceLabel, javaDepSourceInternal))
labels.AsString(provider.DepSourceLabel, javaDepSourceInternal))
}
s = append(s, fmt.Sprintf("%s=java", provider.DepLanguageLabel))
s = append(s, labels.AsString(provider.DepLanguageLabel, "java"))
return s
}

Expand Down Expand Up @@ -368,7 +369,7 @@ func (p *javaServiceClient) initOpenSourceDepLabels() error {
return err
}
return loadDepLabelItems(file, p.depToLabels,
fmt.Sprintf("%s=%s", provider.DepSourceLabel, javaDepSourceOpenSource))
labels.AsString(provider.DepSourceLabel, javaDepSourceOpenSource))
}

// initExcludeDepLabels reads user provided list of excluded packages
Expand Down
68 changes: 34 additions & 34 deletions provider/internal/java/dependency_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package java

import (
"fmt"
"reflect"
"strings"
"testing"

"github.com/go-logr/logr/testr"
"github.com/konveyor/analyzer-lsp/engine/labels"
"github.com/konveyor/analyzer-lsp/provider"
)

Expand Down Expand Up @@ -55,8 +55,8 @@ func Test_parseMavenDepLines(t *testing.T) {
Indirect: false,
ResolvedIdentifier: "4e031bb61df09069aeb2bffb4019e7a5034a4ee0",
Labels: []string{
fmt.Sprintf("%v=internal", provider.DepSourceLabel),
fmt.Sprintf("%s=%s", provider.DepLanguageLabel, "java"),
labels.AsString(provider.DepSourceLabel, "internal"),
labels.AsString(provider.DepLanguageLabel, "java"),
},
FileURIPrefix: "konveyor-jdt://contentstestdata/junit/junit/4.11",
},
Expand All @@ -69,8 +69,8 @@ func Test_parseMavenDepLines(t *testing.T) {
Indirect: true,
ResolvedIdentifier: "42a25dc3219429f0e5d060061f71acb49bf010a0",
Labels: []string{
fmt.Sprintf("%v=internal", provider.DepSourceLabel),
fmt.Sprintf("%s=%s", provider.DepLanguageLabel, "java"),
labels.AsString(provider.DepSourceLabel, "internal"),
labels.AsString(provider.DepLanguageLabel, "java"),
},
FileURIPrefix: "konveyor-jdt://contentstestdata/org/hamcrest/hamcrest-core/1.3",
},
Expand All @@ -85,8 +85,8 @@ func Test_parseMavenDepLines(t *testing.T) {
Indirect: false,
ResolvedIdentifier: "d0831d44e12313df8989fc1d4a9c90452f08858e",
Labels: []string{
fmt.Sprintf("%v=internal", provider.DepSourceLabel),
fmt.Sprintf("%s=%s", provider.DepLanguageLabel, "java"),
labels.AsString(provider.DepSourceLabel, "internal"),
labels.AsString(provider.DepLanguageLabel, "java"),
},
FileURIPrefix: "konveyor-jdt://contentstestdata/io/fabric8/kubernetes-client/6.0.0",
},
Expand All @@ -99,8 +99,8 @@ func Test_parseMavenDepLines(t *testing.T) {
Indirect: true,
ResolvedIdentifier: "70690b98acb07a809c55d15d7cf45f53ec1026e1",
Labels: []string{
fmt.Sprintf("%v=internal", provider.DepSourceLabel),
fmt.Sprintf("%s=%s", provider.DepLanguageLabel, "java"),
labels.AsString(provider.DepSourceLabel, "internal"),
labels.AsString(provider.DepLanguageLabel, "java"),
},
FileURIPrefix: "konveyor-jdt://contentstestdata/io/fabric8/kubernetes-httpclient-okhttp/6.0.0",
},
Expand All @@ -113,8 +113,8 @@ func Test_parseMavenDepLines(t *testing.T) {
Indirect: true,
ResolvedIdentifier: "d3e1ce1d2b3119adf270b2d00d947beb03fe3321",
Labels: []string{
fmt.Sprintf("%v=internal", provider.DepSourceLabel),
fmt.Sprintf("%s=%s", provider.DepLanguageLabel, "java"),
labels.AsString(provider.DepSourceLabel, "internal"),
labels.AsString(provider.DepLanguageLabel, "java"),
},
FileURIPrefix: "konveyor-jdt://contentstestdata/com/squareup/okhttp3/okhttp/3.12.12",
},
Expand All @@ -127,8 +127,8 @@ func Test_parseMavenDepLines(t *testing.T) {
Indirect: true,
ResolvedIdentifier: "bc28b5a964c8f5721eb58ee3f3c47a9bcbf4f4d8",
Labels: []string{
fmt.Sprintf("%v=internal", provider.DepSourceLabel),
fmt.Sprintf("%s=%s", provider.DepLanguageLabel, "java"),
labels.AsString(provider.DepSourceLabel, "internal"),
labels.AsString(provider.DepLanguageLabel, "java"),
},
FileURIPrefix: "konveyor-jdt://contentstestdata/com/squareup/okio/okio/1.15.0",
},
Expand All @@ -141,8 +141,8 @@ func Test_parseMavenDepLines(t *testing.T) {
Indirect: true,
ResolvedIdentifier: "d952189f6abb148ff72aab246aa8c28cf99b469f",
Labels: []string{
fmt.Sprintf("%v=internal", provider.DepSourceLabel),
fmt.Sprintf("%s=%s", provider.DepLanguageLabel, "java"),
labels.AsString(provider.DepSourceLabel, "internal"),
labels.AsString(provider.DepLanguageLabel, "java"),
},
FileURIPrefix: "konveyor-jdt://contentstestdata/com/squareup/okhttp3/logging-interceptor/3.12.12",
},
Expand All @@ -155,8 +155,8 @@ func Test_parseMavenDepLines(t *testing.T) {
Indirect: true,
ResolvedIdentifier: "d3ebf0f291297649b4c8dc3ecc81d2eddedc100d",
Labels: []string{
fmt.Sprintf("%v=internal", provider.DepSourceLabel),
fmt.Sprintf("%s=%s", provider.DepLanguageLabel, "java"),
labels.AsString(provider.DepSourceLabel, "internal"),
labels.AsString(provider.DepLanguageLabel, "java"),
},
FileURIPrefix: "konveyor-jdt://contentstestdata/io/fabric8/zjsonpatch/0.3.0",
},
Expand Down Expand Up @@ -190,8 +190,8 @@ func Test_parseMavenDepLines(t *testing.T) {
Indirect: false,
ResolvedIdentifier: "4e031bb61df09069aeb2bffb4019e7a5034a4ee0",
Labels: []string{
fmt.Sprintf("%v=open-source", provider.DepSourceLabel),
fmt.Sprintf("%s=%s", provider.DepLanguageLabel, "java"),
labels.AsString(provider.DepSourceLabel, "open-source"),
labels.AsString(provider.DepLanguageLabel, "java"),
},
FileURIPrefix: "konveyor-jdt://contentstestdata/junit/junit/4.11",
},
Expand All @@ -204,9 +204,9 @@ func Test_parseMavenDepLines(t *testing.T) {
Indirect: true,
ResolvedIdentifier: "42a25dc3219429f0e5d060061f71acb49bf010a0",
Labels: []string{
fmt.Sprintf(provider.DepExcludeLabel),
fmt.Sprintf("%v=internal", provider.DepSourceLabel),
fmt.Sprintf("%s=%s", provider.DepLanguageLabel, "java"),
labels.AsString(provider.DepExcludeLabel, ""),
labels.AsString(provider.DepSourceLabel, "internal"),
labels.AsString(provider.DepLanguageLabel, "java"),
},
FileURIPrefix: "konveyor-jdt://contentstestdata/org/hamcrest/hamcrest-core/1.3",
},
Expand All @@ -221,8 +221,8 @@ func Test_parseMavenDepLines(t *testing.T) {
Indirect: false,
ResolvedIdentifier: "d0831d44e12313df8989fc1d4a9c90452f08858e",
Labels: []string{
fmt.Sprintf("%v=internal", provider.DepSourceLabel),
fmt.Sprintf("%s=%s", provider.DepLanguageLabel, "java"),
labels.AsString(provider.DepSourceLabel, "internal"),
labels.AsString(provider.DepLanguageLabel, "java"),
},
FileURIPrefix: "konveyor-jdt://contentstestdata/io/fabric8/kubernetes-client/6.0.0",
},
Expand All @@ -235,8 +235,8 @@ func Test_parseMavenDepLines(t *testing.T) {
Indirect: true,
ResolvedIdentifier: "70690b98acb07a809c55d15d7cf45f53ec1026e1",
Labels: []string{
fmt.Sprintf("%v=internal", provider.DepSourceLabel),
fmt.Sprintf("%s=%s", provider.DepLanguageLabel, "java"),
labels.AsString(provider.DepSourceLabel, "internal"),
labels.AsString(provider.DepLanguageLabel, "java"),
},
FileURIPrefix: "konveyor-jdt://contentstestdata/io/fabric8/kubernetes-httpclient-okhttp/6.0.0",
},
Expand All @@ -249,8 +249,8 @@ func Test_parseMavenDepLines(t *testing.T) {
Indirect: true,
ResolvedIdentifier: "d3e1ce1d2b3119adf270b2d00d947beb03fe3321",
Labels: []string{
fmt.Sprintf("%v=internal", provider.DepSourceLabel),
fmt.Sprintf("%s=%s", provider.DepLanguageLabel, "java"),
labels.AsString(provider.DepSourceLabel, "internal"),
labels.AsString(provider.DepLanguageLabel, "java"),
},
FileURIPrefix: "konveyor-jdt://contentstestdata/com/squareup/okhttp3/okhttp/3.12.12",
},
Expand All @@ -263,8 +263,8 @@ func Test_parseMavenDepLines(t *testing.T) {
Indirect: true,
ResolvedIdentifier: "bc28b5a964c8f5721eb58ee3f3c47a9bcbf4f4d8",
Labels: []string{
fmt.Sprintf("%v=internal", provider.DepSourceLabel),
fmt.Sprintf("%s=%s", provider.DepLanguageLabel, "java"),
labels.AsString(provider.DepSourceLabel, "internal"),
labels.AsString(provider.DepLanguageLabel, "java"),
},
FileURIPrefix: "konveyor-jdt://contentstestdata/com/squareup/okio/okio/1.15.0",
},
Expand All @@ -277,8 +277,8 @@ func Test_parseMavenDepLines(t *testing.T) {
Indirect: true,
ResolvedIdentifier: "d952189f6abb148ff72aab246aa8c28cf99b469f",
Labels: []string{
fmt.Sprintf("%v=internal", provider.DepSourceLabel),
fmt.Sprintf("%s=%s", provider.DepLanguageLabel, "java"),
labels.AsString(provider.DepSourceLabel, "internal"),
labels.AsString(provider.DepLanguageLabel, "java"),
},
FileURIPrefix: "konveyor-jdt://contentstestdata/com/squareup/okhttp3/logging-interceptor/3.12.12",
},
Expand All @@ -291,8 +291,8 @@ func Test_parseMavenDepLines(t *testing.T) {
Indirect: true,
ResolvedIdentifier: "d3ebf0f291297649b4c8dc3ecc81d2eddedc100d",
Labels: []string{
fmt.Sprintf("%v=internal", provider.DepSourceLabel),
fmt.Sprintf("%s=%s", provider.DepLanguageLabel, "java"),
labels.AsString(provider.DepSourceLabel, "internal"),
labels.AsString(provider.DepLanguageLabel, "java"),
},
FileURIPrefix: "konveyor-jdt://contentstestdata/io/fabric8/zjsonpatch/0.3.0",
},
Expand Down

0 comments on commit b38354f

Please sign in to comment.