-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathtable.go
167 lines (140 loc) · 3.59 KB
/
table.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright 2017 Seamia Corporation. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"reflect"
"strconv"
"github.com/emicklei/proto"
"github.com/seamia/protodot/plus"
)
//----------------------------------------------------------------------------------------------------------------------
// presentation
type table struct {
buffer bytes.Buffer
name string
}
func newTable(name string, full FullName, unique UniqueName, style string) *table {
t := table{
name: name,
}
entry := OneOfEntry{
Name: name,
Unique: unique,
Type: string(full),
}
if err := plus.ApplyTemplate("message.prefix", &t, entry); err != nil {
alert("failed to render", err)
}
return &t
}
func (t *table) Write(p []byte) (n int, err error) {
return t.buffer.Write(p)
}
func (t *table) WriteString(s string) {
t.Write([]byte(s))
}
var kind2entry = map[Kind]string{
Simple: "entry.simple",
Message: "entry.message",
Enum: "entry.enum",
Missing: "entry.missing",
}
func (t *table) addRow(repeated, typ, name, ordinal string, kind Kind) {
tmplName := kind2entry[kind]
if len(tmplName) > 0 {
entry := OneOfEntry{
Name: name,
Type: typ,
Ordinal: ordinal,
Prefix: repeated,
}
if err := plus.ApplyTemplate(tmplName, t, entry); err != nil {
alert("failed to render", err)
}
} else {
alert("unhandled kind", kind)
}
}
var kind2map = map[Kind]string{
Simple: "map.simple",
Message: "map.message",
Enum: "map.enum",
Missing: "map.missing",
}
func (t *table) addMapRow(name, keyType, typ, ordinal string, kind Kind) {
tmplName := kind2map[kind]
if len(tmplName) > 0 {
entry := OneOfEntry{
Name: name,
Type: typ,
Ordinal: ordinal,
Prefix: "",
KeyType: keyType,
}
if err := plus.ApplyTemplate(tmplName, t, entry); err != nil {
alert("failed to render", err)
}
} else {
alert("unhandled kind:", kind)
}
}
type OneOfEntry struct {
Unique UniqueName
Name string
KeyType string
Type string
Ordinal string
Prefix string
}
var kind2template = map[Kind]string{
Simple: "oneof.entry.simple",
Message: "oneof.entry.message",
Enum: "oneof.entry.enum",
Missing: "oneof.entry.missing",
}
func (t *table) addOneof(fullname FullName, what *proto.Oneof, pbs *pbstate) {
entry := OneOfEntry{
Name: what.Name,
}
if err := plus.ApplyTemplate("oneof.entry.prefix", t, entry); err != nil {
alert("failed to render", err)
}
for _, element := range what.Elements {
switch actual := element.(type) {
case *proto.OneOfField:
if tmplName := kind2template[pbs.getKind(fullname, OriginalName(actual.Type))]; len(tmplName) > 0 {
payload := OneOfEntry{
Name: actual.Name,
Type: actual.Type,
Ordinal: strconv.Itoa(actual.Sequence),
}
if err := plus.ApplyTemplate(tmplName, t, payload); err != nil {
alert("failed to render", err)
}
} else {
alert("failed to get template name")
}
case *proto.Option:
ignoring("ignoring options for now")
case *proto.Comment:
ignoring("ignoring comments for now")
case *proto.Group:
ignoring("ignoring group for now")
default:
rname := reflect.TypeOf(actual).Elem().Name()
unhandled("\t", "UNKNOWN1", actual, "", rname)
}
}
if err := plus.ApplyTemplate("oneof.entry.suffix", t, entry); err != nil {
alert("failed to render", err)
}
}
func (t *table) generate() string {
entry := OneOfEntry{Name: t.name}
if err := plus.ApplyTemplate("message.suffix", t, entry); err != nil {
alert("failed to render", err)
}
return t.buffer.String()
}