-
Notifications
You must be signed in to change notification settings - Fork 11
/
extensions.go
47 lines (41 loc) · 1.42 KB
/
extensions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package protofmt
import "github.com/emicklei/proto"
// keyValuePair returns key = value or "value"
func keyValuePair(o *proto.Option, embedded bool) (cols []aligned) {
equals := alignedEquals
name := o.Name
if len(o.Constant.OrderedMap) > 0 {
cols = append(cols, leftAligned(name), equals)
cols = append(cols, columnsPrintablesFromMap(o.Constant.OrderedMap)...)
return
}
if embedded {
return append(cols, leftAligned(name), equals, leftAligned(o.Constant.SourceRepresentation())) // numbers right, strings left? TODO
}
return append(cols, rightAligned(name), equals, rightAligned(o.Constant.SourceRepresentation()))
}
func alignedInlinePrefix(c *proto.Comment) aligned {
prefix := " //"
if c.ExtraSlash {
prefix = " ///"
}
return notAligned(prefix)
}
func columnsPrintables(c *proto.Comment) (list []columnsPrintable) {
for _, each := range c.Lines {
list = append(list, inlineComment{each, c.ExtraSlash})
}
return
}
func typeAssertColumnsPrintable(v proto.Visitee) (columnsPrintable, bool) {
return asColumnsPrintable(v), len(asColumnsPrintable(v).columns()) > 0
}
func columnsPrintablesFromMap(m proto.LiteralMap) (cols []aligned) {
cols = append(cols, leftAligned("{"), alignedSpace)
for _, each := range m {
// TODO only works for simple constants
cols = append(cols, leftAligned(each.Name), alignedColon, leftAligned(each.SourceRepresentation()))
}
cols = append(cols, alignedSpace, leftAligned("}"))
return
}