cfa0fab34f1bebc8414a0bdbd968d567de1843a6
[ric-plt/sdl.git] / src / redis / asyncredisreply.cpp
1 /*
2    Copyright (c) 2018-2019 Nokia.
3
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7
8        http://www.apache.org/licenses/LICENSE-2.0
9
10    Unless required by applicable law or agreed to in writing, software
11    distributed under the License is distributed on an "AS IS" BASIS,
12    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    See the License for the specific language governing permissions and
14    limitations under the License.
15 */
16
17 #include "private/redis/asyncredisreply.hpp"
18
19 using namespace shareddatalayer;
20 using namespace shareddatalayer::redis;
21
22 AsyncRedisReply::AsyncRedisReply():
23     type(Type::NIL),
24     integer(0),
25     dataItem { { }, 0 }
26 {
27 }
28
29 AsyncRedisReply::AsyncRedisReply(const redisReply& rr):
30     integer(0),
31     dataItem { { }, 0 },
32     typeMap { { REDIS_REPLY_NIL, Type::NIL }, { REDIS_REPLY_INTEGER, Type::INTEGER },
33               { REDIS_REPLY_STATUS, Type::STATUS }, { REDIS_REPLY_STRING, Type::STRING },
34               { REDIS_REPLY_ARRAY, Type::ARRAY } }
35 {
36     auto res(typeMap.find(rr.type));
37     if (res != typeMap.end())
38     {
39         type = res->second;
40         parseReply(rr);
41     }
42 }
43
44 AsyncRedisReply::Type AsyncRedisReply::getType() const
45 {
46     return type;
47 }
48
49 long long AsyncRedisReply::getInteger() const
50 {
51     return integer;
52 }
53
54 const AsyncRedisReply::DataItem* AsyncRedisReply::getString() const
55 {
56     return &dataItem;
57 }
58
59 const AsyncRedisReply::ReplyVector* AsyncRedisReply::getArray() const
60 {
61     return &replyVector;
62 }
63
64 void AsyncRedisReply::parseReply(const redisReply& rr)
65 {
66     switch (type)
67     {
68         case Type::INTEGER:
69             integer = rr.integer;
70             break;
71         case Type::STATUS:
72         case Type::STRING:
73             dataItem.str = std::string(rr.str, static_cast<size_t>(rr.len));
74             dataItem.len = rr.len;
75             break;
76         case Type::ARRAY:
77             parseArray(rr);
78             break;
79         case Type::NIL:
80         default:
81             break;
82     }
83 }
84
85 void AsyncRedisReply::parseArray(const redisReply& rr)
86 {
87     for (auto i(0U); i < rr.elements; ++i)
88         replyVector.push_back(std::make_shared<AsyncRedisReply>(*rr.element[i]));
89 }