1
0
mirror of https://github.com/gogf/gf.git synced 2025-04-05 11:18:50 +08:00

feat(gf/gen/pbentity): add a TablesEx configuration to exclude the specified table names (#4060)

This commit is contained in:
wwwfeng 2024-12-26 10:17:51 +08:00 committed by GitHub
parent 2c1fcec88c
commit 96e833db6e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 113 additions and 14 deletions

View File

@ -1,8 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Define the latest Go version requirement
LATEST_GO_VERSION="1.23"
coverage=$1 coverage=$1
# find all path that contains go.mod. # find all path that contains go.mod.

View File

@ -28,10 +28,21 @@ concurrency:
env: env:
TZ: "Asia/Shanghai" TZ: "Asia/Shanghai"
# for unit testing cases of some components that only execute on the latest go version.
LATEST_GO_VERSION: "1.23"
jobs: jobs:
code-test: code-test:
strategy:
matrix:
# 🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥
# When adding new go version to the list, make sure:
# 1. Update the `LATEST_GO_VERSION` env variable.
# 2. Update the `Report Coverage` action.
# 🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥
go-version: [ "1.20", "1.21", "1.22", "1.23" ]
goarch: [ "386", "amd64" ]
runs-on: ubuntu-20.04 runs-on: ubuntu-20.04
# Service containers to run with `code-test` # Service containers to run with `code-test`
@ -185,11 +196,6 @@ jobs:
ports: ports:
- 2181:2181 - 2181:2181
strategy:
matrix:
go-version: [ "1.20", "1.21", "1.22", "1.23" ]
goarch: [ "386", "amd64" ]
steps: steps:
# TODO: szenius/set-timezone update to node16 # TODO: szenius/set-timezone update to node16
# sudo ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime # sudo ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
@ -256,7 +262,8 @@ jobs:
- name: Report Coverage - name: Report Coverage
uses: codecov/codecov-action@v4 uses: codecov/codecov-action@v4
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/master' }} # Only report coverage on the latest go version and amd64 arch
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/master' && matrix.go-version == '1.23' && matrix.goarch == 'amd64' }}
with: with:
flags: go-${{ matrix.go-version }}-${{ matrix.goarch }} flags: go-${{ matrix.go-version }}-${{ matrix.goarch }}
token: ${{ secrets.CODECOV_TOKEN }} token: ${{ secrets.CODECOV_TOKEN }}

View File

@ -29,17 +29,22 @@ concurrency:
env: env:
TZ: "Asia/Shanghai" TZ: "Asia/Shanghai"
# for unit testing cases of some components that only execute on the latest go version.
LATEST_GO_VERSION: "1.23"
jobs: jobs:
code-test: code-test:
runs-on: ubuntu-latest
strategy: strategy:
matrix: matrix:
# 🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥
# When adding new go version to the list, make sure:
# 1. Update the `LATEST_GO_VERSION` env variable.
# 🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥
go-version: [ "1.20", "1.21", "1.22", "1.23" ] go-version: [ "1.20", "1.21", "1.22", "1.23" ]
goarch: [ "386", "amd64" ] goarch: [ "386", "amd64" ]
runs-on: ubuntu-latest
steps: steps:
- name: Setup Timezone - name: Setup Timezone
uses: szenius/set-timezone@v2.0 uses: szenius/set-timezone@v2.0

View File

@ -16,6 +16,5 @@ replace (
github.com/gogf/gf/contrib/drivers/oracle/v2 => ../../contrib/drivers/oracle github.com/gogf/gf/contrib/drivers/oracle/v2 => ../../contrib/drivers/oracle
github.com/gogf/gf/contrib/drivers/pgsql/v2 => ../../contrib/drivers/pgsql github.com/gogf/gf/contrib/drivers/pgsql/v2 => ../../contrib/drivers/pgsql
github.com/gogf/gf/contrib/drivers/sqlite/v2 => ../../contrib/drivers/sqlite github.com/gogf/gf/contrib/drivers/sqlite/v2 => ../../contrib/drivers/sqlite
github.com/gogf/gf/contrib/drivers/dm/v2 => ../../contrib/drivers/dm
github.com/gogf/gf/v2 => ../../ github.com/gogf/gf/v2 => ../../
) )

View File

@ -286,3 +286,84 @@ func Test_Issue_3685(t *testing.T) {
} }
}) })
} }
// https://github.com/gogf/gf/issues/3955
func Test_Issue_3955(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
err error
db = testDB
table1 = "table_user_a"
table2 = "table_user_b"
sqlContent = fmt.Sprintf(
gtest.DataContent(`genpbentity`, `user.tpl.sql`),
table1,
)
sqlContent2 = fmt.Sprintf(
gtest.DataContent(`genpbentity`, `user.tpl.sql`),
table2,
)
)
dropTableWithDb(db, table1)
dropTableWithDb(db, table2)
array := gstr.SplitAndTrim(sqlContent, ";")
for _, v := range array {
if _, err = db.Exec(ctx, v); err != nil {
t.AssertNil(err)
}
}
array = gstr.SplitAndTrim(sqlContent2, ";")
for _, v := range array {
if _, err = db.Exec(ctx, v); err != nil {
t.AssertNil(err)
}
}
defer dropTableWithDb(db, table1)
defer dropTableWithDb(db, table2)
var (
path = gfile.Temp(guid.S())
in = genpbentity.CGenPbEntityInput{
Path: path,
Package: "unittest",
Link: link,
Tables: "",
Prefix: "",
RemovePrefix: "",
RemoveFieldPrefix: "",
NameCase: "",
JsonCase: "",
Option: "",
TablesEx: "table_user_a",
}
)
err = gutil.FillStructWithDefault(&in)
t.AssertNil(err)
err = gfile.Mkdir(path)
t.AssertNil(err)
defer gfile.Remove(path)
_, err = genpbentity.CGenPbEntity{}.PbEntity(ctx, in)
t.AssertNil(err)
files, err := gfile.ScanDir(path, "*.proto", false)
t.AssertNil(err)
t.AssertEQ(len(files), 1)
t.Assert(files, []string{
path + filepath.FromSlash("/table_user_b.proto"),
})
expectFiles := []string{
path + filepath.FromSlash("/table_user_b.proto"),
}
for i := range files {
t.Assert(gfile.GetContents(files[i]), gfile.GetContents(expectFiles[i]))
}
})
}

View File

@ -10,6 +10,7 @@ import (
"bytes" "bytes"
"context" "context"
"fmt" "fmt"
"github.com/gogf/gf/v2/container/gset"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings" "strings"
@ -43,6 +44,7 @@ type (
Prefix string `name:"prefix" short:"f" brief:"{CGenPbEntityBriefPrefix}"` Prefix string `name:"prefix" short:"f" brief:"{CGenPbEntityBriefPrefix}"`
RemovePrefix string `name:"removePrefix" short:"r" brief:"{CGenPbEntityBriefRemovePrefix}"` RemovePrefix string `name:"removePrefix" short:"r" brief:"{CGenPbEntityBriefRemovePrefix}"`
RemoveFieldPrefix string `name:"removeFieldPrefix" short:"rf" brief:"{CGenPbEntityBriefRemoveFieldPrefix}"` RemoveFieldPrefix string `name:"removeFieldPrefix" short:"rf" brief:"{CGenPbEntityBriefRemoveFieldPrefix}"`
TablesEx string `name:"tablesEx" short:"x" brief:"{CGenDaoBriefTablesEx}"`
NameCase string `name:"nameCase" short:"n" brief:"{CGenPbEntityBriefNameCase}" d:"Camel"` NameCase string `name:"nameCase" short:"n" brief:"{CGenPbEntityBriefNameCase}" d:"Camel"`
JsonCase string `name:"jsonCase" short:"j" brief:"{CGenPbEntityBriefJsonCase}" d:"none"` JsonCase string `name:"jsonCase" short:"j" brief:"{CGenPbEntityBriefJsonCase}" d:"none"`
Option string `name:"option" short:"o" brief:"{CGenPbEntityBriefOption}"` Option string `name:"option" short:"o" brief:"{CGenPbEntityBriefOption}"`
@ -115,6 +117,7 @@ CONFIGURATION SUPPORT
CGenPbEntityBriefTables = `generate models only for given tables, multiple table names separated with ','` CGenPbEntityBriefTables = `generate models only for given tables, multiple table names separated with ','`
CGenPbEntityBriefPrefix = `add specified prefix for all entity names and entity proto files` CGenPbEntityBriefPrefix = `add specified prefix for all entity names and entity proto files`
CGenPbEntityBriefRemovePrefix = `remove specified prefix of the table, multiple prefix separated with ','` CGenPbEntityBriefRemovePrefix = `remove specified prefix of the table, multiple prefix separated with ','`
CGenPbEntityBriefTablesEx = `generate all models exclude the specified tables, multiple prefix separated with ','`
CGenPbEntityBriefRemoveFieldPrefix = `remove specified prefix of the field, multiple prefix separated with ','` CGenPbEntityBriefRemoveFieldPrefix = `remove specified prefix of the field, multiple prefix separated with ','`
CGenPbEntityBriefOption = `extra protobuf options` CGenPbEntityBriefOption = `extra protobuf options`
CGenPbEntityBriefGroup = ` CGenPbEntityBriefGroup = `
@ -240,6 +243,7 @@ func init() {
`CGenPbEntityBriefTables`: CGenPbEntityBriefTables, `CGenPbEntityBriefTables`: CGenPbEntityBriefTables,
`CGenPbEntityBriefPrefix`: CGenPbEntityBriefPrefix, `CGenPbEntityBriefPrefix`: CGenPbEntityBriefPrefix,
`CGenPbEntityBriefRemovePrefix`: CGenPbEntityBriefRemovePrefix, `CGenPbEntityBriefRemovePrefix`: CGenPbEntityBriefRemovePrefix,
`CGenPbEntityBriefTablesEx`: CGenPbEntityBriefTablesEx,
`CGenPbEntityBriefRemoveFieldPrefix`: CGenPbEntityBriefRemoveFieldPrefix, `CGenPbEntityBriefRemoveFieldPrefix`: CGenPbEntityBriefRemoveFieldPrefix,
`CGenPbEntityBriefGroup`: CGenPbEntityBriefGroup, `CGenPbEntityBriefGroup`: CGenPbEntityBriefGroup,
`CGenPbEntityBriefNameCase`: CGenPbEntityBriefNameCase, `CGenPbEntityBriefNameCase`: CGenPbEntityBriefNameCase,
@ -290,6 +294,9 @@ func doGenPbEntityForArray(ctx context.Context, index int, in CGenPbEntityInput)
in.Package = modName + "/" + defaultPackageSuffix in.Package = modName + "/" + defaultPackageSuffix
} }
removePrefixArray := gstr.SplitAndTrim(in.RemovePrefix, ",") removePrefixArray := gstr.SplitAndTrim(in.RemovePrefix, ",")
excludeTables := gset.NewStrSetFrom(gstr.SplitAndTrim(in.TablesEx, ","))
// It uses user passed database configuration. // It uses user passed database configuration.
if in.Link != "" { if in.Link != "" {
var ( var (
@ -331,6 +338,9 @@ func doGenPbEntityForArray(ctx context.Context, index int, in CGenPbEntityInput)
} }
for _, tableName := range tableNames { for _, tableName := range tableNames {
if excludeTables.Contains(tableName) {
continue
}
newTableName := tableName newTableName := tableName
for _, v := range removePrefixArray { for _, v := range removePrefixArray {
newTableName = gstr.TrimLeftStr(newTableName, v, 1) newTableName = gstr.TrimLeftStr(newTableName, v, 1)