Skip to content

Commit

Permalink
implement expression conversions via BzlExprValue interface (bazel-co…
Browse files Browse the repository at this point in the history
…ntrib#1714)

* label: implement BzlExprValue

* rule: GlobValue: implement BzlExprValue
  • Loading branch information
t-8ch authored Jan 4, 2024
1 parent e7e69c4 commit 5ca287b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
5 changes: 4 additions & 1 deletion label/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ go_library(
srcs = ["label.go"],
importpath = "github.com/bazelbuild/bazel-gazelle/label",
visibility = ["//visibility:public"],
deps = ["//pathtools"],
deps = [
"//pathtools",
"@com_github_bazelbuild_buildtools//build",
],
)

go_test(
Expand Down
7 changes: 7 additions & 0 deletions label/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"strings"

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

// A Label represents a label of a build target in Bazel. Labels have three
Expand Down Expand Up @@ -210,6 +211,12 @@ func (l Label) Contains(other Label) bool {
return result
}

func (l Label) BzlExpr() bzl.Expr {
return &bzl.StringExpr {
Value: l.String(),
}
}

var nonWordRe = regexp.MustCompile(`\W+`)

// ImportPathToBazelRepoName converts a Go import path into a bazel repo name
Expand Down
39 changes: 17 additions & 22 deletions rule/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ 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 All @@ -38,6 +37,23 @@ type GlobValue struct {
Excludes []string
}

func (g GlobValue) BzlExpr() bzl.Expr {
patternsValue := ExprFromValue(g.Patterns)
globArgs := []bzl.Expr{patternsValue}
if len(g.Excludes) > 0 {
excludesValue := ExprFromValue(g.Excludes)
globArgs = append(globArgs, &bzl.AssignExpr{
LHS: &bzl.LiteralExpr{Token: "exclude"},
Op: "=",
RHS: excludesValue,
})
}
return &bzl.CallExpr{
X: &bzl.LiteralExpr{Token: "glob"},
List: globArgs,
}
}

// BzlExprValue is implemented by types that have custom translations
// to Starlark values.
type BzlExprValue interface {
Expand Down Expand Up @@ -182,27 +198,6 @@ func ExprFromValue(val interface{}) bzl.Expr {
args[i] = &bzl.KeyValueExpr{Key: k, Value: v}
}
return &bzl.DictExpr{List: args, ForceMultiLine: true}

case reflect.Struct:
switch val := val.(type) {
case GlobValue:
patternsValue := ExprFromValue(val.Patterns)
globArgs := []bzl.Expr{patternsValue}
if len(val.Excludes) > 0 {
excludesValue := ExprFromValue(val.Excludes)
globArgs = append(globArgs, &bzl.AssignExpr{
LHS: &bzl.LiteralExpr{Token: "exclude"},
Op: "=",
RHS: excludesValue,
})
}
return &bzl.CallExpr{
X: &bzl.LiteralExpr{Token: "glob"},
List: globArgs,
}
case label.Label:
return &bzl.StringExpr{Value: val.String()}
}
}

log.Panicf("type not supported: %T", val)
Expand Down

0 comments on commit 5ca287b

Please sign in to comment.