1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #ifndef DATA_HANDLER_H
16 #define DATA_HANDLER_H
17
18 #include "unified_data.h"
19 #include "tlv_util.h"
20 #include "types_export.h"
21
22 namespace OHOS::UDMF {
23 using namespace DistributedDB;
24
25 class DataHandler {
26 public:
27 static Status MarshalToEntries(const UnifiedData &unifiedData, std::vector<Entry> &entries);
28 static Status UnmarshalEntries(const std::string &key, const std::vector<Entry> &entries,
29 UnifiedData &unifiedData);
30 template <typename T>
31 static Status MarshalToEntries(const T &data, Value &value, TAG tag);
32 template <typename T>
33 static Status UnmarshalEntries(const Value &value, T &data, TAG tag);
34
35 private:
36 static Status BuildEntries(const std::vector<std::shared_ptr<UnifiedRecord>> &records,
37 const std::string &unifiedKey, std::vector<Entry> &entries);
38 static Status UnmarshalEntryItem(UnifiedData &unifiedData, const std::vector<Entry> &entries,
39 const std::string &key, std::map<std::string, std::shared_ptr<UnifiedRecord>> &records,
40 std::map<std::string, std::map<std::string, ValueType>> &innerEntries);
41 };
42
43 template <typename T>
MarshalToEntries(const T & data,Value & value,TAG tag)44 Status DataHandler::MarshalToEntries(const T &data, Value &value, TAG tag)
45 {
46 auto tlvObject = TLVObject(value);
47 if (!TLVUtil::Writing(data, tlvObject, tag)) {
48 return E_WRITE_PARCEL_ERROR;
49 }
50 return E_OK;
51 }
52
53 template <typename T>
UnmarshalEntries(const Value & value,T & data,TAG tag)54 Status DataHandler::UnmarshalEntries(const Value &value, T &data, TAG tag)
55 {
56 auto tlvObject = TLVObject(const_cast<std::vector<uint8_t> &>(value));
57 if (!TLVUtil::ReadTlv(data, tlvObject, tag)) {
58 return E_READ_PARCEL_ERROR;
59 }
60 return E_OK;
61 }
62 } // namespace UDMF::OHOS
63 #endif // DATA_HANDLER_H