Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 1 | package bp2build |
| 2 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 3 | import ( |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 4 | "fmt" |
| 5 | "reflect" |
Chris Parsons | 58852a0 | 2021-12-09 18:10:18 -0500 | [diff] [blame] | 6 | |
| 7 | "android/soong/android" |
| 8 | "android/soong/bazel" |
Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 9 | "android/soong/starlark_fmt" |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 10 | ) |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 11 | |
| 12 | // Configurability support for bp2build. |
| 13 | |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 14 | type selects map[string]reflect.Value |
| 15 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 3a019a6 | 2022-06-23 16:02:44 +0000 | [diff] [blame] | 16 | func getStringValue(str bazel.StringAttribute) (reflect.Value, []selects) { |
| 17 | value := reflect.ValueOf(str.Value) |
| 18 | |
| 19 | if !str.HasConfigurableValues() { |
| 20 | return value, []selects{} |
| 21 | } |
| 22 | |
| 23 | ret := selects{} |
| 24 | for _, axis := range str.SortedConfigurationAxes() { |
| 25 | configToStrs := str.ConfigurableValues[axis] |
| 26 | for config, strs := range configToStrs { |
| 27 | selectKey := axis.SelectKey(config) |
| 28 | ret[selectKey] = reflect.ValueOf(strs) |
| 29 | } |
| 30 | } |
| 31 | // if there is a select, use the base value as the conditions default value |
| 32 | if len(ret) > 0 { |
| 33 | ret[bazel.ConditionsDefaultSelectKey] = value |
| 34 | value = reflect.Zero(value.Type()) |
| 35 | } |
| 36 | |
| 37 | return value, []selects{ret} |
| 38 | } |
| 39 | |
Zi Wang | 1cb1180 | 2022-12-09 16:08:54 -0800 | [diff] [blame] | 40 | func getStringListValues(list bazel.StringListAttribute) (reflect.Value, []selects, bool) { |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 41 | value := reflect.ValueOf(list.Value) |
Zi Wang | 0f82844 | 2022-12-28 11:18:11 -0800 | [diff] [blame] | 42 | prepend := list.Prepend |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 43 | if !list.HasConfigurableValues() { |
Zi Wang | 1cb1180 | 2022-12-09 16:08:54 -0800 | [diff] [blame] | 44 | return value, []selects{}, prepend |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 45 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 46 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 47 | var ret []selects |
| 48 | for _, axis := range list.SortedConfigurationAxes() { |
| 49 | configToLists := list.ConfigurableValues[axis] |
| 50 | archSelects := map[string]reflect.Value{} |
| 51 | for config, labels := range configToLists { |
| 52 | selectKey := axis.SelectKey(config) |
| 53 | archSelects[selectKey] = reflect.ValueOf(labels) |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 54 | } |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 55 | if len(archSelects) > 0 { |
| 56 | ret = append(ret, archSelects) |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
Zi Wang | 1cb1180 | 2022-12-09 16:08:54 -0800 | [diff] [blame] | 60 | return value, ret, prepend |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 63 | func getLabelValue(label bazel.LabelAttribute) (reflect.Value, []selects) { |
Rupert Shuttleworth | 22cd2eb | 2021-05-27 02:15:54 -0400 | [diff] [blame] | 64 | value := reflect.ValueOf(label.Value) |
| 65 | if !label.HasConfigurableValues() { |
| 66 | return value, []selects{} |
Lukacs T. Berki | 56bb083 | 2021-05-12 12:36:45 +0200 | [diff] [blame] | 67 | } |
| 68 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 69 | ret := selects{} |
| 70 | for _, axis := range label.SortedConfigurationAxes() { |
| 71 | configToLabels := label.ConfigurableValues[axis] |
| 72 | for config, labels := range configToLabels { |
| 73 | selectKey := axis.SelectKey(config) |
| 74 | ret[selectKey] = reflect.ValueOf(labels) |
Rupert Shuttleworth | 22cd2eb | 2021-05-27 02:15:54 -0400 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
Liz Kammer | dff00ea | 2021-10-04 13:44:34 -0400 | [diff] [blame] | 78 | // if there is a select, use the base value as the conditions default value |
| 79 | if len(ret) > 0 { |
| 80 | ret[bazel.ConditionsDefaultSelectKey] = value |
| 81 | value = reflect.Zero(value.Type()) |
| 82 | } |
| 83 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 84 | return value, []selects{ret} |
Lukacs T. Berki | 1353e59 | 2021-04-30 15:35:09 +0200 | [diff] [blame] | 85 | } |
| 86 | |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 87 | func getBoolValue(boolAttr bazel.BoolAttribute) (reflect.Value, []selects) { |
| 88 | value := reflect.ValueOf(boolAttr.Value) |
| 89 | if !boolAttr.HasConfigurableValues() { |
| 90 | return value, []selects{} |
| 91 | } |
| 92 | |
| 93 | ret := selects{} |
| 94 | for _, axis := range boolAttr.SortedConfigurationAxes() { |
| 95 | configToBools := boolAttr.ConfigurableValues[axis] |
| 96 | for config, bools := range configToBools { |
| 97 | selectKey := axis.SelectKey(config) |
| 98 | ret[selectKey] = reflect.ValueOf(bools) |
| 99 | } |
| 100 | } |
| 101 | // if there is a select, use the base value as the conditions default value |
| 102 | if len(ret) > 0 { |
| 103 | ret[bazel.ConditionsDefaultSelectKey] = value |
| 104 | value = reflect.Zero(value.Type()) |
| 105 | } |
| 106 | |
| 107 | return value, []selects{ret} |
| 108 | } |
Zi Wang | 9f609db | 2023-01-04 11:06:54 -0800 | [diff] [blame] | 109 | func getLabelListValues(list bazel.LabelListAttribute) (reflect.Value, []selects, bool) { |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 110 | value := reflect.ValueOf(list.Value.Includes) |
Zi Wang | 9f609db | 2023-01-04 11:06:54 -0800 | [diff] [blame] | 111 | prepend := list.Prepend |
Liz Kammer | 2b07ec7 | 2021-05-26 15:08:27 -0400 | [diff] [blame] | 112 | var ret []selects |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 113 | for _, axis := range list.SortedConfigurationAxes() { |
| 114 | configToLabels := list.ConfigurableValues[axis] |
| 115 | if !configToLabels.HasConfigurableValues() { |
| 116 | continue |
Liz Kammer | 2b07ec7 | 2021-05-26 15:08:27 -0400 | [diff] [blame] | 117 | } |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 118 | archSelects := map[string]reflect.Value{} |
Chris Parsons | 51f8c39 | 2021-08-03 21:01:05 -0400 | [diff] [blame] | 119 | defaultVal := configToLabels[bazel.ConditionsDefaultConfigKey] |
Chris Parsons | 58852a0 | 2021-12-09 18:10:18 -0500 | [diff] [blame] | 120 | // Skip empty list values unless ether EmitEmptyList is true, or these values differ from the default. |
| 121 | emitEmptyList := list.EmitEmptyList || len(defaultVal.Includes) > 0 |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 122 | for config, labels := range configToLabels { |
Chris Parsons | 51f8c39 | 2021-08-03 21:01:05 -0400 | [diff] [blame] | 123 | // Omit any entries in the map which match the default value, for brevity. |
| 124 | if config != bazel.ConditionsDefaultConfigKey && labels.Equals(defaultVal) { |
| 125 | continue |
| 126 | } |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 127 | selectKey := axis.SelectKey(config) |
Chris Parsons | 58852a0 | 2021-12-09 18:10:18 -0500 | [diff] [blame] | 128 | if use, value := labelListSelectValue(selectKey, labels, emitEmptyList); use { |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 129 | archSelects[selectKey] = value |
Liz Kammer | 2b07ec7 | 2021-05-26 15:08:27 -0400 | [diff] [blame] | 130 | } |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 131 | } |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 132 | if len(archSelects) > 0 { |
| 133 | ret = append(ret, archSelects) |
Liz Kammer | 2b07ec7 | 2021-05-26 15:08:27 -0400 | [diff] [blame] | 134 | } |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Zi Wang | 9f609db | 2023-01-04 11:06:54 -0800 | [diff] [blame] | 137 | return value, ret, prepend |
Liz Kammer | 2b07ec7 | 2021-05-26 15:08:27 -0400 | [diff] [blame] | 138 | } |
| 139 | |
Jingwen Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 140 | func labelListSelectValue(selectKey string, list bazel.LabelList, emitEmptyList bool) (bool, reflect.Value) { |
| 141 | if selectKey == bazel.ConditionsDefaultSelectKey || emitEmptyList || len(list.Includes) > 0 { |
Liz Kammer | 2b07ec7 | 2021-05-26 15:08:27 -0400 | [diff] [blame] | 142 | return true, reflect.ValueOf(list.Includes) |
| 143 | } else if len(list.Excludes) > 0 { |
| 144 | // if there is still an excludes -- we need to have an empty list for this select & use the |
| 145 | // value in conditions default Includes |
| 146 | return true, reflect.ValueOf([]string{}) |
| 147 | } |
| 148 | return false, reflect.Zero(reflect.TypeOf([]string{})) |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 151 | var ( |
| 152 | emptyBazelList = "[]" |
| 153 | bazelNone = "None" |
| 154 | ) |
| 155 | |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 156 | // prettyPrintAttribute converts an Attribute to its Bazel syntax. May contain |
| 157 | // select statements. |
| 158 | func prettyPrintAttribute(v bazel.Attribute, indent int) (string, error) { |
| 159 | var value reflect.Value |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 160 | var configurableAttrs []selects |
Zi Wang | 1cb1180 | 2022-12-09 16:08:54 -0800 | [diff] [blame] | 161 | var prepend bool |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 162 | var defaultSelectValue *string |
Jingwen Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 163 | var emitZeroValues bool |
Chris Parsons | 51f8c39 | 2021-08-03 21:01:05 -0400 | [diff] [blame] | 164 | // If true, print the default attribute value, even if the attribute is zero. |
| 165 | shouldPrintDefault := false |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 166 | switch list := v.(type) { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 3a019a6 | 2022-06-23 16:02:44 +0000 | [diff] [blame] | 167 | case bazel.StringAttribute: |
| 168 | if err := list.Collapse(); err != nil { |
| 169 | return "", err |
| 170 | } |
| 171 | value, configurableAttrs = getStringValue(list) |
| 172 | defaultSelectValue = &bazelNone |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 173 | case bazel.StringListAttribute: |
Zi Wang | 1cb1180 | 2022-12-09 16:08:54 -0800 | [diff] [blame] | 174 | value, configurableAttrs, prepend = getStringListValues(list) |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 175 | defaultSelectValue = &emptyBazelList |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 176 | case bazel.LabelListAttribute: |
Zi Wang | 9f609db | 2023-01-04 11:06:54 -0800 | [diff] [blame] | 177 | value, configurableAttrs, prepend = getLabelListValues(list) |
Jingwen Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 178 | emitZeroValues = list.EmitEmptyList |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 179 | defaultSelectValue = &emptyBazelList |
Chris Parsons | 51f8c39 | 2021-08-03 21:01:05 -0400 | [diff] [blame] | 180 | if list.ForceSpecifyEmptyList && (!value.IsNil() || list.HasConfigurableValues()) { |
| 181 | shouldPrintDefault = true |
| 182 | } |
Lukacs T. Berki | 1353e59 | 2021-04-30 15:35:09 +0200 | [diff] [blame] | 183 | case bazel.LabelAttribute: |
Chris Parsons | 58852a0 | 2021-12-09 18:10:18 -0500 | [diff] [blame] | 184 | if err := list.Collapse(); err != nil { |
| 185 | return "", err |
| 186 | } |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 187 | value, configurableAttrs = getLabelValue(list) |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 188 | defaultSelectValue = &bazelNone |
| 189 | case bazel.BoolAttribute: |
Chris Parsons | 58852a0 | 2021-12-09 18:10:18 -0500 | [diff] [blame] | 190 | if err := list.Collapse(); err != nil { |
| 191 | return "", err |
| 192 | } |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 193 | value, configurableAttrs = getBoolValue(list) |
| 194 | defaultSelectValue = &bazelNone |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 195 | default: |
| 196 | return "", fmt.Errorf("Not a supported Bazel attribute type: %s", v) |
| 197 | } |
| 198 | |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 199 | var err error |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 200 | ret := "" |
| 201 | if value.Kind() != reflect.Invalid { |
Jingwen Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 202 | s, err := prettyPrint(value, indent, false) // never emit zero values for the base value |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 203 | if err != nil { |
| 204 | return ret, err |
| 205 | } |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 206 | |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 207 | ret += s |
| 208 | } |
Zi Wang | 1cb1180 | 2022-12-09 16:08:54 -0800 | [diff] [blame] | 209 | // Convenience function to prepend/append selects components to an attribute value. |
| 210 | concatenateSelects := func(selectsData selects, defaultValue *string, s string, prepend bool) (string, error) { |
Jingwen Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 211 | selectMap, err := prettyPrintSelectMap(selectsData, defaultValue, indent, emitZeroValues) |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 212 | if err != nil { |
| 213 | return "", err |
| 214 | } |
Zi Wang | 1cb1180 | 2022-12-09 16:08:54 -0800 | [diff] [blame] | 215 | var left, right string |
| 216 | if prepend { |
| 217 | left, right = selectMap, s |
| 218 | } else { |
| 219 | left, right = s, selectMap |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 220 | } |
Zi Wang | 1cb1180 | 2022-12-09 16:08:54 -0800 | [diff] [blame] | 221 | if left != "" && right != "" { |
| 222 | left += " + " |
| 223 | } |
| 224 | left += right |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 225 | |
Zi Wang | 1cb1180 | 2022-12-09 16:08:54 -0800 | [diff] [blame] | 226 | return left, nil |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 227 | } |
| 228 | |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 229 | for _, configurableAttr := range configurableAttrs { |
Zi Wang | 1cb1180 | 2022-12-09 16:08:54 -0800 | [diff] [blame] | 230 | ret, err = concatenateSelects(configurableAttr, defaultSelectValue, ret, prepend) |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 231 | if err != nil { |
| 232 | return "", err |
| 233 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 234 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 235 | |
Chris Parsons | 51f8c39 | 2021-08-03 21:01:05 -0400 | [diff] [blame] | 236 | if ret == "" && shouldPrintDefault { |
| 237 | return *defaultSelectValue, nil |
| 238 | } |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 239 | return ret, nil |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | // prettyPrintSelectMap converts a map of select keys to reflected Values as a generic way |
| 243 | // to construct a select map for any kind of attribute type. |
Jingwen Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 244 | func prettyPrintSelectMap(selectMap map[string]reflect.Value, defaultValue *string, indent int, emitZeroValues bool) (string, error) { |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 245 | if selectMap == nil { |
| 246 | return "", nil |
| 247 | } |
| 248 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 249 | var selects string |
| 250 | for _, selectKey := range android.SortedStringKeys(selectMap) { |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 251 | if selectKey == bazel.ConditionsDefaultSelectKey { |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 252 | // Handle default condition later. |
| 253 | continue |
| 254 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 255 | value := selectMap[selectKey] |
Chris Parsons | 58852a0 | 2021-12-09 18:10:18 -0500 | [diff] [blame] | 256 | if isZero(value) && !emitZeroValues && isZero(selectMap[bazel.ConditionsDefaultSelectKey]) { |
| 257 | // Ignore zero values to not generate empty lists. However, always note zero values if |
| 258 | // the default value is non-zero. |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 259 | continue |
| 260 | } |
Chris Parsons | 58852a0 | 2021-12-09 18:10:18 -0500 | [diff] [blame] | 261 | s, err := prettyPrintSelectEntry(value, selectKey, indent, true) |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 262 | if err != nil { |
| 263 | return "", err |
| 264 | } |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 265 | // s could still be an empty string, e.g. unset slices of structs with |
| 266 | // length of 0. |
| 267 | if s != "" { |
| 268 | selects += s + ",\n" |
| 269 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | if len(selects) == 0 { |
| 273 | // No conditions (or all values are empty lists), so no need for a map. |
| 274 | return "", nil |
| 275 | } |
| 276 | |
| 277 | // Create the map. |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 278 | ret := "select({\n" |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 279 | ret += selects |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 280 | |
| 281 | // Handle the default condition |
Jingwen Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 282 | s, err := prettyPrintSelectEntry(selectMap[bazel.ConditionsDefaultSelectKey], bazel.ConditionsDefaultSelectKey, indent, emitZeroValues) |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 283 | if err != nil { |
| 284 | return "", err |
| 285 | } |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 286 | if s != "" { |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 287 | // Print the custom default value. |
| 288 | ret += s |
| 289 | ret += ",\n" |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 290 | } else if defaultValue != nil { |
| 291 | // Print an explicit empty list (the default value) even if the value is |
| 292 | // empty, to avoid errors about not finding a configuration that matches. |
Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 293 | ret += fmt.Sprintf("%s\"%s\": %s,\n", starlark_fmt.Indention(indent+1), bazel.ConditionsDefaultSelectKey, *defaultValue) |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 296 | ret += starlark_fmt.Indention(indent) |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 297 | ret += "})" |
| 298 | |
| 299 | return ret, nil |
| 300 | } |
| 301 | |
| 302 | // prettyPrintSelectEntry converts a reflect.Value into an entry in a select map |
| 303 | // with a provided key. |
Jingwen Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 304 | func prettyPrintSelectEntry(value reflect.Value, key string, indent int, emitZeroValues bool) (string, error) { |
Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 305 | s := starlark_fmt.Indention(indent + 1) |
Jingwen Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 306 | v, err := prettyPrint(value, indent+1, emitZeroValues) |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 307 | if err != nil { |
| 308 | return "", err |
| 309 | } |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 310 | if v == "" { |
| 311 | return "", nil |
| 312 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 313 | s += fmt.Sprintf("\"%s\": %s", key, v) |
| 314 | return s, nil |
| 315 | } |