Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/proto/marshal/rules/stringy_numbers.py: 83%
24 statements
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-25 06:37 +0000
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-25 06:37 +0000
1# Copyright (C) 2021 Google LLC
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
15from proto.primitives import ProtoType
18class StringyNumberRule:
19 """A marshal between certain numeric types and strings
21 This is a necessary hack to allow round trip conversion
22 from messages to dicts back to messages.
24 See https://github.com/protocolbuffers/protobuf/issues/2679
25 and
26 https://developers.google.com/protocol-buffers/docs/proto3#json
27 for more details.
28 """
30 def to_python(self, value, *, absent: bool = None):
31 return value
33 def to_proto(self, value):
34 if value is not None:
35 return self._python_type(value)
37 return None
40class Int64Rule(StringyNumberRule):
41 _python_type = int
42 _proto_type = ProtoType.INT64
45class UInt64Rule(StringyNumberRule):
46 _python_type = int
47 _proto_type = ProtoType.UINT64
50class SInt64Rule(StringyNumberRule):
51 _python_type = int
52 _proto_type = ProtoType.SINT64
55class Fixed64Rule(StringyNumberRule):
56 _python_type = int
57 _proto_type = ProtoType.FIXED64
60class SFixed64Rule(StringyNumberRule):
61 _python_type = int
62 _proto_type = ProtoType.SFIXED64
65STRINGY_NUMBER_RULES = [
66 Int64Rule,
67 UInt64Rule,
68 SInt64Rule,
69 Fixed64Rule,
70 SFixed64Rule,
71]