• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// 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"
20	"strconv"
21	"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	if item {
39		return "True"
40	} else {
41		return "False"
42	}
43}
44
45// PrintsStringList returns a Starlark-compatible string of a list of Strings/Labels.
46func PrintStringList(items []string, indentLevel int) string {
47	return PrintList(items, indentLevel, func(s string) string {
48		if strings.Contains(s, "\"") {
49			return `'''%s'''`
50		}
51		return `"%s"`
52	})
53}
54
55// PrintList returns a Starlark-compatible string of list formmated as requested.
56func PrintList(items []string, indentLevel int, formatString func(string) string) string {
57	if len(items) == 0 {
58		return "[]"
59	} else if len(items) == 1 {
60		return fmt.Sprintf("["+formatString(items[0])+"]", items[0])
61	}
62	list := make([]string, 0, len(items)+2)
63	list = append(list, "[")
64	innerIndent := Indention(indentLevel + 1)
65	for _, item := range items {
66		list = append(list, fmt.Sprintf(`%s`+formatString(item)+`,`, innerIndent, item))
67	}
68	list = append(list, Indention(indentLevel)+"]")
69	return strings.Join(list, "\n")
70}
71
72// PrintStringListDict returns a Starlark-compatible string formatted as dictionary with
73// string keys and list of string values.
74func PrintStringListDict(dict map[string][]string, indentLevel int) string {
75	formattedValueDict := make(map[string]string, len(dict))
76	for k, v := range dict {
77		formattedValueDict[k] = PrintStringList(v, indentLevel+1)
78	}
79	return PrintDict(formattedValueDict, indentLevel)
80}
81
82// PrintBoolDict returns a starlark-compatible string containing a dictionary with string keys and
83// values printed with no additional formatting.
84func PrintBoolDict(dict map[string]bool, indentLevel int) string {
85	formattedValueDict := make(map[string]string, len(dict))
86	for k, v := range dict {
87		formattedValueDict[k] = PrintBool(v)
88	}
89	return PrintDict(formattedValueDict, indentLevel)
90}
91
92// PrintStringIntDict returns a Starlark-compatible string formatted as dictionary with
93// string keys and int values.
94func PrintStringIntDict(dict map[string]int, indentLevel int) string {
95	valDict := make(map[string]string, len(dict))
96	for k, v := range dict {
97		valDict[k] = strconv.Itoa(v)
98	}
99	return PrintDict(valDict, indentLevel)
100}
101
102// PrintDict returns a starlark-compatible string containing a dictionary with string keys and
103// values printed with no additional formatting.
104func PrintDict(dict map[string]string, indentLevel int) string {
105	if len(dict) == 0 {
106		return "{}"
107	}
108	items := make([]string, 0, len(dict))
109	for k, v := range dict {
110		items = append(items, fmt.Sprintf(`%s"%s": %s,`, Indention(indentLevel+1), k, v))
111	}
112	sort.Strings(items)
113	return fmt.Sprintf(`{
114%s
115%s}`, strings.Join(items, "\n"), Indention(indentLevel))
116}
117