sapling/eden/fs/config/FieldConverter.h
Katie Mancini 1876c4e77b adding logging for selective paths
Summary:
Eden can sometimes unexpectedly fetch files from the server, and we want
to know why this is happening. This adds logging for the source of
data fetching in edens backing store to help obviate why these fetches
are happening.

This temporarily adds the logging in the HgQueuedBacking store to get a naive
version of logging rolled out sooner. Follow up changes will move this logging
closer to the data fetching itself if possible (in HgDatapackStore and HgImporter).

Reviewed By: chadaustin

Differential Revision: D22012572

fbshipit-source-id: b1b012ce4ee133fbacecd586b7365c3c5a5386df
2020-06-23 10:02:41 -07:00

128 lines
3.6 KiB
C++

/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This software may be used and distributed according to the terms of the
* GNU General Public License version 2.
*/
#pragma once
#include <chrono>
#include <map>
#include <optional>
#include <string>
#include <type_traits>
#include <folly/Expected.h>
#include <folly/Range.h>
#include "eden/fs/utils/PathFuncs.h"
namespace facebook {
namespace eden {
/**
* Converters are used to convert strings into ConfigSettings. For example,
* they are used to convert the string settings of configuration files.
*/
template <typename T, typename Enable = void>
class FieldConverter {};
template <>
class FieldConverter<AbsolutePath> {
public:
/**
* Convert the passed string piece to an AbsolutePath.
* @param convData is a map of conversion data that can be used by conversions
* method (for example $HOME value.)
* @return the converted AbsolutePath or an error message.
*/
folly::Expected<AbsolutePath, std::string> fromString(
folly::StringPiece value,
const std::map<std::string, std::string>& convData) const;
std::string toDebugString(const AbsolutePath& path) const {
return path.value();
}
};
template <>
class FieldConverter<std::string> {
public:
folly::Expected<std::string, std::string> fromString(
folly::StringPiece value,
const std::map<std::string, std::string>& convData) const;
std::string toDebugString(const std::string& value) const {
return value;
}
};
template <typename T>
class FieldConverter<std::optional<T>> {
public:
folly::Expected<std::optional<T>, std::string> fromString(
folly::StringPiece value,
const std::map<std::string, std::string>& convData) const {
return FieldConverter<T>{}.fromString(value, convData);
}
std::string toDebugString(const std::optional<T>& value) const {
return FieldConverter<T>{}.toDebugString(value.value_or(T{}));
}
};
/*
* FieldConverter implementation for integers, floating point, and bool types
*/
template <typename T>
class FieldConverter<
T,
typename std::enable_if<std::is_arithmetic<T>::value>::type> {
public:
/**
* Convert the passed string piece to a boolean.
* @param convData is a map of conversion data that can be used by conversions
* method (for example $HOME value.)
* @return the converted boolean or an error message.
*/
folly::Expected<T, std::string> fromString(
folly::StringPiece value,
const std::map<std::string, std::string>& /* convData */) const {
auto result = folly::tryTo<T>(value);
if (result.hasValue()) {
return result.value();
}
return folly::makeUnexpected<std::string>(
folly::makeConversionError(result.error(), value).what());
}
std::string toDebugString(T value) const {
if constexpr (std::is_same<T, bool>::value) {
return value ? "true" : "false";
}
return folly::to<std::string>(value);
}
};
/*
* FieldConverter implementation for nanoseconds.
*
* We could fairly easily implement this for other duration types, but we would
* have to decide what to do if the config specifies a more granular input
* value. e.g., if we wanted to parse a config field as `std::chrono::minutes`
* what should we do if the value in the config file was "10s"?
*/
template <>
class FieldConverter<std::chrono::nanoseconds> {
public:
folly::Expected<std::chrono::nanoseconds, std::string> fromString(
folly::StringPiece value,
const std::map<std::string, std::string>& convData) const;
std::string toDebugString(std::chrono::nanoseconds value) const;
};
} // namespace eden
} // namespace facebook