|
| 1 | +"""Protobuf conversion utilities. |
| 2 | +
|
| 3 | +This module provides utilities for converting protobuf Value and Struct objects |
| 4 | +to Python native types. Handles both Google protobuf and betterproto variants. |
| 5 | +""" |
| 6 | + |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +from typing import Any |
| 10 | + |
| 11 | + |
| 12 | +def value_to_python(value: Any) -> Any: |
| 13 | + """Convert protobuf Value or Python native type to Python native type. |
| 14 | +
|
| 15 | + Handles: |
| 16 | + 1. Python native types (from betterproto's Struct which stores values directly) |
| 17 | + 2. Google protobuf Value objects (from google.protobuf.struct_pb2) |
| 18 | + 3. betterproto Value objects (from betterproto.lib.google.protobuf) |
| 19 | +
|
| 20 | + Args: |
| 21 | + value: A protobuf Value, native Python type, or nested structure |
| 22 | +
|
| 23 | + Returns: |
| 24 | + Python native type (None, bool, int, float, str, list, or dict) |
| 25 | + """ |
| 26 | + # Handle Python native types (from betterproto or already converted) |
| 27 | + if value is None: |
| 28 | + return None |
| 29 | + if isinstance(value, (bool, int, float, str)): |
| 30 | + # Note: bool must be checked before int since bool is a subclass of int |
| 31 | + return value |
| 32 | + if isinstance(value, list): |
| 33 | + return [value_to_python(v) for v in value] |
| 34 | + if isinstance(value, dict): |
| 35 | + return {k: value_to_python(v) for k, v in value.items()} |
| 36 | + |
| 37 | + # Handle Google protobuf Value objects using WhichOneof |
| 38 | + if hasattr(value, "WhichOneof"): |
| 39 | + kind = value.WhichOneof("kind") |
| 40 | + if kind == "null_value": |
| 41 | + return None |
| 42 | + elif kind == "number_value": |
| 43 | + return value.number_value |
| 44 | + elif kind == "string_value": |
| 45 | + return value.string_value |
| 46 | + elif kind == "bool_value": |
| 47 | + return value.bool_value |
| 48 | + elif kind == "struct_value": |
| 49 | + return struct_to_dict(value.struct_value) |
| 50 | + elif kind == "list_value": |
| 51 | + return [value_to_python(v) for v in value.list_value.values] |
| 52 | + |
| 53 | + # Handle betterproto Value objects using is_set method |
| 54 | + if hasattr(value, "is_set"): |
| 55 | + try: |
| 56 | + if value.is_set("null_value"): |
| 57 | + return None |
| 58 | + elif value.is_set("number_value"): |
| 59 | + return value.number_value |
| 60 | + elif value.is_set("string_value"): |
| 61 | + return value.string_value |
| 62 | + elif value.is_set("bool_value"): |
| 63 | + return value.bool_value |
| 64 | + elif value.is_set("struct_value"): |
| 65 | + sv = value.struct_value |
| 66 | + if isinstance(sv, dict): |
| 67 | + return {k: value_to_python(v) for k, v in sv.get("fields", sv).items()} |
| 68 | + return struct_to_dict(sv) |
| 69 | + elif value.is_set("list_value"): |
| 70 | + lv = value.list_value |
| 71 | + # Handle dict-style list_value (betterproto can store dicts) |
| 72 | + if isinstance(lv, dict): |
| 73 | + return [value_to_python(v) for v in lv.get("values", [])] |
| 74 | + return [value_to_python(v) for v in lv.values] |
| 75 | + except (AttributeError, TypeError): |
| 76 | + pass # Not a betterproto Value, fall through |
| 77 | + |
| 78 | + # Fallback: return the value as-is |
| 79 | + return value |
| 80 | + |
| 81 | + |
| 82 | +def struct_to_dict(struct: Any) -> dict[str, Any]: |
| 83 | + """Convert protobuf Struct to Python dict. |
| 84 | +
|
| 85 | + Args: |
| 86 | + struct: A protobuf Struct object (Google or betterproto) |
| 87 | +
|
| 88 | + Returns: |
| 89 | + Python dict with converted values |
| 90 | + """ |
| 91 | + if not struct: |
| 92 | + return {} |
| 93 | + |
| 94 | + # Handle dict-like struct (betterproto sometimes returns dicts directly) |
| 95 | + if isinstance(struct, dict): |
| 96 | + return {k: value_to_python(v) for k, v in struct.items()} |
| 97 | + |
| 98 | + # Handle struct with fields attribute |
| 99 | + if hasattr(struct, "fields"): |
| 100 | + return {k: value_to_python(v) for k, v in struct.fields.items()} |
| 101 | + |
| 102 | + return {} |
0 commit comments