@@ -18,6 +18,8 @@ def encode_generic(data: Any) -> str:
1818 Returns:
1919 GCF-formatted text string.
2020 """
21+ if data is None or not isinstance (data , (dict , list )):
22+ return str (data ) if data is not None else "-"
2123 lines : list [str ] = []
2224 _encode_value (data , lines , depth = 0 )
2325 return "\n " .join (lines ) + "\n " if lines else "\n "
@@ -33,15 +35,16 @@ def _encode_value(value: Any, lines: list[str], depth: int) -> None:
3335 lines .append (_indent (depth ) + _format_value (value ))
3436
3537
36- def _encode_dict (d : dict , lines : list [str ], depth : int ) -> None :
38+ def _encode_dict (d : dict , lines : list [str ], depth : int , name : str | None = None ) -> None :
3739 """Encode a dict into key=value pairs with section headers for nested values."""
3840 prefix = _indent (depth )
41+ if name is not None :
42+ lines .append (f"{ prefix } ## { name } " )
3943 for key , value in d .items ():
4044 if isinstance (value , list ):
4145 _encode_array (value , key , lines , depth )
4246 elif isinstance (value , dict ):
43- lines .append (f"{ prefix } ## { key } " )
44- _encode_dict (value , lines , depth + 1 )
47+ _encode_dict (value , lines , depth + 1 , name = key )
4548 else :
4649 lines .append (f"{ prefix } { key } ={ _format_value (value )} " )
4750
@@ -85,14 +88,12 @@ def _encode_tabular(items: list[dict], name: str, lines: list[str], depth: int)
8588
8689 if nested_fields :
8790 lines .append (f"{ prefix } @{ i } { row_str } " )
88- inner_prefix = _indent (depth + 1 )
8991 for nk in nested_fields :
9092 nv = item .get (nk )
9193 if isinstance (nv , list ):
9294 _encode_array (nv , nk , lines , depth + 1 )
9395 elif isinstance (nv , dict ):
94- lines .append (f"{ inner_prefix } ## { nk } " )
95- _encode_dict (nv , lines , depth + 2 )
96+ _encode_dict (nv , lines , depth + 1 , name = nk )
9697 else :
9798 lines .append (f"{ prefix } { row_str } " )
9899
0 commit comments