/
main.go
204 lines (176 loc) · 5.71 KB
/
main.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
// The MIT License
//
// Copyright (c) 2023 Temporal Technologies Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package main
import (
"bytes"
"fmt"
"go/format"
"io"
"strings"
"text/template"
"google.golang.org/protobuf/compiler/protogen"
"go.temporal.io/api/internal/strcase"
)
type (
headerInput struct {
Pkg string
Imports []string
}
msgInput struct {
Type string
}
shortValue struct {
Num int32
Str string
}
enumInput struct {
Type string
ShortValues []shortValue
}
)
const (
msgImport = "google.golang.org/protobuf/proto"
enumImport = "fmt"
headerTmpl = `
// Code generated by protoc-gen-go-helpers. DO NOT EDIT.
package {{.Pkg}}
import ({{range .Imports}}
"{{.}}"{{end}}
)
`
msgTmplStr = `
// Marshal an object of type {{.Type}} to the protobuf v3 wire format
func (val *{{.Type}}) Marshal() ([]byte, error) {
return proto.Marshal(val)
}
// Unmarshal an object of type {{.Type}} from the protobuf v3 wire format
func (val *{{.Type}}) Unmarshal(buf []byte) error {
return proto.Unmarshal(buf, val)
}
// Size returns the size of the object, in bytes, once serialized
func (val *{{.Type}}) Size() int {
return proto.Size(val)
}
// Equal returns whether two {{.Type}} values are equivalent by recursively
// comparing the message's fields.
// For more information see the documentation for
// https://pkg.go.dev/google.golang.org/protobuf/proto#Equal
func (this *{{.Type}}) Equal(that interface{}) bool {
if that == nil {
return this == nil
}
var that1 *{{.Type}}
switch t := that.(type) {
case *{{.Type}}:
that1 = t
case {{.Type}}:
that1 = &t
default:
return false
}
return proto.Equal(this, that1)
}`
enumTmplStr = `
var (
{{.Type}}_shorthandValue = map[string]int32{ {{range .ShortValues}}
"{{.Str}}": {{.Num}},{{end}}
}
)
// {{.Type}}FromString parses a {{.Type}} value from either the protojson
// canonical SCREAMING_CASE enum or the traditional temporal PascalCase enum to {{.Type}}
func {{.Type}}FromString(s string) ({{.Type}}, error) {
if v, ok := {{.Type}}_value[s]; ok {
return {{.Type}}(v), nil
} else if v, ok := {{.Type}}_shorthandValue[s]; ok {
return {{.Type}}(v), nil
}
return {{.Type}}(0), fmt.Errorf("%s is not a valid {{.Type}}", s)
}`
)
// NOTE: If our implementation of Equal is too slow (its reflection-based) it doesn't look too
// hard to generate unrolled versions...
func main() {
opts := protogen.Options{}
opts.Run(func(plugin *protogen.Plugin) error {
header, err := template.New("header").Parse(headerTmpl)
if err != nil {
return fmt.Errorf("failed to parse header template: %w", err)
}
msgTmpl, err := template.New("message").Parse(msgTmplStr)
if err != nil {
return fmt.Errorf("failed to parse message template: %w", err)
}
enumTmpl, err := template.New("enum").Parse(enumTmplStr)
if err != nil {
return fmt.Errorf("failed to parse enum template: %w", err)
}
for _, file := range plugin.Files {
if !file.Generate || !strings.Contains(string(file.GoImportPath), "go.temporal.io") || (len(file.Proto.MessageType) == 0 && len(file.Proto.EnumType) == 0) {
continue
}
hi := headerInput{
Pkg: string(file.GoPackageName),
}
var body bytes.Buffer
if len(file.Proto.MessageType) > 0 {
hi.Imports = append(hi.Imports, msgImport)
}
for _, msg := range file.Proto.MessageType {
if err := msgTmpl.Execute(&body, msgInput{Type: *msg.Name}); err != nil {
return fmt.Errorf("failed to execute message template on type %s: %w", *msg.Name, err)
}
}
if len(file.Proto.EnumType) > 0 {
hi.Imports = append(hi.Imports, enumImport)
}
for _, enum := range file.Proto.EnumType {
// Preprocess enum to create the mapping
input := enumInput{
Type: *enum.Name,
}
pfx := strcase.ToScreamingSnake(*enum.Name)
for _, val := range enum.Value {
input.ShortValues = append(input.ShortValues, shortValue{
Num: *val.Number,
Str: strcase.ToCamel(strings.TrimPrefix(val.GetName(), pfx)),
})
}
if err := enumTmpl.Execute(&body, input); err != nil {
return fmt.Errorf("failed to execute enum template on type %s: %w", *enum.Name, err)
}
}
// Prepend header
var src bytes.Buffer
if err := header.Execute(&src, hi); err != nil {
return fmt.Errorf("failed to execute header template: %w", err)
}
io.Copy(&src, &body)
fmtd, err := format.Source(src.Bytes())
if err != nil {
return fmt.Errorf("failed to format generated source: \n%s\n%w", src.String(), err)
}
gf := plugin.NewGeneratedFile(fmt.Sprintf("%s.go-helpers.pb.go", file.GeneratedFilenamePrefix), ".")
gf.Write(fmtd)
}
return nil
})
}