• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #ifndef DATASHARE_JSUTILS_H
17 #define DATASHARE_JSUTILS_H
18 
19 #include <iostream>
20 #include <map>
21 #include <string>
22 #include <variant>
23 #include <vector>
24 
25 #include "datashare_template.h"
26 #include "datashare_value_object.h"
27 #include "napi/native_api.h"
28 
29 #define NAPI_ASSERT_ERRCODE(env, assertion, error)                                                          \
30 do {                                                                                                        \
31     if (!(assertion)) {                                                                                     \
32         napi_throw_error((env), std::to_string((error)->GetCode()).c_str(), (error)->GetMessage().c_str()); \
33         return;                                                                                             \
34     }                                                                                                       \
35 } while (0)
36 
37 #define NAPI_ASSERT_CALL_ERRCODE(env, assertion, call, retVal) \
38     do {                                                       \
39         if (!(assertion)) {                                    \
40             call;                                              \
41             return retVal;                                     \
42         }                                                      \
43     } while (0)
44 
45 #define NAPI_ASSERT_CALL_ERRCODE_SYNC(env, assertion, call, error, retVal)                                      \
46     do {                                                                                                        \
47         if (!(assertion)) {                                                                                     \
48             call;                                                                                               \
49             napi_throw_error((env), std::to_string((error)->GetCode()).c_str(), (error)->GetMessage().c_str()); \
50             return retVal;                                                                                      \
51         }                                                                                                       \
52     } while (0)
53 
54 namespace OHOS {
55 namespace DataShare {
56 class DataShareJSUtils final {
57 public:
58     static constexpr int32_t DEFAULT_BUF_SIZE = 1024;
59     static constexpr int32_t ASYNC_RST_SIZE = 2;
60     static constexpr int32_t SYNC_RESULT_ELEMNT_NUM = 2;
61 
62     static std::string Convert2String(napi_env env, napi_value jsStr, size_t max = DEFAULT_BUF_SIZE);
63     static std::vector<std::string> Convert2StrVector(napi_env env, napi_value value, size_t strMax);
64     static std::vector<uint8_t> Convert2U8Vector(napi_env env, napi_value jsValue);
65     static std::string ConvertAny2String(napi_env env, const napi_value jsValue);
66     static std::string UnwrapStringFromJS(napi_env env, napi_value param, const std::string &defaultValue = "");
67     static DataShareValueObject Convert2ValueObject(napi_env env, napi_value value, bool &status);
68     static Template Convert2Template(napi_env env, napi_value value);
69     static TemplateId Convert2TemplateId(napi_env env, napi_value value);
70     static Data Convert2PublishedData(napi_env env, napi_value value);
71 
72     static napi_value Convert2JSValue(napi_env env, const std::monostate &value = {});
73     static napi_value Convert2JSValue(napi_env env, const std::vector<std::string> &value);
74     static napi_value Convert2JSValue(napi_env env, const std::string &value);
75     static napi_value Convert2JSValue(napi_env env, const std::vector<uint8_t> &value, bool isTypedArray = true);
76     static napi_value Convert2JSValue(napi_env env, int32_t value);
77     static napi_value Convert2JSValue(napi_env env, int64_t value);
78     static napi_value Convert2JSValue(napi_env env, double value);
79     static napi_value Convert2JSValue(napi_env env, bool value);
80     static napi_value Convert2JSValue(napi_env env, const std::map<std::string, int> &value);
81     template<class... Types>
82     static napi_value Convert2JSValue(napi_env env, const std::variant<Types...> &value);
83     static napi_value Convert2JSValue(napi_env env, const TemplateId &templateId);
84     static napi_value Convert2JSValue(napi_env env, const RdbChangeNode &changeNode);
85     static napi_value Convert2JSValue(napi_env env, PublishedDataItem &publishedDataItem);
86     static napi_value Convert2JSValue(napi_env env, std::vector<PublishedDataItem> &publishedDataItems);
87     static napi_value Convert2JSValue(napi_env env, PublishedDataChangeNode &changeNode);
88     static napi_value Convert2JSValue(napi_env env, const OperationResult &results);
89     static napi_value Convert2JSValue(napi_env env, const std::vector<OperationResult> &results);
90     static std::vector<uint8_t> ConvertU8Vector(napi_env env, napi_value jsValue);
91 
92     static bool Equals(napi_env env, napi_value value, napi_ref copy);
93 
94     static bool UnwrapPublishedDataItem(napi_env env, napi_value value, PublishedDataItem &publishedDataItem);
95     static bool UnwrapPublishedDataItemVector(napi_env env, napi_value value,
96         std::vector<PublishedDataItem> &publishedDataItems);
97     static bool UnwrapTemplatePredicates(napi_env env, napi_value jsPredicates,
98         std::vector<PredicateTemplateNode> &predicates);
99     static bool UnwrapStringByPropertyName(napi_env env, napi_value jsObject, const char *propertyName,
100         std::string &value);
101     static bool IsArrayForNapiValue(napi_env env, napi_value param, uint32_t &arraySize);
102 private:
103     template<typename _VTp>
ReadVariant(napi_env env,uint32_t step,uint32_t index,const _VTp & output)104     static napi_value ReadVariant(napi_env env, uint32_t step, uint32_t index, const _VTp &output)
105     {
106         (void)step;
107         (void)index;
108         (void)output;
109         return Convert2JSValue(env);
110     }
111 
112     template<typename _VTp, typename _First, typename ..._Rest>
ReadVariant(napi_env env,uint32_t step,uint32_t index,const _VTp & value)113     static napi_value ReadVariant(napi_env env, uint32_t step, uint32_t index, const _VTp &value)
114     {
115         if (step == index) {
116             auto *realValue = std::get_if<_First>(&value);
117             if (realValue == nullptr) {
118                 return nullptr;
119             }
120             return Convert2JSValue(env, *realValue);
121         }
122         return ReadVariant<_VTp, _Rest...>(env, step + 1, index, value);
123     }
124 };
125 
126 template<class... Types>
Convert2JSValue(napi_env env,const std::variant<Types...> & value)127 napi_value DataShareJSUtils::Convert2JSValue(napi_env env, const std::variant<Types...> &value)
128 {
129     return ReadVariant<decltype(value), Types...>(env, 0, value.index(), value);
130 }
131 } // namespace DataShare
132 } // namespace OHOS
133 
134 #endif // DATASHARE_JSUTILS_H