Skip to content

Commit

Permalink
Use setters instead of setting ivars explicitly so we get integer ran…
Browse files Browse the repository at this point in the history
…ge checks. TODO don't know if this is right or not
  • Loading branch information
davebenvenuti committed Jan 6, 2025
1 parent 64fe1fe commit cf35b3f
Show file tree
Hide file tree
Showing 16 changed files with 524 additions and 656 deletions.
7 changes: 6 additions & 1 deletion lib/protoboeuf/codegen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,10 @@ def iv_name(field)
"@#{field.name}"
end

def setter_name(field)
"self.#{field.name}"
end

def initialize_required_field(field)
<<~RUBY
#{bounds_check(field, lvar_read(field)).chomp}
Expand Down Expand Up @@ -1677,7 +1681,8 @@ def decode_code(field)
decode_repeated(field)
end
else
decode_subtype(field, field.type, iv_name(field), "=")
# Use the setter instead of setting the instance variable directly so we get things like integer range checks
decode_subtype(field, field.type, setter_name(field), "=")
end
end

Expand Down
23 changes: 10 additions & 13 deletions lib/protoboeuf/protobuf/any.rb
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ def decode_from(buff, index, len)
raise "integer decoding error"
end

@type_url =
self.type_url =
buff.byteslice(index, value).force_encoding(Encoding::UTF_8)
index += value

Expand Down Expand Up @@ -419,7 +419,7 @@ def decode_from(buff, index, len)
raise "integer decoding error"
end

@value = buff.byteslice(index, value)
self.value = buff.byteslice(index, value)
index += value

## END PULL_BYTES
Expand Down Expand Up @@ -563,18 +563,15 @@ def to_json_with_debug(options = {})
# By default the protobuf JSON printer should convert the field name to lowerCamelCase and use that as the JSON name.
# See: https://protobuf.dev/programming-guides/json/#json-options
private def json_field_name(name)
# new_name = name.gsub(/_[a-z]/) { |m| m.delete_prefix("_").capitalize }
return name unless name.include?("_")

new_name =
name
.split(/_+/)
.each_with_index
.map { |part, i| i.zero? ? part : part.downcase.capitalize }
.join
# TODO FIELD_NAME11
#new_name == "FIELDName11" ? "fieldName11" : new_name
new_name == "FIELDName11" ? "FIELD_NAME11" : new_name
# Names like FIELD_NAME11 (all caps + underscores + numbers) should remain as-is
return name if name =~ /[A-Zd_]+/

name
.split(/_+/)
.each_with_index
.map { |part, i| i.zero? ? part : part.downcase.capitalize }
.join
end
end
end
Expand Down
19 changes: 8 additions & 11 deletions lib/protoboeuf/protobuf/boolvalue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def decode_from(buff, index, len)
if tag == 0x8
found = true
## PULL BOOLEAN
@value = (buff.getbyte(index) == 1)
self.value = (buff.getbyte(index) == 1)
index += 1
## END PULL BOOLEAN

Expand Down Expand Up @@ -358,18 +358,15 @@ def to_json_with_debug(options = {})
# By default the protobuf JSON printer should convert the field name to lowerCamelCase and use that as the JSON name.
# See: https://protobuf.dev/programming-guides/json/#json-options
private def json_field_name(name)
# new_name = name.gsub(/_[a-z]/) { |m| m.delete_prefix("_").capitalize }
return name unless name.include?("_")
# Names like FIELD_NAME11 (all caps + underscores + numbers) should remain as-is
return name if name =~ /[A-Zd_]+/

new_name =
name
.split(/_+/)
.each_with_index
.map { |part, i| i.zero? ? part : part.downcase.capitalize }
.join
# TODO FIELD_NAME11
#new_name == "FIELDName11" ? "fieldName11" : new_name
new_name == "FIELDName11" ? "FIELD_NAME11" : new_name
name
.split(/_+/)
.each_with_index
.map { |part, i| i.zero? ? part : part.downcase.capitalize }
.join
end
end
end
Expand Down
21 changes: 9 additions & 12 deletions lib/protoboeuf/protobuf/bytesvalue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ def decode_from(buff, index, len)
raise "integer decoding error"
end

@value = buff.byteslice(index, value)
self.value = buff.byteslice(index, value)
index += value

## END PULL_BYTES
Expand Down Expand Up @@ -417,18 +417,15 @@ def to_json_with_debug(options = {})
# By default the protobuf JSON printer should convert the field name to lowerCamelCase and use that as the JSON name.
# See: https://protobuf.dev/programming-guides/json/#json-options
private def json_field_name(name)
# new_name = name.gsub(/_[a-z]/) { |m| m.delete_prefix("_").capitalize }
return name unless name.include?("_")

new_name =
name
.split(/_+/)
.each_with_index
.map { |part, i| i.zero? ? part : part.downcase.capitalize }
.join
# TODO FIELD_NAME11
#new_name == "FIELDName11" ? "fieldName11" : new_name
new_name == "FIELDName11" ? "FIELD_NAME11" : new_name
# Names like FIELD_NAME11 (all caps + underscores + numbers) should remain as-is
return name if name =~ /[A-Zd_]+/

name
.split(/_+/)
.each_with_index
.map { |part, i| i.zero? ? part : part.downcase.capitalize }
.join
end
end
end
Expand Down
Loading

0 comments on commit cf35b3f

Please sign in to comment.