value.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* Copyright 2016 The Bazel Authors. All rights reserved.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package rule
  13. import (
  14. "fmt"
  15. "log"
  16. "reflect"
  17. "sort"
  18. bzl "github.com/bazelbuild/buildtools/build"
  19. )
  20. // KeyValue represents a key-value pair. This gets converted into a
  21. // rule attribute, i.e., a Skylark keyword argument.
  22. type KeyValue struct {
  23. Key string
  24. Value interface{}
  25. }
  26. // GlobValue represents a Bazel glob expression.
  27. type GlobValue struct {
  28. Patterns []string
  29. Excludes []string
  30. }
  31. // ExprFromValue converts a value into an expression that can be written into
  32. // a Bazel build file. The following types of values can be converted:
  33. //
  34. // * bools, integers, floats, strings.
  35. // * slices, arrays (converted to lists).
  36. // * maps (converted to select expressions; keys must be rules in
  37. // @io_bazel_rules_go//go/platform).
  38. // * GlobValue (converted to glob expressions).
  39. // * PlatformStrings (converted to a concatenation of a list and selects).
  40. //
  41. // Converting unsupported types will cause a panic.
  42. func ExprFromValue(val interface{}) bzl.Expr {
  43. if e, ok := val.(bzl.Expr); ok {
  44. return e
  45. }
  46. rv := reflect.ValueOf(val)
  47. switch rv.Kind() {
  48. case reflect.Bool:
  49. tok := "False"
  50. if rv.Bool() {
  51. tok = "True"
  52. }
  53. return &bzl.LiteralExpr{Token: tok}
  54. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
  55. reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  56. return &bzl.LiteralExpr{Token: fmt.Sprintf("%d", val)}
  57. case reflect.Float32, reflect.Float64:
  58. return &bzl.LiteralExpr{Token: fmt.Sprintf("%f", val)}
  59. case reflect.String:
  60. return &bzl.StringExpr{Value: val.(string)}
  61. case reflect.Slice, reflect.Array:
  62. var list []bzl.Expr
  63. for i := 0; i < rv.Len(); i++ {
  64. elem := ExprFromValue(rv.Index(i).Interface())
  65. list = append(list, elem)
  66. }
  67. return &bzl.ListExpr{List: list}
  68. case reflect.Map:
  69. rkeys := rv.MapKeys()
  70. sort.Sort(byString(rkeys))
  71. args := make([]bzl.Expr, len(rkeys))
  72. for i, rk := range rkeys {
  73. label := fmt.Sprintf("@io_bazel_rules_go//go/platform:%s", mapKeyString(rk))
  74. k := &bzl.StringExpr{Value: label}
  75. v := ExprFromValue(rv.MapIndex(rk).Interface())
  76. if l, ok := v.(*bzl.ListExpr); ok {
  77. l.ForceMultiLine = true
  78. }
  79. args[i] = &bzl.KeyValueExpr{Key: k, Value: v}
  80. }
  81. args = append(args, &bzl.KeyValueExpr{
  82. Key: &bzl.StringExpr{Value: "//conditions:default"},
  83. Value: &bzl.ListExpr{},
  84. })
  85. sel := &bzl.CallExpr{
  86. X: &bzl.LiteralExpr{Token: "select"},
  87. List: []bzl.Expr{&bzl.DictExpr{List: args, ForceMultiLine: true}},
  88. }
  89. return sel
  90. case reflect.Struct:
  91. switch val := val.(type) {
  92. case GlobValue:
  93. patternsValue := ExprFromValue(val.Patterns)
  94. globArgs := []bzl.Expr{patternsValue}
  95. if len(val.Excludes) > 0 {
  96. excludesValue := ExprFromValue(val.Excludes)
  97. globArgs = append(globArgs, &bzl.KeyValueExpr{
  98. Key: &bzl.StringExpr{Value: "excludes"},
  99. Value: excludesValue,
  100. })
  101. }
  102. return &bzl.CallExpr{
  103. X: &bzl.LiteralExpr{Token: "glob"},
  104. List: globArgs,
  105. }
  106. case PlatformStrings:
  107. var pieces []bzl.Expr
  108. if len(val.Generic) > 0 {
  109. pieces = append(pieces, ExprFromValue(val.Generic))
  110. }
  111. if len(val.OS) > 0 {
  112. pieces = append(pieces, ExprFromValue(val.OS))
  113. }
  114. if len(val.Arch) > 0 {
  115. pieces = append(pieces, ExprFromValue(val.Arch))
  116. }
  117. if len(val.Platform) > 0 {
  118. pieces = append(pieces, ExprFromValue(val.Platform))
  119. }
  120. if len(pieces) == 0 {
  121. return &bzl.ListExpr{}
  122. } else if len(pieces) == 1 {
  123. return pieces[0]
  124. } else {
  125. e := pieces[0]
  126. if list, ok := e.(*bzl.ListExpr); ok {
  127. list.ForceMultiLine = true
  128. }
  129. for _, piece := range pieces[1:] {
  130. e = &bzl.BinaryExpr{X: e, Y: piece, Op: "+"}
  131. }
  132. return e
  133. }
  134. }
  135. }
  136. log.Panicf("type not supported: %T", val)
  137. return nil
  138. }
  139. func mapKeyString(k reflect.Value) string {
  140. switch s := k.Interface().(type) {
  141. case string:
  142. return s
  143. case Platform:
  144. return s.String()
  145. default:
  146. log.Panicf("unexpected map key: %v", k)
  147. return ""
  148. }
  149. }
  150. type byString []reflect.Value
  151. var _ sort.Interface = byString{}
  152. func (s byString) Len() int {
  153. return len(s)
  154. }
  155. func (s byString) Less(i, j int) bool {
  156. return mapKeyString(s[i]) < mapKeyString(s[j])
  157. }
  158. func (s byString) Swap(i, j int) {
  159. s[i], s[j] = s[j], s[i]
  160. }