Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export PATH := $(BIN):$(PATH)
export GOBIN := $(abspath $(BIN))
# Set to use a different Python interpreter. For example, `PYTHON=python make test`.
PYTHON ?= python3
CONFORMANCE_ARGS ?= --strict --expected_failures=tests/conformance/nonconforming.yaml --timeout 10s
CONFORMANCE_ARGS ?= --strict --strict_message --expected_failures=tests/conformance/nonconforming.yaml --timeout 10s
ADD_LICENSE_HEADER := $(BIN)/license-header \
--license-type apache \
--copyright-holder "Buf Technologies, Inc." \
Expand Down
23 changes: 22 additions & 1 deletion protovalidate/internal/string_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from decimal import Decimal

import celpy # type: ignore
from celpy import celtypes # type: ignore

Expand Down Expand Up @@ -146,16 +148,32 @@ def format_string(self, arg: celtypes.Value) -> celpy.Result:
if isinstance(arg, celtypes.StringType):
return arg
if isinstance(arg, celtypes.BytesType):
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.DurationType):
return celtypes.StringType(self._format_duration(arg))
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")
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:
Expand All @@ -167,6 +185,9 @@ def format_list(self, arg: celtypes.ListType) -> celpy.Result:
result += "]"
return celtypes.StringType(result)

def _format_duration(self, arg: celtypes.DurationType) -> celpy.Result:
return f"{arg.seconds + Decimal(arg.microseconds) / Decimal(1_000_000):f}s"


_default_format = StringFormat("en_US")
format = _default_format.format # noqa: A001
Expand Down
1 change: 1 addition & 0 deletions tests/conformance/nonconforming.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# celpy doesn't support nano seconds
# ref: https://github.com/cloud-custodian/cel-python/issues/43
standard_constraints/well_known_types/duration:
- gte_lte/invalid/above
- lte/invalid
Expand Down
Loading