Add extra line about src files are part of RIC platform project
[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 /*
18  * This source code is part of the near-RT RIC (RAN Intelligent Controller)
19  * platform project (RICP).
20 */
21
22 #include "private/redis/asyncredisreply.hpp"
23
24 using namespace shareddatalayer;
25 using namespace shareddatalayer::redis;
26
27 AsyncRedisReply::AsyncRedisReply():
28     type(Type::NIL),
29     integer(0),
30     dataItem { { }, 0 }
31 {
32 }
33
34 AsyncRedisReply::AsyncRedisReply(const redisReply& rr):
35     integer(0),
36     dataItem { { }, 0 },
37     typeMap { { REDIS_REPLY_NIL, Type::NIL }, { REDIS_REPLY_INTEGER, Type::INTEGER },
38               { REDIS_REPLY_STATUS, Type::STATUS }, { REDIS_REPLY_STRING, Type::STRING },
39               { REDIS_REPLY_ARRAY, Type::ARRAY } }
40 {
41     auto res(typeMap.find(rr.type));
42     if (res != typeMap.end())
43     {
44         type = res->second;
45         parseReply(rr);
46     }
47 }
48
49 AsyncRedisReply::Type AsyncRedisReply::getType() const
50 {
51     return type;
52 }
53
54 long long AsyncRedisReply::getInteger() const
55 {
56     return integer;
57 }
58
59 const AsyncRedisReply::DataItem* AsyncRedisReply::getString() const
60 {
61     return &dataItem;
62 }
63
64 const AsyncRedisReply::ReplyVector* AsyncRedisReply::getArray() const
65 {
66     return &replyVector;
67 }
68
69 void AsyncRedisReply::parseReply(const redisReply& rr)
70 {
71     switch (type)
72     {
73         case Type::INTEGER:
74             integer = rr.integer;
75             break;
76         case Type::STATUS:
77         case Type::STRING:
78             dataItem.str = std::string(rr.str, static_cast<size_t>(rr.len));
79             dataItem.len = rr.len;
80             break;
81         case Type::ARRAY:
82             parseArray(rr);
83             break;
84         case Type::NIL:
85         default:
86             break;
87     }
88 }
89
90 void AsyncRedisReply::parseArray(const redisReply& rr)
91 {
92     for (auto i(0U); i < rr.elements; ++i)
93         replyVector.push_back(std::make_shared<AsyncRedisReply>(*rr.element[i]));
94 }