From efd9b91068be2a34db91fa94b6c0933014cf5984 Mon Sep 17 00:00:00 2001 From: Kamil Cudnik Date: Thu, 27 Oct 2016 14:08:02 -0700 Subject: [PATCH] Use json array in consumer-producer to save fields order --- common/consumertable.cpp | 7 ++++--- common/json.cpp | 14 +++++++------- tests/redis_ut.cpp | 15 +-------------- 3 files changed, 12 insertions(+), 24 deletions(-) diff --git a/common/consumertable.cpp b/common/consumertable.cpp index 347b13f30..9af10c0ab 100644 --- a/common/consumertable.cpp +++ b/common/consumertable.cpp @@ -65,10 +65,11 @@ void ConsumerTable::pop(KeyOpFieldsValuesTuple &kco) "local ret = {key, op}\n" "local jj = cjson.decode(value)\n" + "local size = #jj\n" - "for k,v in pairs(jj) do\n" - " table.insert(ret, k)\n" - " table.insert(ret, v)\n" + "for idx=1,size,2 do\n" + " table.insert(ret, jj[idx])\n" + " table.insert(ret, jj[idx+1])\n" "end\n" "if op == 'get' or op == 'getresponse' then\n" diff --git a/common/json.cpp b/common/json.cpp index 21b7086e3..e618a2e73 100644 --- a/common/json.cpp +++ b/common/json.cpp @@ -10,11 +10,13 @@ namespace swss { string JSon::buildJson(const vector &fv) { - nlohmann::json j; + nlohmann::json j = nlohmann::json::array(); + // we use array to save order for (auto &i : fv) { - j[fvField(i)] = fvValue(i); + j.push_back(fvField(i)); + j.push_back(fvValue(i)); } return j.dump(); @@ -26,12 +28,10 @@ void JSon::readJson(const string &jsonstr, vector &fv) FieldValueTuple e; - std::map m = j; - - for (auto&& kv : m) + for (size_t i = 0; i < j.size(); i+=2) { - fvField(e) = kv.first; - fvValue(e) = kv.second; + fvField(e) = j[i]; + fvValue(e) = j[i+1]; fv.push_back(e); } } diff --git a/tests/redis_ut.cpp b/tests/redis_ut.cpp index 1c645992c..463689189 100644 --- a/tests/redis_ut.cpp +++ b/tests/redis_ut.cpp @@ -62,20 +62,7 @@ void validateFields(const string& key, const vector& f) int i = 0; EXPECT_EQ(maxNumOfFields, f.size()); - // since we are using cjson in lua script - // retrived fields order is different than producer - // since value is passed as json, order does not matter - // but we need to compensate it here - - auto copy = f; - - std::sort(copy.begin(), copy.end(), - [](const FieldValueTuple & a, const FieldValueTuple & b) -> bool - { - return readNumberAtEOL(fvField(a)) < readNumberAtEOL(fvField(b)); - }); - - for (auto fv : copy) + for (auto fv : f) { EXPECT_EQ(i, readNumberAtEOL(fvField(fv))); EXPECT_EQ(i, readNumberAtEOL(fvValue(fv)));