forked from coinbase/protoc-gen-rbi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rbs.go
164 lines (129 loc) · 4.81 KB
/
rbs.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
package main
import (
"github.com/fundthmcalculus/protoc-gen-rbs/ruby_types"
"log"
"strings"
"text/template"
pgs "github.com/lyft/protoc-gen-star"
pgsgo "github.com/lyft/protoc-gen-star/lang/go"
)
type rbsModule struct {
*pgs.ModuleBase
context pgsgo.Context
template *template.Template
serviceTemplate *template.Template
}
func RBS() *rbsModule { return &rbsModule{ModuleBase: &pgs.ModuleBase{}} }
func (m *rbsModule) InitContext(c pgs.BuildContext) {
m.ModuleBase.InitContext(c)
m.context = pgsgo.InitContext(c.Parameters())
funcs := map[string]interface{}{
"increment": m.increment,
"optional": m.optional,
"optionalOneOf": m.optionalOneOf,
"willGenerateInvalidRuby": m.willGenerateInvalidRuby,
"rubyModules": ruby_types.RubyModules,
"rubyPackage": ruby_types.RubyPackage,
"rubyMessageType": ruby_types.RubyMessageType,
"rbsGetterFieldType": ruby_types.RbsGetterFieldType,
"rbsSetterFieldType": ruby_types.RbsSetterFieldType,
"rbsInitializerFieldType": ruby_types.RbsInitializerFieldType,
"rubyFieldValue": ruby_types.RubyFieldValue,
"rbsMethodParamType": ruby_types.RbsMethodParamType,
"rbsMethodReturnType": ruby_types.RbsMethodReturnType,
}
m.template = template.Must(template.New("rbs").Funcs(funcs).Parse(templateRbs))
m.serviceTemplate = template.Must(template.New("rbsService").Funcs(funcs).Parse(serviceTemplateRbs))
}
func (m *rbsModule) Name() string { return "rbi" }
func (m *rbsModule) Execute(targets map[string]pgs.File, pkgs map[string]pgs.Package) []pgs.Artifact {
for _, t := range targets {
m.generate(t)
grpc, err := m.context.Params().BoolDefault("grpc", true)
if err != nil {
log.Panicf("Bad parameter: grpc\n")
}
if len(t.Services()) > 0 && grpc {
m.generateServices(t)
}
}
return m.Artifacts()
}
func (m *rbsModule) generate(f pgs.File) {
op := strings.TrimSuffix(f.InputPath().String(), ".proto") + "_pb.rbs"
m.AddGeneratorTemplateFile(op, m.template, f)
}
func (m *rbsModule) generateServices(f pgs.File) {
op := strings.TrimSuffix(f.InputPath().String(), ".proto") + "_services_pb.rbs"
m.AddGeneratorTemplateFile(op, m.serviceTemplate, f)
}
func (m *rbsModule) increment(i int) int {
return i + 1
}
func (m *rbsModule) optional(field pgs.Field) bool {
return field.Descriptor().GetProto3Optional()
}
func (m *rbsModule) optionalOneOf(oneOf pgs.OneOf) bool {
return len(oneOf.Fields()) == 1 && oneOf.Fields()[0].Descriptor().GetProto3Optional()
}
func (m *rbsModule) willGenerateInvalidRuby(fields []pgs.Field) bool {
for _, field := range fields {
if !validRubyField.MatchString(string(field.Name())) {
return true
}
}
return false
}
const templateRbs = `# Code generated by protoc-gen-rbi. DO NOT EDIT.
# source: {{ .InputPath }}
{{ range rubyModules . }}
module {{ . }}
end{{ end }}
{{ range .AllMessages }}
class {{ rubyMessageType . }}
def self.decode: (String) -> {{ rubyMessageType . }}
def self.encode: ({{ rubyMessageType . }}) -> String
def self.decode_json: (String, untyped kw) -> {{ rubyMessageType . }}
def self.encode_json: ({{ rubyMessageType . }}, untyped kw) -> String
def self.descriptor: () -> Google::Protobuf::Descriptor
# Constants of the form Constant_1 are invalid. We've declined to type this as a result, taking a hash instead.
def initialize: (::Hash[untyped, untyped]) -> void
def initialize: ({{ $index := 0 }}{{ range .Fields }}{{ if gt $index 0 }},{{ end }}{{ $index = increment $index }}
{{ .Name }}: {{ rbsInitializerFieldType . }}{{ end }}
) -> void
{{ range .Fields }}
def {{ .Name }}: () -> {{ rbsGetterFieldType . }}
def {{ .Name }}=: ({{ rbsSetterFieldType . }}) -> void
def clear_{{ .Name }}: () -> void
def has_{{ .Name }}?: () -> bool
{{ end }}{{ range .OneOfs }}
def {{ .Name }}: () -> Symbol?
{{ end }}
def []: (String) -> untyped
def []=: (String, untyped value) -> void
def to_h: () -> ::Hash[Symbol, untyped]
end
{{ end }}{{ range .AllEnums }}
module {{ rubyMessageType . }}{{ range .Values }}
# TODO - Not sure how to represent this
# self::{{ .Name }} = T.let({{ .Value }}, Integer)
def {{ .Name }}: () -> Integer # = {{ .Value}}{{ end }}
def self.lookup: (value: Integer) -> Symbol?
def self.resolve: (value: Symbol) -> Integer?
def self.descriptor: () -> ::Google::Protobuf::EnumDescriptor
end
{{ end }}`
const serviceTemplateRbs = `# Code generated by protoc-gen-rbi. DO NOT EDIT.
# source: {{ .InputPath }}
{{ range .Services }}
module {{ rubyPackage .File }}::{{ .Name }}
class Service
end
class Stub < GRPC::ClientStub
def initialize: (String, GRPC::Core::ChannelCredentials, untyped kw) -> void
{{ range .Methods }}
def {{ .Name.LowerSnakeCase }}: ({{ rbsMethodParamType . }}) -> {{ rbsMethodReturnType . }}
{{ end }}
end
end
{{ end }}`