Skip to content

Commit

Permalink
Merge pull request #94 from Shopify/regen-well-known
Browse files Browse the repository at this point in the history
 Regenerate .rb files for well-known types
  • Loading branch information
maximecb authored May 2, 2024
2 parents ae87abf + 814f406 commit 9c97773
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 49 deletions.
5 changes: 4 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ BENCHMARK_PROTOBOEUF_PB = "bench/lib/protoboeuf/benchmark_pb.rb"

well_known_types = Rake::FileList[File.join(BASE_DIR, "lib/protoboeuf/protobuf/*.proto")]

WELL_KNOWN_PB = well_known_types.pathmap("%X.rb")

# Clobber/clean rules
rb_files.each { |x| CLOBBER.append x }
CLOBBER.append BENCHMARK_UPSTREAM_PB
CLOBBER.append BENCHMARK_PROTOBOEUF_PB
CLOBBER.append WELL_KNOWN_PB

rule ".rb" => "%X.proto" do |t|
require_relative "lib/protoboeuf/codegen"
Expand All @@ -25,7 +28,7 @@ rule ".rb" => "%X.proto" do |t|
File.binwrite t.name, unit.to_ruby
end

task :well_known_types => well_known_types.pathmap("%X.rb")
task :well_known_types => WELL_KNOWN_PB

# Makefile-like rule to generate "_pb.rb"
rule "_pb.rb" => "test/fixtures/%{_pb,}n.proto" do |task|
Expand Down
14 changes: 9 additions & 5 deletions lib/protoboeuf/protobuf/boolvalue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ def self.decode(buff)
end

def self.encode(obj)
obj._encode
buff = obj._encode "".b
buff.force_encoding(Encoding::ASCII_8BIT)
end
# required field readers
attr_accessor :value
Expand Down Expand Up @@ -37,15 +38,13 @@ def decode_from(buff, index, len)
end

return self if index >= len
raise NotImplementedError
end
end
def _encode
buff = "".b
def _encode(buff)
val = @value
if val == true
## encode the tag
buff << 0x08

buff << 1
elsif val == false
# Default value, encode nothing
Expand All @@ -55,6 +54,11 @@ def _encode

buff
end
def to_h
result = {}
result["value".to_sym] = @value
result
end
end
end
end
16 changes: 10 additions & 6 deletions lib/protoboeuf/protobuf/bytesvalue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ def self.decode(buff)
end

def self.encode(obj)
obj._encode
buff = obj._encode "".b
buff.force_encoding(Encoding::ASCII_8BIT)
end
# required field readers
attr_accessor :value
Expand Down Expand Up @@ -94,27 +95,30 @@ def decode_from(buff, index, len)
end

return self if index >= len
raise NotImplementedError
end
end
def _encode
buff = "".b
def _encode(buff)
val = @value.b
if val.bytesize > 0
## encode the tag
buff << 0x0a
len = val.bytesize
while len > 0
while len != 0
byte = len & 0x7F
len >>= 7
byte |= 0x80 if len > 0
buff << byte
end

buff.concat(val)
end

buff
end
def to_h
result = {}
result["value".to_sym] = @value
result
end
end
end
end
14 changes: 9 additions & 5 deletions lib/protoboeuf/protobuf/int32value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ def self.decode(buff)
end

def self.encode(obj)
obj._encode
buff = obj._encode "".b
buff.force_encoding(Encoding::ASCII_8BIT)
end
# required field readers
attr_accessor :value
Expand Down Expand Up @@ -101,15 +102,13 @@ def decode_from(buff, index, len)
end

return self if index >= len
raise NotImplementedError
end
end
def _encode
buff = "".b
def _encode(buff)
val = @value
if val != 0
## encode the tag
buff << 0x08

while val != 0
byte = val & 0x7F

Expand All @@ -126,6 +125,11 @@ def _encode

buff
end
def to_h
result = {}
result["value".to_sym] = @value
result
end
end
end
end
14 changes: 9 additions & 5 deletions lib/protoboeuf/protobuf/int64value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ def self.decode(buff)
end

def self.encode(obj)
obj._encode
buff = obj._encode "".b
buff.force_encoding(Encoding::ASCII_8BIT)
end
# required field readers
attr_accessor :value
Expand Down Expand Up @@ -101,15 +102,13 @@ def decode_from(buff, index, len)
end

return self if index >= len
raise NotImplementedError
end
end
def _encode
buff = "".b
def _encode(buff)
val = @value
if val != 0
## encode the tag
buff << 0x08

while val != 0
byte = val & 0x7F

Expand All @@ -126,6 +125,11 @@ def _encode

buff
end
def to_h
result = {}
result["value".to_sym] = @value
result
end
end
end
end
24 changes: 13 additions & 11 deletions lib/protoboeuf/protobuf/stringvalue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ def self.decode(buff)
end

def self.encode(obj)
obj._encode
buff = obj._encode "".b
buff.force_encoding(Encoding::ASCII_8BIT)
end
# required field readers
attr_accessor :value
Expand Down Expand Up @@ -93,28 +94,29 @@ def decode_from(buff, index, len)
end

return self if index >= len
raise NotImplementedError
end
end
def _encode
buff = "".b
val =
@value.encode(Encoding::UTF_8).force_encoding(Encoding::ASCII_8BIT)
if val.bytesize > 0
## encode the tag
def _encode(buff)
val = @value
if ((len = val.bytesize) > 0)
buff << 0x0a
len = val.bytesize
while len > 0
while len != 0
byte = len & 0x7F
len >>= 7
byte |= 0x80 if len > 0
buff << byte
end
buff.concat(val)

buff << val
end

buff
end
def to_h
result = {}
result["value".to_sym] = @value
result
end
end
end
end
17 changes: 11 additions & 6 deletions lib/protoboeuf/protobuf/timestamp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ def self.decode(buff)
end

def self.encode(obj)
obj._encode
buff = obj._encode "".b
buff.force_encoding(Encoding::ASCII_8BIT)
end
# required field readers
attr_accessor :seconds, :nanos
Expand Down Expand Up @@ -177,15 +178,13 @@ def decode_from(buff, index, len)
end

return self if index >= len
raise NotImplementedError
end
end
def _encode
buff = "".b
def _encode(buff)
val = @seconds
if val != 0
## encode the tag
buff << 0x08

while val != 0
byte = val & 0x7F

Expand All @@ -202,8 +201,8 @@ def _encode

val = @nanos
if val != 0
## encode the tag
buff << 0x10

while val != 0
byte = val & 0x7F

Expand All @@ -220,6 +219,12 @@ def _encode

buff
end
def to_h
result = {}
result["seconds".to_sym] = @seconds
result["nanos".to_sym] = @nanos
result
end
end
end
end
14 changes: 9 additions & 5 deletions lib/protoboeuf/protobuf/uint32value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ def self.decode(buff)
end

def self.encode(obj)
obj._encode
buff = obj._encode "".b
buff.force_encoding(Encoding::ASCII_8BIT)
end
# required field readers
attr_accessor :value
Expand Down Expand Up @@ -90,15 +91,13 @@ def decode_from(buff, index, len)
end

return self if index >= len
raise NotImplementedError
end
end
def _encode
buff = "".b
def _encode(buff)
val = @value
if val != 0
## encode the tag
buff << 0x08

while val != 0
byte = val & 0x7F
val >>= 7
Expand All @@ -109,6 +108,11 @@ def _encode

buff
end
def to_h
result = {}
result["value".to_sym] = @value
result
end
end
end
end
14 changes: 9 additions & 5 deletions lib/protoboeuf/protobuf/uint64value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ def self.decode(buff)
end

def self.encode(obj)
obj._encode
buff = obj._encode "".b
buff.force_encoding(Encoding::ASCII_8BIT)
end
# required field readers
attr_accessor :value
Expand Down Expand Up @@ -90,15 +91,13 @@ def decode_from(buff, index, len)
end

return self if index >= len
raise NotImplementedError
end
end
def _encode
buff = "".b
def _encode(buff)
val = @value
if val != 0
## encode the tag
buff << 0x08

while val != 0
byte = val & 0x7F
val >>= 7
Expand All @@ -109,6 +108,11 @@ def _encode

buff
end
def to_h
result = {}
result["value".to_sym] = @value
result
end
end
end
end

0 comments on commit 9c97773

Please sign in to comment.