Remove unnecessary conversion from FilePath to string.

PiperOrigin-RevId: 768137824
This commit is contained in:
Francis Tsui
2025-06-06 10:53:33 -07:00
committed by Copybara-Service
parent 18637b63fa
commit 56d7ca1c2e
4 changed files with 17 additions and 21 deletions
@@ -48,8 +48,7 @@ PreferencesManager::PreferencesManager(FilePath file_path) {
}
path->append(file_path);
preferences_repository_ =
std::make_unique<PreferencesRepository>(path->ToString());
preferences_repository_ = std::make_unique<PreferencesRepository>(*path);
value_ = preferences_repository_->LoadPreferences();
}
@@ -69,15 +69,14 @@ json PreferencesRepository::LoadPreferences() {
bool PreferencesRepository::SavePreferences(json preferences) {
absl::MutexLock lock(&mutex_);
try {
FilePath path{path_};
if (!Files::FileExists(path) && !Files::CreateDirectories(path)) {
if (!Files::FileExists(path_) && !Files::CreateDirectories(path_)) {
LOG(ERROR) << "Failed to create preferences path.";
return false;
}
FilePath full_name = path;
FilePath full_name = path_;
full_name.append(FilePath(kPreferencesFileName));
FilePath full_name_backup = path;
FilePath full_name_backup = path_;
full_name_backup.append(FilePath(kPreferencesBackupFileName));
// Create a backup without moving the bytes on disk
@@ -114,10 +113,9 @@ bool PreferencesRepository::SavePreferences(json preferences) {
}
std::optional<json> PreferencesRepository::AttemptLoad() {
FilePath path{path_};
FilePath full_name = path;
FilePath full_name = path_;
full_name.append(FilePath(kPreferencesFileName));
if (!Files::DirectoryExists(path) || !Files::FileExists(full_name)) {
if (!Files::DirectoryExists(path_) || !Files::FileExists(full_name)) {
return std::nullopt;
}
@@ -146,10 +144,9 @@ std::optional<json> PreferencesRepository::AttemptLoad() {
}
std::optional<json> PreferencesRepository::RestoreFromBackup() {
FilePath path{path_};
FilePath full_name = path;
FilePath full_name = path_;
full_name.append(FilePath(kPreferencesFileName));
FilePath full_name_backup = path;
FilePath full_name_backup = path_;
full_name_backup.append(FilePath(kPreferencesBackupFileName));
if (!Files::FileExists(full_name_backup)) {
@@ -16,20 +16,19 @@
#define PLATFORM_IMPLEMENTATION_WINDOWS_PREFERENCES_REPOSITORY_H_
#include <optional>
#include <string>
#include "absl/base/thread_annotations.h"
#include "absl/strings/string_view.h"
#include "absl/synchronization/mutex.h"
#include "nlohmann/json.hpp"
#include "nlohmann/json_fwd.hpp"
#include "internal/base/file_path.h"
namespace nearby {
namespace windows {
class PreferencesRepository {
public:
explicit PreferencesRepository(absl::string_view path) : path_(path) {}
explicit PreferencesRepository(const FilePath& path) : path_(path) {}
nlohmann::json LoadPreferences() ABSL_LOCKS_EXCLUDED(&mutex_);
bool SavePreferences(nlohmann::json preferences) ABSL_LOCKS_EXCLUDED(&mutex_);
@@ -39,7 +38,7 @@ class PreferencesRepository {
private:
absl::Mutex mutex_;
const std::string path_;
const FilePath path_;
};
} // namespace windows
@@ -36,7 +36,8 @@ constexpr char kPreferencesBackupFileName[] = "preferences_bak.json";
constexpr char kPreferencesPath[] = "Google/Nearby/Sharing";
TEST(PreferencesRepository, LoadWithBadPath) {
PreferencesRepository preferences_repository{"c:\\users\\a\\b\\c\\d\\e\\f"};
PreferencesRepository preferences_repository{
FilePath{"c:\\users\\a\\b\\c\\d\\e\\f"}};
json result = preferences_repository.LoadPreferences();
EXPECT_TRUE(result.empty());
}
@@ -56,7 +57,7 @@ TEST(PreferencesRepository, RecoverFromBadPreferences) {
pref_file << "\"Bad top level object\"";
pref_file.close();
PreferencesRepository preferences_repository{full_path.ToString()};
PreferencesRepository preferences_repository{full_path};
EXPECT_EQ(preferences_repository.LoadPreferences(), json::object());
}
@@ -71,7 +72,7 @@ TEST(PreferencesRepository, SaveAndLoadPreferences) {
Files::RemoveFile(full_name);
}
PreferencesRepository preferences_repository{full_path.ToString()};
PreferencesRepository preferences_repository{full_path};
json data;
data["key1"] = "value1";
data["key2"] = "value2";
@@ -100,7 +101,7 @@ TEST(PreferencesRepository, LoadFromBackup) {
Files::RemoveFile(full_name_backup);
}
PreferencesRepository preferences_repository{full_path.ToString()};
PreferencesRepository preferences_repository{full_path};
json data;
data["key1"] = "value1";
data["key2"] = "value2";
@@ -137,7 +138,7 @@ TEST(PreferencesRepository, RecoverFromCorruption) {
Files::RemoveFile(full_name_backup);
}
PreferencesRepository preferences_repository{full_path.ToString()};
PreferencesRepository preferences_repository{full_path};
json data;
data["key1"] = "value1";
data["key2"] = "value2";