Files
Medios-Macina/SYS/field_access.py
2026-02-11 18:16:07 -08:00

38 lines
1.0 KiB
Python

"""Lightweight helpers for accessing fields on mixed pipeline objects.
This intentionally avoids importing cmdlet modules so it can be used from
providers and core pipeline code without pulling in the full cmdlet stack.
"""
from __future__ import annotations
from typing import Any, Optional
def get_field(obj: Any, field: str, default: Optional[Any] = None) -> Any:
"""Extract a field from either a dict or object with fallback default.
- Supports dict.get(field)
- Supports getattr(obj, field)
- If obj is a list, uses the first element
- If obj has `.extra` dict (PipeObject pattern), also checks extra[field]
"""
if isinstance(obj, list):
if not obj:
return default
obj = obj[0]
if isinstance(obj, dict):
return obj.get(field, default)
value = getattr(obj, field, None)
if value is not None:
return value
extra_val = getattr(obj, "extra", None)
if isinstance(extra_val, dict):
return extra_val.get(field, default)
return default