1 /*
2 * Copyright (c) 2023 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
16 #include "data_properties.h"
17 #include "string_ex.h"
18
19 namespace OHOS::RdbBMSAdapter {
20 static const std::string SEPARATOR = "/";
21 const std::string DataProperties::MODULE_SCOPE = "module";
22 const std::string DataProperties::APPLICATION_SCOPE = "application";
23 const std::string DataProperties::RDB_TYPE = "rdb";
24 const std::string DataProperties::PUBLISHED_DATA_TYPE = "publishedData";
25 static constexpr int PATH_SIZE = 2;
Marshal(json & node) const26 bool DataProperties::Marshal(json &node) const
27 {
28 SetValue(node[GET_NAME(path)], storeName + SEPARATOR + tableName);
29 SetValue(node[GET_NAME(scope)], scope);
30 SetValue(node[GET_NAME(type)], type);
31 return true;
32 }
33
Unmarshal(const json & node)34 bool DataProperties::Unmarshal(const json &node)
35 {
36 std::string path;
37 bool ret = GetValue(node, GET_NAME(path), path);
38 if (!ret) {
39 return false;
40 }
41 std::vector<std::string> splitPath;
42 SplitStr(path, SEPARATOR, splitPath);
43 if (splitPath.size() < PATH_SIZE) {
44 return false;
45 }
46
47 if (splitPath[0].empty() || splitPath[1].empty()) {
48 return false;
49 }
50 storeName = splitPath[0];
51 tableName = splitPath[1];
52 GetValue(node, GET_NAME(scope), scope);
53 GetValue(node, GET_NAME(type), type);
54 return true;
55 }
56 } // namespace OHOS::RdbBMSAdapter