From 64adeeba90c919e0db61dba8732e97b8c7a35b76 Mon Sep 17 00:00:00 2001 From: John Chadwick Date: Mon, 28 Apr 2025 16:40:12 -0400 Subject: [PATCH 1/2] Fix various v0.11.0 conformance issues We still have the nanosecond granularity issues that we've always had, but this should fix everything else. --- protovalidate/internal/extra_func.py | 6 +++++- protovalidate/internal/string_format.py | 15 ++------------- tests/conformance/nonconforming.yaml | 15 --------------- 3 files changed, 7 insertions(+), 29 deletions(-) diff --git a/protovalidate/internal/extra_func.py b/protovalidate/internal/extra_func.py index c6cb19d5..61513888 100644 --- a/protovalidate/internal/extra_func.py +++ b/protovalidate/internal/extra_func.py @@ -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 while len(p16) < 8: # Delete 0 entries at pos, insert a 0 p16.insert(self._double_colon_at, 0x00000000) @@ -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. diff --git a/protovalidate/internal/string_format.py b/protovalidate/internal/string_format.py index b6b1338d..9353ccf9 100644 --- a/protovalidate/internal/string_format.py +++ b/protovalidate/internal/string_format.py @@ -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): @@ -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) diff --git a/tests/conformance/nonconforming.yaml b/tests/conformance/nonconforming.yaml index 9a014fb4..b46684df 100644 --- a/tests/conformance/nonconforming.yaml +++ b/tests/conformance/nonconforming.yaml @@ -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 From fe060fc693dcec11dc1bb1aa5b3a110068c912bf Mon Sep 17 00:00:00 2001 From: John Chadwick Date: Tue, 29 Apr 2025 01:41:06 -0400 Subject: [PATCH 2/2] Remove get_bits case for invalid double colon --- protovalidate/internal/extra_func.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/protovalidate/internal/extra_func.py b/protovalidate/internal/extra_func.py index 61513888..ae2b02c0 100644 --- a/protovalidate/internal/extra_func.py +++ b/protovalidate/internal/extra_func.py @@ -548,8 +548,6 @@ def get_bits(self) -> int: # Handle double colon, fill pieces with 0 if self._double_colon_seen: - if len(p16) >= 7: - return 0 while len(p16) < 8: # Delete 0 entries at pos, insert a 0 p16.insert(self._double_colon_at, 0x00000000)