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 #include "cloud/cloud_extra_data.h"
16 #include "cloud/cloud_config_manager.h"
17 
18 namespace OHOS::DistributedData {
Marshal(Serializable::json & node) const19 bool Context::Marshal(Serializable::json &node) const
20 {
21     SetValue(node[GET_NAME(traceId)], traceId);
22     SetValue(node[GET_NAME(prepareTraceId)], prepareTraceId);
23     return true;
24 }
25 
Unmarshal(const Serializable::json & node)26 bool Context::Unmarshal(const Serializable::json &node)
27 {
28     GetValue(node, GET_NAME(traceId), traceId);
29     GetValue(node, GET_NAME(prepareTraceId), prepareTraceId);
30     return true;
31 }
32 
Marshal(Serializable::json & node) const33 bool ExtensionInfo::Marshal(Serializable::json &node) const
34 {
35     SetValue(node[GET_NAME(accountId)], accountId);
36     SetValue(node[GET_NAME(bundleName)], bundleName);
37     SetValue(node[GET_NAME(containerName)], containerName);
38     SetValue(node[GET_NAME(databaseScopes)], databaseScopes);
39     SetValue(node[GET_NAME(recordTypes)], recordTypes);
40     return true;
41 }
42 
Unmarshal(const Serializable::json & node)43 bool ExtensionInfo::Unmarshal(const Serializable::json &node)
44 {
45     GetValue(node, GET_NAME(accountId), accountId);
46     GetValue(node, GET_NAME(bundleName), bundleName);
47     bundleName = CloudConfigManager::GetInstance().ToLocal(bundleName);
48     GetValue(node, GET_NAME(containerName), containerName);
49     GetValue(node, GET_NAME(databaseScopes), databaseScopes);
50     if (!Unmarshall(databaseScopes, scopes)) {
51         return false;
52     }
53     GetValue(node, GET_NAME(recordTypes), recordTypes);
54     if (!Unmarshall(recordTypes, tables)) {
55         return false;
56     }
57     std::string data;
58     GetValue(node, GET_NAME(context), data);
59     if (data.empty()) {
60         return true;
61     }
62     return context.Unmarshall(data);
63 }
64 
Marshal(Serializable::json & node) const65 bool ExtraData::Marshal(Serializable::json &node) const
66 {
67     SetValue(node[GET_NAME(header)], header);
68     SetValue(node[GET_NAME(data)], data);
69     return true;
70 }
71 
Unmarshal(const Serializable::json & node)72 bool ExtraData::Unmarshal(const Serializable::json &node)
73 {
74     GetValue(node, GET_NAME(header), header);
75     GetValue(node, GET_NAME(data), data);
76     return info.Unmarshall(data);
77 }
78 
isPrivate() const79 bool ExtraData::isPrivate() const
80 {
81     return (std::find(info.scopes.begin(), info.scopes.end(), PRIVATE_TABLE) != info.scopes.end());
82 }
83 
isShared() const84 bool ExtraData::isShared() const
85 {
86     return (std::find(info.scopes.begin(), info.scopes.end(), SHARED_TABLE) != info.scopes.end());
87 }
88 } // namespace OHOS::DistributedData