Skip to content

Conversation

@stefanvanburen
Copy link
Member

We can't currently pass the conformance tests involving duration+timestamp types, as cel-python does not support nanoseconds in its duration implementation (linked issue). Otherwise, this gets us to a conformant spot.

Related to #255.

We can't currently pass the conformance tests involving
duration+timestamp types, as cel-python does not support nanoseconds in
its duration implementation (linked issue). Otherwise, this gets us to a
conformant spot.

Related to #255.
@stefanvanburen stefanvanburen requested a review from a user February 18, 2025 18:38
Comment on lines 149 to 161
return celtypes.StringType(arg.hex())
return celtypes.StringType(arg)
if isinstance(arg, celtypes.ListType):
return self.format_list(arg)
if isinstance(arg, celtypes.BoolType):
# True -> true
return celtypes.StringType(str(arg).lower())
if isinstance(arg, celtypes.DoubleType):
return celtypes.StringType(f"{arg:.0f}")
if isinstance(arg, celtypes.TimestampType):
base = arg.isoformat()
if arg.getMilliseconds() != 0:
base = arg.isoformat(timespec="milliseconds")
return celtypes.StringType(base.removesuffix("+00:00") + "Z")
Copy link
Member Author

Choose a reason for hiding this comment

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

just for context, most protovalidate rules are defined in CEL using the %s verb (in fact, I don't think I've seen another verb used). format_string handles that verb, so we need to add some additional handling here.

Comment on lines 169 to 172
if isinstance(arg, celtypes.DurationType):
return celtypes.StringType(f'duration("{arg.seconds + (arg.microseconds // 1e6):.0f}s")')
if isinstance(arg, celtypes.DoubleType):
return celtypes.StringType(f"{arg:f}")
Copy link
Member Author

Choose a reason for hiding this comment

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

format_value handles lists, where we add more context to what type of value is in the list (think repr instead of str in Python). hence the additional context.

@stefanvanburen
Copy link
Member Author

Also for my future context: the definition of format in cel-go, which explains how StringFormat should work.

Still issues with tests using below-microsecond nanosecond values, but
these tests use millisecond values, so we can fix them up.
@stefanvanburen
Copy link
Member Author

Last note for myself and others: cel-python doesn't support nanosecond resolution in DurationType because it subclasses datetime.timedelta, which doesn't handle nanoseconds. It'd probably be a large lift to replace that in cel-python.

Going to merge this now as I think it's a step in the right direction; I do think that we want to more fully test StringFormat separately as the conformance tests are only tickling the %s verb currently.

@stefanvanburen stefanvanburen merged commit f8dd975 into main Feb 18, 2025
12 checks passed
@stefanvanburen stefanvanburen deleted the svanburen/conformance-messages branch February 18, 2025 21:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants