Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
godebug
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
godebug
Commits
eadb3ce3
Commit
eadb3ce3
authored
Apr 06, 2016
by
Kyle Lemons
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Restructure keyval WriteTo code to match lists
parent
68d2bbec
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
60 additions
and
33 deletions
+60
-33
pretty/structure.go
pretty/structure.go
+38
-33
pretty/structure_test.go
pretty/structure_test.go
+22
-0
No files found.
pretty/structure.go
View file @
eadb3ce3
...
...
@@ -61,47 +61,54 @@ func (l keyvals) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
func
(
l
keyvals
)
Less
(
i
,
j
int
)
bool
{
return
l
[
i
]
.
key
<
l
[
j
]
.
key
}
func
(
l
keyvals
)
WriteTo
(
w
*
bytes
.
Buffer
,
indent
string
,
cfg
*
Config
)
{
padding
:=
""
inner
:=
indent
+
" "
w
.
WriteByte
(
'{'
)
if
!
cfg
.
Compact
&&
!
cfg
.
Diffable
{
switch
{
case
cfg
.
Compact
:
// All on one line:
for
i
,
kv
:=
range
l
{
if
i
>
0
{
w
.
WriteByte
(
','
)
}
w
.
WriteString
(
kv
.
key
)
w
.
WriteByte
(
':'
)
kv
.
val
.
WriteTo
(
w
,
indent
,
cfg
)
}
case
cfg
.
Diffable
:
w
.
WriteByte
(
'\n'
)
inner
:=
indent
+
" "
// Each value gets its own line:
for
_
,
kv
:=
range
l
{
w
.
WriteString
(
inner
)
w
.
WriteString
(
kv
.
key
)
w
.
WriteString
(
": "
)
kv
.
val
.
WriteTo
(
w
,
inner
,
cfg
)
w
.
WriteString
(
",
\n
"
)
}
w
.
WriteString
(
indent
)
default
:
keyWidth
:=
0
for
_
,
kv
:=
range
l
{
if
kw
:=
len
(
kv
.
key
);
kw
>
keyWidth
{
keyWidth
=
kw
}
}
padding
=
strings
.
Repeat
(
" "
,
keyWidth
+
1
)
inner
+=
" "
+
padding
}
w
.
WriteByte
(
'{'
)
for
i
,
kv
:=
range
l
{
if
cfg
.
Compact
{
w
.
WriteString
(
kv
.
key
)
w
.
WriteByte
(
':'
)
}
else
{
if
i
>
0
||
cfg
.
Diffable
{
w
.
WriteString
(
"
\n
"
)
w
.
WriteString
(
indent
)
alignKey
:=
indent
+
" "
alignValue
:=
strings
.
Repeat
(
" "
,
keyWidth
)
inner
:=
alignKey
+
alignValue
+
" "
// First and last line shared with bracket:
for
i
,
kv
:=
range
l
{
if
i
>
0
{
w
.
WriteString
(
",
\n
"
)
w
.
WriteString
(
alignKey
)
}
w
.
WriteString
(
kv
.
key
)
w
.
WriteByte
(
':'
)
if
cfg
.
Diffable
{
w
.
WriteByte
(
' '
)
}
else
{
w
.
WriteString
(
padding
[
len
(
kv
.
key
)
:
])
}
}
kv
.
val
.
WriteTo
(
w
,
inner
,
cfg
)
if
i
+
1
<
len
(
l
)
||
cfg
.
Diffable
{
w
.
WriteByte
(
','
)
w
.
WriteString
(
": "
)
w
.
WriteString
(
alignValue
[
len
(
kv
.
key
)
:
])
kv
.
val
.
WriteTo
(
w
,
inner
,
cfg
)
}
}
if
!
cfg
.
Compact
&&
cfg
.
Diffable
{
w
.
WriteString
(
"
\n
"
)
w
.
WriteString
(
indent
)
}
w
.
WriteByte
(
'}'
)
}
...
...
@@ -136,9 +143,7 @@ func (l list) WriteTo(w *bytes.Buffer, indent string, cfg *Config) {
v
.
WriteTo
(
w
,
inner
,
cfg
)
w
.
WriteString
(
",
\n
"
)
}
if
len
(
l
)
>
0
{
w
.
WriteString
(
indent
)
}
w
.
WriteString
(
indent
)
default
:
inner
:=
indent
+
" "
// First and last line shared with bracket:
...
...
pretty/structure_test.go
View file @
eadb3ce3
...
...
@@ -63,6 +63,17 @@ func TestWriteTo(t *testing.T) {
[]`
,
diffable
:
`
[
]`
,
},
{
desc
:
"empty nested list"
,
node
:
list
{
list
{}},
normal
:
`
[[]]`
,
diffable
:
`
[
[
],
]`
,
},
{
...
...
@@ -87,6 +98,17 @@ func TestWriteTo(t *testing.T) {
{}`
,
diffable
:
`
{
}`
,
},
{
desc
:
"empty nested keyvals"
,
node
:
keyvals
{{
"k"
,
keyvals
{}}},
normal
:
`
{k: {}}`
,
diffable
:
`
{
k: {
},
}`
,
},
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment