Browse Source

cmake: yaml: properly store strings

Store strings in YAML as single-quoted entries to avoid issues with
special characters. This also fixes a quirk with the current test
suite where the quotes in the expected value are filtered out by the
YAML import.

Signed-off-by: Luca Burelli <l.burelli@arduino.cc>
pull/88410/head
Luca Burelli 3 months ago committed by Benjamin Cabé
parent
commit
4a50fbf804
  1. 12
      cmake/modules/yaml.cmake
  2. 2
      tests/cmake/yaml/CMakeLists.txt

12
cmake/modules/yaml.cmake

@ -586,7 +586,9 @@ function(to_yaml in_json level yaml genex) @@ -586,7 +586,9 @@ function(to_yaml in_json level yaml genex)
string(REPLACE "\n" "\n${indent_${level}} " indent_yaml "${non_indent_yaml}")
set(${yaml} "${${yaml}}${indent_${level}} - ${indent_yaml}\n")
else()
set(${yaml} "${${yaml}}${indent_${level}} - ${item}\n")
# Assume a string, escape single quotes.
string(REPLACE "'" "''" item "${item}")
set(${yaml} "${${yaml}}${indent_${level}} - '${item}'\n")
endif()
endforeach()
endif()
@ -595,10 +597,12 @@ function(to_yaml in_json level yaml genex) @@ -595,10 +597,12 @@ function(to_yaml in_json level yaml genex)
# - with unexpanded generator expressions: save as YAML comment
# - if it matches the special prefix: convert to YAML list
# - otherwise: save as YAML scalar
# Single quotes must be escaped in the value.
string(REPLACE "'" "''" subjson "${subjson}")
if(subjson MATCHES "\\$<.*>" AND ${genex})
# Yet unexpanded generator expression: save as comment
string(SUBSTRING ${indent_${level}} 1 -1 short_indent)
set(${yaml} "${${yaml}}#${short_indent}${member}: ${subjson}\n")
set(${yaml} "${${yaml}}#${short_indent}${member}: '${subjson}'\n")
elseif(subjson MATCHES "^@YAML-LIST@")
# List-as-string: convert to list
set(${yaml} "${${yaml}}${indent_${level}}${member}:")
@ -608,12 +612,12 @@ function(to_yaml in_json level yaml genex) @@ -608,12 +612,12 @@ function(to_yaml in_json level yaml genex)
else()
set(${yaml} "${${yaml}}\n")
foreach(item ${subjson})
set(${yaml} "${${yaml}}${indent_${level}} - ${item}\n")
set(${yaml} "${${yaml}}${indent_${level}} - '${item}'\n")
endforeach()
endif()
else()
# Raw strings: save as is
set(${yaml} "${${yaml}}${indent_${level}}${member}: ${subjson}\n")
set(${yaml} "${${yaml}}${indent_${level}}${member}: '${subjson}'\n")
endif()
else()
# Other JSON data type -> YAML scalar, as-is

2
tests/cmake/yaml/CMakeLists.txt

@ -475,7 +475,7 @@ function(test_setting_map_list_entry_commas) @@ -475,7 +475,7 @@ function(test_setting_map_list_entry_commas)
test_assert(TEST "${readback_name}" STREQUAL "${new_entry_name_${index}}"
COMMENT "list values mismatch."
)
test_assert(TEST "'${readback_str}'" STREQUAL "${new_entry_str_${index}}"
test_assert(TEST "${readback_str}" STREQUAL "${new_entry_str_${index}}"
COMMENT "list values mismatch."
)
endforeach()

Loading…
Cancel
Save