From 90ab75642a713dd17dabbe6becdde5434fc79eb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= Date: Sat, 30 Dec 2023 11:51:38 +0100 Subject: [PATCH] rule: allow usage of Label struct with Rule APIs (#1705) 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. --- rule/BUILD.bazel | 1 + rule/value.go | 4 ++++ rule/value_test.go | 5 +++++ 3 files changed, 10 insertions(+) diff --git a/rule/BUILD.bazel b/rule/BUILD.bazel index ce4c7b2ee..7884d50ef 100644 --- a/rule/BUILD.bazel +++ b/rule/BUILD.bazel @@ -32,6 +32,7 @@ go_test( ], embed = [":rule"], deps = [ + "//label", "@com_github_bazelbuild_buildtools//build", "@com_github_google_go_cmp//cmp", ], diff --git a/rule/value.go b/rule/value.go index 0c9bcf35c..7eb34f8fa 100644 --- a/rule/value.go +++ b/rule/value.go @@ -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 @@ -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). @@ -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()} } } diff --git a/rule/value_test.go b/rule/value_test.go index aae341ea6..f0071e4ea 100644 --- a/rule/value_test.go +++ b/rule/value_test.go @@ -19,6 +19,7 @@ import ( "testing" bzl "github.com/bazelbuild/buildtools/build" + "github.com/bazelbuild/bazel-gazelle/label" "github.com/google/go-cmp/cmp" ) @@ -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)