Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion protovalidate/internal/extra_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,8 @@ def get_bits(self) -> int:

# Handle double colon, fill pieces with 0
if self._double_colon_seen:
if len(p16) >= 7:
return 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the return of 0?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is because get_bits is supposed to return 0 if the address is invalid.

    def get_bits(self) -> int:
        """Return the 128-bit value of an address parsed through address() or address_prefix().

        Return 0 if no address was parsed successfully.

        """

OTOH, though, it doesn't really matter, so I removed it.

while len(p16) < 8:
# Delete 0 entries at pos, insert a 0
p16.insert(self._double_colon_at, 0x00000000)
Expand Down Expand Up @@ -677,7 +679,9 @@ def __address_part(self) -> bool:

break

return self._double_colon_seen or len(self._pieces) == 8
if self._double_colon_seen:
return len(self._pieces) < 8
return len(self._pieces) == 8

def __zone_id(self) -> bool:
"""Determine whether the current position is a zoneID.
Expand Down
15 changes: 2 additions & 13 deletions protovalidate/internal/string_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def format_string(self, arg: celtypes.Value) -> celpy.Result:
# True -> true
return celtypes.StringType(str(arg).lower())
if isinstance(arg, celtypes.DoubleType):
return celtypes.StringType(f"{arg:.0f}")
return celtypes.StringType(f"{arg:g}")
if isinstance(arg, celtypes.DurationType):
return celtypes.StringType(self._format_duration(arg))
if isinstance(arg, celtypes.TimestampType):
Expand All @@ -168,23 +168,12 @@ def format_string(self, arg: celtypes.Value) -> celpy.Result:
return celtypes.StringType(base.removesuffix("+00:00") + "Z")
return celtypes.StringType(arg)

def format_value(self, arg: celtypes.Value) -> celpy.Result:
if isinstance(arg, (celtypes.StringType, str)):
return celtypes.StringType(quote(arg))
if isinstance(arg, celtypes.UintType):
return celtypes.StringType(arg)
if isinstance(arg, celtypes.DurationType):
return celtypes.StringType(f'duration("{self._format_duration(arg)}")')
if isinstance(arg, celtypes.DoubleType):
return celtypes.StringType(f"{arg:f}")
return self.format_string(arg)

def format_list(self, arg: celtypes.ListType) -> celpy.Result:
result = "["
for i in range(len(arg)):
if i > 0:
result += ", "
result += self.format_value(arg[i])
result += self.format_string(arg[i])
result += "]"
return celtypes.StringType(result)

Expand Down
15 changes: 0 additions & 15 deletions tests/conformance/nonconforming.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,7 @@
standard_rules/well_known_types/duration:
- gte_lte/invalid/above
- lte/invalid
- in/invalid
- not in/valid
- not in/invalid
standard_rules/well_known_types/timestamp:
- gte_lte/invalid/above
- lte/invalid
kitchen_sink:
- field/embedded/invalid
- field/transitive/invalid
- many/all-non-message-fields/invalid
- field/invalid
standard_rules/repeated:
- items/in/invalid
- items/not_in/invalid
library/is_ip:
- version/6/invalid/ipv6/7h16_double_colon_1h16
- version/6/invalid/ipv6/7h16_double_colon
- version/6/invalid/ipv6/double_colon_8h16
- version/6/invalid/ipv6/1h16_double_colon_7h16
Loading