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

Add utility for string and redis #434

Merged
merged 18 commits into from
Dec 29, 2020
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
3 changes: 2 additions & 1 deletion common/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ libswsscommon_la_SOURCES = \
exec.cpp \
subscriberstatetable.cpp \
timestamp.cpp \
warm_restart.cpp
warm_restart.cpp \
redisutility.cpp

libswsscommon_la_CXXFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(LIBNL_CFLAGS)
libswsscommon_la_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(LIBNL_CPPFLAGS)
Expand Down
56 changes: 56 additions & 0 deletions common/boolean.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#pragma once

#include <iostream>
#include <ios>

namespace swss
{
class Boolean
{
public:
Boolean() = default;
Boolean(bool boolean) : m_boolean(boolean)
{
}
operator bool() const
Copy link
Contributor

@qiluo-msft qiluo-msft Dec 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

operator bool() [](start = 4, length = 15)

You need another cast operator

    operator bool&()
    {
        return m_boolean;
    }
``` #Closed

{
return m_boolean;
}
operator bool&()
{
return m_boolean;
}
protected:
bool m_boolean;
};

class AlphaBoolean : public Boolean
{
public:
AlphaBoolean() = default;
AlphaBoolean(bool boolean) : Boolean(boolean)
{
}
};

static inline std::ostream &operator<<(std::ostream &out, const AlphaBoolean &b)
{
return out << std::boolalpha << (bool)(b);
}

static inline std::istream &operator>>(std::istream &in, AlphaBoolean &b)
{
return in >> std::boolalpha >> (bool &)(b);
}

static inline std::ostream &operator<<(std::ostream &out, const Boolean &b)
{
return out << (bool)(b);
}

static inline std::istream &operator>>(std::istream &in, Boolean &b)
{
return in >> (bool &)(b);
}

}
33 changes: 33 additions & 0 deletions common/redisutility.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "redisutility.h"
#include "stringutility.h"

#include <boost/algorithm/string.hpp>


boost::optional<std::string> swss::fvsGetValue(
const std::vector<FieldValueTuple> &fvt,
const std::string &field,
bool case_insensitive)
{
boost::optional<std::string> ret;

for (auto itr = fvt.begin(); itr != fvt.end(); itr++)
{
bool is_equal = false;
if (case_insensitive)
{
is_equal = boost::iequals(fvField(*itr), field);
}
else
{
is_equal = (fvField(*itr) == field);
}
if (is_equal)
{
ret = fvValue(*itr);
break;
}
}

return ret;
}
16 changes: 16 additions & 0 deletions common/redisutility.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include "rediscommand.h"

#include <boost/optional.hpp>

#include <vector>
#include <algorithm>

namespace swss
{
boost::optional<std::string> fvsGetValue(
const std::vector<FieldValueTuple> &fvt,
const std::string &field,
bool case_insensitive = false);
}
136 changes: 136 additions & 0 deletions common/stringutility.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#pragma once

#include "logger.h"
#include "tokenize.h"
#include "schema.h"

#include <boost/lexical_cast.hpp>
#include <boost/algorithm/hex.hpp>

#include <inttypes.h>
#include <algorithm>
#include <iterator>
#include <vector>
#include <string>
#include <sstream>
#include <cctype>

namespace swss {

template<typename T>
static inline void lexical_convert(const std::string &str, T &t)
{
t = boost::lexical_cast<T>(str);
}

namespace lexical_convert_detail
{

template <typename T, typename... Args>
void lexical_convert(
std::vector<std::string>::const_iterator begin,
std::vector<std::string>::const_iterator end,
T &t)
{
if (begin == end)
{
SWSS_LOG_THROW("Insufficient corpus");
}
auto cur_itr = begin++;
if (begin != end)
{
SWSS_LOG_THROW("Too much corpus");
}
swss::lexical_convert(*cur_itr, t);
}

template <typename T, typename... Args>
void lexical_convert(
std::vector<std::string>::const_iterator begin,
std::vector<std::string>::const_iterator end,
T &t,
Args &... args)
{
if (begin == end)
{
SWSS_LOG_THROW("Insufficient corpus");
}
swss::lexical_convert(*(begin++), t);
return lexical_convert(begin, end, args...);
}

}

template <typename T, typename... Args>
void lexical_convert(const std::vector<std::string> &strs, T &t, Args &... args)
{
lexical_convert_detail::lexical_convert(strs.begin(), strs.end(), t, args...);
}

namespace join_detail
{

template <typename T>
void join(std::ostringstream &ostream, char, const T &t)
{
ostream << t;
}

template <typename T, typename... Args>
void join(std::ostringstream &ostream, char delimiter, const T &t, const Args &... args)
{
ostream << t << delimiter;
join(ostream, delimiter, args...);
}

}

template <typename T, typename... Args>
static inline std::string join(char delimiter, const T &t, const Args &... args)
{
std::ostringstream ostream;
join_detail::join(ostream, delimiter, t, args...);
return ostream.str();
}

static inline bool hex_to_binary(const std::string &hex_str, std::uint8_t *buffer, size_t buffer_length)
{
if (hex_str.length() != (buffer_length * 2))
{
SWSS_LOG_DEBUG("Buffer length isn't sufficient");
return false;
}

try
{
boost::algorithm::unhex(hex_str, buffer);
}
catch(const boost::algorithm::non_hex_input &e)
{
SWSS_LOG_DEBUG("Invalid hex string %s", hex_str.c_str());
return false;
}

return true;
}

template<typename T>
static inline void hex_to_binary(const std::string &s, T &value)
{
return hex_to_binary(s, &value, sizeof(T));
}

static inline std::string binary_to_hex(const void *buffer, size_t length)
{
std::string s;
auto buf = static_cast<const std::uint8_t *>(buffer);

boost::algorithm::hex(
buf,
buf + length,
std::back_inserter<std::string>(s));

return s;
}

}
3 changes: 3 additions & 0 deletions tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ tests_SOURCES = redis_ut.cpp \
logger_ut.cpp \
redis_multi_ns_ut.cpp \
fdb_flush.cpp \
stringutility_ut.cpp \
redisutility_ut.cpp \
boolean_ut.cpp \
main.cpp

tests_CFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_GTEST) $(LIBNL_CFLAGS)
Expand Down
80 changes: 80 additions & 0 deletions tests/boolean_ut.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include "common/boolean.h"

#include "gtest/gtest.h"

#include <sstream>

TEST(BOOLEAN, boolean)
{
swss::Boolean b;

b = true;
std::ostringstream ost;
ost << b;
EXPECT_EQ(ost.str(), "1");

b = false;
std::ostringstream osf;
osf << b;
EXPECT_EQ(osf.str(), "0");

b = false;
std::istringstream ist("1");
ist >> b;
EXPECT_TRUE(b);
EXPECT_FALSE(ist.fail());

b = true;
std::istringstream isf("0");
isf >> b;
EXPECT_FALSE(b);
EXPECT_FALSE(isf.fail());
}

TEST(BOOLEAN, alpha_boolean)
{
swss::AlphaBoolean bt(true);
swss::AlphaBoolean bf(false);

EXPECT_TRUE(bt);
EXPECT_FALSE(bf);

std::ostringstream ost;
ost << bt;
EXPECT_EQ(ost.str(), "true");

std::ostringstream osf;
osf << bf;
EXPECT_EQ(osf.str(), "false");

std::istringstream is;

bt = false;
is.str("true");
is >> bt;
EXPECT_TRUE(bt);
EXPECT_FALSE(is.fail());
is.clear();

bt = false;
is.str("true123");
int i;
is >> bt >> i;
EXPECT_TRUE(bt);
EXPECT_EQ(i, 123);
EXPECT_FALSE(is.fail());
is.clear();

bf = true;
is.str("false");
is >> bf;
EXPECT_FALSE(bf);
EXPECT_FALSE(is.fail());
is.clear();

bf = true;
is.str("abdef");
EXPECT_FALSE(is >> bt);
EXPECT_TRUE(is.fail());
is.clear();
}
32 changes: 32 additions & 0 deletions tests/redisutility_ut.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "common/redisutility.h"
#include "common/stringutility.h"
#include "common/boolean.h"

#include "gtest/gtest.h"

TEST(REDISUTILITY, fvsGetValue)
{
std::vector<swss::FieldValueTuple> fvt;
fvt.push_back(std::make_pair("int", "123"));
fvt.push_back(std::make_pair("bool", "true"));
fvt.push_back(std::make_pair("string", "name"));

auto si = swss::fvsGetValue(fvt, "int");
EXPECT_TRUE(si);
auto sb = swss::fvsGetValue(fvt, "bool");
EXPECT_TRUE(sb);
auto ss = swss::fvsGetValue(fvt, "string");
EXPECT_TRUE(ss);

int i;
swss::AlphaBoolean b;
std::string s;
ASSERT_NO_THROW(swss::lexical_convert({*si, *sb, *ss}, i, b, s));
EXPECT_EQ(i, 123);
EXPECT_EQ(b, true);
EXPECT_EQ(s, "name");

EXPECT_FALSE(swss::fvsGetValue(fvt, "Int"));
EXPECT_TRUE(swss::fvsGetValue(fvt, "Int", true));
EXPECT_FALSE(swss::fvsGetValue(fvt, "double"));
}
Loading