1 /*
2  * Copyright (c) 2022 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 "metadata/strategy_meta_data.h"
16 
17 #include "utils/constant.h"
18 namespace OHOS::DistributedData {
Marshal(json & node) const19 bool StrategyMeta::Marshal(json &node) const
20 {
21     bool ret = true;
22     ret = SetValue(node[GET_NAME(devId)], devId) && ret;
23     ret = SetValue(node[GET_NAME(userId)], userId) && ret;
24     ret = SetValue(node[GET_NAME(bundleName)], bundleName) && ret;
25     ret = SetValue(node[GET_NAME(instanceId)], instanceId) && ret;
26     ret = SetValue(node[GET_NAME(storeId)], storeId) && ret;
27     ret = SetValue(node[GET_NAME(capabilityEnabled)], capabilityEnabled) && ret;
28     ret = SetValue(node[GET_NAME(capabilityRange)], capabilityRange) && ret;
29     return ret;
30 }
31 
Unmarshal(const json & node)32 bool StrategyMeta::Unmarshal(const json &node)
33 {
34     bool ret = true;
35     ret = GetValue(node, GET_NAME(devId), devId) && ret;
36     ret = GetValue(node, GET_NAME(userId), userId) && ret;
37     ret = GetValue(node, GET_NAME(bundleName), bundleName) && ret;
38     ret = GetValue(node, GET_NAME(instanceId), instanceId) && ret;
39     ret = GetValue(node, GET_NAME(storeId), storeId) && ret;
40     ret = GetValue(node, GET_NAME(capabilityEnabled), capabilityEnabled) && ret;
41     ret = GetValue(node, GET_NAME(capabilityRange), capabilityRange) && ret;
42     return ret;
43 }
44 
StrategyMeta(const std::string & devId,const std::string & userId,const std::string & bundleName,const std::string & storeId)45 StrategyMeta::StrategyMeta(
46     const std::string &devId, const std::string &userId, const std::string &bundleName, const std::string &storeId)
47     : devId(devId), userId(userId), bundleName(bundleName), storeId(storeId)
48 {
49 }
50 
IsEffect() const51 bool StrategyMeta::IsEffect() const
52 {
53     return (!capabilityRange.localLabel.empty()) && (!capabilityRange.remoteLabel.empty());
54 }
55 
GetKey()56 std::string StrategyMeta::GetKey()
57 {
58     if (instanceId == 0) {
59         return Constant::Join(PREFIX, Constant::KEY_SEPARATOR, { devId, userId, "default", bundleName, storeId });
60     }
61     return Constant::Join(PREFIX, Constant::KEY_SEPARATOR,
62         { devId, userId, "default", bundleName, storeId, std::to_string(instanceId) });
63 }
64 
GetPrefix(const std::initializer_list<std::string> & fields)65 std::string StrategyMeta::GetPrefix(const std::initializer_list<std::string> &fields)
66 {
67     return Constant::Join(PREFIX, Constant::KEY_SEPARATOR, fields);
68 }
69 } // namespace OHOS::DistributedData
70