Merge pull request #1443 from gportay/add-default-value-to-variant-helpers

[libcalamares] Add default value to variant helpers
This commit is contained in:
Adriaan de Groot 2020-07-03 16:12:34 -04:00 committed by GitHub
commit 0be6f63d2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 41 deletions

View File

@ -38,21 +38,19 @@ namespace CalamaresUtils
bool bool
getBool( const QVariantMap& map, const QString& key, bool d ) getBool( const QVariantMap& map, const QString& key, bool d )
{ {
bool result = d;
if ( map.contains( key ) ) if ( map.contains( key ) )
{ {
auto v = map.value( key ); auto v = map.value( key );
if ( v.type() == QVariant::Bool ) if ( v.type() == QVariant::Bool )
{ {
result = v.toBool(); return v.toBool();
} }
} }
return d;
return result;
} }
QString QString
getString( const QVariantMap& map, const QString& key ) getString( const QVariantMap& map, const QString& key, const QString& d )
{ {
if ( map.contains( key ) ) if ( map.contains( key ) )
{ {
@ -62,11 +60,11 @@ getString( const QVariantMap& map, const QString& key )
return v.toString(); return v.toString();
} }
} }
return QString(); return d;
} }
QStringList QStringList
getStringList( const QVariantMap& map, const QString& key ) getStringList( const QVariantMap& map, const QString& key, const QStringList& d )
{ {
if ( map.contains( key ) ) if ( map.contains( key ) )
{ {
@ -76,60 +74,53 @@ getStringList( const QVariantMap& map, const QString& key )
return v.toStringList(); return v.toStringList();
} }
} }
return QStringList(); return d;
} }
qint64 qint64
getInteger( const QVariantMap& map, const QString& key, qint64 d ) getInteger( const QVariantMap& map, const QString& key, qint64 d )
{ {
qint64 result = d;
if ( map.contains( key ) ) if ( map.contains( key ) )
{ {
auto v = map.value( key ); auto v = map.value( key );
result = v.toString().toLongLong(nullptr, 0); return v.toString().toLongLong(nullptr, 0);
} }
return d;
return result;
} }
quint64 quint64
getUnsignedInteger( const QVariantMap& map, const QString& key, quint64 u ) getUnsignedInteger( const QVariantMap& map, const QString& key, quint64 d )
{ {
quint64 result = u;
if ( map.contains( key ) ) if ( map.contains( key ) )
{ {
auto v = map.value( key ); auto v = map.value( key );
result = v.toString().toULongLong(nullptr, 0); return v.toString().toULongLong(nullptr, 0);
} }
return d;
return result;
} }
double double
getDouble( const QVariantMap& map, const QString& key, double d ) getDouble( const QVariantMap& map, const QString& key, double d )
{ {
double result = d;
if ( map.contains( key ) ) if ( map.contains( key ) )
{ {
auto v = map.value( key ); auto v = map.value( key );
if ( v.type() == QVariant::Int ) if ( v.type() == QVariant::Int )
{ {
result = v.toInt(); return v.toInt();
} }
else if ( v.type() == QVariant::Double ) else if ( v.type() == QVariant::Double )
{ {
result = v.toDouble(); return v.toDouble();
} }
} }
return d;
return result;
} }
QVariantMap QVariantMap
getSubMap( const QVariantMap& map, const QString& key, bool& success ) getSubMap( const QVariantMap& map, const QString& key, bool& success, const QVariantMap& d )
{ {
success = false; success = false;
if ( map.contains( key ) ) if ( map.contains( key ) )
{ {
auto v = map.value( key ); auto v = map.value( key );
@ -139,7 +130,7 @@ getSubMap( const QVariantMap& map, const QString& key, bool& success )
return v.toMap(); return v.toMap();
} }
} }
return QVariantMap(); return d;
} }
} // namespace CalamaresUtils } // namespace CalamaresUtils

View File

@ -33,45 +33,44 @@
namespace CalamaresUtils namespace CalamaresUtils
{ {
/** /**
* Get a bool value from a mapping with a given key; returns the default * Get a bool value from a mapping with a given key; returns @p d if no value.
* if no value is stored in the map.
*/ */
DLLEXPORT bool getBool( const QVariantMap& map, const QString& key, bool d ); DLLEXPORT bool getBool( const QVariantMap& map, const QString& key, bool d = false );
/** /**
* Get a string value from a mapping; returns empty QString if no value. * Get a string value from a mapping with a given key; returns @p d if no value.
*/ */
DLLEXPORT QString getString( const QVariantMap& map, const QString& key ); DLLEXPORT QString getString( const QVariantMap& map, const QString& key, const QString& d = QString() );
/** /**
* Get a string list from a mapping; returns empty list if no value. * Get a string list from a mapping with a given key; returns @p d if no value.
*/ */
DLLEXPORT QStringList getStringList( const QVariantMap& map, const QString& key ); DLLEXPORT QStringList getStringList( const QVariantMap& map, const QString& key, const QStringList& d = QStringList() );
/** /**
* Get an integer value from a mapping; returns @p d if no value. * Get an integer value from a mapping with a given key; returns @p d if no value.
*/ */
DLLEXPORT qint64 getInteger( const QVariantMap& map, const QString& key, qint64 d ); DLLEXPORT qint64 getInteger( const QVariantMap& map, const QString& key, qint64 d = 0);
/** /**
* Get an unsigned integer value from a mapping; returns @p u if no value. * Get an unsigned integer value from a mapping with a given key; returns @p d if no value.
*/ */
DLLEXPORT quint64 getUnsignedInteger( const QVariantMap& map, const QString& key, quint64 u ); DLLEXPORT quint64 getUnsignedInteger( const QVariantMap& map, const QString& key, quint64 d = 0 );
/** /**
* Get a double value from a mapping (integers are converted); returns @p d if no value. * Get a double value from a mapping with a given key (integers are converted); returns @p d if no value.
*/ */
DLLEXPORT double getDouble( const QVariantMap& map, const QString& key, double d ); DLLEXPORT double getDouble( const QVariantMap& map, const QString& key, double d = 0.0 );
/** /**
* Returns a sub-map (i.e. a nested map) from the given mapping with the * Returns a sub-map (i.e. a nested map) from a given mapping with a
* given key. @p success is set to true if the @p key exists * given key. @p success is set to true if the @p key exists
* in @p map and converts to a map, false otherwise. * in @p map and converts to a map, false otherwise.
* *
* Returns an empty map if there is no such key or it is not a map-value. * Returns @p d if there is no such key or it is not a map-value.
* (e.g. if @p success is false). * (e.g. if @p success is false).
*/ */
DLLEXPORT QVariantMap getSubMap( const QVariantMap& map, const QString& key, bool& success ); DLLEXPORT QVariantMap getSubMap( const QVariantMap& map, const QString& key, bool& success, const QVariantMap& d = QVariantMap() );
} // namespace CalamaresUtils } // namespace CalamaresUtils
#endif #endif