!465 可选参数提供默认值

Merge pull request !465 from sunyaozu/master
This commit is contained in:
openharmony_ci 2023-07-13 03:56:22 +00:00 committed by Gitee
commit d2afdd2162
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 24 additions and 31 deletions

View File

@ -206,7 +206,7 @@ private:
static napi_value GetLanguageInfoArray(napi_env env, napi_callback_info info);
static napi_value getCountryInfoArray(napi_env env, napi_callback_info info);
static bool GetStringArrayFromJsParam(napi_env env, napi_value &jsArray, std::vector<std::string> &strArray);
static bool GetSortOptionsFromJsParam(napi_env env, napi_value &jsOptions, SortOptions &options);
static void GetSortOptionsFromJsParam(napi_env env, napi_value &jsOptions, SortOptions &options, bool isRegion);
static bool GetBoolOptionValue(napi_env env, napi_value &options, const std::string &optionName, bool &boolVal);
static napi_value CreateLocaleItemArray(napi_env env, const std::vector<LocaleItem> &localeItemList);
static napi_value CreateLocaleItem(napi_env env, const LocaleItem &localeItem);

View File

@ -4022,10 +4022,7 @@ napi_value I18nAddon::GetLanguageInfoArray(napi_env env, napi_callback_info info
return nullptr;
}
SortOptions options;
isSuccess = GetSortOptionsFromJsParam(env, argv[1], options);
if (!isSuccess) {
return nullptr;
}
GetSortOptionsFromJsParam(env, argv[1], options, false);
I18nAddon *obj = nullptr;
status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
if (status != napi_ok || !obj || !obj->systemLocaleManager_) {
@ -4055,10 +4052,7 @@ napi_value I18nAddon::getCountryInfoArray(napi_env env, napi_callback_info info)
return nullptr;
}
SortOptions options;
isSuccess = GetSortOptionsFromJsParam(env, argv[1], options);
if (!isSuccess) {
return nullptr;
}
GetSortOptionsFromJsParam(env, argv[1], options, true);
I18nAddon *obj = nullptr;
status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
if (status != napi_ok || !obj || !obj->systemLocaleManager_) {
@ -4166,29 +4160,30 @@ bool I18nAddon::GetStringArrayFromJsParam(napi_env env, napi_value &jsArray, std
return true;
}
bool I18nAddon::GetSortOptionsFromJsParam(napi_env env, napi_value &jsOptions, SortOptions &options)
void I18nAddon::GetSortOptionsFromJsParam(napi_env env, napi_value &jsOptions, SortOptions &options, bool isRegion)
{
if (jsOptions == nullptr) {
HiLog::Error(LABEL, "SortOptions js param not found.");
ErrorUtil::NapiThrow(env, I18N_NOT_FOUND, true);
return false;
}
std::string localeTag;
bool isUseLocalName = true;
bool isSuggestedFirst = true;
if (jsOptions == nullptr) {
localeTag = LocaleConfig::GetSystemLocale();
if (isRegion) {
isUseLocalName = false;
}
} else {
GetOptionValue(env, jsOptions, "locale", localeTag);
options.localeTag = localeTag;
bool isUseLocalName;
if (localeTag.length() == 0) {
localeTag = LocaleConfig::GetSystemLocale();
}
bool isSuccess = GetBoolOptionValue(env, jsOptions, "isUseLocalName", isUseLocalName);
if (!isSuccess) {
return false;
if (!isSuccess && isRegion) {
isUseLocalName = false;
}
GetBoolOptionValue(env, jsOptions, "isSuggestedFirst", isSuggestedFirst);
}
options.localeTag = localeTag;
options.isUseLocalName = isUseLocalName;
bool isSuggestedFirst;
isSuccess = GetBoolOptionValue(env, jsOptions, "isSuggestedFirst", isSuggestedFirst);
if (!isSuccess) {
return false;
}
options.isSuggestedFirst = isSuggestedFirst;
return true;
}
bool I18nAddon::GetBoolOptionValue(napi_env env, napi_value &options, const std::string &optionName, bool &boolVal)
@ -4203,15 +4198,13 @@ bool I18nAddon::GetBoolOptionValue(napi_env env, napi_value &options, const std:
bool hasProperty = false;
status = napi_has_named_property(env, options, optionName.c_str(), &hasProperty);
if (status != napi_ok || !hasProperty) {
HiLog::Error(LABEL, "option don't have property %{public}s", optionName.c_str());
ErrorUtil::NapiThrow(env, I18N_NOT_VALID, true);
HiLog::Info(LABEL, "option don't have property %{public}s", optionName.c_str());
return false;
}
napi_value optionValue = nullptr;
status = napi_get_named_property(env, options, optionName.c_str(), &optionValue);
if (status != napi_ok) {
HiLog::Error(LABEL, "get option %{public}s failed", optionName.c_str());
ErrorUtil::NapiThrow(env, I18N_NOT_VALID, true);
HiLog::Info(LABEL, "get option %{public}s failed", optionName.c_str());
return false;
}
napi_get_value_bool(env, optionValue, &boolVal);