zflag

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2023 License: MIT Imports: 13 Imported by: 1 module, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

zflag

format gin response && validator transfer

Usage
// We can omit c.Abort()
func Test(c *gin.Context) {
    h := new(dto.ParamTest)
    if err := c.ShouldBindJSON(&h); err != nil {
        zflag.Done(c, zflag.ParamErr(err))
    } else {
        if err := service.Test(c, h); err != nil {
            zflag.Done(c, zflag.ResponseErr(zflag.ErrQuery.WithMessage(err.Error())))
        } else {
            zflag.Done(c, zflag.Success(zflag.EmptyData))
        }
    }
}

// {"flag":1,"data":{},"message":"","detail":"","timestamp":""}
Transfer
func main() {
    s := gin.New()
    
    // transfer en-US -> zh-CN
    zflag.Transfer()
    
    s.GET("/test", Test)
    s.Run(":8080")
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Ok     = defaultFlag(1, "", "")
	ErrNil = defaultFlag(-1, "未知错误", "")

	ErrParams        = defaultFlag(900, "param error", "")
	ErrVerifyAuth    = defaultFlag(901, "用户异常", "")
	ErrLogin         = defaultFlag(902, "登录失败", "")
	ErrNoPermissions = defaultFlag(903, "无权访问", "请联系管理员赋权")
	ErrVersionLow    = defaultFlag(904, "版本号过低", "请下载最新版本")
	ErrSign          = defaultFlag(905, "签名错误", "请检查签名")
	ErrXPwd          = defaultFlag(906, "鉴权失败", "")
	ErrAuth          = defaultFlag(907, "登录状态已过期", "请重新登录再试")
)

default flag -1 - 999

View Source
var (
	EmptyData = make(map[string]string, 0)
	EmptyList = make([]map[string]string, 0)
)
View Source
var ValiTrans ut.Translator

Functions

func Done

func Done(c *gin.Context, res *ResponseBean)

func Health added in v0.0.2

func Health(c *gin.Context)

Health @Description: 服务健康检查,暂时为空动作 @param c

func SetVersion added in v0.0.2

func SetVersion(v string)

func Transfer

func Transfer()

Types

type Flag

type Flag interface {
	Code() int
	Message() string
	Detail() string
	String() string
	WithMessage(string) Flag
	WithDetail(string) Flag
}

func NewFlag

func NewFlag(flag int, message string, detail string) Flag

type ResponseBean

type ResponseBean struct {
	Kind    ResponseKind `json:"-" xml:"-"`
	Version string       `json:"-" xml:"version,attr"`
	Code    int          `json:"code"`
	Data    interface{}  `json:"data"`
	Message string       `json:"message"`
	Detail  string       `json:"detail"`
	T       string       `json:"t"`
}

ResponseBean @Description: 接口返回值定义

func ParamErr

func ParamErr(err error) *ResponseBean

func Response

func Response(code int, data interface{}, message string, detail string, kind ResponseKind) *ResponseBean

func ResponseErr

func ResponseErr(flag Flag) *ResponseBean

func Success

func Success(data interface{}) *ResponseBean

type ResponseKind

type ResponseKind string
const (
	ResponseKindJson   ResponseKind = "json"
	ResponseKindXml    ResponseKind = "xml"
	ResponseKindString ResponseKind = "string"
	ResponseKindFile   ResponseKind = "file"
)

Jump to

Keyboard shortcuts

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