mirror of
https://gitee.com/zlgopen/awtk.git
synced 2024-11-30 02:58:26 +08:00
add value validator
This commit is contained in:
parent
3482d6daef
commit
5c1c202827
@ -1,4 +1,7 @@
|
||||
# 最新动态
|
||||
* 2019/01/24
|
||||
* 增加value\_validator/value\_validator\_delegate
|
||||
|
||||
* 2019/01/23
|
||||
* 增加value\_converter/value\_converter\_delegate
|
||||
|
||||
|
31
src/mvvm/value_validator.c
Normal file
31
src/mvvm/value_validator.c
Normal file
@ -0,0 +1,31 @@
|
||||
/**
|
||||
* File: value_validator.c
|
||||
* Author: AWTK Develop Team
|
||||
* Brief: value validator
|
||||
*
|
||||
* Copyright (c) 2019 - 2019 Guangzhou ZHIYUAN Electronics Co.,Ltd.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* License file for more details.
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* History:
|
||||
* ================================================================
|
||||
* 2019-01-23 Li XianJing <xianjimli@hotmail.com> created
|
||||
*
|
||||
*/
|
||||
|
||||
#include "mvvm/value_validator.h"
|
||||
|
||||
bool_t value_validator_is_valid(value_validator_t* validator, const value_t* value, str_t* msg) {
|
||||
return_value_if_fail(validator != NULL && validator->object.vt != NULL, RET_BAD_PARAMS);
|
||||
return_value_if_fail(validator->is_valid != NULL && validator->object.ref_count > 0,
|
||||
RET_BAD_PARAMS);
|
||||
return_value_if_fail(value != NULL && msg != NULL, RET_BAD_PARAMS);
|
||||
|
||||
return validator->is_valid(validator, value, msg);
|
||||
}
|
68
src/mvvm/value_validator.h
Normal file
68
src/mvvm/value_validator.h
Normal file
@ -0,0 +1,68 @@
|
||||
/**
|
||||
* File: value_validator.h
|
||||
* Author: AWTK Develop Team
|
||||
* Brief: value validator
|
||||
*
|
||||
* Copyright (c) 2019 - 2019 Guangzhou ZHIYUAN Electronics Co.,Ltd.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* License file for more details.
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* History:
|
||||
* ================================================================
|
||||
* 2019-01-23 Li XianJing <xianjimli@hotmail.com> created
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef TK_VALUE_VALIDATOR_H
|
||||
#define TK_VALUE_VALIDATOR_H
|
||||
|
||||
#include "tkc/str.h"
|
||||
#include "tkc/object.h"
|
||||
|
||||
BEGIN_C_DECLS
|
||||
|
||||
struct _value_validator_t;
|
||||
typedef struct _value_validator_t value_validator_t;
|
||||
|
||||
typedef ret_t (*value_validator_is_valid_t)(value_validator_t* validator, const value_t* value,
|
||||
str_t* msg);
|
||||
|
||||
/**
|
||||
* @class value_validator_t
|
||||
* @parent object_t
|
||||
* @annotation ["scriptable"]
|
||||
*
|
||||
* 值合法性检查。
|
||||
*
|
||||
*/
|
||||
struct _value_validator_t {
|
||||
object_t object;
|
||||
|
||||
/*private*/
|
||||
value_validator_is_valid_t is_valid;
|
||||
};
|
||||
|
||||
/**
|
||||
* @method value_validator_is_valid
|
||||
* 检查值是否有效。
|
||||
*
|
||||
* @annotation ["scriptable"]
|
||||
* @param {value_validator_t*} validator validator对象。
|
||||
* @param {value_t*} value 待验证的值。
|
||||
* @param {str_t*} str 返回错误信息。
|
||||
*
|
||||
* @return {bool_t} 返回TRUE表示有效,否则表示无效。
|
||||
*/
|
||||
bool_t value_validator_is_valid(value_validator_t* validator, const value_t* value, str_t* msg);
|
||||
|
||||
#define VALUE_VALIDATOR(validator) ((value_validator_t*)(validator))
|
||||
|
||||
END_C_DECLS
|
||||
|
||||
#endif /*TK_VALUE_VALIDATOR_H*/
|
53
src/mvvm/value_validator_delegate.c
Normal file
53
src/mvvm/value_validator_delegate.c
Normal file
@ -0,0 +1,53 @@
|
||||
/**
|
||||
* File: value_validator_delegate.c
|
||||
* Author: AWTK Develop Team
|
||||
* Brief: value validator delegate
|
||||
*
|
||||
* Copyright (c) 2019 - 2019 Guangzhou ZHIYUAN Electronics Co.,Ltd.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* License file for more details.
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* History:
|
||||
* ================================================================
|
||||
* 2019-01-23 Li XianJing <xianjimli@hotmail.com> created
|
||||
*
|
||||
*/
|
||||
|
||||
#include "mvvm/value_validator_delegate.h"
|
||||
|
||||
static const object_vtable_t s_value_validator_delegate_vtable = {
|
||||
.type = "value_validator_delegate",
|
||||
.desc = "value_validator_delegate",
|
||||
.size = sizeof(value_validator_delegate_t),
|
||||
.is_collection = FALSE};
|
||||
|
||||
static ret_t value_validator_delegate_is_valid(value_validator_t* c, const value_t* value,
|
||||
str_t* msg) {
|
||||
value_validator_delegate_t* value_convert_delegate = VALUE_VALIDATOR_DELEGATE(c);
|
||||
|
||||
return value_convert_delegate->is_valid(value, msg);
|
||||
}
|
||||
|
||||
value_validator_t* value_validator_delegate_create(value_is_valid_t is_valid) {
|
||||
object_t* obj = NULL;
|
||||
value_validator_t* value_convert = NULL;
|
||||
return_value_if_fail(is_valid != NULL, NULL);
|
||||
value_validator_delegate_t* value_convert_delegate = NULL;
|
||||
|
||||
obj = object_create(&s_value_validator_delegate_vtable);
|
||||
return_value_if_fail(obj != NULL, NULL);
|
||||
|
||||
value_convert = VALUE_VALIDATOR(obj);
|
||||
value_convert->is_valid = value_validator_delegate_is_valid;
|
||||
|
||||
value_convert_delegate = VALUE_VALIDATOR_DELEGATE(obj);
|
||||
value_convert_delegate->is_valid = is_valid;
|
||||
|
||||
return value_convert;
|
||||
}
|
61
src/mvvm/value_validator_delegate.h
Normal file
61
src/mvvm/value_validator_delegate.h
Normal file
@ -0,0 +1,61 @@
|
||||
/**
|
||||
* File: value_validator_delegate.h
|
||||
* Author: AWTK Develop Team
|
||||
* Brief: value validator delegate
|
||||
*
|
||||
* Copyright (c) 2019 - 2019 Guangzhou ZHIYUAN Electronics Co.,Ltd.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* License file for more details.
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* History:
|
||||
* ================================================================
|
||||
* 2019-01-23 Li XianJing <xianjimli@hotmail.com> created
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef TK_VALUE_VALIDATOR_DELEGATE_H
|
||||
#define TK_VALUE_VALIDATOR_DELEGATE_H
|
||||
|
||||
#include "mvvm/value_validator.h"
|
||||
|
||||
BEGIN_C_DECLS
|
||||
|
||||
typedef bool_t (*value_is_valid_t)(const value_t* value, str_t* msg);
|
||||
|
||||
/**
|
||||
* @class value_validator_delegate_t
|
||||
* @parent value_validator_t
|
||||
*
|
||||
* 把有效性检查函数包装成value_validator_t接口。
|
||||
*
|
||||
*/
|
||||
typedef struct _value_validator_delegate_t {
|
||||
value_validator_t value_validator;
|
||||
|
||||
/*private*/
|
||||
value_is_valid_t is_valid;
|
||||
} value_validator_delegate_t;
|
||||
|
||||
/**
|
||||
* @method value_validator_delegate_create
|
||||
*
|
||||
* 创建value_validator对象。
|
||||
*
|
||||
* @annotation ["constructor"]
|
||||
* @param {value_is_valid_t} is_valid 有效性检查函数。
|
||||
*
|
||||
* @return {value_validator_t*} 返回value_validator对象。
|
||||
*/
|
||||
value_validator_t* value_validator_delegate_create(value_is_valid_t is_valid);
|
||||
|
||||
#define VALUE_VALIDATOR_DELEGATE(validator) ((value_validator_delegate_t*)(validator))
|
||||
|
||||
END_C_DECLS
|
||||
|
||||
#endif /*TK_VALUE_VALIDATOR_DELEGATE_H*/
|
@ -29,7 +29,7 @@ TEST(ValueConverterDelegate, basic) {
|
||||
value_set_int(&v, 1234);
|
||||
|
||||
ASSERT_EQ(value_converter_to_view(c, &v, &v_view), RET_OK);
|
||||
ASSERT_EQ(string(value_str(&v_view)), string(value_str(&v)));
|
||||
ASSERT_EQ(string(value_str(&v_view)), string("1234"));
|
||||
|
||||
ASSERT_EQ(value_converter_to_model(c, &v_view, &v_model), RET_OK);
|
||||
ASSERT_EQ(value_int(&v_model), 1234);
|
||||
|
35
tests/value_validator_delegate_test.cc
Normal file
35
tests/value_validator_delegate_test.cc
Normal file
@ -0,0 +1,35 @@
|
||||
#include "tkc/utils.h"
|
||||
#include "mvvm/value_validator_delegate.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
using std::string;
|
||||
|
||||
static bool_t is_valid_age(const value_t* value, str_t* msg) {
|
||||
int32_t age = value_int(value);
|
||||
|
||||
if (age > 0 && age < 150) {
|
||||
return TRUE;
|
||||
} else {
|
||||
str_set(msg, "error");
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
TEST(ValueValidatorDelegate, basic) {
|
||||
value_t v;
|
||||
str_t str;
|
||||
value_validator_t* c = value_validator_delegate_create(is_valid_age);
|
||||
|
||||
str_init(&str, 0);
|
||||
value_set_int(&v, 1234);
|
||||
ASSERT_EQ(value_validator_is_valid(c, &v, &str), FALSE);
|
||||
ASSERT_EQ(string(str.str), "error");
|
||||
|
||||
value_set_int(&v, 123);
|
||||
ASSERT_EQ(value_validator_is_valid(c, &v, &str), TRUE);
|
||||
|
||||
str_reset(&str);
|
||||
object_unref(OBJECT(c));
|
||||
}
|
Loading…
Reference in New Issue
Block a user