blob: 064fc21697cd9c632cfa195f47989b177e3c8c57 [file] [log] [blame]
Liz Kammer72beb342022-02-03 08:42:10 -05001// Copyright 2022 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package starlark_fmt
16
17import (
18 "fmt"
19 "sort"
Alix Espino4fd7e742023-02-24 14:46:43 +000020 "strconv"
Liz Kammer72beb342022-02-03 08:42:10 -050021 "strings"
22)
23
24const (
25 indent = 4
26)
27
28// Indention returns an indent string of the specified level.
29func Indention(level int) string {
30 if level < 0 {
31 panic(fmt.Errorf("indent level cannot be less than 0, but got %d", level))
32 }
33 return strings.Repeat(" ", level*indent)
34}
35
36// PrintBool returns a Starlark compatible bool string.
37func PrintBool(item bool) string {
38 return strings.Title(fmt.Sprintf("%t", item))
39}
40
41// PrintsStringList returns a Starlark-compatible string of a list of Strings/Labels.
42func PrintStringList(items []string, indentLevel int) string {
Sam Delmerico932c01c2022-03-25 16:33:26 +000043 return PrintList(items, indentLevel, func(s string) string {
44 if strings.Contains(s, "\"") {
45 return `'''%s'''`
46 }
47 return `"%s"`
48 })
Liz Kammer72beb342022-02-03 08:42:10 -050049}
50
51// PrintList returns a Starlark-compatible string of list formmated as requested.
Sam Delmerico932c01c2022-03-25 16:33:26 +000052func PrintList(items []string, indentLevel int, formatString func(string) string) string {
Liz Kammer72beb342022-02-03 08:42:10 -050053 if len(items) == 0 {
54 return "[]"
55 } else if len(items) == 1 {
Sam Delmerico932c01c2022-03-25 16:33:26 +000056 return fmt.Sprintf("["+formatString(items[0])+"]", items[0])
Liz Kammer72beb342022-02-03 08:42:10 -050057 }
58 list := make([]string, 0, len(items)+2)
59 list = append(list, "[")
60 innerIndent := Indention(indentLevel + 1)
61 for _, item := range items {
Sam Delmerico932c01c2022-03-25 16:33:26 +000062 list = append(list, fmt.Sprintf(`%s`+formatString(item)+`,`, innerIndent, item))
Liz Kammer72beb342022-02-03 08:42:10 -050063 }
64 list = append(list, Indention(indentLevel)+"]")
65 return strings.Join(list, "\n")
66}
67
68// PrintStringListDict returns a Starlark-compatible string formatted as dictionary with
69// string keys and list of string values.
70func PrintStringListDict(dict map[string][]string, indentLevel int) string {
71 formattedValueDict := make(map[string]string, len(dict))
72 for k, v := range dict {
73 formattedValueDict[k] = PrintStringList(v, indentLevel+1)
74 }
75 return PrintDict(formattedValueDict, indentLevel)
76}
77
78// PrintBoolDict returns a starlark-compatible string containing a dictionary with string keys and
79// values printed with no additional formatting.
80func PrintBoolDict(dict map[string]bool, indentLevel int) string {
81 formattedValueDict := make(map[string]string, len(dict))
82 for k, v := range dict {
83 formattedValueDict[k] = PrintBool(v)
84 }
85 return PrintDict(formattedValueDict, indentLevel)
86}
87
Alix Espino4fd7e742023-02-24 14:46:43 +000088// PrintStringIntDict returns a Starlark-compatible string formatted as dictionary with
89// string keys and int values.
90func PrintStringIntDict(dict map[string]int, indentLevel int) string {
91 valDict := make(map[string]string, len(dict))
92 for k, v := range dict {
93 valDict[k] = strconv.Itoa(v)
94 }
95 return PrintDict(valDict, indentLevel)
96}
97
Liz Kammer72beb342022-02-03 08:42:10 -050098// PrintDict returns a starlark-compatible string containing a dictionary with string keys and
99// values printed with no additional formatting.
100func PrintDict(dict map[string]string, indentLevel int) string {
101 if len(dict) == 0 {
102 return "{}"
103 }
104 items := make([]string, 0, len(dict))
105 for k, v := range dict {
106 items = append(items, fmt.Sprintf(`%s"%s": %s,`, Indention(indentLevel+1), k, v))
107 }
108 sort.Strings(items)
109 return fmt.Sprintf(`{
110%s
111%s}`, strings.Join(items, "\n"), Indention(indentLevel))
112}