-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathproto.go
290 lines (254 loc) · 6.75 KB
/
proto.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package main
import (
"bytes"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"unicode"
)
// BytesHint is a comment we add to fields with `bytes` as data type.
const ProtoBytesHint = ` // When we map to the bytes type it means that we have no matching message
// type and just put the raw bytes into the field. This is specifically true
// for our geometry data of locations which cannot be translated to valid
// GeoJSON using Protocol Buffers (as they do not support arrays of arrays).
// To indicate that this is a different field than the field in the
// olca-schema definition, we append the _bytes suffix to the field name
`
const ProtoRootFooter = `// This enumeration type is added for compatibility with the @type attribute of
// the openLCA JSON-LD format. In the proto messages we limit its usage to
// instances of CategorizedEntity and Ref while it is allowed for every type in
// the JSON-LD format. Thus, you should use ignoringUnknownFields flag when
// parsing openLCA JSON-LD messages with the generated proto parsers.
enum ProtoType {
Undefined = 0;
Actor = 1;
Currency = 2;
DQSystem = 3;
Epd = 4;
Flow = 5;
FlowProperty = 6;
ImpactCategory = 7;
ImpactMethod = 8;
Location = 9;
NwSet = 10;
Parameter = 11;
Process = 12;
ProductSystem = 13;
Project = 14;
Result = 15;
SocialIndicator = 16;
Source = 17;
Unit = 18;
UnitGroup = 19;
}
`
type protoWriter struct {
buff *bytes.Buffer
model *YamlModel
pack string
}
func writeProtos(args *args) {
model, err := ReadYamlModel(args)
check(err, "could not read YAML model")
buildDir := filepath.Join(args.home, "build")
mkdir(buildDir)
for _, pack := range model.Packages() {
var buff bytes.Buffer
w := protoWriter{
buff: &buff,
model: model,
pack: pack,
}
w.writePack()
fileName := "olca.proto"
if pack != "" {
fileName = "olca." + pack + ".proto"
}
outFile := filepath.Join(buildDir, fileName)
log.Println("write file:", fileName)
err = os.WriteFile(outFile, buff.Bytes(), os.ModePerm)
check(err, "failed to write to file", outFile)
}
}
func (w *protoWriter) writePack() {
w.writeFileHeader()
for _, t := range w.model.Types {
if t.Package != w.pack {
continue
}
if t.IsEnum() {
w.writeEnum(t.Enum)
w.writeln()
}
if t.IsClass() {
class := t.Class
comment := formatComment(class.Doc, "")
if comment != "" {
w.write(comment)
}
w.writeln("message Proto" + class.Name + " {")
w.writeln()
w.writeFieldsOf(class)
w.writeln("}")
w.writeln()
w.writeln()
}
}
if w.pack == "" {
w.write(ProtoRootFooter)
}
}
func (w *protoWriter) writeFileHeader() {
w.writeln("// Generated from olca-schema (https://github.com/GreenDelta/olca-schema)")
w.writeln("// DO NOT EDIT!")
w.writeln()
w.writeln("syntax = \"proto3\";")
w.writeln()
// package names
if w.pack == "" {
w.writeln("package protolca;")
w.writeln("option java_package = \"org.openlca.proto\";")
} else {
w.writeln("package protolca." + w.pack + ";")
w.writeln("option java_package = \"org.openlca.proto." + w.pack + "\";")
}
// other options
w.writeln("option java_outer_classname = \"Proto\";")
w.writeln("option go_package = \".;protolca\";")
w.writeln("option csharp_namespace = \"ProtoLCA\";")
w.writeln("option java_multiple_files = true;")
w.writeln()
// imports
if w.pack != "" {
w.writeln("import \"olca.proto\"")
w.writeln()
}
w.writeln()
}
func (w *protoWriter) writeEnum(enum *YamlEnum) {
if enum.Name == "ModelType" {
return
}
comment := formatComment(enum.Doc, "")
if comment != "" {
w.write(comment)
}
w.writeln("enum Proto" + enum.Name + " {")
w.writeln()
// write the 'UNDEFINED_*' item
w.indln("// This default option was added automatically")
w.indln("// and means that no values was set.")
w.indln(protoUndefinedOf(enum) + " = 0;")
w.writeln()
for _, item := range enum.Items {
comment := formatComment(item.Doc, " ")
if comment != "" {
w.write(comment)
}
w.indln(item.Name + " = " + strconv.Itoa(item.Index) + ";")
w.writeln()
}
w.writeln("}")
w.writeln()
}
// Writes the fields of the given class to the given buffer. This function
// climbs up the class hierarchy and inlines the fields of the corresponding
// super classes (as there is no extension mechanism in proto3).
func (w *protoWriter) writeFieldsOf(class *YamlClass) {
// write fields of super classes recursively
if class.SuperClass != "" {
super := w.model.TypeMap[class.SuperClass]
if super != nil && super.Class != nil {
w.writeFieldsOf(super.Class)
}
}
// @type field
if class.Name == "Ref" || class.Name == "RootEntity" {
w.indln("// The type name of the respective entity.")
w.indln("ProtoType type = 1 [json_name = \"@type\"];")
w.writeln()
}
// @id field
if class.Name == "Ref" || class.Name == "RefEntity" {
w.indln("// The reference ID (typically an UUID) of the entity.")
w.indln("string id = 2 [json_name = \"@id\"];")
w.writeln()
}
// write fields
for _, field := range class.Props {
if strings.HasPrefix(field.Name, "@") {
continue
}
// field comment
comment := formatComment(field.Doc, " ")
if comment != "" {
w.write(comment)
}
protoType := toProtoType(field.Type)
protoField := toSnakeCase(field.Name)
if protoType == "bytes" {
w.write(ProtoBytesHint)
protoField += "_bytes"
}
w.write(" ")
if field.IsOptional {
w.write("optional ")
}
w.writeln(protoType + " " + protoField +
" = " + strconv.Itoa(field.Index) + ";")
w.writeln()
}
}
// Maps the given olca-schema type to a corresponding proto3 type.
func toProtoType(schemaType string) string {
switch schemaType {
case "string", "double", "float":
return schemaType
case "dateTime", "date":
return "string"
case "int", "integer":
return "int32"
case "boolean":
return "bool"
case "GeoJSON":
return "bytes"
case "ModelType":
return "ProtoCategoryType"
}
if strings.HasPrefix(schemaType, "Ref[") {
return "ProtoRef"
}
if strings.HasPrefix(schemaType, "List[") {
t := strings.TrimSuffix(
strings.TrimPrefix(schemaType, "List["), "]")
return "repeated " + toProtoType(t)
}
return "Proto" + schemaType
}
// Generates the name of the `UNDEFINED` option for the given
// enumeration type. As this option has to have a unique name
// we include the name of the enumeration into that name.
func protoUndefinedOf(enum *YamlEnum) string {
var buff bytes.Buffer
for _, char := range enum.Name {
if unicode.IsUpper(char) {
buff.WriteRune('_')
}
buff.WriteRune(char)
}
return "UNDEFINED" + strings.ToUpper(buff.String())
}
func (w *protoWriter) indln(s string) {
w.writeln(" ", s)
}
func (w *protoWriter) writeln(xs ...string) {
for _, x := range xs {
w.buff.WriteString(x)
}
w.buff.WriteRune('\n')
}
func (w *protoWriter) write(s string) {
w.buff.WriteString(s)
}