Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: double value is converted to int for DConfig #451

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/dconfigfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1288,12 +1288,22 @@ class Q_DECL_HIDDEN DConfigFilePrivate : public DObjectPrivate {
return cache->setValue(key, value, configMeta->serial(key), cache->uid(), appid);

// convert copy to meta's type, it promises `setValue` don't change meta's type.
// canConvert isn't explicit, e.g: QString is also can convert to double.
auto copy = value;
if (!copy.convert(metaValue.metaType())) {
qCWarning(cfLog) << "check type error, meta type is " << metaValue.metaType().name()
<< ", and now type is " << value.metaType().name();
return false;
}

// TODO it's a bug of qt, MetaType of 1.0 is qlonglong instead of double in json file.
static const QVector<QMetaType> filterConvertType {
QMetaType{QMetaType::Double}
};
// reset to origin value.
if (filterConvertType.contains(value.metaType())) {
copy = value;
}
#else
if (metaValue.type() == value.type())
return cache->setValue(key, value, configMeta->serial(key), cache->uid(), appid);
Expand All @@ -1304,6 +1314,13 @@ class Q_DECL_HIDDEN DConfigFilePrivate : public DObjectPrivate {
<< ", and now type is " << value.type();
return false;
}

static const QVector<QVariant::Type> filterConvertType {
QVariant::Double
};
if (filterConvertType.contains(value.type())) {
copy = value;
}
#endif

return cache->setValue(key, copy, configMeta->serial(key), cache->uid(), appid);
Expand Down
8 changes: 8 additions & 0 deletions tests/data/dconf-example.meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@
"permissions": "readwrite",
"visibility": "public"
},
"numberDouble": {
"value": 1.0,
"serial": 0,
"flags": ["global"],
"name": "double value type",
"permissions": "readwrite",
"visibility": "public"
},
"array": {
"value": ["value1", "value2"],
"serial": 0,
Expand Down
5 changes: 5 additions & 0 deletions tests/ut_dconfigfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@
ASSERT_FALSE(config.setValue("number", "1ab", "test", userCache.get()));
ASSERT_EQ(config.value("number", userCache.get()).type(), type);
}
{
const auto type = config.value("numberDouble", userCache.get()).type();

Check warning on line 128 in tests/ut_dconfigfile.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Variable 'type' is assigned a value that is never used.
ASSERT_TRUE(config.setValue("numberDouble", 1.2, "test", userCache.get()));
ASSERT_EQ(config.value("numberDouble", userCache.get()), 1.2);
}
{
const auto type = config.value("array", userCache.get()).type();
const QStringList array{"value1", "value2"};
Expand Down
Loading