mirror of
https://github.com/zhufuyi/sponge.git
synced 2025-10-26 02:10:56 +08:00
fix: mongodb model tag
This commit is contained in:
@@ -3,6 +3,7 @@ package parser
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/huandu/xstrings"
|
"github.com/huandu/xstrings"
|
||||||
"github.com/jinzhu/inflection"
|
"github.com/jinzhu/inflection"
|
||||||
@@ -68,7 +69,7 @@ func newTableInfo(data tmplData) TableInfo {
|
|||||||
TableNamePluralCamelFCL: customFirstLetterToLower(customEndOfLetterToLower(data.TableName, pluralName)),
|
TableNamePluralCamelFCL: customFirstLetterToLower(customEndOfLetterToLower(data.TableName, pluralName)),
|
||||||
TableNameSnake: xstrings.ToSnakeCase(data.TName),
|
TableNameSnake: xstrings.ToSnakeCase(data.TName),
|
||||||
TableComment: data.Comment,
|
TableComment: data.Comment,
|
||||||
Columns: getColumns(data.Fields),
|
Columns: getColumns(data.DBDriver, data.Fields),
|
||||||
PrimaryKey: getPrimaryKeyInfo(data.CrudInfo),
|
PrimaryKey: getPrimaryKeyInfo(data.CrudInfo),
|
||||||
DBDriver: data.DBDriver,
|
DBDriver: data.DBDriver,
|
||||||
ColumnSubStructure: data.SubStructs,
|
ColumnSubStructure: data.SubStructs,
|
||||||
@@ -84,7 +85,7 @@ func (table TableInfo) getCode() []byte {
|
|||||||
return code
|
return code
|
||||||
}
|
}
|
||||||
|
|
||||||
func getColumns(fields []tmplField) []Field {
|
func getColumns(dbDriver string, fields []tmplField) []Field {
|
||||||
var columns []Field
|
var columns []Field
|
||||||
|
|
||||||
for _, field := range fields {
|
for _, field := range fields {
|
||||||
@@ -95,13 +96,23 @@ func getColumns(fields []tmplField) []Field {
|
|||||||
ColumnComment: field.Comment,
|
ColumnComment: field.Comment,
|
||||||
IsPrimaryKey: field.IsPrimaryKey,
|
IsPrimaryKey: field.IsPrimaryKey,
|
||||||
GoType: field.GoType,
|
GoType: field.GoType,
|
||||||
Tag: field.Tag,
|
Tag: handleTag(dbDriver, field.Tag),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return columns
|
return columns
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func handleTag(dbDriver string, tag string) string {
|
||||||
|
if dbDriver == DBDriverMongodb {
|
||||||
|
tag = strings.ReplaceAll(tag, `bson:"column:`, `bson:"`)
|
||||||
|
tag = strings.ReplaceAll(tag, `;type:"`, `"`)
|
||||||
|
tag = strings.ReplaceAll(tag, `;type:;primary_key`, ``)
|
||||||
|
tag = strings.ReplaceAll(tag, `bson:"id" json:"id"`, `bson:"_id" json:"id"`)
|
||||||
|
}
|
||||||
|
return tag
|
||||||
|
}
|
||||||
|
|
||||||
func getPrimaryKeyInfo(info *CrudInfo) *PrimaryKey {
|
func getPrimaryKeyInfo(info *CrudInfo) *PrimaryKey {
|
||||||
if info == nil {
|
if info == nil {
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
Reference in New Issue
Block a user