1 /*
2 * Copyright (c) 2025 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 #ifndef OS_ACCOUNT_INTERFACES_INNERKITS_ACCOUNT_IAM_NATIVE_INCLUDE_ACCOUNT_IAM_INFO_PARSE_H
17 #define OS_ACCOUNT_INTERFACES_INNERKITS_ACCOUNT_IAM_NATIVE_INCLUDE_ACCOUNT_IAM_INFO_PARSE_H
18
19 #include "account_log_wrapper.h"
20 #include "attributes.h"
21
22 namespace OHOS {
23 namespace AccountSA {
24 template <typename>
25 constexpr bool DEPENDENT_FALSE = false;
26
27 template<typename T>
GetOptionalValueFromAttributes(const std::vector<Attributes::AttributeKey> keys,const Attributes::AttributeKey & key,const Attributes & extraInfo,std::optional<T> & data)28 void GetOptionalValueFromAttributes(const std::vector<Attributes::AttributeKey> keys,
29 const Attributes::AttributeKey &key, const Attributes &extraInfo, std::optional<T> &data)
30 {
31 if (std::find(keys.begin(), keys.end(), key) == keys.end()) {
32 return;
33 }
34 if constexpr (std::is_same_v<T, int32_t>) {
35 int32_t value = -1;
36 if (extraInfo.GetInt32Value(key, value)) {
37 data = value;
38 return;
39 }
40 } else if constexpr (std::is_same_v<T, std::string>) {
41 std::string value = "";
42 if (extraInfo.GetStringValue(key, value)) {
43 data = value;
44 return;
45 }
46 } else {
47 static_assert(DEPENDENT_FALSE<T>, "Non-exhaustive handling of types");
48 }
49 ACCOUNT_LOGE("Get %{public}d from extraInfo failed", key);
50 }
51
52 template<typename T>
GetValueFromAttributes(const std::vector<Attributes::AttributeKey> keys,const Attributes::AttributeKey & key,const Attributes & extraInfo,T & value)53 void GetValueFromAttributes(const std::vector<Attributes::AttributeKey> keys,
54 const Attributes::AttributeKey &key, const Attributes &extraInfo, T &value)
55 {
56 std::optional<T> temp;
57 GetOptionalValueFromAttributes<T>(keys, key, extraInfo, temp);
58 if (!temp.has_value()) {
59 ACCOUNT_LOGE("Get %{public}d from extraInfo failed", key);
60 return;
61 }
62 value = temp.value();
63 }
64 } // namespace AccountSA
65 } // namespace OHOS
66 #endif // OS_ACCOUNT_INTERFACES_INNERKITS_ACCOUNT_IAM_NATIVE_INCLUDE_ACCOUNT_IAM_INFO_PARSE_H
67