common

package
v0.0.0-...-e9de818 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2024 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MongoTimeout    = 30 * time.Second
	DefaultPage     = 1
	DefaultPageSize = 100
)
View Source
const (
	SuccessCode = iota
	AuthFailedErrorCode
	DuplicateFieldErrorCode
	DBOperateErrorCode
	RedisOperateErrorCode
	ParamInvalidErrorCode
	DBNoRowAffectedErrorCode
	TemporarilyUnavailable
	ErrorIDLen
	ErrorID
	UnknownErrorCode
	TimeOutErrorCode
	RemoteAllFailedErrorCode
	ProjectIDRespect
	SomeFieldIsNull
	ExceedLimitErrorCode
	SSO_ERROR
	OTPErrorCode
	PasswordNeedChanged
	NeedCaptchaCheck
	LoginIpNotInWhiteList
	UserLocked
)

Variables

View Source
var ErrorDescriptions = map[int]string{
	SuccessCode:              "success",
	AuthFailedErrorCode:      "auth failed",
	DuplicateFieldErrorCode:  "duplicate field",
	DBOperateErrorCode:       "db operate error",
	RedisOperateErrorCode:    "redis operate error",
	ParamInvalidErrorCode:    "param invalid",
	DBNoRowAffectedErrorCode: "db no row affected",
	TemporarilyUnavailable:   "resource temporarily unavailable",
	ErrorIDLen:               "ID MAX LEN IS 1-15",
	ErrorID:                  "ID ONLY SYUUPRT 'A-Z/a-z/0-9/-/_'",
	UnknownErrorCode:         "unknown error",
	ProjectIDRespect:         "PROJECT ID REPECT",
	SomeFieldIsNull:          "SOME FIELD IS NUL",
	TimeOutErrorCode:         "get result timeout",
	RemoteAllFailedErrorCode: "all remote instance failed",
	SSO_ERROR:                "sso error",
	OTPErrorCode:             "otp required",
	PasswordNeedChanged:      "password has not been updated for a long time",
	NeedCaptchaCheck:         "need captcha check",
	LoginIpNotInWhiteList:    "login ip not in whitelist",
	UserLocked:               "user locked",
}

Functions

func CountTop

func CountTop(ctx context.Context, collection *mongo.Collection, key string, top int) (ret []struct {
	Name  string `bson:"name" json:"name"`
	Value int    `bson:"value" json:"value"`
},
	err error)

func CreateResponse

func CreateResponse(c *gin.Context, code int, data interface{})

func ExportFromList

func ExportFromList(c *gin.Context, exportList [][]string, defs MongoDBDefs, filename string)

func ExportFromMongoDB

func ExportFromMongoDB(c *gin.Context, collection *mongo.Collection, filter interface{}, defs MongoDBDefs, filename string)

func MarshalBasicBson

func MarshalBasicBson(d bson.RawValue) (r string)

Types

type ExportHostsResp

type ExportHostsResp struct {
	Code int             `json:"code"`
	Msg  string          `json:"msg"`
	Data *ExportProgress `json:"data"`
}

type ExportProgress

type ExportProgress struct {
	ExportTotal int64 `json:"export_total"`
	ExportCount int64 `json:"export_count"`
	//init exporting saving success failed
	Status   string `json:"status"`
	FileName string `json:"file_name"`
}

type FilterContent

type FilterContent struct {
	Key       string       `json:"key" binding:"required" bson:"key"`
	Rules     []FilterRule `json:"rules" binding:"required,dive" bson:"rules"`
	Condition string       `json:"condition" binding:"required,oneof=$and $or $nor" bson:"condition"`
}

type FilterQuery

type FilterQuery struct {
	Filter    []FilterContent `json:"filter" binding:"dive" bson:"filter"`
	Condition string          `json:"condition" binding:"oneof=$and $or $nor" bson:"condition"`
}

func BindFilterQuery

func BindFilterQuery(c *gin.Context) (*FilterQuery, error)

func (*FilterQuery) Transform

func (f *FilterQuery) Transform() bson.M

type FilterRule

type FilterRule struct {
	Operator string      `json:"operator" binding:"required,oneof=$eq $gt $gte $in $lt $lte $ne $nin $regex $time" bson:"operator"`
	Value    interface{} `json:"value" binding:"required" bson:"value"`
}

type ModelPage

type ModelPage struct {
	Total    int64         `json:"total"`
	Count    int64         `json:"count"`
	Pages    int64         `json:"pages"`
	Page     int64         `json:"page"`
	PageSize int64         `json:"page_size"`
	HasPrev  bool          `json:"has_prev"`
	HasNext  bool          `json:"has_next"`
	Items    []interface{} `json:"items"`
}

func DBModelPaginate

func DBModelPaginate(collection *mongo.Collection, pageOption PageOption, pageFunc PageFuncForModal) (*ModelPage, error)

type MongoDBDefs

type MongoDBDefs []struct {
	Key    string
	Header string
}

type MongoElem

type MongoElem struct {
	Value interface{} `bson:"$elemMatch"` // 传入数组类型
}

type MongoExists

type MongoExists struct {
	Value interface{} `bson:"$exists"` // 传入数组类型
}

type MongoGte

type MongoGte struct {
	Value interface{} `bson:"$gte"` // 传入数组类型
}

type MongoInside

type MongoInside struct {
	Inside interface{} `bson:"$in"` // 传入数组类型
}

type MongoLte

type MongoLte struct {
	Value interface{} `bson:"$lte"`
}

type MongoNe

type MongoNe struct {
	Value interface{} `bson:"$ne"`
}

type MongoNinside

type MongoNinside struct {
	Value interface{} `bson:"$nin"` // 传入数组类型
}

type MongoRegex

type MongoRegex struct {
	Regex string `bson:"$regex"`
}

type PageFunc

type PageFunc func(*mongo.Cursor) error // 处理monogo迭代返回结果

type PageFuncForModal

type PageFuncForModal func(*mongo.Cursor) (interface{}, error)

type PageOption

type PageOption struct {
	Page     int64
	PageSize int64
	Filter   interface{}
	Sorter   interface{}
}

type PageRequest

type PageRequest struct {
	Page       int64  `form:"page,default=1" binding:"required,numeric,min=1"`
	PageSize   int64  `form:"page_size,default=100" binding:"required,numeric,min=1,max=5000"`
	OrderKey   string `form:"order_key"`
	OrderValue int    `form:"order_value"`
}

type PageResponse

type PageResponse struct {
	Total    int64 `json:"total" bson:"total"`
	Page     int64 `json:"page"`
	PageSize int64 `json:"page_size"`
}

PageResponse 返回定义

func DBAggregatePaginate

func DBAggregatePaginate(collection *mongo.Collection, pipe []interface{}, pageOption PageSearch, pageFunc PageFunc) (*PageResponse, error)

func DBSearchPaginate

func DBSearchPaginate(collection *mongo.Collection, pageOption PageSearch, pageFunc PageFunc, opts ...*options.FindOptions) (*PageResponse, error)

DBSearchPaginate 分页查询

type PageSearch

type PageSearch struct {
	Page     int64
	PageSize int64
	Filter   interface{}
	Sorter   interface{}
}

PageSearch 查询定义

type Response

type Response struct {
	Code    int         `json:"code"`
	Message string      `json:"msg"`
	Data    interface{} `json:"data"`
}

func (*Response) SetError

func (response *Response) SetError(code int)

Jump to

Keyboard shortcuts

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