Skip to content

Commit

Permalink
WIP support for protoc insertion points
Browse files Browse the repository at this point in the history
  • Loading branch information
davebenvenuti committed Jan 22, 2025
1 parent e599e20 commit b5b14d9
Show file tree
Hide file tree
Showing 18 changed files with 1,091 additions and 747 deletions.
56 changes: 41 additions & 15 deletions lib/protoboeuf/codegen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,34 @@ def result
def class_body
enum.value.map { |const|
"#{const.name} = #{const.number}"
}.join("\n") + "\n\n" + lookup + "\n\n" + resolve
}.join("\n") + "\n\n" + class_methods
end

def class_methods
<<~RUBY
class << self
#{CodeGen.protoc_insertion_point("class_methods")}
#{lookup}
#{resolve}
end
RUBY
end

def lookup
# This ends up being a class method
type_signature(params: { val: "Integer" }, returns: "Symbol", newline: true) +
"def self.lookup(val)\n" \
"def lookup(val)\n" \
"if " + enum.value.map { |const|
"val == #{const.number} then :#{const.name}"
}.join(" elsif ") + " end; end"
end

def resolve
# This ends up being a class methdo
type_signature(params: { val: "Symbol" }, returns: "Integer", newline: true) +
"def self.resolve(val)\n" \
"def resolve(val)\n" \
"if " + enum.value.map { |const|
"val == :#{const.name} then #{const.number}"
}.join(" elsif ") + " end; end"
Expand Down Expand Up @@ -127,8 +141,8 @@ def result
private

def class_body
prelude +
constants +
constants +
class_methods +
enums +
readers +
writers +
Expand Down Expand Up @@ -587,17 +601,21 @@ def encode_sfixed32(field, value_expr, tagged)
eocode
end

def prelude
def class_methods
<<~RUBY
#{extend_t_sig}
#{type_signature(params: { buff: "String" }, returns: message.name)}
def self.decode(buff)
allocate.decode_from(buff.b, 0, buff.bytesize)
end
class << self
#{CodeGen.protoc_insertion_point("class_methods")}
#{type_signature(params: { obj: message.name }, returns: "String")}
def self.encode(obj)
obj._encode("".b)
#{extend_t_sig}
#{type_signature(params: { buff: "String" }, returns: message.name)}
def decode(buff)
allocate.decode_from(buff.b, 0, buff.bytesize)
end
#{type_signature(params: { obj: message.name }, returns: "String")}
def encode(obj)
obj._encode("".b)
end
end
RUBY
end
Expand Down Expand Up @@ -1714,7 +1732,9 @@ def to_ruby(this_file = nil, options = {})
MessageCompiler.result(message, toplevel_enums, generate_types:, requires:, syntax: file.syntax, options:)
end.join

head += requires.reject { |r| r == this_file }.map { |r| "require #{r.dump}" }.join("\n") + "\n\n"
head += requires.reject { |r|
r == this_file
}.map { |r| "require #{r.dump}" }.join("\n") + CodeGen.protoc_insertion_point("requires") + "\n\n\n"
head += modules.map { |m| "module #{m}\n" }.join

tail = "\n" + modules.map { "end" }.join("\n")
Expand Down Expand Up @@ -1796,6 +1816,12 @@ def wire_type(field)
end
end
end

def protoc_insertion_point(name)
<<~RUBY
# @@protoc_insertion_point(#{name})
RUBY
end
end
end
end
84 changes: 45 additions & 39 deletions lib/protoboeuf/google/api/field_behavior.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 11 additions & 5 deletions lib/protoboeuf/google/protobuf/any.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 11 additions & 5 deletions lib/protoboeuf/google/protobuf/boolvalue.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 11 additions & 5 deletions lib/protoboeuf/google/protobuf/bytesvalue.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b5b14d9

Please sign in to comment.