Skip to content

Commit

Permalink
extended api
Browse files Browse the repository at this point in the history
  • Loading branch information
zhufuyi committed Jun 28, 2024
1 parent 5888ec7 commit 994a908
Show file tree
Hide file tree
Showing 52 changed files with 7,891 additions and 1,066 deletions.
17 changes: 17 additions & 0 deletions cmd/sponge/commands/generate/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@ const (
DBDriverMongodb = "mongodb"

undeterminedDBDriver = "undetermined" // used in services created based on protobuf.

// code name
codeNameModel = "model"
codeNameCache = "cache"
codeNameDao = "dao"
codeNameHandler = "handler"
codeNameHandlerPb = "handler-pb"
codeNameService = "service"
codeNameServiceHTTP = "service-http"
codeNameHTTP = "http"
codeNameHTTPPb = "http-pb"
codeNameGRPC = "grpc"
codeNameGRPCPb = "grpc-pb"
codeNameGRPCGW = "grpc-gw-pb"
codeNameGRPCHTTP = "grpc-http-pb"
codeNameGRPCConn = "grpc-conn"
codeNameProtobuf = "protobuf"
)

var (
Expand Down
87 changes: 79 additions & 8 deletions cmd/sponge/commands/generate/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,14 @@ Examples:
# generate dao code with multiple table names.
sponge %s dao --module-name=yourModuleName --db-driver=mysql --db-dsn=root:123456@(192.168.3.37:3306)/test --db-table=t1,t2
# generate dao code with extened api.
sponge %s dao --module-name=yourModuleName --db-driver=mysql --db-dsn=root:123456@(192.168.3.37:3306)/test --db-table=user --extended-api=true
# generate dao code and specify the server directory, Note: code generation will be canceled when the latest generated file already exists.
sponge %s dao --db-driver=mysql --db-dsn=root:123456@(192.168.3.37:3306)/test --db-table=user --out=./yourServerDir
# if you want the generated code to suited to mono-repo, you need to specify the parameter --suited-mono-repo=true --serverName=yourServerName
`, parentName, parentName, parentName),
`, parentName, parentName, parentName, parentName),
SilenceErrors: true,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -96,6 +99,7 @@ Examples:
outPath: outPath,
serverName: serverName,
isEmbed: sqlArgs.IsEmbed,
isExtendedApi: sqlArgs.IsExtendedApi,
suitedMonoRepo: suitedMonoRepo,
}
outPath, err = g.generateCode()
Expand All @@ -122,6 +126,7 @@ using help:
cmd.Flags().StringVarP(&dbTables, "db-table", "t", "", "table name, multiple names separated by commas")
_ = cmd.MarkFlagRequired("db-table")
cmd.Flags().BoolVarP(&sqlArgs.IsEmbed, "embed", "e", false, "whether to embed gorm.model struct")
cmd.Flags().BoolVarP(&sqlArgs.IsExtendedApi, "extended-api", "a", false, "whether to generate extended crud api, additional includes: DeleteByIDs, GetByCondition, ListByIDs, ListByLatestID")
cmd.Flags().StringVarP(&serverName, "server-name", "s", "", "server name")
cmd.Flags().BoolVarP(&suitedMonoRepo, "suited-mono-repo", "l", false, "whether the generated code is suitable for mono-repo")
cmd.Flags().IntVarP(&sqlArgs.JSONNamedType, "json-name-type", "j", 1, "json tags name type, 0:snake case, 1:camel case")
Expand All @@ -139,6 +144,7 @@ type daoGenerator struct {
codes map[string]string
outPath string
isEmbed bool
isExtendedApi bool
serverName string
suitedMonoRepo bool

Expand Down Expand Up @@ -172,16 +178,28 @@ func (g *daoGenerator) generateCode() (string, error) {
switch strings.ToLower(g.dbDriver) {
case DBDriverMysql, DBDriverPostgresql, DBDriverTidb, DBDriverSqlite:
g.fields = append(g.fields, getExpectedSQLForDeletionField(g.isEmbed)...)
if g.isExtendedApi {
var fields []replacer.Field
replaceFiles, fields = daoExtendedApi(r)
g.fields = append(g.fields, fields...)
}

case DBDriverMongodb:
replaceFiles = map[string][]string{
"internal/cache": {
"userExample.go.mgo",
},
"internal/dao": {
"userExample.go.mgo",
},
if g.isExtendedApi {
var fields []replacer.Field
replaceFiles, fields = daoMongoDBExtendedApi(r)
g.fields = append(g.fields, fields...)
} else {
replaceFiles = map[string][]string{
"internal/cache": {
"userExample.go.mgo",
},
"internal/dao": {
"userExample.go.mgo",
},
}
}

default:
return "", errors.New("unsupported db driver: " + g.dbDriver)
}
Expand Down Expand Up @@ -254,3 +272,56 @@ func (g *daoGenerator) addFields(r replacer.Replacer) []replacer.Field {

return fields
}

func daoExtendedApi(r replacer.Replacer) (map[string][]string, []replacer.Field) {
replaceFiles := map[string][]string{
"internal/dao": {
"userExample.go.exp", "userExample_test.go.exp",
},
}
var fields []replacer.Field

fields = append(fields, deleteFieldsMark(r, daoFile+".exp", startMark, endMark)...)
fields = append(fields, deleteFieldsMark(r, daoTestFile+".exp", startMark, endMark)...)

fields = append(fields, []replacer.Field{
{
Old: "userExample.go.exp",
New: "userExample.go",
},
{
Old: "userExample_test.go.exp",
New: "userExample_test.go",
},
}...)

return replaceFiles, fields
}

func daoMongoDBExtendedApi(r replacer.Replacer) (map[string][]string, []replacer.Field) {
replaceFiles := map[string][]string{
"internal/cache": {
"userExample.go.mgo",
},
"internal/dao": {
"userExample.go.mgo.exp",
},
}

var fields []replacer.Field

fields = append(fields, deleteFieldsMark(r, daoMgoFile+".exp", startMark, endMark)...)

fields = append(fields, []replacer.Field{
{
Old: "userExample.go.mgo.exp",
New: "userExample.go",
},
{
Old: "userExample.go.mgo",
New: "userExample.go",
},
}...)

return replaceFiles, fields
}
129 changes: 117 additions & 12 deletions cmd/sponge/commands/generate/handler-pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ Examples:
# generate handler and protobuf code with multiple table names.
sponge web handler-pb --module-name=yourModuleName --server-name=yourServerName --db-driver=mysql --db-dsn=root:123456@(192.168.3.37:3306)/test --db-table=t1,t2
# generate handler and protobuf code with extended api.
sponge web handler-pb --module-name=yourModuleName --server-name=yourServerName --db-driver=mysql --db-dsn=root:123456@(192.168.3.37:3306)/test --db-table=user --extended-api=true
# generate handler and protobuf code and specify the server directory, Note: code generation will be canceled when the latest generated file already exists.
sponge web handler-pb --db-driver=mysql --db-dsn=root:123456@(192.168.3.37:3306)/test --db-table=user --out=./yourServerDir
`,
Expand Down Expand Up @@ -88,6 +91,7 @@ Examples:
serverName: serverName,
dbDriver: sqlArgs.DBDriver,
isEmbed: sqlArgs.IsEmbed,
isExtendedApi: sqlArgs.IsExtendedApi,
codes: codes,
outPath: outPath,
suitedMonoRepo: suitedMonoRepo,
Expand Down Expand Up @@ -121,6 +125,7 @@ using help:
cmd.Flags().StringVarP(&dbTables, "db-table", "t", "", "table name, multiple names separated by commas")
_ = cmd.MarkFlagRequired("db-table")
cmd.Flags().BoolVarP(&sqlArgs.IsEmbed, "embed", "e", false, "whether to embed gorm.model struct")
cmd.Flags().BoolVarP(&sqlArgs.IsExtendedApi, "extended-api", "a", false, "whether to generate extended crud api, additional includes: DeleteByIDs, GetByCondition, ListByIDs, ListByLatestID")
cmd.Flags().BoolVarP(&suitedMonoRepo, "suited-mono-repo", "l", false, "whether the generated code is suitable for mono-repo")
cmd.Flags().IntVarP(&sqlArgs.JSONNamedType, "json-name-type", "j", 1, "json tags name type, 0:snake case, 1:camel case")
cmd.Flags().StringVarP(&outPath, "out", "o", "", "output directory, default is ./handler-pb_<time>,"+
Expand All @@ -134,6 +139,7 @@ type handlerPbGenerator struct {
serverName string
dbDriver string
isEmbed bool
isExtendedApi bool
codes map[string]string
outPath string
suitedMonoRepo bool
Expand Down Expand Up @@ -181,20 +187,31 @@ func (g *handlerPbGenerator) generateCode() (string, error) {
switch strings.ToLower(g.dbDriver) {
case DBDriverMysql, DBDriverPostgresql, DBDriverTidb, DBDriverSqlite:
g.fields = append(g.fields, getExpectedSQLForDeletionField(g.isEmbed)...)

if g.isExtendedApi {
var fields []replacer.Field
replaceFiles, fields = handlerPbExtendedApi(r)
g.fields = append(g.fields, fields...)
}
case DBDriverMongodb:
replaceFiles = map[string][]string{
"internal/cache": {
"userExample.go.mgo",
},
"internal/dao": {
"userExample.go.mgo",
},
"internal/handler": {
"userExample_logic.go.mgo",
},
if g.isExtendedApi {
var fields []replacer.Field
replaceFiles, fields = handlerPbMongoDBExtendedApi(r)
g.fields = append(g.fields, fields...)
} else {
replaceFiles = map[string][]string{
"internal/cache": {
"userExample.go.mgo",
},
"internal/dao": {
"userExample.go.mgo",
},
"internal/handler": {
"userExample_logic.go.mgo",
},
}
g.fields = append(g.fields, deleteFieldsMark(r, handlerLogicFile+".mgo", startMark, endMark)...)
}
handlerLogicFile += ".mgo"

default:
return "", errors.New("unsupported db driver: " + g.dbDriver)
}
Expand Down Expand Up @@ -312,3 +329,91 @@ func (g *handlerPbGenerator) addFields(r replacer.Replacer) []replacer.Field {

return fields
}

func handlerPbExtendedApi(r replacer.Replacer) (map[string][]string, []replacer.Field) {
replaceFiles := map[string][]string{
"internal/dao": {
"userExample.go.exp", "userExample_test.go.exp",
},
"internal/ecode": {
"userExample_http.go.exp",
},
"internal/handler": {
"userExample_logic.go.exp", "userExample_logic_test.go.exp",
},
}

var fields []replacer.Field

fields = append(fields, deleteFieldsMark(r, daoFile+".exp", startMark, endMark)...)
fields = append(fields, deleteFieldsMark(r, daoTestFile+".exp", startMark, endMark)...)
fields = append(fields, deleteFieldsMark(r, handlerLogicFile+".exp", startMark, endMark)...)

fields = append(fields, []replacer.Field{
{
Old: "userExample_http.go.exp",
New: "userExample_http.go",
},
{
Old: "userExample_logic_test.go.exp",
New: "userExample_test.go",
},
{
Old: "userExample_test.go.exp",
New: "userExample_test.go",
},
{
Old: "userExample_logic.go.exp",
New: "userExample.go",
},
{
Old: "userExample.go.exp",
New: "userExample.go",
},
}...)

return replaceFiles, fields
}

func handlerPbMongoDBExtendedApi(r replacer.Replacer) (map[string][]string, []replacer.Field) {
replaceFiles := map[string][]string{
"internal/cache": {
"userExample.go.mgo",
},
"internal/dao": {
"userExample.go.mgo.exp",
},
"internal/ecode": {
"userExample_http.go.exp",
},
"internal/handler": {
"userExample_logic.go.mgo.exp",
},
}

var fields []replacer.Field

fields = append(fields, deleteFieldsMark(r, daoMgoFile+".exp", startMark, endMark)...)
fields = append(fields, deleteFieldsMark(r, handlerLogicFile+".mgo.exp", startMark, endMark)...)

fields = append(fields, []replacer.Field{
{
Old: "userExample_http.go.exp",
New: "userExample_http.go",
},
{
Old: "userExample.go.mgo.exp",
New: "userExample.go",
},
{
Old: "userExample_logic.go.mgo.exp",
New: "userExample.go",
},
{
Old: "userExample.go.mgo",
New: "userExample.go",
},
}...)

return replaceFiles, fields
}
Loading

0 comments on commit 994a908

Please sign in to comment.