Skip to content

Commit

Permalink
Merge pull request #44 from jez/jez-hide-options
Browse files Browse the repository at this point in the history
Add two options for minimizing RBI output
  • Loading branch information
mdehoog committed May 8, 2023
2 parents a43817f + e1de9fa commit f96debe
Show file tree
Hide file tree
Showing 26 changed files with 2,924 additions and 8 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ test: init install
$(PROTOC_BINARY) --proto_path=testdata --ruby_out=testdata $(PROTOS)
$(PROTOC_BINARY) --proto_path=testdata --ruby_grpc_out=testdata --plugin=protoc-gen-ruby_grpc=$(GRPC_PLUGIN) $(PROTOS)
$(PROTOC_BINARY) --proto_path=testdata --rbi_out=grpc=true:testdata $(PROTOS)
$(PROTOC_BINARY) --proto_path=testdata --rbi_out=hide_common_methods=true:testdata/hide_common_methods $(PROTOS)
$(PROTOC_BINARY) --proto_path=testdata --rbi_out=use_abstract_message=true:testdata/use_abstract_message $(PROTOS)
$(PROTOC_BINARY) --proto_path=testdata --rbi_out=grpc=true,hide_common_methods=true,use_abstract_message=true:testdata/all $(PROTOS)
git diff --exit-code testdata
40 changes: 32 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,19 @@ var (

type rbiModule struct {
*pgs.ModuleBase
ctx pgsgo.Context
tpl *template.Template
serviceTpl *template.Template
ctx pgsgo.Context
tpl *template.Template
serviceTpl *template.Template
hideCommonMethods bool
useAbstractMessage bool
}

func (m *rbiModule) HideCommonMethods() bool {
return m.hideCommonMethods
}

func (m *rbiModule) UseAbstractMessage() bool {
return m.useAbstractMessage
}

func RBI() *rbiModule { return &rbiModule{ModuleBase: &pgs.ModuleBase{}} }
Expand All @@ -29,6 +39,18 @@ func (m *rbiModule) InitContext(c pgs.BuildContext) {
m.ModuleBase.InitContext(c)
m.ctx = pgsgo.InitContext(c.Parameters())

hideCommonMethods, err := m.ctx.Params().BoolDefault("hide_common_methods", false)
if err != nil {
log.Panicf("Bad parameter: hide_common_methods\n")
}
m.hideCommonMethods = hideCommonMethods

useAbstractMessage, err := m.ctx.Params().BoolDefault("use_abstract_message", false)
if err != nil {
log.Panicf("Bad parameter: use_abstract_message\n")
}
m.useAbstractMessage = useAbstractMessage

funcs := map[string]interface{}{
"increment": m.increment,
"optional": m.optional,
Expand All @@ -42,6 +64,8 @@ func (m *rbiModule) InitContext(c pgs.BuildContext) {
"rubyFieldValue": ruby_types.RubyFieldValue,
"rubyMethodParamType": ruby_types.RubyMethodParamType,
"rubyMethodReturnType": ruby_types.RubyMethodReturnType,
"hideCommonMethods": m.HideCommonMethods,
"useAbstractMessage": m.UseAbstractMessage,
}

m.tpl = template.Must(template.New("rbi").Funcs(funcs).Parse(tpl))
Expand Down Expand Up @@ -111,10 +135,10 @@ const tpl = `# Code generated by protoc-gen-rbi. DO NOT EDIT.
# source: {{ .InputPath }}
# typed: strict
{{ range .AllMessages }}
class {{ rubyMessageType . }}
class {{ rubyMessageType . }}{{ if useAbstractMessage }} < ::Google::Protobuf::AbstractMessage{{ else }}
include ::Google::Protobuf::MessageExts
extend ::Google::Protobuf::MessageExts::ClassMethods
{{ end }}{{ if hideCommonMethods }}{{ else }}
sig { params(str: String).returns({{ rubyMessageType . }}) }
def self.decode(str)
end
Expand All @@ -134,7 +158,7 @@ class {{ rubyMessageType . }}
sig { returns(::Google::Protobuf::Descriptor) }
def self.descriptor
end
{{ if willGenerateInvalidRuby .Fields }}
{{ end }}{{ if willGenerateInvalidRuby .Fields }}
# Constants of the form Constant_1 are invalid. We've declined to type this as a result, taking a hash instead.
sig { params(args: T::Hash[T.untyped, T.untyped]).void }
def initialize(args); end
Expand Down Expand Up @@ -171,7 +195,7 @@ class {{ rubyMessageType . }}
sig { returns(T.nilable(Symbol)) }
def {{ .Name }}
end
{{ end }}{{ end }}
{{ end }}{{ end }}{{ if hideCommonMethods }}{{ else }}
sig { params(field: String).returns(T.untyped) }
def [](field)
end
Expand All @@ -183,7 +207,7 @@ class {{ rubyMessageType . }}
sig { returns(T::Hash[Symbol, T.untyped]) }
def to_h
end
end
{{ end }}end
{{ end }}{{ range .AllEnums }}
module {{ rubyMessageType . }}{{ range .Values }}
self::{{ .Name }} = T.let({{ .Value }}, Integer){{ end }}
Expand Down
33 changes: 33 additions & 0 deletions testdata/all/broken_field_name_pb.rbi
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Code generated by protoc-gen-rbi. DO NOT EDIT.
# source: broken_field_name.proto
# typed: strict

class Example::Broken_field_name < ::Google::Protobuf::AbstractMessage
# Constants of the form Constant_1 are invalid. We've declined to type this as a result, taking a hash instead.
sig { params(args: T::Hash[T.untyped, T.untyped]).void }
def initialize(args); end

sig { returns(String) }
def name
end

sig { params(value: String).void }
def name=(value)
end

sig { void }
def clear_name
end

sig { returns(String) }
def Field_name_1
end

sig { params(value: String).void }
def Field_name_1=(value)
end

sig { void }
def clear_Field_name_1
end
end
27 changes: 27 additions & 0 deletions testdata/all/broken_package_name_pb.rbi
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Code generated by protoc-gen-rbi. DO NOT EDIT.
# source: broken_package_name.proto
# typed: strict

class Package2test::Message2test < ::Google::Protobuf::AbstractMessage
sig do
params(
field2test: T.nilable(String)
).void
end
def initialize(
field2test: ""
)
end

sig { returns(String) }
def field2test
end

sig { params(value: String).void }
def field2test=(value)
end

sig { void }
def clear_field2test
end
end
51 changes: 51 additions & 0 deletions testdata/all/example_pb.rbi
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Code generated by protoc-gen-rbi. DO NOT EDIT.
# source: example.proto
# typed: strict

class Example::Request < ::Google::Protobuf::AbstractMessage
sig do
params(
name: T.nilable(String)
).void
end
def initialize(
name: ""
)
end

sig { returns(String) }
def name
end

sig { params(value: String).void }
def name=(value)
end

sig { void }
def clear_name
end
end

class Example::Response < ::Google::Protobuf::AbstractMessage
sig do
params(
greeting: T.nilable(String)
).void
end
def initialize(
greeting: ""
)
end

sig { returns(String) }
def greeting
end

sig { params(value: String).void }
def greeting=(value)
end

sig { void }
def clear_greeting
end
end
29 changes: 29 additions & 0 deletions testdata/all/example_services_pb.rbi
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Code generated by protoc-gen-rbi. DO NOT EDIT.
# source: example.proto
# typed: strict

module Example::Greeter
class Service
include ::GRPC::GenericService
end

class Stub < ::GRPC::ClientStub
sig do
params(
host: String,
creds: T.any(::GRPC::Core::ChannelCredentials, Symbol),
kw: T.untyped,
).void
end
def initialize(host, creds, **kw)
end

sig do
params(
request: Example::Request
).returns(Example::Response)
end
def hello(request)
end
end
end
51 changes: 51 additions & 0 deletions testdata/all/lowercase_pb.rbi
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Code generated by protoc-gen-rbi. DO NOT EDIT.
# source: lowercase.proto
# typed: strict

class Example::Lowercase < ::Google::Protobuf::AbstractMessage
sig do
params(
example_proto_field: T.nilable(String)
).void
end
def initialize(
example_proto_field: ""
)
end

sig { returns(String) }
def example_proto_field
end

sig { params(value: String).void }
def example_proto_field=(value)
end

sig { void }
def clear_example_proto_field
end
end

class Example::Lowercase_with_underscores < ::Google::Protobuf::AbstractMessage
sig do
params(
example_proto_field: T.nilable(String)
).void
end
def initialize(
example_proto_field: ""
)
end

sig { returns(String) }
def example_proto_field
end

sig { params(value: String).void }
def example_proto_field=(value)
end

sig { void }
def clear_example_proto_field
end
end
3 changes: 3 additions & 0 deletions testdata/all/services_pb.rbi
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Code generated by protoc-gen-rbi. DO NOT EDIT.
# source: services.proto
# typed: strict
79 changes: 79 additions & 0 deletions testdata/all/services_services_pb.rbi
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Code generated by protoc-gen-rbi. DO NOT EDIT.
# source: services.proto
# typed: strict

module Testdata::SimpleMathematics
class Service
include ::GRPC::GenericService
end

class Stub < ::GRPC::ClientStub
sig do
params(
host: String,
creds: T.any(::GRPC::Core::ChannelCredentials, Symbol),
kw: T.untyped,
).void
end
def initialize(host, creds, **kw)
end

sig do
params(
request: Testdata::Subdir::IntegerMessage
).returns(Testdata::Subdir::IntegerMessage)
end
def negate(request)
end

sig do
params(
request: T::Enumerable[Testdata::Subdir::IntegerMessage]
).returns(Testdata::Subdir::IntegerMessage)
end
def median(request)
end
end
end

module Testdata::ComplexMathematics
class Service
include ::GRPC::GenericService
end

class Stub < ::GRPC::ClientStub
sig do
params(
host: String,
creds: T.any(::GRPC::Core::ChannelCredentials, Symbol),
kw: T.untyped,
).void
end
def initialize(host, creds, **kw)
end

sig do
params(
request: Testdata::Subdir::IntegerMessage
).returns(T::Enumerable[Testdata::Subdir::IntegerMessage])
end
def fibonacci(request)
end

sig do
params(
request: T::Enumerable[Testdata::Subdir::IntegerMessage]
).returns(T::Enumerable[Testdata::Subdir::IntegerMessage])
end
def running_max(request)
end

sig do
params(
request: T::Enumerable[Testdata::Subdir::IntegerMessage]
).returns(T::Enumerable[Testdata::Subdir::IntegerMessage])
end
def periodic_max(request)
end
end
end
Loading

0 comments on commit f96debe

Please sign in to comment.