Skip to content

Commit

Permalink
修复部分逻辑问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Luoxin committed Mar 4, 2024
1 parent 3405e13 commit c2ef22c
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 80 deletions.
35 changes: 18 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,27 @@ protogen_go_path: "<protoc-gen-go的绝对路径>"
output_path: "<生成的代码的目录>"
go_module_prefix: "<go module的前缀,在生成时会拼接proto中的 go_package 字段当做包名>"
# 默认的标签,会追加到原始标签中,key 为标签名, value.key 为字段名, value.value 为标签值
default_tag:
gorm:
id: "column:id;primaryKey;autoIncrement;not null"
created_at: "column:created_at;<-:create;autoCreateTime;not null"
updated_at: "column:updated_at;<-;autoUpdateTime;not null"
deleted_at: "column:deleted_at;autoDeleteTime"
name: "type:varchar(255);not null"
username: "type:varchar(255);not null"
email: "type:varchar(255);not null"
validate:
name: "required,max=200,min=2"
username: "required,max=100"
email: "required,email"
# 数据表相关的配置
tables:
# 是否开启,默认为为 false,如果单独使用命令,则不会生效
enable: true
# 是否关闭,默认为为 false,如果单独使用命令,则不会生效
disable: false
# 是否关闭自动生成的 gorm tag,默认为 false
# 关闭自动生成 id 相关的gorm tag,默认为 false
disable_field_id: false
# 关闭自动生成 created_at 的gorm tag,默认为 false
Expand All @@ -94,21 +110,6 @@ tables:
disable_field_deleted_at: false
# 关闭自动生成 column 的gorm tag,默认为 false
disable_gorm_tag_column: false
# 默认的标签,会追加到原始标签中,key 为标签名, value.key 为字段名, value.value 为标签值
default_tag:
gorm:
id: "column:id;primaryKey;autoIncrement;not null"
created_at: "column:created_at;<-:create;autoCreateTime;not null"
updated_at: "column:updated_at;<-;autoUpdateTime;not null"
deleted_at: "column:deleted_at;autoDeleteTime"
name: "type:varchar(255);not null"
username: "type:varchar(255);not null"
email: "type:varchar(255);not null"
validate:
name: "required"
username: "required"
email: "required,email"
```

## 生成样例
Expand Down
2 changes: 1 addition & 1 deletion codegen/generate_struct_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ func InjectTagParseFile(inputPath string) ([]textArea, error) {
}
}

tagsMap := state.Config.Tables.DefaultTag[fieldName]
tagsMap := state.Config.DefaultTag[fieldName]
if len(tagsMap) == 0 {
tagsMap = make(map[string]string)
}
Expand Down
35 changes: 18 additions & 17 deletions example.codegen.cfg.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,27 @@ protogen_go_path: "<protoc-gen-go的绝对路径>"
output_path: "<生成的代码的目录>"
go_module_prefix: "<go module的前缀,在生成时会拼接proto中的 go_package 字段当做包名>"

# 默认的标签,会追加到原始标签中,key 为标签名, value.key 为字段名, value.value 为标签值
default_tag:
gorm:
id: "column:id;primaryKey;autoIncrement;not null"
created_at: "column:created_at;<-:create;autoCreateTime;not null"
updated_at: "column:updated_at;<-;autoUpdateTime;not null"
deleted_at: "column:deleted_at;autoDeleteTime"
name: "type:varchar(255);not null"
username: "type:varchar(255);not null"
email: "type:varchar(255);not null"
validate:
name: "required,max=200,min=2"
username: "required,max=100"
email: "required,email"

# 数据表相关的配置
tables:
# 是否开启,默认为为 false,如果单独使用命令,则不会生效
enable: true
# 是否关闭,默认为为 false,如果单独使用命令,则不会生效
disable: false

# 是否关闭自动生成的 gorm tag,默认为 false
# 关闭自动生成 id 相关的gorm tag,默认为 false
disable_field_id: false
# 关闭自动生成 created_at 的gorm tag,默认为 false
Expand All @@ -21,18 +37,3 @@ tables:
disable_field_deleted_at: false
# 关闭自动生成 column 的gorm tag,默认为 false
disable_gorm_tag_column: false

# 默认的标签,会追加到原始标签中,key 为标签名, value.key 为字段名, value.value 为标签值
default_tag:
gorm:
id: "column:id;primaryKey;autoIncrement;not null"
created_at: "column:created_at;<-:create;autoCreateTime;not null"
updated_at: "column:updated_at;<-;autoUpdateTime;not null"
deleted_at: "column:deleted_at;autoDeleteTime"
name: "type:varchar(255);not null"
username: "type:varchar(255);not null"
email: "type:varchar(255);not null"
validate:
name: "required,max=200,min=2"
username: "required,max=100"
email: "required,email"
101 changes: 56 additions & 45 deletions state/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ type CfgTables struct {
DisableFieldDeletedAt bool `json:"disable_field_deleted_at,omitempty" yaml:"disable_field_deleted_at,omitempty" toml:"disable_field_deleted_at,omitempty"`
// 是否禁用自动添加 gorm tag: column
DisableGormTagColumn bool `json:"disable_gorm_tag_column,omitempty" yaml:"disable_gorm_tag_column,omitempty" toml:"disable_gorm_tag_column,omitempty"`

// 对于原始数据,key 为 tag 名。value.key 为字段名,value.value 为 tag 内容
// 例如: {"gorm": {"id": "column:id;primaryKey;autoIncrement;not null"}}
// 从初始化后整理为,key 为字段名,value.key 为 tag 名,value.value 为 tag 内容
// 例如: {"id": {"gorm": "column:id;primaryKey;autoIncrement;not null"}}
DefaultTag map[string]map[string]string `json:"default_tag,omitempty" yaml:"default_tag,omitempty" toml:"default_tag,omitempty"`
}

func (p *CfgTables) apply() (err error) {
Expand All @@ -47,45 +41,6 @@ func (p *CfgTables) apply() (err error) {
p.DisableGormTagColumn = true
}

if len(p.DefaultTag) == 0 {
p.DefaultTag = make(map[string]map[string]string, 1)
}

if _, ok := p.DefaultTag["gorm"]; !ok {
p.DefaultTag["gorm"] = make(map[string]string)
}

if _, ok := p.DefaultTag["gorm"]["id"]; !p.DisableFieldId && !ok {
p.DefaultTag["gorm"]["id"] = "column:id;primaryKey;autoIncrement;not null"
}

if _, ok := p.DefaultTag["gorm"]["created_at"]; !p.DisableFieldCreatedAt && !ok {
p.DefaultTag["gorm"]["created_at"] = "autoCreateTime;<-:create;column:created_at;not null"
}

if _, ok := p.DefaultTag["gorm"]["updated_at"]; !p.DisableFieldUpdatedAt && !ok {
p.DefaultTag["gorm"]["updated_at"] = "autoUpdateTime;<-:;column:updated_at;not null"
}

if _, ok := p.DefaultTag["gorm"]["deleted_at"]; !p.DisableFieldDeletedAt && !ok {
p.DefaultTag["gorm"]["deleted_at"] = "column:deleted_at;not null"
}

{
newTag := make(map[string]map[string]string)

for tag, values := range p.DefaultTag {
for field, value := range values {
if _, ok := newTag[field]; !ok {
newTag[field] = make(map[string]string)
}

newTag[field][tag] = value
}
}

p.DefaultTag = newTag
}
return nil
}

Expand All @@ -97,6 +52,12 @@ type Cfg struct {

OutputPath string `json:"output_path,omitempty" yaml:"output_path,omitempty" toml:"output_path,omitempty"`

// 对于原始数据,key 为 tag 名。value.key 为字段名,value.value 为 tag 内容
// 例如: {"gorm": {"id": "column:id;primaryKey;autoIncrement;not null"}}
// 从初始化后整理为,key 为字段名,value.key 为 tag 名,value.value 为 tag 内容
// 例如: {"id": {"gorm": "column:id;primaryKey;autoIncrement;not null"}}
DefaultTag map[string]map[string]string `json:"default_tag,omitempty" yaml:"default_tag,omitempty" toml:"default_tag,omitempty"`

Tables *CfgTables `json:"tables,omitempty" yaml:"tables,omitempty" toml:"tables,omitempty"`
}

Expand Down Expand Up @@ -126,6 +87,56 @@ func (p *Cfg) apply() (err error) {
return err
}

{
if len(p.DefaultTag) == 0 {
p.DefaultTag = make(map[string]map[string]string, 1)
}

if _, ok := p.DefaultTag["gorm"]; !ok {
p.DefaultTag["gorm"] = make(map[string]string)
}

if _, ok := p.DefaultTag["gorm"]["id"]; !p.Tables.DisableFieldId && !ok {
p.DefaultTag["gorm"]["id"] = "column:id;primaryKey;autoIncrement;not null"
} else if p.Tables.DisableFieldId && ok {
delete(p.DefaultTag["gorm"], "id")
}

if _, ok := p.DefaultTag["gorm"]["created_at"]; !p.Tables.DisableFieldCreatedAt && !ok {
p.DefaultTag["gorm"]["created_at"] = "autoCreateTime;<-:create;column:created_at;not null"
} else if p.Tables.DisableFieldCreatedAt && ok {
delete(p.DefaultTag["gorm"], "created_at")
}

if _, ok := p.DefaultTag["gorm"]["updated_at"]; !p.Tables.DisableFieldUpdatedAt && !ok {
p.DefaultTag["gorm"]["updated_at"] = "autoUpdateTime;<-:;column:updated_at;not null"
} else if p.Tables.DisableFieldUpdatedAt && ok {
delete(p.DefaultTag["gorm"], "updated_at")
}

if _, ok := p.DefaultTag["gorm"]["deleted_at"]; !p.Tables.DisableFieldDeletedAt && !ok {
p.DefaultTag["gorm"]["deleted_at"] = "column:deleted_at;not null"
} else if p.Tables.DisableFieldDeletedAt && ok {
delete(p.DefaultTag["gorm"], "deleted_at")
}

{
newTag := make(map[string]map[string]string)

for tag, values := range p.DefaultTag {
for field, value := range values {
if _, ok := newTag[field]; !ok {
newTag[field] = make(map[string]string)
}

newTag[field][tag] = value
}
}

p.DefaultTag = newTag
}
}

return nil
}

Expand Down

0 comments on commit c2ef22c

Please sign in to comment.