File tree Expand file tree Collapse file tree 4 files changed +40
-6
lines changed Expand file tree Collapse file tree 4 files changed +40
-6
lines changed Original file line number Diff line number Diff line change 5
5
6
6
from crossplane .function .proto .v1 import run_function_pb2 as crossplane_dot_function_dot_proto_dot_v1_dot_run__function__pb2
7
7
8
- GRPC_GENERATED_VERSION = '1.66.0 '
8
+ GRPC_GENERATED_VERSION = '1.66.2 '
9
9
GRPC_VERSION = grpc .__version__
10
10
_version_not_supported = False
11
11
Original file line number Diff line number Diff line change 5
5
6
6
from crossplane .function .proto .v1beta1 import run_function_pb2 as crossplane_dot_function_dot_proto_dot_v1beta1_dot_run__function__pb2
7
7
8
- GRPC_GENERATED_VERSION = '1.66.0 '
8
+ GRPC_GENERATED_VERSION = '1.66.2 '
9
9
GRPC_VERSION = grpc .__version__
10
10
_version_not_supported = False
11
11
Original file line number Diff line number Diff line change 16
16
17
17
import dataclasses
18
18
import datetime
19
+ from typing import Any
19
20
20
21
import pydantic
21
22
from google .protobuf import struct_pb2 as structpb
@@ -62,17 +63,28 @@ def dict_to_struct(d: dict) -> structpb.Struct:
62
63
return s
63
64
64
65
66
+ def __to_python_native (v : structpb .Value ) -> Any :
67
+ """Convert a protobuf Value to a Python native type."""
68
+ if isinstance (v , structpb .ListValue ):
69
+ return __listvalue_to_list (v )
70
+ elif isinstance (v , structpb .Struct ):
71
+ return struct_to_dict (v )
72
+ return v
73
+
74
+
75
+ def __listvalue_to_list (lv : structpb .ListValue ) -> list :
76
+ """Convert a protobuf ListValue to a list."""
77
+ return [__to_python_native (v ) for v in lv ]
78
+
79
+
65
80
def struct_to_dict (s : structpb .Struct ) -> dict :
66
81
"""Create a dict from the supplied Struct well-known type.
67
82
68
83
Crossplane sends observed and desired resources to a function encoded as a
69
84
protobuf struct. This function makes it possible to convert resources to a
70
85
dictionary.
71
86
"""
72
- return {
73
- k : (struct_to_dict (v ) if isinstance (v , structpb .Struct ) else v )
74
- for k , v in s .items ()
75
- }
87
+ return {k : __to_python_native (v ) for k , v in s .items ()}
76
88
77
89
78
90
@dataclasses .dataclass
Original file line number Diff line number Diff line change @@ -275,6 +275,28 @@ class TestCase:
275
275
),
276
276
want = {"foo" : {"bar" : "baz" }},
277
277
),
278
+ TestCase (
279
+ reason = "Convert a nested struct containing ListValues to a dictionary." ,
280
+ s = structpb .Struct (
281
+ fields = {
282
+ "foo" : structpb .Value (
283
+ struct_value = structpb .Struct (
284
+ fields = {
285
+ "bar" : structpb .Value (
286
+ list_value = structpb .ListValue (
287
+ values = [
288
+ structpb .Value (string_value = "baz" ),
289
+ structpb .Value (string_value = "qux" ),
290
+ ]
291
+ )
292
+ )
293
+ }
294
+ )
295
+ )
296
+ }
297
+ ),
298
+ want = {"foo" : {"bar" : ["baz" , "qux" ]}},
299
+ ),
278
300
]
279
301
280
302
for case in cases :
You can’t perform that action at this time.
0 commit comments