filter

package
v0.78.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 2, 2023 License: MIT Imports: 3 Imported by: 15

Documentation

Overview

Package filter 过滤器

包中的各个功能之间的关系如下:

                              |---[SanitizeFuncOf]
                              |
[FilterFunc]---[FilterFuncOf]-|---[RuleFuncOf]-|---[localeutil.LocaleStringer]
                                               |
                                               |---[ValidatorFuncOf]

调用者可以提前声明 FilterFuncOf 实例,在需要时调用 FilterFuncOf 实例, 生成一个类型无关的方法 FilterFunc 传递给 [server.FilterProblem]。 这样可以绕过 Go 不支持泛型方法的尴尬。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func And

func And[T any](v ...func(T) bool) func(T) bool

AndFunc 以与的形式串联多个验证器函数

func Not

func Not[T any](v func(T) bool) func(T) bool

Not 验证器的取反

func Or

func Or[T any](v ...func(T) bool) func(T) bool

Or 以或的形式并联多个验证器函数

func Sanitizers

func Sanitizers[T any](s ...func(*T)) func(*T)

Types

type FilterFunc

type FilterFunc func() (string, localeutil.LocaleStringer)

FilterFunc 过滤器

当前方法由 FilterFuncOf 生成,验证的数据也有其提供, 但是只有在调用当前方法时才真正对数据进行验证。 如果符合要求返回 "", nil,否则返回字段名和错误信息。

type FilterFuncOf added in v0.76.0

type FilterFuncOf[T any] func(name string, v *T) FilterFunc

FilterFuncOf 生成某类型的过滤器

name 字段名,对于切片等类型会返回带下标的字段名; v 必须是指针类型,否则无法对其内容进行修改;

func New

func New[T any](rule RulerFuncOf[T], sanitize ...func(*T)) FilterFuncOf[T]

New 返回生成 FilterFunc 的方法

sanitize 表示对字断 v 的一些清理,比如去除空白字符等,如果指定了此参数,会在 rule 之前执行;

func NewFromVS added in v0.76.0

func NewFromVS[T any](msg localeutil.LocaleStringer, v ValidatorFuncOf[T], s ...func(*T)) FilterFuncOf[T]

NewFromVS 返回生成 FilterFunc 的方法

msg 和 v 组成验证规则; s 表示对字段的一些清理,比如去除空白字符等,如果指定了此参数,会在 rule 之前执行;

type RulerFuncOf

type RulerFuncOf[T any] func(string, T) (string, localeutil.LocaleStringer)

RulerFuncOf 数据验证规则

这是验证器与错误信息的组合。

传递参数为字段名与需要验证的值; 返回字段名和错误信息,如果验证成功,则返回两个空值;

func NewMapRule

func NewMapRule[K comparable, V any, M ~map[K]V](v func(V) bool, msg localeutil.LocaleStringer) RulerFuncOf[M]

NewMapRule 声明用于验证 map 元素的规则

func NewMapRules

func NewMapRules[K comparable, V any, M ~map[K]V](r ...RulerFuncOf[V]) RulerFuncOf[M]

func NewRule

func NewRule[T any](v func(T) bool, msg localeutil.LocaleStringer) RulerFuncOf[T]

func NewRules

func NewRules[T any](r ...RulerFuncOf[T]) RulerFuncOf[T]

NewRules 将多个规则合并为一个

按顺序依次验证,直接碰到第一个验证不过的。

func NewSliceRule

func NewSliceRule[T any, S ~[]T](v func(T) bool, msg localeutil.LocaleStringer) RulerFuncOf[S]

NewSliceRule 声明用于验证切片元素的规则

func NewSliceRules

func NewSliceRules[T any, S ~[]T](r ...RulerFuncOf[T]) RulerFuncOf[S]

type SanitizeFuncOf

type SanitizeFuncOf[T any] func(*T)

SanitizeFuncOf 数据修正的函数原型

实现方直接修改传递进来的参数即可。

type ValidatorFuncOf

type ValidatorFuncOf[T any] func(T) bool

ValidatorFuncOf 验证器的函数原型

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL