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 #define LOG_TAG "NapiRdbSendableUtils"
16 #include "napi_rdb_sendable_utils.h"
17
18 #define NAPI_CALL_RETURN_ERR(theCall, retVal) \
19 do { \
20 if ((theCall) != napi_ok) { \
21 return retVal; \
22 } \
23 } while (0)
24
25 namespace OHOS::AppDataMgrJsKit {
26 namespace JSUtils {
27
28 template<>
Convert2Sendable(napi_env env,const Asset & value)29 napi_value Convert2Sendable(napi_env env, const Asset &value)
30 {
31 auto outputStatus = value.status & ~0xF0000000;
32 std::vector<napi_property_descriptor> descriptors = {
33 DECLARE_SENDABLE_PROPERTY(env, "name", value.name),
34 DECLARE_SENDABLE_PROPERTY(env, "uri", value.uri),
35 DECLARE_SENDABLE_PROPERTY(env, "createTime", value.createTime),
36 DECLARE_SENDABLE_PROPERTY(env, "modifyTime", value.modifyTime),
37 DECLARE_SENDABLE_PROPERTY(env, "size", value.size),
38 DECLARE_SENDABLE_PROPERTY(env, "path", value.path),
39 DECLARE_SENDABLE_PROPERTY(env, "status", outputStatus),
40 };
41
42 napi_value object = nullptr;
43 NAPI_CALL_RETURN_ERR(
44 napi_create_sendable_object_with_properties(env, descriptors.size(), descriptors.data(), &object), object);
45 return object;
46 }
47
48 template<>
Convert2Sendable(napi_env env,const RowEntity & rowEntity)49 napi_value Convert2Sendable(napi_env env, const RowEntity &rowEntity)
50 {
51 napi_value map = nullptr;
52 NAPI_CALL_RETURN_ERR(napi_create_sendable_map(env, &map), nullptr);
53 auto &values = rowEntity.Get();
54 for (auto const &[key, value] : values) {
55 NAPI_CALL_RETURN_ERR(napi_map_set_named_property(env, map, key.c_str(), Convert2Sendable(env, value)), nullptr);
56 }
57 return map;
58 }
59
60 template<>
Convert2Sendable(napi_env env,const ValueObject & value)61 napi_value Convert2Sendable(napi_env env, const ValueObject &value)
62 {
63 return Convert2Sendable(env, value.value);
64 }
65
66 template<>
Convert2Sendable(napi_env env,const BigInt & value)67 napi_value Convert2Sendable(napi_env env, const BigInt &value)
68 {
69 return Convert2JSValue(env, value);
70 }
71 }; // namespace JSUtils
72 } // namespace OHOS::AppDataMgrJsKit