Skip to content

Commit

Permalink
feat: parse table name for all method
Browse files Browse the repository at this point in the history
  • Loading branch information
qqxhb committed Nov 22, 2024
1 parent 1c60357 commit b156e6f
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions internal/generate/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,16 +186,12 @@ func (b *QueryStructMeta) ReviseDIYMethod() error {
}
method.Receiver.Package = ""
method.Receiver.Type = b.ModelStructName
b.pasreTableName(method)
methods = append(methods, method)
methodMap[method.MethodName] = true
}
if tableName == nil {
methods = append(methods, parser.DefaultMethodTableName(b.ModelStructName))
} else {
// e.g. return "@@table" => return TableNameUser
tableName.Body = strings.ReplaceAll(tableName.Body, "\"@@table\"", "TableName"+b.ModelStructName)
// e.g. return "t_@@table" => return "t_user"
tableName.Body = strings.ReplaceAll(tableName.Body, "@@table", b.TableName)
}
b.ModelMethods = methods

Expand All @@ -204,7 +200,16 @@ func (b *QueryStructMeta) ReviseDIYMethod() error {
}
return nil
}
func (b *QueryStructMeta) pasreTableName(method *parser.Method) {
if method == nil || method.Body == "" || !strings.Contains(method.Body, "@@table") {
return
}
// e.g. return "@@table" => return TableNameUser
method.Body = strings.ReplaceAll(method.Body, "\"@@table\"", "TableName"+b.ModelStructName)
// e.g. return "t_@@table" => return "t_user"
method.Body = strings.ReplaceAll(method.Body, "@@table", b.TableName)

}
func (b *QueryStructMeta) addMethodFromAddMethodOpt(methods ...interface{}) *QueryStructMeta {
for _, method := range methods {
modelMethods, err := parser.GetModelMethod(method)
Expand Down

0 comments on commit b156e6f

Please sign in to comment.