Skip to content

Commit df59349

Browse files
committed
omit the first line continuation if tuple contains just a few entries; also closes #9
1 parent 7ff8c5b commit df59349

File tree

4 files changed

+26
-37
lines changed

4 files changed

+26
-37
lines changed

main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ func main() {
2828
parser := tuple.NewParser(flagPackagePrefix, flagOffline)
2929
tuples, errors := parser.Load(args[0])
3030
if len(tuples) != 0 {
31-
fmt.Println(tuples)
31+
fmt.Print(tuples)
3232
}
3333
if errors != nil {
34-
fmt.Println(errors)
34+
fmt.Print(errors)
3535
}
3636
}
3737

tuple/tuple.go

+21-30
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"errors"
66
"fmt"
7-
"reflect"
87
"regexp"
98
"sort"
109
"strings"
@@ -112,45 +111,37 @@ func (tt ByAccountAndProject) Less(i, j int) bool {
112111
return tt[i].String() < tt[j].String()
113112
}
114113

114+
// If tuple contains more than largeLimit entries, start tuple list on the new line for easier sorting/editing.
115+
// Otherwise omit the first line continuation for more compact representation.
116+
const largeLimit = 3
117+
115118
func (tt Tuples) String() string {
116119
if len(tt) == 0 {
117120
return ""
118121
}
119122

120-
bufs := make(map[reflect.Type]*bytes.Buffer)
121-
123+
tm := make(map[Source][]string)
122124
for _, t := range tt {
123-
st := reflect.TypeOf(t.Source)
124-
if st == nil {
125-
panic(fmt.Sprintf("unknown source in tuple: %v", t))
126-
}
127-
128-
buf, ok := bufs[st]
129-
if !ok {
130-
buf = &bytes.Buffer{}
131-
bufs[st] = buf
132-
}
133-
134-
var eol string
135-
if buf.Len() == 0 {
136-
buf.WriteString(fmt.Sprintf("%s=\t", t.Source.VarName()))
137-
eol = `\`
138-
}
139-
s := t.String()
140-
if strings.HasPrefix(s, "#") {
141-
eol = ""
142-
} else if eol == "" {
143-
eol = ` \`
144-
}
145-
buf.WriteString(fmt.Sprintf("%s\n\t\t%s", eol, s))
125+
tm[t.Source] = append(tm[t.Source], t.String())
146126
}
147127

148128
var ss []string
149-
for _, buf := range bufs {
150-
s := buf.String()
151-
if len(s) > 0 {
152-
ss = append(ss, s)
129+
for s, ee := range tm {
130+
buf := bytes.NewBufferString(fmt.Sprintf("%s=\t", s.VarName()))
131+
large := len(ee) > largeLimit
132+
if large {
133+
buf.WriteString("\\\n")
134+
}
135+
for i := 0; i < len(ee); i += 1 {
136+
if i > 0 || large {
137+
buf.WriteString("\t\t")
138+
}
139+
buf.WriteString(ee[i])
140+
if i < len(ee)-1 {
141+
buf.WriteString(" \\\n")
142+
}
153143
}
144+
ss = append(ss, buf.String())
154145
}
155146
sort.Sort(sort.StringSlice(ss))
156147

tuple/tuple_online_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ func TestUniqueProjectAndTag(t *testing.T) {
1212
# github.com/json-iterator/go v1.1.7
1313
# github.com/ugorji/go v1.1.7`
1414

15-
expected := `GH_TUPLE= \
16-
json-iterator:go:v1.1.7:json_iterator_go/vendor/github.com/json-iterator/go \
15+
expected := `GH_TUPLE= json-iterator:go:v1.1.7:json_iterator_go/vendor/github.com/json-iterator/go \
1716
ugorji:go:23ab95ef5dc3:ugorji_go/vendor/github.com/ugorji/go
1817
`
1918

tuple/tuple_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,7 @@ gitlab.com/gitlab-org/gitaly-proto/go/gitalypb
186186
ugorji:go:e444a5086c43:ugorji_go/vendor/github.com/ugorji/go \
187187
user:pkg:v3.0.0:user_pkg/vendor/gopkg.in/user/pkg.v3
188188
189-
GL_TUPLE= \
190-
gitlab-org:gitaly-proto:v1.32.0:gitlab_org_gitaly_proto/vendor/gitlab.com/gitlab-org/gitaly-proto \
189+
GL_TUPLE= gitlab-org:gitaly-proto:v1.32.0:gitlab_org_gitaly_proto/vendor/gitlab.com/gitlab-org/gitaly-proto \
191190
gitlab-org:labkit:0c3fc7cdd57c:gitlab_org_labkit/vendor/gitlab.com/gitlab-org/labkit
192191
# Mirrors for the following packages are not currently known, please look them up and handle these tuples manually:
193192
# ::v1.0.0:group_name/vendor/another.vanity_url.org/account/project
@@ -226,6 +225,6 @@ func TestUniqueGroups(t *testing.T) {
226225
}
227226
out := tt.String()
228227
if out != expected {
229-
t.Errorf("expected output %s, got %s", expected, out)
228+
t.Errorf("expected output\n%s, got\n%s", expected, out)
230229
}
231230
}

0 commit comments

Comments
 (0)