• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "unified_meta.h"
17 
18 #include "unified_key.h"
19 
20 namespace OHOS {
21 namespace UDMF {
IsValidType(int32_t value)22 bool UnifiedDataUtils::IsValidType(int32_t value)
23 {
24     return value >= ENTITY && value < UD_BUTT;
25 }
26 
IsValidIntention(int32_t value)27 bool UnifiedDataUtils::IsValidIntention(int32_t value)
28 {
29     return value > UD_INTENTION_BASE && value < UD_INTENTION_BUTT;
30 }
31 
GetVariantSize(UDVariant & variant)32 size_t UnifiedDataUtils::GetVariantSize(UDVariant &variant)
33 {
34     auto int32Value = std::get_if<int32_t>(&variant);
35     if (int32Value != nullptr) {
36         return sizeof(std::get<int32_t>(variant));
37     }
38     auto int64Value = std::get_if<int64_t>(&variant);
39     if (int64Value != nullptr) {
40         return sizeof(std::get<int64_t>(variant));
41     }
42     auto boolValue = std::get_if<bool>(&variant);
43     if (boolValue != nullptr) {
44         return sizeof(std::get<bool>(variant));
45     }
46     auto doubleValue = std::get_if<double>(&variant);
47     if (doubleValue != nullptr) {
48         return sizeof(std::get<double>(variant));
49     }
50     auto strValue = std::get_if<std::string>(&variant);
51     if (strValue != nullptr) {
52         return std::get<std::string>(variant).size();
53     }
54     auto vecValue = std::get_if<std::vector<uint8_t>>(&variant);
55     if (vecValue != nullptr) {
56         return std::get<std::vector<uint8_t>>(variant).size();
57     }
58     return 0;
59 }
60 
GetDetailsSize(UDDetails & details)61 size_t UnifiedDataUtils::GetDetailsSize(UDDetails &details)
62 {
63     size_t total = 0;
64     for (std::pair<std::string, UDVariant> prop : details) {
65         total += prop.first.size();
66         total += GetVariantSize(prop.second);
67     }
68     return total;
69 }
70 
IsPersist(const Intention & intention)71 bool UnifiedDataUtils::IsPersist(const Intention &intention)
72 {
73     return intention >= UD_INTENTION_DATA_HUB && intention < UD_INTENTION_BUTT;
74 }
75 
IsPersist(const std::string & intention)76 bool UnifiedDataUtils::IsPersist(const std::string &intention)
77 {
78     return IsPersist(GetIntentionByString(intention));
79 }
80 
GetIntentionByString(const std::string & intention)81 Intention UnifiedDataUtils::GetIntentionByString(const std::string &intention)
82 {
83     for (const auto &it : UD_INTENTION_MAP) {
84         if (it.second == intention) {
85             return static_cast<Intention>(it.first);
86         }
87     }
88     return UD_INTENTION_BUTT;
89 }
90 
IsValidOptions(const std::string & key,std::string & intention)91 bool UnifiedDataUtils::IsValidOptions(const std::string &key, std::string &intention)
92 {
93     UnifiedKey unifiedKey(key);
94     auto isValidKey = unifiedKey.IsValid();
95     if (key.empty() && IsPersist(intention)) {
96         return true;
97     }
98     if (intention.empty() && isValidKey && IsPersist(unifiedKey.intention)) {
99         return true;
100     }
101     if (isValidKey && unifiedKey.intention == intention && IsPersist(intention)) {
102         intention = "";
103         return true;
104     }
105     return false;
106 }
107 } // namespace UDMF
108 } // namespace OHOS