1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15+ from decimal import Decimal
16+
1517import celpy # type: ignore
1618from celpy import celtypes # type: ignore
1719
@@ -146,16 +148,32 @@ def format_string(self, arg: celtypes.Value) -> celpy.Result:
146148 if isinstance (arg , celtypes .StringType ):
147149 return arg
148150 if isinstance (arg , celtypes .BytesType ):
149- return celtypes .StringType (arg . hex () )
151+ return celtypes .StringType (arg )
150152 if isinstance (arg , celtypes .ListType ):
151153 return self .format_list (arg )
154+ if isinstance (arg , celtypes .BoolType ):
155+ # True -> true
156+ return celtypes .StringType (str (arg ).lower ())
157+ if isinstance (arg , celtypes .DoubleType ):
158+ return celtypes .StringType (f"{ arg :.0f} " )
159+ if isinstance (arg , celtypes .DurationType ):
160+ return celtypes .StringType (self ._format_duration (arg ))
161+ if isinstance (arg , celtypes .TimestampType ):
162+ base = arg .isoformat ()
163+ if arg .getMilliseconds () != 0 :
164+ base = arg .isoformat (timespec = "milliseconds" )
165+ return celtypes .StringType (base .removesuffix ("+00:00" ) + "Z" )
152166 return celtypes .StringType (arg )
153167
154168 def format_value (self , arg : celtypes .Value ) -> celpy .Result :
155169 if isinstance (arg , (celtypes .StringType , str )):
156170 return celtypes .StringType (quote (arg ))
157171 if isinstance (arg , celtypes .UintType ):
158172 return celtypes .StringType (arg )
173+ if isinstance (arg , celtypes .DurationType ):
174+ return celtypes .StringType (f'duration("{ self ._format_duration (arg )} ")' )
175+ if isinstance (arg , celtypes .DoubleType ):
176+ return celtypes .StringType (f"{ arg :f} " )
159177 return self .format_string (arg )
160178
161179 def format_list (self , arg : celtypes .ListType ) -> celpy .Result :
@@ -167,6 +185,9 @@ def format_list(self, arg: celtypes.ListType) -> celpy.Result:
167185 result += "]"
168186 return celtypes .StringType (result )
169187
188+ def _format_duration (self , arg : celtypes .DurationType ) -> celpy .Result :
189+ return f"{ arg .seconds + Decimal (arg .microseconds ) / Decimal (1_000_000 ):f} s"
190+
170191
171192_default_format = StringFormat ("en_US" )
172193format = _default_format .format # noqa: A001
0 commit comments