Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .github/workflows/ci-main.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#!/usr/bin/env bash

# Define the latest Go version requirement
LATEST_GO_VERSION="1.23"

coverage=$1

# find all path that contains go.mod.
Expand Down
21 changes: 14 additions & 7 deletions .github/workflows/ci-main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,21 @@ concurrency:

env:
TZ: "Asia/Shanghai"

# for unit testing cases of some components that only execute on the latest go version.
LATEST_GO_VERSION: "1.23"

jobs:
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

# Service containers to run with `code-test`
Expand Down Expand Up @@ -185,11 +196,6 @@ jobs:
ports:
- 2181:2181

strategy:
matrix:
go-version: [ "1.20", "1.21", "1.22", "1.23" ]
goarch: [ "386", "amd64" ]

steps:
# TODO: szenius/set-timezone update to node16
# sudo ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
Expand Down Expand Up @@ -256,7 +262,8 @@ jobs:

- name: Report Coverage
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:
flags: go-${{ matrix.go-version }}-${{ matrix.goarch }}
token: ${{ secrets.CODECOV_TOKEN }}
11 changes: 8 additions & 3 deletions .github/workflows/ci-sub.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,22 @@ concurrency:

env:
TZ: "Asia/Shanghai"

# for unit testing cases of some components that only execute on the latest go version.
LATEST_GO_VERSION: "1.23"

jobs:
code-test:
runs-on: ubuntu-latest

strategy:
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" ]
goarch: [ "386", "amd64" ]

runs-on: ubuntu-latest

steps:
- name: Setup Timezone
uses: szenius/[email protected]
Expand Down
1 change: 0 additions & 1 deletion cmd/gf/go.work
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@ replace (
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/sqlite/v2 => ../../contrib/drivers/sqlite
github.com/gogf/gf/contrib/drivers/dm/v2 => ../../contrib/drivers/dm
github.com/gogf/gf/v2 => ../../
)
81 changes: 81 additions & 0 deletions cmd/gf/internal/cmd/cmd_z_unit_gen_pbentity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]))
}
})
}
10 changes: 10 additions & 0 deletions cmd/gf/internal/cmd/genpbentity/genpbentity.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"bytes"
"context"
"fmt"
"github.com/gogf/gf/v2/container/gset"
"path/filepath"
"regexp"
"strings"
Expand Down Expand Up @@ -43,6 +44,7 @@ type (
Prefix string `name:"prefix" short:"f" brief:"{CGenPbEntityBriefPrefix}"`
RemovePrefix string `name:"removePrefix" short:"r" brief:"{CGenPbEntityBriefRemovePrefix}"`
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"`
JsonCase string `name:"jsonCase" short:"j" brief:"{CGenPbEntityBriefJsonCase}" d:"none"`
Option string `name:"option" short:"o" brief:"{CGenPbEntityBriefOption}"`
Expand Down Expand Up @@ -115,6 +117,7 @@ CONFIGURATION SUPPORT
CGenPbEntityBriefTables = `generate models only for given tables, multiple table names separated with ','`
CGenPbEntityBriefPrefix = `add specified prefix for all entity names and entity proto files`
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 ','`
CGenPbEntityBriefOption = `extra protobuf options`
CGenPbEntityBriefGroup = `
Expand Down Expand Up @@ -240,6 +243,7 @@ func init() {
`CGenPbEntityBriefTables`: CGenPbEntityBriefTables,
`CGenPbEntityBriefPrefix`: CGenPbEntityBriefPrefix,
`CGenPbEntityBriefRemovePrefix`: CGenPbEntityBriefRemovePrefix,
`CGenPbEntityBriefTablesEx`: CGenPbEntityBriefTablesEx,
`CGenPbEntityBriefRemoveFieldPrefix`: CGenPbEntityBriefRemoveFieldPrefix,
`CGenPbEntityBriefGroup`: CGenPbEntityBriefGroup,
`CGenPbEntityBriefNameCase`: CGenPbEntityBriefNameCase,
Expand Down Expand Up @@ -290,6 +294,9 @@ func doGenPbEntityForArray(ctx context.Context, index int, in CGenPbEntityInput)
in.Package = modName + "/" + defaultPackageSuffix
}
removePrefixArray := gstr.SplitAndTrim(in.RemovePrefix, ",")

excludeTables := gset.NewStrSetFrom(gstr.SplitAndTrim(in.TablesEx, ","))

// It uses user passed database configuration.
if in.Link != "" {
var (
Expand Down Expand Up @@ -331,6 +338,9 @@ func doGenPbEntityForArray(ctx context.Context, index int, in CGenPbEntityInput)
}

for _, tableName := range tableNames {
if excludeTables.Contains(tableName) {
continue
}
newTableName := tableName
for _, v := range removePrefixArray {
newTableName = gstr.TrimLeftStr(newTableName, v, 1)
Expand Down
Loading