Skip to content

Commit

Permalink
rule: allow usage of Label struct with Rule APIs (bazel-contrib#1705)
Browse files Browse the repository at this point in the history
The Label is a convenient structure for custom logic.
Allow its use with the Rule APIs so the user can directly feed
structured data back into Gazelle.
  • Loading branch information
t-8ch authored Dec 30, 2023
1 parent 362ddbd commit 90ab756
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions rule/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ go_test(
],
embed = [":rule"],
deps = [
"//label",
"@com_github_bazelbuild_buildtools//build",
"@com_github_google_go_cmp//cmp",
],
Expand Down
4 changes: 4 additions & 0 deletions rule/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"sort"

bzl "github.com/bazelbuild/buildtools/build"
"github.com/bazelbuild/bazel-gazelle/label"
)

// KeyValue represents a key-value pair. This gets converted into a
Expand Down Expand Up @@ -125,6 +126,7 @@ func (s SelectStringListValue) BzlExpr() bzl.Expr {
// a Bazel build file. The following types of values can be converted:
//
// * bools, integers, floats, strings.
// * labels (converted to strings).
// * slices, arrays (converted to lists).
// * maps (converted to select expressions; keys must be rules in
// @io_bazel_rules_go//go/platform).
Expand Down Expand Up @@ -198,6 +200,8 @@ func ExprFromValue(val interface{}) bzl.Expr {
X: &bzl.LiteralExpr{Token: "glob"},
List: globArgs,
}
case label.Label:
return &bzl.StringExpr{Value: val.String()}
}
}

Expand Down
5 changes: 5 additions & 0 deletions rule/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"testing"

bzl "github.com/bazelbuild/buildtools/build"
"github.com/bazelbuild/bazel-gazelle/label"
"github.com/google/go-cmp/cmp"
)

Expand Down Expand Up @@ -90,6 +91,10 @@ func TestExprFromValue(t *testing.T) {
},
},
},
"labels": {
val: label.New("repo", "pkg", "name"),
want: &bzl.StringExpr{Value: "@repo//pkg:name"},
},
} {
t.Run(name, func(t *testing.T) {
got := ExprFromValue(tt.val)
Expand Down

0 comments on commit 90ab756

Please sign in to comment.