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