Skip to content

Commit

Permalink
Merge pull request #53 from jez/jez-fix-49
Browse files Browse the repository at this point in the history
Don't crash if SourceCodeInfo() has returned `nil`
  • Loading branch information
mdehoog committed May 16, 2024
2 parents f11d5b8 + 828a2c8 commit 392845c
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 5 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
.idea

# Ignore these binary files so that if they are different. We don't actually
# care whether there are differences in these files, as they're generated from
# protoc itself, not from protoc-gen-rbi, and can simply be different if protoc
# changes, or if the input proto file changes.
testbinary/*_bin.proto
testdata/*.proto.bin
15 changes: 13 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,17 @@ install:
vendor:
go mod vendor

test: init install
# Have to be in a separate directory, so we don't get "is already defined in file" errors
testbinary/%_bin.proto: testdata/%.proto
mkdir -p $(shell dirname $@)
cp $< $@

testdata/%_bin-descriptor-set.proto.bin: testbinary/%_bin.proto
$(eval GRPC_TOOLS_LOCATION := $(shell bundle show grpc-tools))
$(eval PROTOC_BINARY := $(GRPC_TOOLS_LOCATION)/bin/grpc_tools_ruby_protoc)
$(PROTOC_BINARY) --descriptor_set_out=$@ $<

test: init install testdata/example_bin-descriptor-set.proto.bin
$(eval PROTOS := $(shell cd testdata && find . -name "*.proto" | sed 's|^./||'))
$(eval GRPC_TOOLS_LOCATION := $(shell bundle show grpc-tools))
$(eval PROTOC_BINARY := $(GRPC_TOOLS_LOCATION)/bin/grpc_tools_ruby_protoc)
Expand All @@ -18,4 +28,5 @@ test: init install
$(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
$(PROTOC_BINARY) --descriptor_set_in=testdata/example_bin-descriptor-set.proto.bin --rbi_out=. testbinary/example_bin.proto
git diff --exit-code testdata testbinary
27 changes: 24 additions & 3 deletions ruby_types/ruby_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,25 @@ func escapeRubyComment(comment string) string {
}

func RubyFieldTypeComment(field pgs.Field) string {
return escapeRubyComment(strings.TrimSpace(field.SourceCodeInfo().LeadingComments()))
sourceCodeInfo := field.SourceCodeInfo()
if sourceCodeInfo == nil {
// Can happen when the Field is a binary representation of the proto source file,
// and thus has no source code.
return ""
}

return escapeRubyComment(strings.TrimSpace(sourceCodeInfo.LeadingComments()))
}

func RubyMessageTypeComment(entity EntityWithParent) string {
return escapeRubyComment(strings.TrimSpace(entity.SourceCodeInfo().LeadingComments()))
sourceCodeInfo := entity.SourceCodeInfo()
if sourceCodeInfo == nil {
// Can happen when the Entity is a binary representation of the proto source file,
// and thus has no source code.
return ""
}

return escapeRubyComment(strings.TrimSpace(sourceCodeInfo.LeadingComments()))
}

func RubyMessageType(entity EntityWithParent) string {
Expand Down Expand Up @@ -227,7 +241,14 @@ func rubyMapType(ft FieldType) string {
}

func RubyMethodTypeComment(method pgs.Method) string {
return escapeRubyComment(strings.TrimSpace(method.SourceCodeInfo().LeadingComments()))
sourceCodeInfo := method.SourceCodeInfo()
if sourceCodeInfo == nil {
// Can happen when the Method is a binary representation of the proto source file,
// and thus has no source code.
return ""
}

return escapeRubyComment(strings.TrimSpace(sourceCodeInfo.LeadingComments()))
}

func RubyMethodParamType(method pgs.Method) string {
Expand Down
121 changes: 121 additions & 0 deletions testbinary/example_bin_pb.rbi
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# Code generated by protoc-gen-rbi. DO NOT EDIT.
# source: testbinary/example_bin.proto
# typed: strict

class Example::Request
include ::Google::Protobuf::MessageExts
extend ::Google::Protobuf::MessageExts::ClassMethods

sig { params(str: String).returns(Example::Request) }
def self.decode(str)
end

sig { params(msg: Example::Request).returns(String) }
def self.encode(msg)
end

sig { params(str: String, kw: T.untyped).returns(Example::Request) }
def self.decode_json(str, **kw)
end

sig { params(msg: Example::Request, kw: T.untyped).returns(String) }
def self.encode_json(msg, **kw)
end

sig { returns(::Google::Protobuf::Descriptor) }
def self.descriptor
end

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

sig { params(field: String).returns(T.untyped) }
def [](field)
end

sig { params(field: String, value: T.untyped).void }
def []=(field, value)
end

sig { returns(T::Hash[Symbol, T.untyped]) }
def to_h
end
end

class Example::Response
include ::Google::Protobuf::MessageExts
extend ::Google::Protobuf::MessageExts::ClassMethods

sig { params(str: String).returns(Example::Response) }
def self.decode(str)
end

sig { params(msg: Example::Response).returns(String) }
def self.encode(msg)
end

sig { params(str: String, kw: T.untyped).returns(Example::Response) }
def self.decode_json(str, **kw)
end

sig { params(msg: Example::Response, kw: T.untyped).returns(String) }
def self.encode_json(msg, **kw)
end

sig { returns(::Google::Protobuf::Descriptor) }
def self.descriptor
end

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

sig { params(field: String).returns(T.untyped) }
def [](field)
end

sig { params(field: String, value: T.untyped).void }
def []=(field, value)
end

sig { returns(T::Hash[Symbol, T.untyped]) }
def to_h
end
end
29 changes: 29 additions & 0 deletions testbinary/example_bin_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: testbinary/example_bin.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

0 comments on commit 392845c

Please sign in to comment.