1 Star 0 Fork 0

yzsunjianguo/common_pkg

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
option.go 2.38 KB
一键复制 编辑 原始数据 按行查看 历史
yzsunjianguo 提交于 2024-02-08 10:53 +08:00 . 初始化
package parser
// NullStyle null type
type NullStyle int
// nolint
const (
NullDisable NullStyle = iota
NullInSql
NullInPointer
)
// Option function
type Option func(*options)
type options struct {
Charset string
Collation string
JSONTag bool
JSONNamedType int
TablePrefix string
ColumnPrefix string
NoNullType bool
NullStyle NullStyle
Package string
GormType bool
ForceTableName bool
IsEmbed bool // is gorm.Model embedded
IsWebProto bool // true: proto file include router path and swagger info, false: normal proto file without router and swagger
}
var defaultOptions = options{
NullStyle: NullInSql,
Package: "model",
}
// WithCharset set charset
func WithCharset(charset string) Option {
return func(o *options) {
o.Charset = charset
}
}
// WithCollation set collation
func WithCollation(collation string) Option {
return func(o *options) {
o.Collation = collation
}
}
// WithTablePrefix set table prefix
func WithTablePrefix(p string) Option {
return func(o *options) {
o.TablePrefix = p
}
}
// WithColumnPrefix set column prefix
func WithColumnPrefix(p string) Option {
return func(o *options) {
o.ColumnPrefix = p
}
}
// WithJSONTag set json tag, 0 for underscore, other values for hump
func WithJSONTag(namedType int) Option {
return func(o *options) {
o.JSONTag = true
o.JSONNamedType = namedType
}
}
// WithNoNullType set NoNullType
func WithNoNullType() Option {
return func(o *options) {
o.NoNullType = true
}
}
// WithNullStyle set NullType
func WithNullStyle(s NullStyle) Option {
return func(o *options) {
o.NullStyle = s
}
}
// WithPackage set package name
func WithPackage(pkg string) Option {
return func(o *options) {
o.Package = pkg
}
}
// WithGormType will write type in gorm tag
func WithGormType() Option {
return func(o *options) {
o.GormType = true
}
}
// WithForceTableName set forceFloats
func WithForceTableName() Option {
return func(o *options) {
o.ForceTableName = true
}
}
// WithEmbed is embed gorm.Model
func WithEmbed() Option {
return func(o *options) {
o.IsEmbed = true
}
}
// WithWebProto set proto file type
func WithWebProto() Option {
return func(o *options) {
o.IsWebProto = true
}
}
func parseOption(options []Option) options {
o := defaultOptions
for _, f := range options {
f(&o)
}
if o.NoNullType {
o.NullStyle = NullDisable
}
return o
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/yzsunjianguo/common_pkg.git
git@gitee.com:yzsunjianguo/common_pkg.git
yzsunjianguo
common_pkg
common_pkg
v1.0.1

搜索帮助