cmds.projects.lib.templates: merge_values(): Don't split strings
is_list_dict() accepts a string value as a list-dict value, and render_values_to_list_dict() passes such values through unchanged. merge_values() then merges them with list extension, and [] += 'xyz' appends the individual characters, so every string value ends up as a list of its characters. Normalize string values to single-element lists in render_values_to_list_dict(), so that merge_values() appends whole values. jw-pkg projects create-pkg-config is hit by this, because all of its values are strings: the generated file contains one line per character. The code path was just never exercised lately. Add unit tests for the value layout guards, the normalization, and the merging of the three supported layouts. Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v0.84.2 Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
2b951f7861
commit
955fb8101b
6 changed files with 103 additions and 5 deletions
|
|
@ -7,7 +7,7 @@ ListDict: TypeAlias = dict[str, list[str]]
|
|||
StrDict: TypeAlias = dict[str, str]
|
||||
RenderValues: TypeAlias = ListDict | StrDict | TupleList
|
||||
|
||||
def is_str_dict(values: RenderValues) -> TypeGuard[StrDict]:
|
||||
def is_str_dict(values: object) -> TypeGuard[StrDict]:
|
||||
if not isinstance(values, dict):
|
||||
return False
|
||||
for key, val in values.items():
|
||||
|
|
@ -17,7 +17,7 @@ def is_str_dict(values: RenderValues) -> TypeGuard[StrDict]:
|
|||
return False
|
||||
return True
|
||||
|
||||
def is_list_dict(values: RenderValues) -> TypeGuard[ListDict]:
|
||||
def is_list_dict(values: object) -> TypeGuard[ListDict]:
|
||||
if not isinstance(values, dict):
|
||||
return False
|
||||
for key, val in values.items():
|
||||
|
|
@ -32,7 +32,7 @@ def is_list_dict(values: RenderValues) -> TypeGuard[ListDict]:
|
|||
return False
|
||||
return True
|
||||
|
||||
def is_tuple_list(values: RenderValues) -> TypeGuard[TupleList]:
|
||||
def is_tuple_list(values: object) -> TypeGuard[TupleList]:
|
||||
if not isinstance(values, list):
|
||||
return False
|
||||
for item in values:
|
||||
|
|
@ -46,7 +46,7 @@ def is_tuple_list(values: RenderValues) -> TypeGuard[TupleList]:
|
|||
return False
|
||||
return True
|
||||
|
||||
def render_values_to_list_dict(values: RenderValues) -> ListDict:
|
||||
def render_values_to_list_dict(values: object) -> ListDict:
|
||||
|
||||
def __tuple_list_to_dict(src: TupleList) -> ListDict:
|
||||
ret: ListDict = {}
|
||||
|
|
@ -56,7 +56,12 @@ def render_values_to_list_dict(values: RenderValues) -> ListDict:
|
|||
return ret
|
||||
|
||||
if is_list_dict(values):
|
||||
return values
|
||||
ret: ListDict = {}
|
||||
for key, val in values.items():
|
||||
# -- A string value is treated as a list with a single element;
|
||||
# merging it unconverted would split it into characters
|
||||
ret[key] = val if isinstance(val, list) else [val]
|
||||
return ret
|
||||
if is_tuple_list(values):
|
||||
return __tuple_list_to_dict(values)
|
||||
raise Exception('Unsupported template value layout')
|
||||
|
|
|
|||
Loading…
Reference in a new issue