add value validator

This commit is contained in:
xianjimli 2019-01-24 06:37:52 +08:00
parent 3482d6daef
commit 5c1c202827
7 changed files with 252 additions and 1 deletions

View File

@ -1,4 +1,7 @@
# 最新动态
* 2019/01/24
* 增加value\_validator/value\_validator\_delegate
* 2019/01/23
* 增加value\_converter/value\_converter\_delegate

View 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);
}

View 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*/

View 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;
}

View 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*/

View File

@ -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);

View 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));
}