Debian Codegraph @

proto/marshal/rules/field_mask.py

raw
1 # Copyright 2022 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 # https://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
15 from google.protobuf import field_mask_pb2
16
17
18 class FieldMaskRule:
19 """A marshal between FieldMask and strings.
20
21 See https://github.com/googleapis/proto-plus-python/issues/333
22 and
23 https://developers.google.com/protocol-buffers/docs/proto3#json
24 for more details.
25 """
26
27 def to_python(self, value, *, absent: bool = None):
28 return value
29
30 def to_proto(self, value):
31 if isinstance(value, str):
32 field_mask_value = field_mask_pb2.FieldMask()
33 field_mask_value.FromJsonString(value=value)
34 return field_mask_value
35
36 return value