Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/proto/marshal/rules/stringy_numbers.py: 80%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

25 statements  

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. 

14 

15from proto.primitives import ProtoType 

16 

17 

18class StringyNumberRule: 

19 """A marshal between certain numeric types and strings 

20 

21 This is a necessary hack to allow round trip conversion 

22 from messages to dicts back to messages. 

23 

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 """ 

29 

30 def to_python(self, value, *, absent: bool = None): 

31 return value 

32 

33 def to_proto(self, value): 

34 if value is not None: 

35 return self._python_type(value) 

36 

37 return None 

38 

39 

40class Int64Rule(StringyNumberRule): 

41 _python_type = int 

42 _proto_type = ProtoType.INT64 

43 

44 

45class UInt64Rule(StringyNumberRule): 

46 _python_type = int 

47 _proto_type = ProtoType.UINT64 

48 

49 

50class SInt64Rule(StringyNumberRule): 

51 _python_type = int 

52 _proto_type = ProtoType.SINT64 

53 

54 

55class Fixed64Rule(StringyNumberRule): 

56 _python_type = int 

57 _proto_type = ProtoType.FIXED64 

58 

59 

60class SFixed64Rule(StringyNumberRule): 

61 _python_type = int 

62 _proto_type = ProtoType.SFIXED64 

63 

64 

65STRINGY_NUMBER_RULES = [ 

66 Int64Rule, 

67 UInt64Rule, 

68 SInt64Rule, 

69 Fixed64Rule, 

70 SFixed64Rule, 

71]