Allow PARAM_WXSTRING_MAP to behave as an array.
In particular, overwrite file contents with current contents of map rather than merging. Fixes https://gitlab.com/kicad/code/kicad/-/issues/16801
This commit is contained in:
parent
445ed380b8
commit
5d4b4f39ec
|
@ -50,7 +50,8 @@ PROJECT_FILE::PROJECT_FILE( const wxString& aFullPath ) :
|
||||||
|
|
||||||
m_params.emplace_back( new PARAM_LIST<FILE_INFO_PAIR>( "boards", &m_boards, {} ) );
|
m_params.emplace_back( new PARAM_LIST<FILE_INFO_PAIR>( "boards", &m_boards, {} ) );
|
||||||
|
|
||||||
m_params.emplace_back( new PARAM_WXSTRING_MAP( "text_variables", &m_TextVars, {} ) );
|
m_params.emplace_back( new PARAM_WXSTRING_MAP( "text_variables",
|
||||||
|
&m_TextVars, {}, false, true /* array behavior, even though stored as a map */ ) );
|
||||||
|
|
||||||
m_params.emplace_back( new PARAM_LIST<wxString>( "libraries.pinned_symbol_libs",
|
m_params.emplace_back( new PARAM_LIST<wxString>( "libraries.pinned_symbol_libs",
|
||||||
&m_PinnedSymbolLibs, {} ) );
|
&m_PinnedSymbolLibs, {} ) );
|
||||||
|
|
|
@ -466,6 +466,16 @@ bool JSON_SETTINGS::SaveToFile( const wxString& aDirectory, bool aForce )
|
||||||
|
|
||||||
nlohmann::json toSave = m_internals->m_original;
|
nlohmann::json toSave = m_internals->m_original;
|
||||||
|
|
||||||
|
|
||||||
|
for( PARAM_BASE* param : m_params )
|
||||||
|
{
|
||||||
|
if( PARAM_WXSTRING_MAP* stringMap = dynamic_cast<PARAM_WXSTRING_MAP*>( param ) )
|
||||||
|
{
|
||||||
|
if( stringMap->ClearUnknownKeys() )
|
||||||
|
toSave[ stringMap->GetJsonPath() ] = nlohmann::json( {} );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
toSave.update( m_internals->begin(), m_internals->end(), /* merge_objects = */ true );
|
toSave.update( m_internals->begin(), m_internals->end(), /* merge_objects = */ true );
|
||||||
|
|
||||||
try
|
try
|
||||||
|
|
|
@ -35,7 +35,8 @@ class PARAM_BASE
|
||||||
public:
|
public:
|
||||||
PARAM_BASE( std::string aJsonPath, bool aReadOnly ) :
|
PARAM_BASE( std::string aJsonPath, bool aReadOnly ) :
|
||||||
m_path( std::move( aJsonPath ) ),
|
m_path( std::move( aJsonPath ) ),
|
||||||
m_readOnly( aReadOnly )
|
m_readOnly( aReadOnly ),
|
||||||
|
m_clearUnknownKeys( false )
|
||||||
{}
|
{}
|
||||||
|
|
||||||
virtual ~PARAM_BASE() = default;
|
virtual ~PARAM_BASE() = default;
|
||||||
|
@ -68,9 +69,18 @@ public:
|
||||||
*/
|
*/
|
||||||
const std::string& GetJsonPath() const { return m_path; }
|
const std::string& GetJsonPath() const { return m_path; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return true if keys should be cleared from source file rather than merged. Useful for
|
||||||
|
* things such as text variables that are semantically an array but stored as a map.
|
||||||
|
*/
|
||||||
|
bool ClearUnknownKeys() const { return m_clearUnknownKeys; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::string m_path; ///< Address of the param in the json files
|
std::string m_path; ///< Address of the param in the json files
|
||||||
bool m_readOnly; ///< Indicates param pointer should never be overwritten
|
bool m_readOnly; ///< Indicates param pointer should never be overwritten
|
||||||
|
bool m_clearUnknownKeys; ///< Keys should be cleared from source rather than merged.
|
||||||
|
///< This is useful for things that are semantically an
|
||||||
|
///< array but stored as a map, such as textVars.
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -575,11 +585,13 @@ class PARAM_WXSTRING_MAP : public PARAM_BASE
|
||||||
public:
|
public:
|
||||||
PARAM_WXSTRING_MAP( const std::string& aJsonPath, std::map<wxString, wxString>* aPtr,
|
PARAM_WXSTRING_MAP( const std::string& aJsonPath, std::map<wxString, wxString>* aPtr,
|
||||||
std::initializer_list<std::pair<const wxString, wxString>> aDefault,
|
std::initializer_list<std::pair<const wxString, wxString>> aDefault,
|
||||||
bool aReadOnly = false ) :
|
bool aReadOnly = false, bool aArrayBehavior = false ) :
|
||||||
PARAM_BASE( aJsonPath, aReadOnly ),
|
PARAM_BASE( aJsonPath, aReadOnly ),
|
||||||
m_ptr( aPtr ),
|
m_ptr( aPtr ),
|
||||||
m_default( aDefault )
|
m_default( aDefault )
|
||||||
{ }
|
{
|
||||||
|
m_clearUnknownKeys = aArrayBehavior;
|
||||||
|
}
|
||||||
|
|
||||||
void Load( JSON_SETTINGS* aSettings, bool aResetIfMissing = true ) const override;
|
void Load( JSON_SETTINGS* aSettings, bool aResetIfMissing = true ) const override;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue