10fbfc30aSopenharmony_ci/* 20fbfc30aSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 30fbfc30aSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 40fbfc30aSopenharmony_ci * you may not use this file except in compliance with the License. 50fbfc30aSopenharmony_ci * You may obtain a copy of the License at 60fbfc30aSopenharmony_ci * 70fbfc30aSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 80fbfc30aSopenharmony_ci * 90fbfc30aSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 100fbfc30aSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 110fbfc30aSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 120fbfc30aSopenharmony_ci * See the License for the specific language governing permissions and 130fbfc30aSopenharmony_ci * limitations under the License. 140fbfc30aSopenharmony_ci */ 150fbfc30aSopenharmony_ci 160fbfc30aSopenharmony_ci#include "napi_util.h" 170fbfc30aSopenharmony_ci#include <endian.h> 180fbfc30aSopenharmony_ci#include <securec.h> 190fbfc30aSopenharmony_ci#include <sstream> 200fbfc30aSopenharmony_ci#include "calendar_log.h" 210fbfc30aSopenharmony_ci#include "napi_queue.h" 220fbfc30aSopenharmony_ci#include "native_util.h" 230fbfc30aSopenharmony_ci#include "event_filter_napi.h" 240fbfc30aSopenharmony_ci#include <climits> 250fbfc30aSopenharmony_ci 260fbfc30aSopenharmony_cinamespace OHOS::CalendarApi::NapiUtil { 270fbfc30aSopenharmony_ciconstexpr int32_t STR_MAX_LENGTH = 4096; 280fbfc30aSopenharmony_ciconstexpr size_t STR_TAIL_LENGTH = 1; 290fbfc30aSopenharmony_ci 300fbfc30aSopenharmony_cinapi_status GetValue(napi_env env, napi_value in, napi_value& out) 310fbfc30aSopenharmony_ci{ 320fbfc30aSopenharmony_ci out = in; 330fbfc30aSopenharmony_ci return napi_ok; 340fbfc30aSopenharmony_ci} 350fbfc30aSopenharmony_ci 360fbfc30aSopenharmony_cinapi_status SetValue(napi_env env, napi_value in, napi_value& out) 370fbfc30aSopenharmony_ci{ 380fbfc30aSopenharmony_ci out = in; 390fbfc30aSopenharmony_ci return napi_ok; 400fbfc30aSopenharmony_ci} 410fbfc30aSopenharmony_ci 420fbfc30aSopenharmony_ci/* napi_value <-> bool */ 430fbfc30aSopenharmony_cinapi_status GetValue(napi_env env, napi_value in, bool& out) 440fbfc30aSopenharmony_ci{ 450fbfc30aSopenharmony_ci return napi_get_value_bool(env, in, &out); 460fbfc30aSopenharmony_ci} 470fbfc30aSopenharmony_ci 480fbfc30aSopenharmony_cinapi_status SetValue(napi_env env, const bool& in, napi_value& out) 490fbfc30aSopenharmony_ci{ 500fbfc30aSopenharmony_ci return napi_get_boolean(env, in, &out); 510fbfc30aSopenharmony_ci} 520fbfc30aSopenharmony_ci 530fbfc30aSopenharmony_ci/* napi_value <-> int32_t */ 540fbfc30aSopenharmony_cinapi_status GetValue(napi_env env, napi_value in, int32_t& out) 550fbfc30aSopenharmony_ci{ 560fbfc30aSopenharmony_ci return napi_get_value_int32(env, in, &out); 570fbfc30aSopenharmony_ci} 580fbfc30aSopenharmony_ci 590fbfc30aSopenharmony_cinapi_status SetValue(napi_env env, const int32_t& in, napi_value& out) 600fbfc30aSopenharmony_ci{ 610fbfc30aSopenharmony_ci return napi_create_int32(env, in, &out); 620fbfc30aSopenharmony_ci} 630fbfc30aSopenharmony_ci 640fbfc30aSopenharmony_ci/* napi_value <-> uint32_t */ 650fbfc30aSopenharmony_cinapi_status GetValue(napi_env env, napi_value in, uint32_t& out) 660fbfc30aSopenharmony_ci{ 670fbfc30aSopenharmony_ci return napi_get_value_uint32(env, in, &out); 680fbfc30aSopenharmony_ci} 690fbfc30aSopenharmony_ci 700fbfc30aSopenharmony_cinapi_status SetValue(napi_env env, const uint32_t& in, napi_value& out) 710fbfc30aSopenharmony_ci{ 720fbfc30aSopenharmony_ci return napi_create_uint32(env, in, &out); 730fbfc30aSopenharmony_ci} 740fbfc30aSopenharmony_ci 750fbfc30aSopenharmony_ci/* napi_value <-> int64_t */ 760fbfc30aSopenharmony_cinapi_status GetValue(napi_env env, napi_value in, int64_t& out) 770fbfc30aSopenharmony_ci{ 780fbfc30aSopenharmony_ci return napi_get_value_int64(env, in, &out); 790fbfc30aSopenharmony_ci} 800fbfc30aSopenharmony_ci 810fbfc30aSopenharmony_cinapi_status SetValue(napi_env env, const int64_t& in, napi_value& out) 820fbfc30aSopenharmony_ci{ 830fbfc30aSopenharmony_ci return napi_create_int64(env, in, &out); 840fbfc30aSopenharmony_ci} 850fbfc30aSopenharmony_ci 860fbfc30aSopenharmony_cinapi_status GetValue(napi_env env, napi_value in, uint64_t& out) 870fbfc30aSopenharmony_ci{ 880fbfc30aSopenharmony_ci bool lossless = true; 890fbfc30aSopenharmony_ci return napi_get_value_bigint_uint64(env, in, &out, &lossless); 900fbfc30aSopenharmony_ci} 910fbfc30aSopenharmony_ci 920fbfc30aSopenharmony_cinapi_status SetValue(napi_env env, const uint64_t& in, napi_value& out) 930fbfc30aSopenharmony_ci{ 940fbfc30aSopenharmony_ci return napi_create_bigint_uint64(env, in, &out); 950fbfc30aSopenharmony_ci} 960fbfc30aSopenharmony_ci 970fbfc30aSopenharmony_ci/* napi_value <-> double */ 980fbfc30aSopenharmony_cinapi_status GetValue(napi_env env, napi_value in, double& out) 990fbfc30aSopenharmony_ci{ 1000fbfc30aSopenharmony_ci return napi_get_value_double(env, in, &out); 1010fbfc30aSopenharmony_ci} 1020fbfc30aSopenharmony_ci 1030fbfc30aSopenharmony_cinapi_status SetValue(napi_env env, const double& in, napi_value& out) 1040fbfc30aSopenharmony_ci{ 1050fbfc30aSopenharmony_ci return napi_create_double(env, in, &out); 1060fbfc30aSopenharmony_ci} 1070fbfc30aSopenharmony_ci 1080fbfc30aSopenharmony_ci/* napi_value <-> std::string */ 1090fbfc30aSopenharmony_cinapi_status GetValue(napi_env env, napi_value in, std::string& out) 1100fbfc30aSopenharmony_ci{ 1110fbfc30aSopenharmony_ci napi_valuetype type = napi_undefined; 1120fbfc30aSopenharmony_ci napi_status status = napi_typeof(env, in, &type); 1130fbfc30aSopenharmony_ci CHECK_RETURN((status == napi_ok) && (type == napi_string), "invalid type", napi_invalid_arg); 1140fbfc30aSopenharmony_ci 1150fbfc30aSopenharmony_ci size_t maxLen = STR_MAX_LENGTH; 1160fbfc30aSopenharmony_ci status = napi_get_value_string_utf8(env, in, NULL, 0, &maxLen); 1170fbfc30aSopenharmony_ci if (maxLen == 0 || maxLen == UINT_MAX) { 1180fbfc30aSopenharmony_ci return status; 1190fbfc30aSopenharmony_ci } 1200fbfc30aSopenharmony_ci LOG_DEBUG("napi_value -> std::string get length %{public}d", (int)maxLen); 1210fbfc30aSopenharmony_ci char* buf = new (std::nothrow) char[STR_TAIL_LENGTH + maxLen]; 1220fbfc30aSopenharmony_ci if (buf != nullptr) { 1230fbfc30aSopenharmony_ci size_t len = 0; 1240fbfc30aSopenharmony_ci status = napi_get_value_string_utf8(env, in, buf, STR_TAIL_LENGTH + maxLen, &len); 1250fbfc30aSopenharmony_ci if (status == napi_ok) { 1260fbfc30aSopenharmony_ci buf[len] = 0; 1270fbfc30aSopenharmony_ci out = std::string(buf); 1280fbfc30aSopenharmony_ci } 1290fbfc30aSopenharmony_ci delete[] buf; 1300fbfc30aSopenharmony_ci } else { 1310fbfc30aSopenharmony_ci status = napi_generic_failure; 1320fbfc30aSopenharmony_ci } 1330fbfc30aSopenharmony_ci return status; 1340fbfc30aSopenharmony_ci} 1350fbfc30aSopenharmony_ci 1360fbfc30aSopenharmony_cinapi_status SetValue(napi_env env, const std::string& in, napi_value& out) 1370fbfc30aSopenharmony_ci{ 1380fbfc30aSopenharmony_ci return napi_create_string_utf8(env, in.c_str(), in.size(), &out); 1390fbfc30aSopenharmony_ci} 1400fbfc30aSopenharmony_ci 1410fbfc30aSopenharmony_ci/* napi_value <-> std::vector<std::string> */ 1420fbfc30aSopenharmony_cinapi_status GetValue(napi_env env, napi_value in, std::vector<std::string>& out) 1430fbfc30aSopenharmony_ci{ 1440fbfc30aSopenharmony_ci LOG_DEBUG("napi_value -> std::vector<std::string>"); 1450fbfc30aSopenharmony_ci return GetValueArray(env, in, out); 1460fbfc30aSopenharmony_ci} 1470fbfc30aSopenharmony_ci 1480fbfc30aSopenharmony_cinapi_status SetValue(napi_env env, const std::vector<std::string>& in, napi_value& out) 1490fbfc30aSopenharmony_ci{ 1500fbfc30aSopenharmony_ci LOG_DEBUG("std::vector<std::string> -> napi_value"); 1510fbfc30aSopenharmony_ci napi_status status = napi_create_array_with_length(env, in.size(), &out); 1520fbfc30aSopenharmony_ci CHECK_RETURN(status == napi_ok, "create array failed!", status); 1530fbfc30aSopenharmony_ci int index = 0; 1540fbfc30aSopenharmony_ci for (auto& item : in) { 1550fbfc30aSopenharmony_ci napi_value element = nullptr; 1560fbfc30aSopenharmony_ci SetValue(env, item, element); 1570fbfc30aSopenharmony_ci status = napi_set_element(env, out, index++, element); 1580fbfc30aSopenharmony_ci CHECK_RETURN((status == napi_ok), "napi_set_element failed!", status); 1590fbfc30aSopenharmony_ci } 1600fbfc30aSopenharmony_ci return status; 1610fbfc30aSopenharmony_ci} 1620fbfc30aSopenharmony_ci 1630fbfc30aSopenharmony_ci/* napi_value <-> std::vector<uint8_t> */ 1640fbfc30aSopenharmony_cinapi_status GetValue(napi_env env, napi_value in, std::vector<uint8_t>& out) 1650fbfc30aSopenharmony_ci{ 1660fbfc30aSopenharmony_ci out.clear(); 1670fbfc30aSopenharmony_ci LOG_DEBUG("napi_value -> std::vector<uint8_t> "); 1680fbfc30aSopenharmony_ci napi_typedarray_type type = napi_biguint64_array; 1690fbfc30aSopenharmony_ci size_t length = 0; 1700fbfc30aSopenharmony_ci napi_value buffer = nullptr; 1710fbfc30aSopenharmony_ci size_t offset = 0; 1720fbfc30aSopenharmony_ci void* data = nullptr; 1730fbfc30aSopenharmony_ci napi_status status = napi_get_typedarray_info(env, in, &type, &length, &data, &buffer, &offset); 1740fbfc30aSopenharmony_ci LOG_DEBUG("array type=%{public}d length=%{public}d offset=%{public}d", (int)type, (int)length, (int)offset); 1750fbfc30aSopenharmony_ci CHECK_RETURN(status == napi_ok, "napi_get_typedarray_info failed!", napi_invalid_arg); 1760fbfc30aSopenharmony_ci CHECK_RETURN(type == napi_uint8_array, "is not Uint8Array!", napi_invalid_arg); 1770fbfc30aSopenharmony_ci CHECK_RETURN((length > 0) && (data != nullptr), "invalid data!", napi_invalid_arg); 1780fbfc30aSopenharmony_ci out.assign((uint8_t*)data, ((uint8_t*)data) + length); 1790fbfc30aSopenharmony_ci return status; 1800fbfc30aSopenharmony_ci} 1810fbfc30aSopenharmony_ci 1820fbfc30aSopenharmony_cinapi_status SetValue(napi_env env, const std::vector<uint8_t>& in, napi_value& out) 1830fbfc30aSopenharmony_ci{ 1840fbfc30aSopenharmony_ci LOG_DEBUG("napi_value <- std::vector<uint8_t> "); 1850fbfc30aSopenharmony_ci CHECK_RETURN(in.size() > 0, "invalid std::vector<uint8_t>", napi_invalid_arg); 1860fbfc30aSopenharmony_ci void* data = nullptr; 1870fbfc30aSopenharmony_ci napi_value buffer = nullptr; 1880fbfc30aSopenharmony_ci napi_status status = napi_create_arraybuffer(env, in.size(), &data, &buffer); 1890fbfc30aSopenharmony_ci CHECK_RETURN((status == napi_ok), "create array buffer failed!", status); 1900fbfc30aSopenharmony_ci 1910fbfc30aSopenharmony_ci if (memcpy_s(data, in.size(), in.data(), in.size()) != EOK) { 1920fbfc30aSopenharmony_ci LOG_ERROR("memcpy_s not EOK"); 1930fbfc30aSopenharmony_ci return napi_invalid_arg; 1940fbfc30aSopenharmony_ci } 1950fbfc30aSopenharmony_ci status = napi_create_typedarray(env, napi_uint8_array, in.size(), buffer, 0, &out); 1960fbfc30aSopenharmony_ci CHECK_RETURN((status == napi_ok), "napi_value <- std::vector<uint8_t> invalid value", status); 1970fbfc30aSopenharmony_ci return status; 1980fbfc30aSopenharmony_ci} 1990fbfc30aSopenharmony_ci 2000fbfc30aSopenharmony_ci/* napi_value <-> std::vector<int32_t> */ 2010fbfc30aSopenharmony_cinapi_status GetValue(napi_env env, napi_value in, std::vector<int32_t>& out) 2020fbfc30aSopenharmony_ci{ 2030fbfc30aSopenharmony_ci LOG_DEBUG("napi_value -> std::vector<int32_t> "); 2040fbfc30aSopenharmony_ci return GetValueArray(env, in, out); 2050fbfc30aSopenharmony_ci} 2060fbfc30aSopenharmony_ci 2070fbfc30aSopenharmony_cinapi_status SetValue(napi_env env, const std::vector<int32_t>& in, napi_value& out) 2080fbfc30aSopenharmony_ci{ 2090fbfc30aSopenharmony_ci LOG_DEBUG("napi_value <- std::vector<int32_t> "); 2100fbfc30aSopenharmony_ci return SetValueArray(env, in, out); 2110fbfc30aSopenharmony_ci} 2120fbfc30aSopenharmony_ci 2130fbfc30aSopenharmony_ci/* napi_value <-> std::vector<uint32_t> */ 2140fbfc30aSopenharmony_cinapi_status GetValue(napi_env env, napi_value in, std::vector<uint32_t>& out) 2150fbfc30aSopenharmony_ci{ 2160fbfc30aSopenharmony_ci LOG_DEBUG("napi_value -> std::vector<uint32_t> "); 2170fbfc30aSopenharmony_ci return GetValueArray(env, in, out); 2180fbfc30aSopenharmony_ci} 2190fbfc30aSopenharmony_ci 2200fbfc30aSopenharmony_cinapi_status SetValue(napi_env env, const std::vector<uint32_t>& in, napi_value& out) 2210fbfc30aSopenharmony_ci{ 2220fbfc30aSopenharmony_ci LOG_DEBUG("napi_value <- std::vector<uint32_t> "); 2230fbfc30aSopenharmony_ci size_t bytes = in.size() * sizeof(uint32_t); 2240fbfc30aSopenharmony_ci CHECK_RETURN(bytes > 0, "invalid std::vector<uint32_t>", napi_invalid_arg); 2250fbfc30aSopenharmony_ci void* data = nullptr; 2260fbfc30aSopenharmony_ci napi_value buffer = nullptr; 2270fbfc30aSopenharmony_ci napi_status status = napi_create_arraybuffer(env, bytes, &data, &buffer); 2280fbfc30aSopenharmony_ci CHECK_RETURN((status == napi_ok), "invalid buffer", status); 2290fbfc30aSopenharmony_ci 2300fbfc30aSopenharmony_ci if (memcpy_s(data, bytes, in.data(), bytes) != EOK) { 2310fbfc30aSopenharmony_ci LOG_ERROR("memcpy_s not EOK"); 2320fbfc30aSopenharmony_ci return napi_invalid_arg; 2330fbfc30aSopenharmony_ci } 2340fbfc30aSopenharmony_ci status = napi_create_typedarray(env, napi_uint32_array, in.size(), buffer, 0, &out); 2350fbfc30aSopenharmony_ci CHECK_RETURN((status == napi_ok), "invalid buffer", status); 2360fbfc30aSopenharmony_ci return status; 2370fbfc30aSopenharmony_ci} 2380fbfc30aSopenharmony_ci 2390fbfc30aSopenharmony_ci/* napi_value <-> std::vector<int64_t> */ 2400fbfc30aSopenharmony_cinapi_status GetValue(napi_env env, napi_value in, std::vector<int64_t>& out) 2410fbfc30aSopenharmony_ci{ 2420fbfc30aSopenharmony_ci LOG_DEBUG("napi_value -> std::vector<int64_t> "); 2430fbfc30aSopenharmony_ci return GetValueArray(env, in, out); 2440fbfc30aSopenharmony_ci} 2450fbfc30aSopenharmony_ci 2460fbfc30aSopenharmony_cinapi_status SetValue(napi_env env, const std::vector<int64_t>& in, napi_value& out) 2470fbfc30aSopenharmony_ci{ 2480fbfc30aSopenharmony_ci LOG_DEBUG("napi_value <- std::vector<int64_t> "); 2490fbfc30aSopenharmony_ci size_t bytes = in.size() * sizeof(int64_t); 2500fbfc30aSopenharmony_ci CHECK_RETURN(bytes > 0, "invalid std::vector<uint32_t>", napi_invalid_arg); 2510fbfc30aSopenharmony_ci void* data = nullptr; 2520fbfc30aSopenharmony_ci napi_value buffer = nullptr; 2530fbfc30aSopenharmony_ci napi_status status = napi_create_arraybuffer(env, bytes, &data, &buffer); 2540fbfc30aSopenharmony_ci CHECK_RETURN((status == napi_ok), "invalid buffer", status); 2550fbfc30aSopenharmony_ci if (!in.data()) { 2560fbfc30aSopenharmony_ci return napi_invalid_arg; 2570fbfc30aSopenharmony_ci } 2580fbfc30aSopenharmony_ci if (memcpy_s(data, bytes, in.data(), bytes) != EOK) { 2590fbfc30aSopenharmony_ci LOG_ERROR("memcpy_s not EOK"); 2600fbfc30aSopenharmony_ci return napi_invalid_arg; 2610fbfc30aSopenharmony_ci } 2620fbfc30aSopenharmony_ci status = napi_create_typedarray(env, napi_bigint64_array, in.size(), buffer, 0, &out); 2630fbfc30aSopenharmony_ci CHECK_RETURN((status == napi_ok), "invalid buffer", status); 2640fbfc30aSopenharmony_ci return status; 2650fbfc30aSopenharmony_ci} 2660fbfc30aSopenharmony_ci 2670fbfc30aSopenharmony_cinapi_status GetValue(napi_env env, napi_value in, CalendarAccount& out) 2680fbfc30aSopenharmony_ci{ 2690fbfc30aSopenharmony_ci LOG_DEBUG("napi_value -> CalendarAccount "); 2700fbfc30aSopenharmony_ci napi_status status = NapiUtil::GetNamedProperty(env, in, "name", out.name); 2710fbfc30aSopenharmony_ci CHECK_RETURN((status == napi_ok), "invalid name", status); 2720fbfc30aSopenharmony_ci status = NapiUtil::GetNamedProperty(env, in, "type", out.type); 2730fbfc30aSopenharmony_ci CHECK_RETURN((status == napi_ok), "invalid type", status); 2740fbfc30aSopenharmony_ci NapiUtil::GetNamedPropertyOptional(env, in, "displayName", out.displayName); 2750fbfc30aSopenharmony_ci return napi_ok; 2760fbfc30aSopenharmony_ci} 2770fbfc30aSopenharmony_ci 2780fbfc30aSopenharmony_cinapi_status SetValue(napi_env env, const CalendarAccount& in, napi_value& out) 2790fbfc30aSopenharmony_ci{ 2800fbfc30aSopenharmony_ci LOG_DEBUG("CalendarAccount -> napi_value "); 2810fbfc30aSopenharmony_ci napi_status status = napi_create_object(env, &out); 2820fbfc30aSopenharmony_ci CHECK_RETURN((status == napi_ok), "invalid entry object", status); 2830fbfc30aSopenharmony_ci 2840fbfc30aSopenharmony_ci napi_value nameValue = nullptr; 2850fbfc30aSopenharmony_ci status = SetValue(env, in.name, nameValue); 2860fbfc30aSopenharmony_ci CHECK_RETURN((status == napi_ok), "invalid entry name", status); 2870fbfc30aSopenharmony_ci napi_set_named_property(env, out, "name", nameValue); 2880fbfc30aSopenharmony_ci napi_value typeValue = nullptr; 2890fbfc30aSopenharmony_ci status = SetValue(env, in.type, typeValue); 2900fbfc30aSopenharmony_ci CHECK_RETURN((status == napi_ok), "invalid entry type", status); 2910fbfc30aSopenharmony_ci napi_set_named_property(env, out, "type", typeValue); 2920fbfc30aSopenharmony_ci 2930fbfc30aSopenharmony_ci if (in.displayName) { 2940fbfc30aSopenharmony_ci napi_value displayNameValue = nullptr; 2950fbfc30aSopenharmony_ci status = SetValue(env, in.displayName.value(), displayNameValue); 2960fbfc30aSopenharmony_ci CHECK_RETURN((status == napi_ok), "invalid entry displayName", status); 2970fbfc30aSopenharmony_ci napi_set_named_property(env, out, "displayName", displayNameValue); 2980fbfc30aSopenharmony_ci } 2990fbfc30aSopenharmony_ci return status; 3000fbfc30aSopenharmony_ci} 3010fbfc30aSopenharmony_ci 3020fbfc30aSopenharmony_ci/* napi_value <-> CalendarConfig */ 3030fbfc30aSopenharmony_cinapi_status GetValue(napi_env env, napi_value in, CalendarConfig& out) 3040fbfc30aSopenharmony_ci{ 3050fbfc30aSopenharmony_ci LOG_DEBUG("napi_value -> CalendarConfig "); 3060fbfc30aSopenharmony_ci NapiUtil::GetNamedPropertyOptional(env, in, "enableReminder", out.enableReminder); 3070fbfc30aSopenharmony_ci bool result = true; 3080fbfc30aSopenharmony_ci napi_status status = napi_has_named_property(env, in, "color", &result); 3090fbfc30aSopenharmony_ci if (status == napi_ok && !result) { 3100fbfc30aSopenharmony_ci const int64_t defaultColor = 0xFF0A59F7; 3110fbfc30aSopenharmony_ci LOG_DEBUG("napi_value color is null, use default color: 0xFF0A59F7"); 3120fbfc30aSopenharmony_ci out.color.emplace<1>(defaultColor); 3130fbfc30aSopenharmony_ci return napi_ok; 3140fbfc30aSopenharmony_ci } 3150fbfc30aSopenharmony_ci napi_value value = NULL; 3160fbfc30aSopenharmony_ci napi_valuetype valueType = napi_undefined; 3170fbfc30aSopenharmony_ci napi_get_named_property(env, in, "color", &value); 3180fbfc30aSopenharmony_ci napi_typeof(env, value, &valueType); 3190fbfc30aSopenharmony_ci if (valueType == napi_string) { 3200fbfc30aSopenharmony_ci LOG_DEBUG("napi_value color is string"); 3210fbfc30aSopenharmony_ci string str = ""; 3220fbfc30aSopenharmony_ci NapiUtil::GetValue(env, value, str); 3230fbfc30aSopenharmony_ci LOG_DEBUG("napi_value color: %{public}s", str.c_str()); 3240fbfc30aSopenharmony_ci bool ok = Native::ColorParse(str, out.color); 3250fbfc30aSopenharmony_ci if (!ok) { 3260fbfc30aSopenharmony_ci return napi_string_expected; 3270fbfc30aSopenharmony_ci } 3280fbfc30aSopenharmony_ci return napi_ok; 3290fbfc30aSopenharmony_ci } 3300fbfc30aSopenharmony_ci LOG_DEBUG("napi_value color is number"); 3310fbfc30aSopenharmony_ci int64_t colorValue; 3320fbfc30aSopenharmony_ci napi_status statusToGetInt64 = napi_get_value_int64(env, value, &colorValue); 3330fbfc30aSopenharmony_ci if (statusToGetInt64 == napi_ok) { 3340fbfc30aSopenharmony_ci out.color.emplace<1>(colorValue); 3350fbfc30aSopenharmony_ci LOG_DEBUG("color: %{public}s", std::to_string(std::get<1>(out.color)).c_str()); 3360fbfc30aSopenharmony_ci } else { 3370fbfc30aSopenharmony_ci LOG_DEBUG("color number -> int_64 err"); 3380fbfc30aSopenharmony_ci } 3390fbfc30aSopenharmony_ci return statusToGetInt64; 3400fbfc30aSopenharmony_ci} 3410fbfc30aSopenharmony_ci 3420fbfc30aSopenharmony_cinapi_status SetValue(napi_env env, const CalendarConfig& in, napi_value& out) 3430fbfc30aSopenharmony_ci{ 3440fbfc30aSopenharmony_ci LOG_DEBUG("CalendarConfig -> napi_value "); 3450fbfc30aSopenharmony_ci napi_status status = napi_create_object(env, &out); 3460fbfc30aSopenharmony_ci CHECK_RETURN((status == napi_ok), "invalid entry object", status); 3470fbfc30aSopenharmony_ci 3480fbfc30aSopenharmony_ci if (in.enableReminder.has_value()) { 3490fbfc30aSopenharmony_ci LOG_DEBUG("config.enableReminder: %{public}d", in.enableReminder.value_or(-1)); 3500fbfc30aSopenharmony_ci napi_value enableRemindValue = nullptr; 3510fbfc30aSopenharmony_ci status = SetValue(env, in.enableReminder.value(), enableRemindValue); 3520fbfc30aSopenharmony_ci CHECK_RETURN((status == napi_ok), "invalid entry enableReminder", status); 3530fbfc30aSopenharmony_ci napi_set_named_property(env, out, "enableReminder", enableRemindValue); 3540fbfc30aSopenharmony_ci } 3550fbfc30aSopenharmony_ci if (std::get_if<1>(&in.color)) { 3560fbfc30aSopenharmony_ci napi_value colorValue = nullptr; 3570fbfc30aSopenharmony_ci string color; 3580fbfc30aSopenharmony_ci std::stringstream ss; 3590fbfc30aSopenharmony_ci ss << std::hex << std::uppercase << std::get<1>(in.color); 3600fbfc30aSopenharmony_ci ss >> color; 3610fbfc30aSopenharmony_ci const int rgbLen = 5; 3620fbfc30aSopenharmony_ci const int argbLen = 7; 3630fbfc30aSopenharmony_ci if (color.size() == rgbLen || color.size() == argbLen) { 3640fbfc30aSopenharmony_ci color = '0' + color; 3650fbfc30aSopenharmony_ci } 3660fbfc30aSopenharmony_ci color = '#' + color; 3670fbfc30aSopenharmony_ci LOG_DEBUG("config.color: %{public}s", color.c_str()); 3680fbfc30aSopenharmony_ci status = SetValue(env, color, colorValue); 3690fbfc30aSopenharmony_ci CHECK_RETURN((status == napi_ok), "invalid entry color", status); 3700fbfc30aSopenharmony_ci napi_set_named_property(env, out, "color", colorValue); 3710fbfc30aSopenharmony_ci } 3720fbfc30aSopenharmony_ci return status; 3730fbfc30aSopenharmony_ci} 3740fbfc30aSopenharmony_ci 3750fbfc30aSopenharmony_cinapi_status GetValue(napi_env env, napi_value in, Location& out) 3760fbfc30aSopenharmony_ci{ 3770fbfc30aSopenharmony_ci LOG_DEBUG("napi_value -> Location "); 3780fbfc30aSopenharmony_ci NapiUtil::GetNamedPropertyOptional(env, in, "location", out.location); 3790fbfc30aSopenharmony_ci NapiUtil::GetNamedPropertyOptional(env, in, "longitude", out.longitude); 3800fbfc30aSopenharmony_ci NapiUtil::GetNamedPropertyOptional(env, in, "latitude", out.latitude); 3810fbfc30aSopenharmony_ci return napi_ok; 3820fbfc30aSopenharmony_ci} 3830fbfc30aSopenharmony_ci 3840fbfc30aSopenharmony_cinapi_status SetValue(napi_env env, const Location& in, napi_value& out) 3850fbfc30aSopenharmony_ci{ 3860fbfc30aSopenharmony_ci LOG_DEBUG("Location -> napi_value "); 3870fbfc30aSopenharmony_ci napi_status status = napi_create_object(env, &out); 3880fbfc30aSopenharmony_ci CHECK_RETURN((status == napi_ok), "invalid entry object", status); 3890fbfc30aSopenharmony_ci 3900fbfc30aSopenharmony_ci if (in.location) { 3910fbfc30aSopenharmony_ci napi_value locationValue = nullptr; 3920fbfc30aSopenharmony_ci status = SetValue(env, in.location.value(), locationValue); 3930fbfc30aSopenharmony_ci CHECK_RETURN((status == napi_ok), "invalid location", status); 3940fbfc30aSopenharmony_ci napi_set_named_property(env, out, "location", locationValue); 3950fbfc30aSopenharmony_ci } 3960fbfc30aSopenharmony_ci if (in.longitude) { 3970fbfc30aSopenharmony_ci napi_value value = nullptr; 3980fbfc30aSopenharmony_ci status = SetValue(env, in.longitude.value(), value); 3990fbfc30aSopenharmony_ci CHECK_RETURN((status == napi_ok), "invalid longitude", status); 4000fbfc30aSopenharmony_ci napi_set_named_property(env, out, "longitude", value); 4010fbfc30aSopenharmony_ci } 4020fbfc30aSopenharmony_ci if (in.latitude) { 4030fbfc30aSopenharmony_ci napi_value value = nullptr; 4040fbfc30aSopenharmony_ci status = SetValue(env, in.latitude.value(), value); 4050fbfc30aSopenharmony_ci CHECK_RETURN((status == napi_ok), "invalid latitude", status); 4060fbfc30aSopenharmony_ci napi_set_named_property(env, out, "latitude", value); 4070fbfc30aSopenharmony_ci } 4080fbfc30aSopenharmony_ci return napi_ok; 4090fbfc30aSopenharmony_ci} 4100fbfc30aSopenharmony_ci 4110fbfc30aSopenharmony_ci/* napi_value <-> RecurrenceRule */ 4120fbfc30aSopenharmony_cinapi_status GetValue(napi_env env, napi_value in, RecurrenceRule& out) 4130fbfc30aSopenharmony_ci{ 4140fbfc30aSopenharmony_ci LOG_DEBUG("napi_value -> RecurrenceRule "); 4150fbfc30aSopenharmony_ci int recurrence = -1; 4160fbfc30aSopenharmony_ci NapiUtil::GetNamedProperty(env, in, "recurrenceFrequency", recurrence); 4170fbfc30aSopenharmony_ci out.recurrenceFrequency = static_cast<RecurrenceType>(recurrence); 4180fbfc30aSopenharmony_ci NapiUtil::GetNamedPropertyOptional(env, in, "expire", out.expire); 4190fbfc30aSopenharmony_ci NapiUtil::GetNamedPropertyOptional(env, in, "count", out.count); 4200fbfc30aSopenharmony_ci NapiUtil::GetNamedPropertyOptional(env, in, "interval", out.interval); 4210fbfc30aSopenharmony_ci NapiUtil::GetNamedPropertyOptional(env, in, "excludedDates", out.excludedDates); 4220fbfc30aSopenharmony_ci NapiUtil::GetNamedPropertyOptional(env, in, "daysOfWeek", out.daysOfWeek); 4230fbfc30aSopenharmony_ci NapiUtil::GetNamedPropertyOptional(env, in, "daysOfMonth", out.daysOfMonth); 4240fbfc30aSopenharmony_ci NapiUtil::GetNamedPropertyOptional(env, in, "daysOfYear", out.daysOfYear); 4250fbfc30aSopenharmony_ci NapiUtil::GetNamedPropertyOptional(env, in, "weeksOfMonth", out.weeksOfMonth); 4260fbfc30aSopenharmony_ci NapiUtil::GetNamedPropertyOptional(env, in, "weeksOfYear", out.weeksOfYear); 4270fbfc30aSopenharmony_ci NapiUtil::GetNamedPropertyOptional(env, in, "monthsOfYear", out.monthsOfYear); 4280fbfc30aSopenharmony_ci return napi_ok; 4290fbfc30aSopenharmony_ci} 4300fbfc30aSopenharmony_ci 4310fbfc30aSopenharmony_cinapi_status SetValue(napi_env env, const RecurrenceRule& in, napi_value& out) 4320fbfc30aSopenharmony_ci{ 4330fbfc30aSopenharmony_ci LOG_DEBUG("RecurrenceRule -> napi_value "); 4340fbfc30aSopenharmony_ci napi_status status = napi_create_object(env, &out); 4350fbfc30aSopenharmony_ci CHECK_RETURN((status == napi_ok), "invalid recurrenceRule", status); 4360fbfc30aSopenharmony_ci status = SetNamedProperty(env, "recurrenceFrequency", in.recurrenceFrequency, out); 4370fbfc30aSopenharmony_ci CHECK_RETURN((status == napi_ok), "invalid recurrenceFrequency", status); 4380fbfc30aSopenharmony_ci SetNamedPropertyOptional(env, "expire", in.expire, out); 4390fbfc30aSopenharmony_ci SetNamedPropertyOptional(env, "count", in.count, out); 4400fbfc30aSopenharmony_ci SetNamedPropertyOptional(env, "interval", in.interval, out); 4410fbfc30aSopenharmony_ci SetNamedPropertyOptional(env, "excludedDates", in.excludedDates, out); 4420fbfc30aSopenharmony_ci SetNamedPropertyOptional(env, "daysOfWeek", in.daysOfWeek, out); 4430fbfc30aSopenharmony_ci SetNamedPropertyOptional(env, "daysOfMonth", in.daysOfMonth, out); 4440fbfc30aSopenharmony_ci SetNamedPropertyOptional(env, "daysOfYear", in.daysOfYear, out); 4450fbfc30aSopenharmony_ci SetNamedPropertyOptional(env, "weeksOfMonth", in.weeksOfMonth, out); 4460fbfc30aSopenharmony_ci SetNamedPropertyOptional(env, "weeksOfYear", in.weeksOfYear, out); 4470fbfc30aSopenharmony_ci SetNamedPropertyOptional(env, "monthsOfYear", in.monthsOfYear, out); 4480fbfc30aSopenharmony_ci return napi_ok; 4490fbfc30aSopenharmony_ci} 4500fbfc30aSopenharmony_ci 4510fbfc30aSopenharmony_ci/* napi_value <-> Attendee */ 4520fbfc30aSopenharmony_cinapi_status GetValue(napi_env env, napi_value in, Attendee& out) 4530fbfc30aSopenharmony_ci{ 4540fbfc30aSopenharmony_ci LOG_DEBUG("Attendee -> napi_value "); 4550fbfc30aSopenharmony_ci NapiUtil::GetNamedProperty(env, in, "name", out.name); 4560fbfc30aSopenharmony_ci NapiUtil::GetNamedProperty(env, in, "email", out.email); 4570fbfc30aSopenharmony_ci optional<std::string> value; 4580fbfc30aSopenharmony_ci NapiUtil::GetNamedPropertyOptional(env, in, "role", value); 4590fbfc30aSopenharmony_ci if (!value.has_value()) { 4600fbfc30aSopenharmony_ci return napi_ok; 4610fbfc30aSopenharmony_ci } 4620fbfc30aSopenharmony_ci 4630fbfc30aSopenharmony_ci if (value == "organizer") { 4640fbfc30aSopenharmony_ci out.role = ORGANIZER; 4650fbfc30aSopenharmony_ci } else { 4660fbfc30aSopenharmony_ci out.role = PARTICIPANT; 4670fbfc30aSopenharmony_ci } 4680fbfc30aSopenharmony_ci return napi_ok; 4690fbfc30aSopenharmony_ci} 4700fbfc30aSopenharmony_ci 4710fbfc30aSopenharmony_cinapi_status SetValue(napi_env env, const Attendee& in, napi_value& out) 4720fbfc30aSopenharmony_ci{ 4730fbfc30aSopenharmony_ci LOG_DEBUG("napi_value -> Attendee "); 4740fbfc30aSopenharmony_ci napi_status status = napi_create_object(env, &out); 4750fbfc30aSopenharmony_ci CHECK_RETURN((status == napi_ok), "invalid entry object", status); 4760fbfc30aSopenharmony_ci 4770fbfc30aSopenharmony_ci napi_value nameValue = nullptr; 4780fbfc30aSopenharmony_ci status = SetValue(env, in.name, nameValue); 4790fbfc30aSopenharmony_ci CHECK_RETURN((status == napi_ok), "invalid entry name", status); 4800fbfc30aSopenharmony_ci napi_set_named_property(env, out, "name", nameValue); 4810fbfc30aSopenharmony_ci napi_value emailValue = nullptr; 4820fbfc30aSopenharmony_ci status = SetValue(env, in.email, emailValue); 4830fbfc30aSopenharmony_ci CHECK_RETURN((status == napi_ok), "invalid entry type", status); 4840fbfc30aSopenharmony_ci napi_set_named_property(env, out, "email", emailValue); 4850fbfc30aSopenharmony_ci if (!in.role.has_value()) { 4860fbfc30aSopenharmony_ci return napi_ok; 4870fbfc30aSopenharmony_ci } 4880fbfc30aSopenharmony_ci std::string value; 4890fbfc30aSopenharmony_ci if (in.role == PARTICIPANT) { 4900fbfc30aSopenharmony_ci value = "organizer"; 4910fbfc30aSopenharmony_ci } else { 4920fbfc30aSopenharmony_ci value = "participant"; 4930fbfc30aSopenharmony_ci } 4940fbfc30aSopenharmony_ci napi_value roleValue = nullptr; 4950fbfc30aSopenharmony_ci status = SetValue(env, value, roleValue); 4960fbfc30aSopenharmony_ci CHECK_RETURN((status == napi_ok), "invalid role", status); 4970fbfc30aSopenharmony_ci napi_set_named_property(env, out, "role", roleValue); 4980fbfc30aSopenharmony_ci return napi_ok; 4990fbfc30aSopenharmony_ci} 5000fbfc30aSopenharmony_ci 5010fbfc30aSopenharmony_ci/* napi_value <-> std::vector<Attendee> */ 5020fbfc30aSopenharmony_cinapi_status GetValue(napi_env env, napi_value in, std::vector<Attendee>& out) 5030fbfc30aSopenharmony_ci{ 5040fbfc30aSopenharmony_ci LOG_DEBUG("napi_value -> std::vector<Attendee> "); 5050fbfc30aSopenharmony_ci return GetValueArray(env, in, out); 5060fbfc30aSopenharmony_ci} 5070fbfc30aSopenharmony_ci 5080fbfc30aSopenharmony_cinapi_status SetValue(napi_env env, const std::vector<Attendee>& in, napi_value& out) 5090fbfc30aSopenharmony_ci{ 5100fbfc30aSopenharmony_ci LOG_DEBUG("std::vector<Attendee> -> napi_value "); 5110fbfc30aSopenharmony_ci return SetValueArray(env, in, out); 5120fbfc30aSopenharmony_ci} 5130fbfc30aSopenharmony_ci 5140fbfc30aSopenharmony_cinapi_status GetValue(napi_env env, napi_value in, EventService& out) 5150fbfc30aSopenharmony_ci{ 5160fbfc30aSopenharmony_ci LOG_DEBUG("napi_value -> EventService"); 5170fbfc30aSopenharmony_ci NapiUtil::GetNamedPropertyOptional(env, in, "description", out.description); 5180fbfc30aSopenharmony_ci NapiUtil::GetNamedProperty(env, in, "type", out.type); 5190fbfc30aSopenharmony_ci return NapiUtil::GetNamedProperty(env, in, "uri", out.uri); 5200fbfc30aSopenharmony_ci} 5210fbfc30aSopenharmony_ci 5220fbfc30aSopenharmony_cinapi_status SetValue(napi_env env, const EventService& in, napi_value& out) 5230fbfc30aSopenharmony_ci{ 5240fbfc30aSopenharmony_ci LOG_DEBUG("EventService -> napi_value"); 5250fbfc30aSopenharmony_ci napi_status status = napi_create_object(env, &out); 5260fbfc30aSopenharmony_ci CHECK_RETURN((status == napi_ok), "invalid entry object", status); 5270fbfc30aSopenharmony_ci 5280fbfc30aSopenharmony_ci napi_value typeValue = nullptr; 5290fbfc30aSopenharmony_ci status = SetValue(env, in.type, typeValue); 5300fbfc30aSopenharmony_ci CHECK_RETURN((status == napi_ok), "invalid typeValue", status); 5310fbfc30aSopenharmony_ci status = napi_set_named_property(env, out, "type", typeValue); 5320fbfc30aSopenharmony_ci CHECK_RETURN((status == napi_ok), "set type failed", status); 5330fbfc30aSopenharmony_ci 5340fbfc30aSopenharmony_ci napi_value uriValue = nullptr; 5350fbfc30aSopenharmony_ci status = SetValue(env, in.uri, uriValue); 5360fbfc30aSopenharmony_ci CHECK_RETURN((status == napi_ok), "invalid entry type", status); 5370fbfc30aSopenharmony_ci status = napi_set_named_property(env, out, "uri", uriValue); 5380fbfc30aSopenharmony_ci CHECK_RETURN((status == napi_ok), "set uri failed", status); 5390fbfc30aSopenharmony_ci 5400fbfc30aSopenharmony_ci if (in.description) { 5410fbfc30aSopenharmony_ci napi_value descriptionValue = nullptr; 5420fbfc30aSopenharmony_ci status = SetValue(env, in.description.value(), descriptionValue); 5430fbfc30aSopenharmony_ci CHECK_RETURN((status == napi_ok), "invalid description", status); 5440fbfc30aSopenharmony_ci status = napi_set_named_property(env, out, "description", descriptionValue); 5450fbfc30aSopenharmony_ci CHECK_RETURN((status == napi_ok), "set description failed", status); 5460fbfc30aSopenharmony_ci } 5470fbfc30aSopenharmony_ci return status; 5480fbfc30aSopenharmony_ci} 5490fbfc30aSopenharmony_ci 5500fbfc30aSopenharmony_ci/* napi_value <-> EventFilter */ 5510fbfc30aSopenharmony_cinapi_status GetValue(napi_env env, napi_value in, EventFilterNapi*& out) 5520fbfc30aSopenharmony_ci{ 5530fbfc30aSopenharmony_ci LOG_DEBUG("napi_value -> EventFilter "); 5540fbfc30aSopenharmony_ci return EventFilterNapi::ToJson(env, in, out); 5550fbfc30aSopenharmony_ci} 5560fbfc30aSopenharmony_ci 5570fbfc30aSopenharmony_cinapi_status SetValue(napi_env env, const EventFilterNapi& in, napi_value& out) 5580fbfc30aSopenharmony_ci{ 5590fbfc30aSopenharmony_ci LOG_DEBUG("EventFilterNapi -> napi_value "); 5600fbfc30aSopenharmony_ci return napi_ok; 5610fbfc30aSopenharmony_ci} 5620fbfc30aSopenharmony_ci 5630fbfc30aSopenharmony_ci/* napi_value <-> EventType */ 5640fbfc30aSopenharmony_cinapi_status GetValue(napi_env env, napi_value in, EventType& out) 5650fbfc30aSopenharmony_ci{ 5660fbfc30aSopenharmony_ci LOG_DEBUG("napi_value -> EventType "); 5670fbfc30aSopenharmony_ci return napi_ok; 5680fbfc30aSopenharmony_ci} 5690fbfc30aSopenharmony_ci 5700fbfc30aSopenharmony_cinapi_status SetValue(napi_env env, const EventType& in, napi_value& out) 5710fbfc30aSopenharmony_ci{ 5720fbfc30aSopenharmony_ci LOG_DEBUG("EventType -> napi_value "); 5730fbfc30aSopenharmony_ci return napi_ok; 5740fbfc30aSopenharmony_ci} 5750fbfc30aSopenharmony_ci 5760fbfc30aSopenharmony_ci/* napi_value <-> Event */ 5770fbfc30aSopenharmony_cinapi_status GetValue(napi_env env, napi_value in, Event& out) 5780fbfc30aSopenharmony_ci{ 5790fbfc30aSopenharmony_ci LOG_DEBUG("napi_value -> Event "); 5800fbfc30aSopenharmony_ci GetNamedPropertyOptional(env, in, "id", out.id); 5810fbfc30aSopenharmony_ci int type = -1; 5820fbfc30aSopenharmony_ci napi_status status = GetNamedProperty(env, in, "type", type); 5830fbfc30aSopenharmony_ci out.type = static_cast<EventType>(type); 5840fbfc30aSopenharmony_ci CHECK_RETURN((status == napi_ok), "invalid entry type", status); 5850fbfc30aSopenharmony_ci GetNamedPropertyOptional(env, in, "title", out.title); 5860fbfc30aSopenharmony_ci GetNamedPropertyOptional(env, in, "location", out.location); 5870fbfc30aSopenharmony_ci status = GetNamedProperty(env, in, "startTime", out.startTime); 5880fbfc30aSopenharmony_ci CHECK_RETURN((status == napi_ok), "invalid entry startTime", status); 5890fbfc30aSopenharmony_ci status = GetNamedProperty(env, in, "endTime", out.endTime); 5900fbfc30aSopenharmony_ci CHECK_RETURN((status == napi_ok), "invalid entry endTime", status); 5910fbfc30aSopenharmony_ci GetNamedPropertyOptional(env, in, "isAllDay", out.isAllDay); 5920fbfc30aSopenharmony_ci GetNamedProperty(env, in, "attendee", out.attendees); // colud be empty not check result 5930fbfc30aSopenharmony_ci GetNamedPropertyOptional(env, in, "timeZone", out.timeZone); 5940fbfc30aSopenharmony_ci GetNamedPropertyOptional(env, in, "reminderTime", out.reminderTime); 5950fbfc30aSopenharmony_ci GetNamedPropertyOptional(env, in, "recurrenceRule", out.recurrenceRule); 5960fbfc30aSopenharmony_ci GetNamedPropertyOptional(env, in, "description", out.description); 5970fbfc30aSopenharmony_ci GetNamedPropertyOptional(env, in, "service", out.service); 5980fbfc30aSopenharmony_ci GetNamedPropertyOptional(env, in, "identifier", out.identifier); 5990fbfc30aSopenharmony_ci GetNamedPropertyOptional(env, in, "isLunar", out.isLunar); 6000fbfc30aSopenharmony_ci return status; 6010fbfc30aSopenharmony_ci} 6020fbfc30aSopenharmony_ci 6030fbfc30aSopenharmony_cinapi_status SetValue(napi_env env, const Event& in, napi_value& out) 6040fbfc30aSopenharmony_ci{ 6050fbfc30aSopenharmony_ci LOG_DEBUG("Event -> napi_value"); 6060fbfc30aSopenharmony_ci napi_status status = napi_create_object(env, &out); 6070fbfc30aSopenharmony_ci CHECK_RETURN((status == napi_ok), "invalid entry object", status); 6080fbfc30aSopenharmony_ci 6090fbfc30aSopenharmony_ci status = SetNamedProperty(env, "id", in.id.value(), out); 6100fbfc30aSopenharmony_ci CHECK_RETURN((status == napi_ok), "invalid entry id", status); 6110fbfc30aSopenharmony_ci 6120fbfc30aSopenharmony_ci status = SetNamedProperty(env, "type", static_cast<int>(in.type), out); 6130fbfc30aSopenharmony_ci CHECK_RETURN((status == napi_ok), "invalid entry type", status); 6140fbfc30aSopenharmony_ci 6150fbfc30aSopenharmony_ci SetNamedPropertyOptional(env, "location", in.location, out); 6160fbfc30aSopenharmony_ci SetNamedPropertyOptional(env, "title", in.title, out); 6170fbfc30aSopenharmony_ci 6180fbfc30aSopenharmony_ci status = SetNamedProperty(env, "startTime", in.startTime, out); 6190fbfc30aSopenharmony_ci CHECK_RETURN((status == napi_ok), "invalid entry startTime", status); 6200fbfc30aSopenharmony_ci 6210fbfc30aSopenharmony_ci status = SetNamedProperty(env, "endTime", in.endTime, out); 6220fbfc30aSopenharmony_ci CHECK_RETURN((status == napi_ok), "invalid entry endTime", status); 6230fbfc30aSopenharmony_ci 6240fbfc30aSopenharmony_ci SetNamedPropertyOptional(env, "isAllDay", in.isAllDay, out); 6250fbfc30aSopenharmony_ci if (!in.attendees.empty()) { 6260fbfc30aSopenharmony_ci status = SetNamedProperty(env, "attendee", in.attendees, out); 6270fbfc30aSopenharmony_ci CHECK_RETURN((status == napi_ok), "invalid entry attendee", status); 6280fbfc30aSopenharmony_ci } 6290fbfc30aSopenharmony_ci SetNamedPropertyOptional(env, "timeZone", in.timeZone, out); 6300fbfc30aSopenharmony_ci SetNamedPropertyOptional(env, "reminderTime", in.reminderTime, out); 6310fbfc30aSopenharmony_ci SetNamedPropertyOptional(env, "description", in.description, out); 6320fbfc30aSopenharmony_ci SetNamedPropertyOptional(env, "service", in.service, out); 6330fbfc30aSopenharmony_ci SetNamedPropertyOptional(env, "recurrenceRule", in.recurrenceRule, out); 6340fbfc30aSopenharmony_ci SetNamedPropertyOptional(env, "identifier", in.identifier, out); 6350fbfc30aSopenharmony_ci SetNamedPropertyOptional(env, "isLunar", in.isLunar, out); 6360fbfc30aSopenharmony_ci return status; 6370fbfc30aSopenharmony_ci} 6380fbfc30aSopenharmony_ci 6390fbfc30aSopenharmony_ci/* napi_value <-> std::vector<Event> */ 6400fbfc30aSopenharmony_cinapi_status GetValue(napi_env env, napi_value in, std::vector<Event>& out) 6410fbfc30aSopenharmony_ci{ 6420fbfc30aSopenharmony_ci LOG_DEBUG("napi_value -> std::vector<Event>"); 6430fbfc30aSopenharmony_ci return GetValueArray(env, in, out); 6440fbfc30aSopenharmony_ci} 6450fbfc30aSopenharmony_ci 6460fbfc30aSopenharmony_cinapi_status SetValue(napi_env env, const std::vector<Event>& in, napi_value& out) 6470fbfc30aSopenharmony_ci{ 6480fbfc30aSopenharmony_ci LOG_DEBUG("std::vector<Event> -> napi_value"); 6490fbfc30aSopenharmony_ci return SetValueArray(env, in, out); 6500fbfc30aSopenharmony_ci} 6510fbfc30aSopenharmony_ci 6520fbfc30aSopenharmony_ci 6530fbfc30aSopenharmony_cinapi_value DefineClass(napi_env env, const std::string& name, 6540fbfc30aSopenharmony_ci const napi_property_descriptor* properties, size_t count, napi_callback newcb) 6550fbfc30aSopenharmony_ci{ 6560fbfc30aSopenharmony_ci // base64("calendar.calendarmanager") as rootPropName, i.e. global.<root> 6570fbfc30aSopenharmony_ci const std::string rootPropName = "Y2FsZW5kYXIuY2FsZW5kYXJtYW5hZ2Vy"; 6580fbfc30aSopenharmony_ci napi_value root = nullptr; 6590fbfc30aSopenharmony_ci bool hasRoot = false; 6600fbfc30aSopenharmony_ci napi_value global = nullptr; 6610fbfc30aSopenharmony_ci napi_get_global(env, &global); 6620fbfc30aSopenharmony_ci napi_has_named_property(env, global, rootPropName.c_str(), &hasRoot); 6630fbfc30aSopenharmony_ci if (hasRoot) { 6640fbfc30aSopenharmony_ci napi_get_named_property(env, global, rootPropName.c_str(), &root); 6650fbfc30aSopenharmony_ci } else { 6660fbfc30aSopenharmony_ci napi_create_object(env, &root); 6670fbfc30aSopenharmony_ci napi_set_named_property(env, global, rootPropName.c_str(), root); 6680fbfc30aSopenharmony_ci } 6690fbfc30aSopenharmony_ci 6700fbfc30aSopenharmony_ci std::string propName = "constructor_of_" + name; 6710fbfc30aSopenharmony_ci napi_value constructor = nullptr; 6720fbfc30aSopenharmony_ci bool hasProp = false; 6730fbfc30aSopenharmony_ci napi_has_named_property(env, root, propName.c_str(), &hasProp); 6740fbfc30aSopenharmony_ci if (hasProp) { 6750fbfc30aSopenharmony_ci napi_get_named_property(env, root, propName.c_str(), &constructor); 6760fbfc30aSopenharmony_ci if (constructor != nullptr) { 6770fbfc30aSopenharmony_ci LOG_DEBUG("got calendar.calendarmanager.%{private}s as constructor", propName.c_str()); 6780fbfc30aSopenharmony_ci return constructor; 6790fbfc30aSopenharmony_ci } 6800fbfc30aSopenharmony_ci hasProp = false; // no constructor. 6810fbfc30aSopenharmony_ci } 6820fbfc30aSopenharmony_ci 6830fbfc30aSopenharmony_ci NAPI_CALL(env, napi_define_class(env, name.c_str(), name.size(), newcb, nullptr, count, properties, &constructor)); 6840fbfc30aSopenharmony_ci NAPI_ASSERT(env, constructor != nullptr, "napi_define_class failed!"); 6850fbfc30aSopenharmony_ci 6860fbfc30aSopenharmony_ci if (!hasProp) { 6870fbfc30aSopenharmony_ci napi_set_named_property(env, root, propName.c_str(), constructor); 6880fbfc30aSopenharmony_ci LOG_DEBUG("save constructor to calendar.calendarmanager.%{private}s", propName.c_str()); 6890fbfc30aSopenharmony_ci } 6900fbfc30aSopenharmony_ci return constructor; 6910fbfc30aSopenharmony_ci} 6920fbfc30aSopenharmony_ci 6930fbfc30aSopenharmony_cinapi_ref NewWithRef(napi_env env, size_t argc, napi_value* argv, void** out, napi_value constructor) 6940fbfc30aSopenharmony_ci{ 6950fbfc30aSopenharmony_ci napi_value object = nullptr; 6960fbfc30aSopenharmony_ci napi_status status = napi_new_instance(env, constructor, argc, argv, &object); 6970fbfc30aSopenharmony_ci CHECK_RETURN(status == napi_ok, "napi_new_instance failed", nullptr); 6980fbfc30aSopenharmony_ci CHECK_RETURN(object != nullptr, "napi_new_instance failed", nullptr); 6990fbfc30aSopenharmony_ci 7000fbfc30aSopenharmony_ci status = napi_unwrap(env, object, out); 7010fbfc30aSopenharmony_ci CHECK_RETURN(status == napi_ok, "napi_unwrap failed", nullptr); 7020fbfc30aSopenharmony_ci CHECK_RETURN(out != nullptr, "napi_unwrap failed", nullptr); 7030fbfc30aSopenharmony_ci 7040fbfc30aSopenharmony_ci napi_ref ref = nullptr; 7050fbfc30aSopenharmony_ci status = napi_create_reference(env, object, 1, &ref); 7060fbfc30aSopenharmony_ci CHECK_RETURN(status == napi_ok, "napi_create_reference failed!", nullptr); 7070fbfc30aSopenharmony_ci CHECK_RETURN(ref != nullptr, "napi_create_reference failed!", nullptr); 7080fbfc30aSopenharmony_ci return ref; 7090fbfc30aSopenharmony_ci} 7100fbfc30aSopenharmony_ci 7110fbfc30aSopenharmony_cinapi_status Unwrap(napi_env env, napi_value in, void** out, napi_value constructor) 7120fbfc30aSopenharmony_ci{ 7130fbfc30aSopenharmony_ci if (constructor != nullptr) { 7140fbfc30aSopenharmony_ci bool isInstance = false; 7150fbfc30aSopenharmony_ci auto status = napi_instanceof(env, in, constructor, &isInstance); 7160fbfc30aSopenharmony_ci if (status != napi_ok) { 7170fbfc30aSopenharmony_ci LOG_ERROR("napi_instanceof failed"); 7180fbfc30aSopenharmony_ci return status; 7190fbfc30aSopenharmony_ci } 7200fbfc30aSopenharmony_ci if (!isInstance) { 7210fbfc30aSopenharmony_ci LOG_ERROR("not a instance of *"); 7220fbfc30aSopenharmony_ci return napi_invalid_arg; 7230fbfc30aSopenharmony_ci } 7240fbfc30aSopenharmony_ci } 7250fbfc30aSopenharmony_ci return napi_unwrap(env, in, out); 7260fbfc30aSopenharmony_ci} 7270fbfc30aSopenharmony_ci 7280fbfc30aSopenharmony_cibool Equals(napi_env env, napi_value value, napi_ref copy) 7290fbfc30aSopenharmony_ci{ 7300fbfc30aSopenharmony_ci if (copy == nullptr) { 7310fbfc30aSopenharmony_ci return value == nullptr? true : false; 7320fbfc30aSopenharmony_ci } 7330fbfc30aSopenharmony_ci 7340fbfc30aSopenharmony_ci napi_value copyValue = nullptr; 7350fbfc30aSopenharmony_ci napi_get_reference_value(env, copy, ©Value); 7360fbfc30aSopenharmony_ci 7370fbfc30aSopenharmony_ci bool isEquals = false; 7380fbfc30aSopenharmony_ci napi_strict_equals(env, value, copyValue, &isEquals); 7390fbfc30aSopenharmony_ci return isEquals; 7400fbfc30aSopenharmony_ci} 7410fbfc30aSopenharmony_ci} // namespace OHOS::CalendarApi::NapiUtil 742