model

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2019 License: MIT, MIT Imports: 2 Imported by: 0

README

Model 数据模型模块

用于处理和用户有关的数据模型

Message 信息

Message 是包含了一套翻译后的数据的集合。

使用方式:

m:=models.NewMessages()
//设置信息
m.
    SetMessage("value1","translated1 %[1]s").
    SetMessage("value2","translated1 %[2]s").

//获取信息
//如果没有对应的翻译,会将传入的字符串原样返回
label:=m.GetMessage("value1")

//获取信息和是否翻译
//如果没有对应的翻译,会将传入的字符串原样返回,并返回false
label,TrueOrFalse=m.LoadMessage("value1")

MessagesChain 信息链

MessagesChain 是一个把多个 Messages 对象按顺序存储,获取翻译时依次获取值的的对象

//创建MessagesChain
m:=models.NewMessagesChain(messages1,messages2)

//将更多messages加入MessagesChain
m.Use(messages3,messages4).Use(message5)

//获取信息
//如果没有对应的翻译,会将传入的字符串原样返回
//将按顺序从已经加入的Messages中查找
label:=m.GetMessage("value1")

//获取信息和是否翻译
//如果没有对应的翻译,会将传入的字符串原样返回,并返回false
//将按顺序从已经加入的Messages中查找
label,TrueOrFalse=m.LoadMessage("value1")

DefaultMessagesChain 默认信息链

DefaultMessagesChain 是一个空的 MessagesChain。是默认情况下的翻译来源。

//直接链入Messages
model.Use(messages1,message2)

//直接获取翻译
label:=model.GetMessage("label1")

Model 模型对象

模型对象是一个用于集成的用户数据验证基础结构

提供了初始化,绑定 Http request,验证并存储用户输出数据错误的接口

//继承对象
type Form stuct{
    model.Model
    httprequest *http.Request
}

//实现InitWithRequest方法
func (model *Form) InitWithRequest(r *http.Request) error {
    model.httprequest=r
    return nil
}

//设置字段名称,可以为Model设置错误提示时每个字段的名称。需要每次在实例化的时候添加
//没有设置过的字段名称会以属性名表示
var FormFieldLabels=map[string]strint{
    "field1":"Field 1",
    "field2:"Field 2"
}

//表单对应的ID
const FormID="formid"

//创建新Form对象
func New() *Form{
    form:=&Form{}
    form.SetFieldLabels(FormFieldLabels)

    //设置表单使用的翻译集,不设置或者为空的话使用model.DefaultMessageChain
    form.SetMessages(nil)

    //设置表单ID,便于在需要的时候快速处理和创建表单
    form.SetModelID(FormID)
}

使用 Model 对象

//创建新表单
form:=NewForm()

//验证字段。第一个参数为false的话,会为model添加名称为第二参数,值为第三参数的错误
form.ValidateField(form.field1==1,"field1","field1 must be 1")
//验证字段。第一个参数为false的话,会为model添加名称为第二参数,值为第三参数的错误
//传入的字符串有两个特殊的占位符
//%[1]s代表原始的Field名
//%[2]s代表通过SetFieldLabels设置的字段名
form.ValidateFieldf(form.field1==1,"field1","%[2]s must be 1")

//添加不转换的错误信息
form.AddPlainError("field", "error msg")
//添加只转换字段信息的错误信息
form.AddError("field", "error msg")
//添加转换字段信息和错误信息
//传入的字符串有两个特殊的占位符
//%[1]s代表原始的Field名
//%[2]s代表通过SetFieldLabels设置的字段名
form.AddErrorf("field", " %[1]s error msg")

//判断model对象是否有错误
TrueOrFalse:=form.HasError()
//返回model的所有错误
errors:=form.Errors()

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultMessagesChain = NewMessagesChain()

DefaultMessagesChain default messages

View Source
var ErrModelNotInited = errors.New("model id is empty.You must use inited model . ")

ErrModelNotInited error raised when model id is empty.

View Source
var ErrNoValidateMethod = errors.New("error no validate method for model")

ErrNoValidateMethod error rasied when model validate method not overrided.

Functions

func GetMessage

func GetMessage(key string) string

GetMessage get translated string for key from default MessagesChain. Return key if translateed string not exist.

func MustValidate

func MustValidate(m Validator) bool

MustValidate return model validate result. Panic if any error raised.

Types

type FieldError

type FieldError struct {
	//Field field name.
	Field string
	//Label field label.
	//If field not found in model's labels,field label is same as field name.
	Label string
	//Msg error message
	Msg string
}

FieldError field error info struct

type Messages

type Messages map[string]string

Messages translate messages map.

func NewMessages

func NewMessages() *Messages

NewMessages create new messages

func (*Messages) GetMessage

func (m *Messages) GetMessage(key string) string

GetMessage get translated string for key. Return key if translateed string not exist.

func (*Messages) LoadMessage

func (m *Messages) LoadMessage(key string) (string, bool)

LoadMessage check if translated string exists for given key. If string exists,return tranlasted string and true. If string does not exist,return key and false.

func (*Messages) SetMessage

func (m *Messages) SetMessage(key string, message string) *Messages

SetMessage set translated string to key. Return messages self.

type MessagesChain

type MessagesChain []MessagesCollection

MessagesChain use model messages interface list as model messages interface.

func NewMessagesChain

func NewMessagesChain(Messages ...MessagesCollection) *MessagesChain

NewMessagesChain create new message chain with given model messages.

func Use

func Use(Messages ...MessagesCollection) *MessagesChain

Use append new model messages to default MessagesChain.

func (*MessagesChain) GetMessage

func (m *MessagesChain) GetMessage(key string) string

GetMessage get translated string for key. Return key if translateed string not exist. Check all model messages in order.

func (*MessagesChain) LoadMessage

func (m *MessagesChain) LoadMessage(key string) (string, bool)

LoadMessage check if translated string exists for given key. If string exists,return tranlasted string and true. If string does not exist,return key and false. Check all model messages in order.

func (*MessagesChain) Use

func (m *MessagesChain) Use(Messages ...MessagesCollection) *MessagesChain

Use append new model messages to MessagesChain.

type MessagesCollection

type MessagesCollection interface {
	//GetMessage get translated string for key.
	//Return key if translateed string not exist.
	GetMessage(key string) string
	//LoadMessage check if translated string exists for given key.
	//If string exists,return tranlasted string and true.
	//If string does not exist,return key and false.
	LoadMessage(key string) (string, bool)
}

MessagesCollection model messages collection interface.

type Model

type Model struct {
	// contains filtered or unexported fields
}

Model model struct.

func (*Model) AddError

func (model *Model) AddError(field string, msg string)

AddError add error by given field and plain msg. Msg will be translated.

func (*Model) AddErrorf

func (model *Model) AddErrorf(field string, msg string)

AddErrorf add error by given field and formatted msg. Msg will be translated.

func (*Model) AddPlainError

func (model *Model) AddPlainError(field string, msg string)

AddPlainError add plain error with given field and msg. Msg will not be translated.

func (*Model) Errors

func (model *Model) Errors() []*FieldError

Errors return error list of model

func (*Model) GetFieldLabel

func (model *Model) GetFieldLabel(field string) string

GetFieldLabel get label by given label name. Return field name itself if not found in field labels of model.

func (*Model) HasError

func (model *Model) HasError() bool

HasError return if model has any error.

func (*Model) ModelID

func (model *Model) ModelID() string

ModelID get model id

func (*Model) SetFieldLabels

func (model *Model) SetFieldLabels(labels map[string]string)

SetFieldLabels set field labels to model

func (*Model) SetMessages

func (model *Model) SetMessages(m *Messages)

SetMessages set translate messages to model. Messages will be used to translate msg field in AddPlainError,AddError,AddErrorf ,AddErrorf and ValidateFieldf method.

func (*Model) SetModelID

func (model *Model) SetModelID(id string)

SetModelID set id to model.

func (*Model) Validate

func (model *Model) Validate() error

Validate method used to validate model. Fail validation will add error to model. Return any error if rasied. You must override this method for your own model,otherwise ErrNoValidateMethod will be raised.

func (*Model) ValidateField

func (model *Model) ValidateField(validated bool, field string, msg string) *ValidatedResult

ValidateField validated field then add error with given field name and plain msg if not validated.

func (*Model) ValidateFieldf

func (model *Model) ValidateFieldf(validated bool, field string, msg string) *ValidatedResult

ValidateFieldf validated field then add error with given field name and formatted msg if not validated.

type ValidatedResult

type ValidatedResult struct {
	Validated bool
	Model     Validator
}

ValidatedResult result of validation.

type Validator

type Validator interface {
	//HasError return if model has any error.
	HasError() bool
	//Errors return error list of model
	Errors() []*FieldError
	//AddError add error by given field and plain msg.
	AddError(field string, msg string)
	//AddErrorf add error by given field and formatted msg.
	AddErrorf(field string, msg string)
	//Validate method used to validate model.
	//Fail validation will add error to model.
	//Return any error if rasied.
	Validate() error
	//ModelID return model id.
	ModelID() string
	//SetModelID set model id.
	SetModelID(string)
}

Validator interface for model that can be validated.

Directories

Path Synopsis
redis
sql
db

Jump to

Keyboard shortcuts

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