"""Shape extraction for untyped JSON documents from external services.Discovery-style clients (Google Drive, Google Sheets) return decoded JSON withno static shape. Reading a nested array or object out of one is the same threesteps every time: confirm the container is a string-keyed object, read the key,and confirm the value has the shape the caller is about to rely on. Thesehelpers own that sequence so callers narrow once, at the boundary, instead ofre-deriving it per call site.A value that fails a check is absent, not empty: the helpers return an emptyresult so the caller sees "no rows" rather than a partially typed value. Theynever coerce a malformed value into a plausible one."""from__future__importannotationsfrom.type_guardsimportis_object_list,is_str_keyed_dict__all__=["str_keyed_mapping","str_keyed_rows"]
[docs]defstr_keyed_mapping(value:object)->dict[str,object]:"""Return ``value`` when it is a string-keyed object, else an empty mapping. A JSON object whose keys are not strings cannot carry the named fields a caller reads, so it is reported as absent rather than partially honoured. """returnvalueifis_str_keyed_dict(value)else{}
[docs]defstr_keyed_rows(container:object,key:str)->list[dict[str,object]]:"""Return ``container[key]`` as the string-keyed objects it contains. Returns an empty list when the container is not a string-keyed object, when the key is absent, or when its value is not an array. Array entries that are not themselves string-keyed objects are dropped: they cannot carry the fields the caller reads. """ifnotis_str_keyed_dict(container):return[]raw_rows=container.get(key)ifnotis_object_list(raw_rows):return[]return[rowforrowinraw_rowsifis_str_keyed_dict(row)]