mirror of
https://gitee.com/johng/gf
synced 2026-06-08 02:27:42 +08:00
Compare commits
39 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b11caba5b0 | |||
| 8824b8b0fe | |||
| f8272bc5f4 | |||
| e8a2629b19 | |||
| dba6c08548 | |||
| 7d464d4de2 | |||
| 4abb32e11b | |||
| 74d0945fa1 | |||
| ffbe9a7197 | |||
| fba878f47a | |||
| 88f188d0f9 | |||
| 2ccbb02c71 | |||
| 01408f1a87 | |||
| dea0ffcfb3 | |||
| d5825ab51c | |||
| 753965b9a9 | |||
| ee211dbdac | |||
| 4d916ae73e | |||
| 59a775aad4 | |||
| e3a00d7f56 | |||
| bd8d046b83 | |||
| 57583a98bc | |||
| f8175e7113 | |||
| 6b84a37794 | |||
| 457f7abcf5 | |||
| 9e9e42b45c | |||
| 8f4d5f7d74 | |||
| b675d01418 | |||
| 17385eeaef | |||
| 23df83cb0b | |||
| 2e471662f4 | |||
| 1e8f4287af | |||
| 21c83670d6 | |||
| 1b23bf495a | |||
| 15b60462a2 | |||
| 1455440efa | |||
| 737da954a0 | |||
| a9a0785252 | |||
| d5561aa323 |
6
.github/workflows/ci-main.sh
vendored
6
.github/workflows/ci-main.sh
vendored
@ -52,6 +52,12 @@ for file in `find . -name go.mod`; do
|
||||
echo "ignore example as go version: $(go version)"
|
||||
continue 1
|
||||
fi
|
||||
echo "the example directory only needs to be built, not unit tests and coverage tests."
|
||||
cd $dirpath
|
||||
go mod tidy
|
||||
go build ./...
|
||||
cd -
|
||||
continue 1
|
||||
fi
|
||||
|
||||
# package otlpgrpc needs golang >= v1.20
|
||||
|
||||
3
.github/workflows/ci-main.yml
vendored
3
.github/workflows/ci-main.yml
vendored
@ -190,8 +190,9 @@ jobs:
|
||||
|
||||
steps:
|
||||
# TODO: szenius/set-timezone update to node16
|
||||
# sudo ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
|
||||
- name: Setup Timezone
|
||||
uses: szenius/set-timezone@v1.1
|
||||
uses: szenius/set-timezone@v2.0
|
||||
with:
|
||||
timezoneLinux: "Asia/Shanghai"
|
||||
|
||||
|
||||
2
.github/workflows/ci-sub.yml
vendored
2
.github/workflows/ci-sub.yml
vendored
@ -42,7 +42,7 @@ jobs:
|
||||
|
||||
steps:
|
||||
- name: Setup Timezone
|
||||
uses: szenius/set-timezone@v1.1
|
||||
uses: szenius/set-timezone@v2.0
|
||||
with:
|
||||
timezoneLinux: "Asia/Shanghai"
|
||||
|
||||
|
||||
4
.github/workflows/golangci-lint.yml
vendored
4
.github/workflows/golangci-lint.yml
vendored
@ -47,8 +47,8 @@ jobs:
|
||||
with:
|
||||
go-version: ${{ matrix.go-version }}
|
||||
- name: golangci-lint
|
||||
uses: golangci/golangci-lint-action@v4
|
||||
uses: golangci/golangci-lint-action@v6
|
||||
with:
|
||||
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
||||
version: v1.56.2
|
||||
version: v1.58.2
|
||||
args: --timeout 3m0s
|
||||
|
||||
@ -38,7 +38,7 @@ linters:
|
||||
# Custom enable linters we want to use.
|
||||
enable:
|
||||
- errcheck # Errcheck is a program for checking for unchecked errors in go programs.
|
||||
- errchkjson # Checks types passed to the json encoding functions. Reports unsupported types and optionally reports occasions, where the check for the returned error can be omitted.
|
||||
- errchkjson # Checks types passed to the JSON encoding functions. Reports unsupported types and optionally reports occasions, where the check for the returned error can be omitted.
|
||||
- funlen # Tool for detection of long functions
|
||||
- goconst # Finds repeated strings that could be replaced by a constant
|
||||
- gocritic # Provides diagnostics that check for bugs, performance and style issues.
|
||||
@ -172,10 +172,6 @@ linters-settings:
|
||||
|
||||
# https://golangci-lint.run/usage/linters/#gosimple
|
||||
gosimple:
|
||||
# Select the Go version to target.
|
||||
# Default: 1.13
|
||||
# Deprecated: use the global `run.go` instead.
|
||||
go: "1.15"
|
||||
# Sxxxx checks in https://staticcheck.io/docs/configuration/options/#checks
|
||||
# Default: ["*"]
|
||||
checks: [
|
||||
@ -184,9 +180,6 @@ linters-settings:
|
||||
|
||||
# https://golangci-lint.run/usage/linters/#govet
|
||||
govet:
|
||||
# Report about shadowed variables.
|
||||
# Default: false
|
||||
check-shadowing: true
|
||||
# Settings per analyzer.
|
||||
settings:
|
||||
# Analyzer name, run `go tool vet help` to see all analyzers.
|
||||
@ -259,10 +252,6 @@ linters-settings:
|
||||
|
||||
# https://golangci-lint.run/usage/linters/#staticcheck
|
||||
staticcheck:
|
||||
# Select the Go version to target.
|
||||
# Default: "1.13"
|
||||
# Deprecated: use the global `run.go` instead.
|
||||
go: "1.20"
|
||||
# SAxxxx checks in https://staticcheck.io/docs/configuration/options/#checks
|
||||
# Default: ["*"]
|
||||
checks: [ "all","-SA1019","-SA4015","-SA1029","-SA1016","-SA9003","-SA4006","-SA6003" ]
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
|
||||
pack: pack.template-single pack.template-mono
|
||||
pack: pack.template-single pack.template-mono pack.template-mono-app
|
||||
|
||||
pack.template-single:
|
||||
@rm -fr temp
|
||||
@ -15,4 +15,18 @@ pack.template-mono:
|
||||
@cd temp && git clone https://github.com/gogf/template-mono
|
||||
@rm -fr temp/template-mono/.git
|
||||
@cd temp && gf pack template-mono ../internal/packed/template-mono.go -n=packed -y
|
||||
@rm -fr temp
|
||||
|
||||
pack.template-mono-app:
|
||||
@rm -fr temp
|
||||
@mkdir temp || exit 0
|
||||
@cd temp && git clone https://github.com/gogf/template-single
|
||||
@cd temp && mv template-single template-mono-app
|
||||
@rm -fr temp/template-mono-app/.git
|
||||
@rm -fr temp/template-mono-app/.gitattributes
|
||||
@rm -fr temp/template-mono-app/.gitignore
|
||||
@rm -fr temp/template-mono-app/go.mod
|
||||
@rm -fr temp/template-mono-app/go.sum
|
||||
@sed -i 's/template-single/template-mono-app/g' temp/template-mono-app/main.go
|
||||
@cd temp && gf pack template-mono-app ../internal/packed/template-mono-app.go -n=packed -y
|
||||
@rm -fr temp
|
||||
@ -88,6 +88,7 @@ func GetCommand(ctx context.Context) (*Command, error) {
|
||||
cmd.Docker,
|
||||
cmd.Install,
|
||||
cmd.Version,
|
||||
cmd.Doc,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@ -3,13 +3,14 @@ module github.com/gogf/gf/cmd/gf/v2
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/gogf/gf/contrib/drivers/clickhouse/v2 v2.7.1
|
||||
github.com/gogf/gf/contrib/drivers/mssql/v2 v2.7.1
|
||||
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.7.1
|
||||
github.com/gogf/gf/contrib/drivers/oracle/v2 v2.7.1
|
||||
github.com/gogf/gf/contrib/drivers/pgsql/v2 v2.7.1
|
||||
github.com/gogf/gf/contrib/drivers/sqlite/v2 v2.7.1
|
||||
github.com/gogf/gf/v2 v2.7.1
|
||||
github.com/gogf/gf/contrib/drivers/clickhouse/v2 v2.7.2
|
||||
github.com/gogf/gf/contrib/drivers/dm/v2 v2.7.1
|
||||
github.com/gogf/gf/contrib/drivers/mssql/v2 v2.7.2
|
||||
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.7.2
|
||||
github.com/gogf/gf/contrib/drivers/oracle/v2 v2.7.2
|
||||
github.com/gogf/gf/contrib/drivers/pgsql/v2 v2.7.2
|
||||
github.com/gogf/gf/contrib/drivers/sqlite/v2 v2.7.2
|
||||
github.com/gogf/gf/v2 v2.7.2
|
||||
github.com/gogf/selfupdate v0.0.0-20231215043001-5c48c528462f
|
||||
github.com/olekukonko/tablewriter v0.0.5
|
||||
golang.org/x/mod v0.17.0
|
||||
@ -18,20 +19,22 @@ require (
|
||||
|
||||
require (
|
||||
aead.dev/minisign v0.2.0 // indirect
|
||||
gitee.com/chunanyong/dm v1.8.12 // indirect
|
||||
github.com/BurntSushi/toml v1.3.2 // indirect
|
||||
github.com/ClickHouse/clickhouse-go/v2 v2.0.15 // indirect
|
||||
github.com/clbanning/mxj/v2 v2.7.0 // indirect
|
||||
github.com/denisenkom/go-mssqldb v0.12.3 // indirect
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
github.com/emirpasic/gods v1.18.1 // indirect
|
||||
github.com/fatih/color v1.16.0 // indirect
|
||||
github.com/fsnotify/fsnotify v1.7.0 // indirect
|
||||
github.com/glebarez/go-sqlite v1.21.2 // indirect
|
||||
github.com/go-logr/logr v1.2.4 // indirect
|
||||
github.com/go-logr/stdr v1.2.2 // indirect
|
||||
github.com/go-sql-driver/mysql v1.7.1 // indirect
|
||||
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe // indirect
|
||||
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
|
||||
github.com/golang-sql/sqlexp v0.1.0 // indirect
|
||||
github.com/google/uuid v1.3.0 // indirect
|
||||
github.com/golang/snappy v0.0.1 // indirect
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/gorilla/websocket v1.5.1 // indirect
|
||||
github.com/grokify/html-strip-tags-go v0.1.0 // indirect
|
||||
github.com/lib/pq v1.10.9 // indirect
|
||||
@ -39,6 +42,7 @@ require (
|
||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/mattn/go-runewidth v0.0.15 // indirect
|
||||
github.com/microsoft/go-mssqldb v1.7.1 // indirect
|
||||
github.com/paulmach/orb v0.7.1 // indirect
|
||||
github.com/pierrec/lz4/v4 v4.1.14 // indirect
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||
|
||||
@ -1,8 +1,13 @@
|
||||
aead.dev/minisign v0.2.0 h1:kAWrq/hBRu4AARY6AlciO83xhNnW9UaC8YipS2uhLPk=
|
||||
aead.dev/minisign v0.2.0/go.mod h1:zdq6LdSd9TbuSxchxwhpA9zEb9YXcVGoE8JakuiGaIQ=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/azcore v0.19.0/go.mod h1:h6H6c8enJmmocHUbLiiGY6sx7f9i+X3m1CHdd5c6Rdw=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/azidentity v0.11.0/go.mod h1:HcM1YX14R7CJcghJGOYCgdezslRSVzqwLf/q+4Y2r/0=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/internal v0.7.0/go.mod h1:yqy467j36fJxcRV2TzfVZ1pCb5vxm4BtZPUdYWe/Xo8=
|
||||
gitee.com/chunanyong/dm v1.8.12 h1:WupbFZL0MRNIIiCPaLDHgFi5jkdkjzjPReuWPaInGwk=
|
||||
gitee.com/chunanyong/dm v1.8.12/go.mod h1:EPRJnuPFgbyOFgJ0TRYCTGzhq+ZT4wdyaj/GW/LLcNg=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.9.1 h1:lGlwhPtrX6EVml1hO0ivjkUxsSyl4dsiw9qcA1k/3IQ=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.5.1 h1:sO0/P7g68FrryJzljemN+6GTssUXdANk6aJ7T1ZxnsQ=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/internal v1.5.1 h1:6oNBlSdi1QqM1PNW7FPA6xOGA5UNsXnkaYZz9vdPGhA=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/azkeys v1.0.1 h1:MyVTgWR8qd/Jw1Le0NZebGBUCLbtak3bJ3z1OlqZBpw=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/internal v1.0.0 h1:D3occbWoio4EBLkbkevetNMAVX197GkzbUMtqjGWn80=
|
||||
github.com/AzureAD/microsoft-authentication-library-for-go v1.2.1 h1:DzHpqpoJVaCgOUdVHxE8QB52S6NiVdDQvGlny1qvPqA=
|
||||
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
|
||||
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
||||
github.com/ClickHouse/clickhouse-go v1.5.4/go.mod h1:EaI/sW7Azgz9UATzd5ZdZHRUhHgv5+JMS9NSr2smCJI=
|
||||
@ -15,12 +20,10 @@ github.com/clbanning/mxj/v2 v2.7.0/go.mod h1:hNiWqW14h+kc+MdF9C6/YoRfjEJoR3ou6tn
|
||||
github.com/cloudflare/golz4 v0.0.0-20150217214814-ef862a3cdc58/go.mod h1:EOBUe0h4xcZ5GoxqC5SDxFQ8gwyZPKQoEzownBlhI80=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/denisenkom/go-mssqldb v0.12.3 h1:pBSGx9Tq67pBOTLmxNuirNTeB8Vjmf886Kx+8Y+8shw=
|
||||
github.com/denisenkom/go-mssqldb v0.12.3/go.mod h1:k0mtMFOnU+AihqFxPMiF05rtiDrorD1Vrm1KEz5hxDo=
|
||||
github.com/dnaeon/go-vcr v1.2.0/go.mod h1:R4UdLID7HZT3taECzJs4YgbbH6PIGXB6W/sc5OLb6RQ=
|
||||
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
||||
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
|
||||
github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
|
||||
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
|
||||
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
|
||||
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
|
||||
@ -38,20 +41,26 @@ github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiU
|
||||
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
|
||||
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
|
||||
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
|
||||
github.com/gogf/gf/contrib/drivers/dm/v2 v2.7.1 h1:8zvXrUCH2ZE/3ZzH+T4Zt1/7H5VIKn2/jM8Qw59V3yg=
|
||||
github.com/gogf/gf/contrib/drivers/dm/v2 v2.7.1/go.mod h1:VbtQfqPKJIl8VUCT8po5M5JRlpP0CdFtdV1vZLOB3II=
|
||||
github.com/gogf/selfupdate v0.0.0-20231215043001-5c48c528462f h1:7xfXR/BhG3JDqO1s45n65Oyx9t4E/UqDOXep6jXdLCM=
|
||||
github.com/gogf/selfupdate v0.0.0-20231215043001-5c48c528462f/go.mod h1:HnYoio6S7VaFJdryKcD/r9HgX+4QzYfr00XiXUo/xz0=
|
||||
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
|
||||
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZkZR4hgp4KJVfY3nMkvmwbVkpv1rVY=
|
||||
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0=
|
||||
github.com/golang-jwt/jwt/v5 v5.2.0 h1:d/ix8ftRUorsN+5eMIlF4T6J8CAt9rch3My2winC1Jw=
|
||||
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 h1:au07oEsX2xN0ktxqI+Sida1w446QrXBRJ0nee3SNZlA=
|
||||
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0=
|
||||
github.com/golang-sql/sqlexp v0.1.0 h1:ZCD6MBpcuOVfGVqsEmY5/4FtYiKz6tSyUv9LPEDei6A=
|
||||
github.com/golang-sql/sqlexp v0.1.0/go.mod h1:J4ad9Vo8ZCWQ2GMrC4UCQy1JpCbwU9m3EOqtpKwwwHI=
|
||||
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
||||
github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
|
||||
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
|
||||
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
|
||||
github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbuBVKCudVG457BR2GZFIz3uw3hQ=
|
||||
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
|
||||
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/gorilla/handlers v1.4.2/go.mod h1:Qkdc/uu4tH4g6mTK6auzZ766c4CA0Ng8+o/OAirnOIQ=
|
||||
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
|
||||
@ -61,6 +70,7 @@ github.com/grokify/html-strip-tags-go v0.1.0/go.mod h1:ZdzgfHEzAfz9X6Xe5eBLVblWI
|
||||
github.com/jmoiron/sqlx v1.2.0/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks=
|
||||
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
|
||||
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
|
||||
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
|
||||
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
|
||||
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
|
||||
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||
@ -75,8 +85,9 @@ github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m
|
||||
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
|
||||
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
||||
github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
|
||||
github.com/microsoft/go-mssqldb v1.7.1 h1:KU/g8aWeM3Hx7IMOFpiwYiUkU+9zeISb4+tx3ScVfsM=
|
||||
github.com/microsoft/go-mssqldb v1.7.1/go.mod h1:kOvZKUdrhhFQmxLZqbwUV0rHkNkZpthMITIb2Ko1IoA=
|
||||
github.com/mkevac/debugcharts v0.0.0-20191222103121-ae1c48aa8615/go.mod h1:Ad7oeElCZqA1Ufj0U9/liOF4BtVepxRcTvr2ey7zTvM=
|
||||
github.com/modocache/gover v0.0.0-20171022184752-b58185e213c5/go.mod h1:caMODM3PzxT8aQXRPkAt8xlV/e7d7w8GM5g0fa5F0D8=
|
||||
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
|
||||
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
|
||||
github.com/paulmach/orb v0.7.1 h1:Zha++Z5OX/l168sqHK3k4z18LDvr+YAO/VjK0ReQ9rU=
|
||||
@ -85,7 +96,7 @@ github.com/paulmach/protoscan v0.2.1/go.mod h1:SpcSwydNLrxUGSDvXvO0P7g7AuhJ7lcKf
|
||||
github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
|
||||
github.com/pierrec/lz4/v4 v4.1.14 h1:+fL8AQEZtz/ijeNnpduH0bROTu0O3NZAlPjQxGn8LwE=
|
||||
github.com/pierrec/lz4/v4 v4.1.14/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
|
||||
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4/go.mod h1:4OwLy04Bl9Ef3GJJCoec+30X3LQs/0/m4HFRt/2LUSA=
|
||||
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmdv1U2eRNDiU2ErMBj1gwrq8eQ=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||
@ -104,10 +115,9 @@ github.com/sijms/go-ora/v2 v2.7.10/go.mod h1:EHxlY6x7y9HAsdfumurRfTd+v8NrEOTR3Xl
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
|
||||
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
|
||||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||
github.com/tklauser/go-sysconf v0.3.10/go.mod h1:C8XykCvCb+Gn0oNCWPIlcb0RuglQTYaQ2hGm7jmxEFk=
|
||||
github.com/tklauser/numcpus v0.4.0/go.mod h1:1+UI3pD8NW14VMwdgJNJ1ESk2UnwhAnz5hMwiKKqXCQ=
|
||||
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
@ -124,9 +134,7 @@ go.opentelemetry.io/otel/trace v1.14.0/go.mod h1:8avnQLK+CG77yNLUae4ea2JDQ6iT+go
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
|
||||
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
|
||||
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
|
||||
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
|
||||
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
@ -137,8 +145,6 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||
golang.org/x/net v0.0.0-20210610132358-84b48f89b13b/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
|
||||
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
@ -152,10 +158,7 @@ golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7w
|
||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20191220220014-0732a990476f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210228012217-479acdf4ea46/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220429233432-b5fbb4746d32/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
@ -163,10 +166,9 @@ golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
|
||||
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
|
||||
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
@ -184,10 +186,7 @@ google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
modernc.org/libc v1.22.5 h1:91BNch/e5B0uPbJFgqbxXuOnxBQjlS//icfQEGmvyjE=
|
||||
|
||||
@ -16,5 +16,6 @@ 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 => ../../
|
||||
)
|
||||
|
||||
177
cmd/gf/internal/cmd/cmd_doc.go
Normal file
177
cmd/gf/internal/cmd/cmd_doc.go
Normal file
@ -0,0 +1,177 @@
|
||||
// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog"
|
||||
"github.com/gogf/gf/v2/encoding/gcompress"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gfile"
|
||||
)
|
||||
|
||||
const (
|
||||
// DocURL is the download address of the document
|
||||
DocURL = "https://github.com/gogf/gf/archive/refs/heads/gh-pages.zip"
|
||||
)
|
||||
|
||||
var (
|
||||
Doc = cDoc{}
|
||||
)
|
||||
|
||||
type cDoc struct {
|
||||
g.Meta `name:"doc" brief:"download https://pages.goframe.org/ to run locally"`
|
||||
}
|
||||
|
||||
type cDocInput struct {
|
||||
g.Meta `name:"doc" config:"gfcli.doc"`
|
||||
Path string `short:"p" name:"path" brief:"download docs directory path, default is \"%temp%/goframe\""`
|
||||
Port int `short:"o" name:"port" brief:"http server port, default is 8080" d:"8080"`
|
||||
Update bool `short:"u" name:"update" brief:"clean docs directory and update docs"`
|
||||
Clean bool `short:"c" name:"clean" brief:"clean docs directory"`
|
||||
Proxy string `short:"x" name:"proxy" brief:"proxy for download, such as https://hub.gitmirror.com/;https://ghproxy.com/;https://ghproxy.net/;https://ghps.cc/"`
|
||||
}
|
||||
|
||||
type cDocOutput struct{}
|
||||
|
||||
func (c cDoc) Index(ctx context.Context, in cDocInput) (out *cDocOutput, err error) {
|
||||
docs := NewDocSetting(ctx, in)
|
||||
mlog.Print("Directory where the document is downloaded:", docs.TempDir)
|
||||
if in.Clean {
|
||||
mlog.Print("Cleaning document directory")
|
||||
err = docs.Clean()
|
||||
if err != nil {
|
||||
mlog.Print("Failed to clean document directory:", err)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
if in.Update {
|
||||
mlog.Print("Cleaning old document directory")
|
||||
err = docs.Clean()
|
||||
if err != nil {
|
||||
mlog.Print("Failed to clean old document directory:", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
err = docs.DownloadDoc()
|
||||
if err != nil {
|
||||
mlog.Print("Failed to download document:", err)
|
||||
return
|
||||
}
|
||||
s := g.Server()
|
||||
s.SetServerRoot(docs.DocDir)
|
||||
s.SetPort(in.Port)
|
||||
s.SetDumpRouterMap(false)
|
||||
mlog.Printf("Access address http://127.0.0.1:%d", in.Port)
|
||||
s.Run()
|
||||
return
|
||||
}
|
||||
|
||||
// DocSetting doc setting
|
||||
type DocSetting struct {
|
||||
TempDir string
|
||||
DocURL string
|
||||
DocDir string
|
||||
DocZipFile string
|
||||
}
|
||||
|
||||
// NewDocSetting new DocSetting
|
||||
func NewDocSetting(ctx context.Context, in cDocInput) *DocSetting {
|
||||
fileName := "gf-doc-md.zip"
|
||||
tempDir := in.Path
|
||||
if tempDir == "" {
|
||||
tempDir = gfile.Temp("goframe/docs")
|
||||
} else {
|
||||
tempDir = gfile.Abs(path.Join(tempDir, "docs"))
|
||||
}
|
||||
|
||||
return &DocSetting{
|
||||
TempDir: filepath.FromSlash(tempDir),
|
||||
DocDir: filepath.FromSlash(path.Join(tempDir, "gf-gh-pages")),
|
||||
DocURL: in.Proxy + DocURL,
|
||||
DocZipFile: filepath.FromSlash(path.Join(tempDir, fileName)),
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Clean clean the temporary directory
|
||||
func (d *DocSetting) Clean() error {
|
||||
if _, err := os.Stat(d.TempDir); err == nil {
|
||||
err = gfile.Remove(d.TempDir)
|
||||
if err != nil {
|
||||
mlog.Print("Failed to delete temporary directory:", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DownloadDoc download the document
|
||||
func (d *DocSetting) DownloadDoc() error {
|
||||
if _, err := os.Stat(d.TempDir); err != nil {
|
||||
err = gfile.Mkdir(d.TempDir)
|
||||
if err != nil {
|
||||
mlog.Print("Failed to create temporary directory:", err)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
// Check if the file exists
|
||||
if _, err := os.Stat(d.DocDir); err == nil {
|
||||
mlog.Print("Document already exists, no need to download and unzip")
|
||||
return nil
|
||||
}
|
||||
|
||||
if _, err := os.Stat(d.DocZipFile); err == nil {
|
||||
mlog.Print("File already exists, no need to download")
|
||||
} else {
|
||||
mlog.Printf("File does not exist, start downloading: %s", d.DocURL)
|
||||
startTime := time.Now()
|
||||
// Download the file
|
||||
resp, err := http.Get(d.DocURL)
|
||||
if err != nil {
|
||||
mlog.Print("Failed to download file:", err)
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// Create the file
|
||||
out, err := os.Create(d.DocZipFile)
|
||||
if err != nil {
|
||||
mlog.Print("Failed to create file:", err)
|
||||
return err
|
||||
}
|
||||
defer out.Close()
|
||||
|
||||
// Write the response body to the file
|
||||
_, err = io.Copy(out, resp.Body)
|
||||
if err != nil {
|
||||
mlog.Print("Failed to write file:", err)
|
||||
return err
|
||||
}
|
||||
mlog.Printf("Download successful, time-consuming: %v", time.Since(startTime))
|
||||
}
|
||||
|
||||
mlog.Print("Start unzipping the file...")
|
||||
// Unzip the file
|
||||
err := gcompress.UnZipFile(d.DocZipFile, d.TempDir)
|
||||
if err != nil {
|
||||
mlog.Print("Failed to unzip the file, please run again:", err)
|
||||
gfile.Remove(d.DocZipFile)
|
||||
return err
|
||||
}
|
||||
|
||||
mlog.Print("Download and unzip successful")
|
||||
return nil
|
||||
}
|
||||
@ -8,6 +8,7 @@ package cmd
|
||||
|
||||
import (
|
||||
_ "github.com/gogf/gf/contrib/drivers/clickhouse/v2"
|
||||
_ "github.com/gogf/gf/contrib/drivers/dm/v2"
|
||||
_ "github.com/gogf/gf/contrib/drivers/mssql/v2"
|
||||
_ "github.com/gogf/gf/contrib/drivers/mysql/v2"
|
||||
_ "github.com/gogf/gf/contrib/drivers/oracle/v2"
|
||||
|
||||
@ -12,6 +12,9 @@ import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/cmd/gf/v2/internal/utility/allyes"
|
||||
"github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog"
|
||||
"github.com/gogf/gf/cmd/gf/v2/internal/utility/utils"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gcmd"
|
||||
"github.com/gogf/gf/v2/os/gfile"
|
||||
@ -19,9 +22,6 @@ import (
|
||||
"github.com/gogf/gf/v2/os/gres"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
"github.com/gogf/gf/v2/util/gtag"
|
||||
|
||||
"github.com/gogf/gf/cmd/gf/v2/internal/utility/allyes"
|
||||
"github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -34,13 +34,15 @@ type cInit struct {
|
||||
}
|
||||
|
||||
const (
|
||||
cInitRepoPrefix = `github.com/gogf/`
|
||||
cInitMonoRepo = `template-mono`
|
||||
cInitSingleRepo = `template-single`
|
||||
cInitBrief = `create and initialize an empty GoFrame project`
|
||||
cInitEg = `
|
||||
cInitRepoPrefix = `github.com/gogf/`
|
||||
cInitMonoRepo = `template-mono`
|
||||
cInitMonoRepoApp = `template-mono-app`
|
||||
cInitSingleRepo = `template-single`
|
||||
cInitBrief = `create and initialize an empty GoFrame project`
|
||||
cInitEg = `
|
||||
gf init my-project
|
||||
gf init my-mono-repo -m
|
||||
gf init my-mono-repo -a
|
||||
`
|
||||
cInitNameBrief = `
|
||||
name for the project. It will create a folder with NAME in current directory.
|
||||
@ -61,11 +63,12 @@ func init() {
|
||||
}
|
||||
|
||||
type cInitInput struct {
|
||||
g.Meta `name:"init"`
|
||||
Name string `name:"NAME" arg:"true" v:"required" brief:"{cInitNameBrief}"`
|
||||
Mono bool `name:"mono" short:"m" brief:"initialize a mono-repo instead a single-repo" orphan:"true"`
|
||||
Update bool `name:"update" short:"u" brief:"update to the latest goframe version" orphan:"true"`
|
||||
Module string `name:"module" short:"g" brief:"custom go module"`
|
||||
g.Meta `name:"init"`
|
||||
Name string `name:"NAME" arg:"true" v:"required" brief:"{cInitNameBrief}"`
|
||||
Mono bool `name:"mono" short:"m" brief:"initialize a mono-repo instead a single-repo" orphan:"true"`
|
||||
MonoApp bool `name:"monoApp" short:"a" brief:"initialize a mono-repo-app instead a single-repo" orphan:"true"`
|
||||
Update bool `name:"update" short:"u" brief:"update to the latest goframe version" orphan:"true"`
|
||||
Module string `name:"module" short:"g" brief:"custom go module"`
|
||||
}
|
||||
|
||||
type cInitOutput struct{}
|
||||
@ -86,11 +89,15 @@ func (c cInit) Index(ctx context.Context, in cInitInput) (out *cInitOutput, err
|
||||
templateRepoName string
|
||||
gitignoreFile = in.Name + "/" + cInitGitignore
|
||||
)
|
||||
|
||||
if in.Mono {
|
||||
templateRepoName = cInitMonoRepo
|
||||
} else if in.MonoApp {
|
||||
templateRepoName = cInitMonoRepoApp
|
||||
} else {
|
||||
templateRepoName = cInitSingleRepo
|
||||
}
|
||||
|
||||
err = gres.Export(templateRepoName, in.Name, gres.ExportOption{
|
||||
RemovePrefix: templateRepoName,
|
||||
})
|
||||
@ -101,7 +108,8 @@ func (c cInit) Index(ctx context.Context, in cInitInput) (out *cInitOutput, err
|
||||
// build ignoreFiles from the .gitignore file
|
||||
ignoreFiles := make([]string, 0, 10)
|
||||
ignoreFiles = append(ignoreFiles, cInitGitDir)
|
||||
if overwrote {
|
||||
// in.MonoApp is a mono-repo-app, it should ignore the .gitignore file
|
||||
if overwrote && !in.MonoApp {
|
||||
err = gfile.ReadLines(gitignoreFile, func(line string) error {
|
||||
// Add only hidden files or directories
|
||||
// If other directories are added, it may cause the entire directory to be ignored
|
||||
@ -118,10 +126,14 @@ func (c cInit) Index(ctx context.Context, in cInitInput) (out *cInitOutput, err
|
||||
}
|
||||
}
|
||||
|
||||
// Replace module name.
|
||||
// Get template name and module name.
|
||||
if in.Module == "" {
|
||||
in.Module = gfile.Basename(gfile.RealPath(in.Name))
|
||||
}
|
||||
if in.MonoApp {
|
||||
pwd := gfile.Pwd() + string(os.PathSeparator) + in.Name
|
||||
in.Module = utils.GetImportPath(pwd)
|
||||
}
|
||||
|
||||
// Replace template name to project name.
|
||||
err = gfile.ReplaceDirFunc(func(path, content string) string {
|
||||
|
||||
@ -63,7 +63,7 @@ func init() {
|
||||
}
|
||||
|
||||
type cPackInput struct {
|
||||
g.Meta `name:"pack"`
|
||||
g.Meta `name:"pack" config:"gfcli.pack"`
|
||||
Src string `name:"SRC" arg:"true" v:"required" brief:"{cPackSrcBrief}"`
|
||||
Dst string `name:"DST" arg:"true" v:"required" brief:"{cPackDstBrief}"`
|
||||
Name string `name:"name" short:"n" brief:"{cPackNameBrief}"`
|
||||
|
||||
@ -9,6 +9,7 @@ package cmd
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
@ -55,7 +56,7 @@ The "run" command is used for running go codes with hot-compiled-like feature,
|
||||
which compiles and runs the go codes asynchronously when codes change.
|
||||
`
|
||||
cRunFileBrief = `building file path.`
|
||||
cRunPathBrief = `output directory path for built binary file. it's "manifest/output" in default`
|
||||
cRunPathBrief = `output directory path for built binary file. it's "./" in default`
|
||||
cRunExtraBrief = `the same options as "go run"/"go build" except some options as follows defined`
|
||||
cRunArgsBrief = `custom arguments for your process`
|
||||
cRunWatchPathsBrief = `watch additional paths for live reload, separated by ",". i.e. "manifest/config/*.yaml"`
|
||||
@ -81,7 +82,7 @@ func init() {
|
||||
|
||||
type (
|
||||
cRunInput struct {
|
||||
g.Meta `name:"run"`
|
||||
g.Meta `name:"run" config:"gfcli.run"`
|
||||
File string `name:"FILE" arg:"true" brief:"{cRunFileBrief}" v:"required"`
|
||||
Path string `name:"path" short:"p" brief:"{cRunPathBrief}" d:"./"`
|
||||
Extra string `name:"extra" short:"e" brief:"{cRunExtraBrief}"`
|
||||
@ -104,13 +105,14 @@ func (c cRun) Index(ctx context.Context, in cRunInput) (out *cRunOutput, err err
|
||||
|
||||
app := &cRunApp{
|
||||
File: in.File,
|
||||
Path: in.Path,
|
||||
Path: filepath.FromSlash(in.Path),
|
||||
Options: in.Extra,
|
||||
Args: in.Args,
|
||||
WatchPaths: in.WatchPaths,
|
||||
}
|
||||
dirty := gtype.NewBool()
|
||||
|
||||
var outputPath = app.genOutputPath()
|
||||
callbackFunc := func(event *gfsnotify.Event) {
|
||||
if gfile.ExtName(event.Path) != "go" {
|
||||
return
|
||||
@ -125,7 +127,7 @@ func (c cRun) Index(ctx context.Context, in cRunInput) (out *cRunOutput, err err
|
||||
gtimer.SetTimeout(ctx, 1500*gtime.MS, func(ctx context.Context) {
|
||||
defer dirty.Set(false)
|
||||
mlog.Printf(`watched file changes: %s`, event.String())
|
||||
app.Run(ctx)
|
||||
app.Run(ctx, outputPath)
|
||||
})
|
||||
}
|
||||
|
||||
@ -143,24 +145,21 @@ func (c cRun) Index(ctx context.Context, in cRunInput) (out *cRunOutput, err err
|
||||
}
|
||||
}
|
||||
|
||||
go app.Run(ctx)
|
||||
go app.Run(ctx, outputPath)
|
||||
|
||||
gproc.AddSigHandlerShutdown(func(sig os.Signal) {
|
||||
app.End(ctx, sig, outputPath)
|
||||
os.Exit(0)
|
||||
})
|
||||
gproc.Listen()
|
||||
|
||||
select {}
|
||||
}
|
||||
|
||||
func (app *cRunApp) Run(ctx context.Context) {
|
||||
func (app *cRunApp) Run(ctx context.Context, outputPath string) {
|
||||
// Rebuild and run the codes.
|
||||
renamePath := ""
|
||||
mlog.Printf("build: %s", app.File)
|
||||
outputPath := gfile.Join(app.Path, gfile.Name(app.File))
|
||||
if runtime.GOOS == "windows" {
|
||||
outputPath += ".exe"
|
||||
if gfile.Exists(outputPath) {
|
||||
renamePath = outputPath + "~"
|
||||
if err := gfile.Rename(outputPath, renamePath); err != nil {
|
||||
mlog.Print(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// In case of `pipe: too many open files` error.
|
||||
// Build the app.
|
||||
buildCommand := fmt.Sprintf(
|
||||
@ -198,6 +197,36 @@ func (app *cRunApp) Run(ctx context.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
func (app *cRunApp) End(ctx context.Context, sig os.Signal, outputPath string) {
|
||||
// Delete the binary file.
|
||||
// firstly, kill the process.
|
||||
if process != nil {
|
||||
if err := process.Kill(); err != nil {
|
||||
mlog.Debugf("kill process error: %s", err.Error())
|
||||
}
|
||||
}
|
||||
if err := gfile.Remove(outputPath); err != nil {
|
||||
mlog.Printf("delete binary file error: %s", err.Error())
|
||||
} else {
|
||||
mlog.Printf("deleted binary file: %s", outputPath)
|
||||
}
|
||||
}
|
||||
|
||||
func (app *cRunApp) genOutputPath() (outputPath string) {
|
||||
var renamePath string
|
||||
outputPath = gfile.Join(app.Path, gfile.Name(app.File))
|
||||
if runtime.GOOS == "windows" {
|
||||
outputPath += ".exe"
|
||||
if gfile.Exists(outputPath) {
|
||||
renamePath = outputPath + "~"
|
||||
if err := gfile.Rename(outputPath, renamePath); err != nil {
|
||||
mlog.Print(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
return filepath.FromSlash(outputPath)
|
||||
}
|
||||
|
||||
func matchWatchPaths(watchPaths []string, eventPath string) bool {
|
||||
for _, path := range watchPaths {
|
||||
absPath, err := filepath.Abs(path)
|
||||
|
||||
@ -47,7 +47,7 @@ gf tpl parse -p ./template -v values.json -o ./template.parsed
|
||||
|
||||
type (
|
||||
cTplParseInput struct {
|
||||
g.Meta `name:"parse" brief:"{cTplParseBrief}" eg:"{cTplParseEg}"`
|
||||
g.Meta `name:"parse" config:"gfcli.tpl.parse" brief:"{cTplParseBrief}" eg:"{cTplParseEg}"`
|
||||
Path string `name:"path" short:"p" brief:"template file or folder path" v:"required"`
|
||||
Pattern string `name:"pattern" short:"n" brief:"template file pattern when path is a folder, default is:*" d:"*"`
|
||||
Recursive bool `name:"recursive" short:"c" brief:"recursively parsing files if path is folder, default is:true" d:"true"`
|
||||
|
||||
@ -48,7 +48,7 @@ func init() {
|
||||
}
|
||||
|
||||
type cUpInput struct {
|
||||
g.Meta `name:"up" config:"gfcli.up"`
|
||||
g.Meta `name:"up" config:"gfcli.up"`
|
||||
All bool `name:"all" short:"a" brief:"upgrade both version and cli, auto fix codes" orphan:"true"`
|
||||
Cli bool `name:"cli" short:"c" brief:"also upgrade CLI tool" orphan:"true"`
|
||||
Fix bool `name:"fix" short:"f" brief:"auto fix codes(it only make sense if cli is to be upgraded)" orphan:"true"`
|
||||
|
||||
@ -83,56 +83,12 @@ func Test_Gen_Ctrl_Default(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
// https://github.com/gogf/gf/issues/3460
|
||||
func Test_Gen_Ctrl_UseMerge_Issue3460(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var (
|
||||
ctrlPath = gfile.Temp(guid.S())
|
||||
//ctrlPath = gtest.DataPath("issue", "3460", "controller")
|
||||
apiFolder = gtest.DataPath("issue", "3460", "api")
|
||||
in = genctrl.CGenCtrlInput{
|
||||
SrcFolder: apiFolder,
|
||||
DstFolder: ctrlPath,
|
||||
WatchFile: "",
|
||||
SdkPath: "",
|
||||
SdkStdVersion: false,
|
||||
SdkNoV1: false,
|
||||
Clear: false,
|
||||
Merge: true,
|
||||
}
|
||||
)
|
||||
|
||||
err := gfile.Mkdir(ctrlPath)
|
||||
t.AssertNil(err)
|
||||
defer gfile.Remove(ctrlPath)
|
||||
|
||||
_, err = genctrl.CGenCtrl{}.Ctrl(ctx, in)
|
||||
t.AssertNil(err)
|
||||
|
||||
files, err := gfile.ScanDir(ctrlPath, "*.go", true)
|
||||
t.AssertNil(err)
|
||||
t.Assert(files, []string{
|
||||
filepath.Join(ctrlPath, "/hello/hello.go"),
|
||||
filepath.Join(ctrlPath, "/hello/hello_new.go"),
|
||||
filepath.Join(ctrlPath, "/hello/hello_v1_req.go"),
|
||||
filepath.Join(ctrlPath, "/hello/hello_v2_req.go"),
|
||||
})
|
||||
|
||||
expectCtrlPath := gtest.DataPath("issue", "3460", "controller")
|
||||
expectFiles := []string{
|
||||
filepath.Join(expectCtrlPath, "/hello/hello.go"),
|
||||
filepath.Join(expectCtrlPath, "/hello/hello_new.go"),
|
||||
filepath.Join(expectCtrlPath, "/hello/hello_v1_req.go"),
|
||||
filepath.Join(expectCtrlPath, "/hello/hello_v2_req.go"),
|
||||
}
|
||||
|
||||
// Line Feed maybe \r\n or \n
|
||||
for i, expectFile := range expectFiles {
|
||||
val := gfile.GetContents(files[i])
|
||||
expect := gfile.GetContents(expectFile)
|
||||
t.Assert(val, expect)
|
||||
}
|
||||
})
|
||||
func expectFilesContent(t *gtest.T, paths []string, expectPaths []string) {
|
||||
for i, expectFile := range expectPaths {
|
||||
val := gfile.GetContents(paths[i])
|
||||
expect := gfile.GetContents(expectFile)
|
||||
t.Assert(val, expect)
|
||||
}
|
||||
}
|
||||
|
||||
// gf gen ctrl -m
|
||||
@ -300,10 +256,54 @@ type DictTypeAddRes struct {
|
||||
|
||||
}
|
||||
|
||||
func expectFilesContent(t *gtest.T, paths []string, expectPaths []string) {
|
||||
for i, expectFile := range expectPaths {
|
||||
val := gfile.GetContents(paths[i])
|
||||
expect := gfile.GetContents(expectFile)
|
||||
t.Assert(val, expect)
|
||||
}
|
||||
// https://github.com/gogf/gf/issues/3460
|
||||
func Test_Gen_Ctrl_UseMerge_Issue3460(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var (
|
||||
ctrlPath = gfile.Temp(guid.S())
|
||||
//ctrlPath = gtest.DataPath("issue", "3460", "controller")
|
||||
apiFolder = gtest.DataPath("issue", "3460", "api")
|
||||
in = genctrl.CGenCtrlInput{
|
||||
SrcFolder: apiFolder,
|
||||
DstFolder: ctrlPath,
|
||||
WatchFile: "",
|
||||
SdkPath: "",
|
||||
SdkStdVersion: false,
|
||||
SdkNoV1: false,
|
||||
Clear: false,
|
||||
Merge: true,
|
||||
}
|
||||
)
|
||||
|
||||
err := gfile.Mkdir(ctrlPath)
|
||||
t.AssertNil(err)
|
||||
defer gfile.Remove(ctrlPath)
|
||||
|
||||
_, err = genctrl.CGenCtrl{}.Ctrl(ctx, in)
|
||||
t.AssertNil(err)
|
||||
|
||||
files, err := gfile.ScanDir(ctrlPath, "*.go", true)
|
||||
t.AssertNil(err)
|
||||
t.Assert(files, []string{
|
||||
filepath.Join(ctrlPath, "/hello/hello.go"),
|
||||
filepath.Join(ctrlPath, "/hello/hello_new.go"),
|
||||
filepath.Join(ctrlPath, "/hello/hello_v1_req.go"),
|
||||
filepath.Join(ctrlPath, "/hello/hello_v2_req.go"),
|
||||
})
|
||||
|
||||
expectCtrlPath := gtest.DataPath("issue", "3460", "controller")
|
||||
expectFiles := []string{
|
||||
filepath.Join(expectCtrlPath, "/hello/hello.go"),
|
||||
filepath.Join(expectCtrlPath, "/hello/hello_new.go"),
|
||||
filepath.Join(expectCtrlPath, "/hello/hello_v1_req.go"),
|
||||
filepath.Join(expectCtrlPath, "/hello/hello_v2_req.go"),
|
||||
}
|
||||
|
||||
// Line Feed maybe \r\n or \n
|
||||
for i, expectFile := range expectFiles {
|
||||
val := gfile.GetContents(files[i])
|
||||
expect := gfile.GetContents(expectFile)
|
||||
t.Assert(val, expect)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@ -57,12 +57,16 @@ func Test_Gen_Service_Default(t *testing.T) {
|
||||
t.AssertNil(err)
|
||||
t.Assert(files, []string{
|
||||
dstFolder + filepath.FromSlash("/article.go"),
|
||||
dstFolder + filepath.FromSlash("/delivery.go"),
|
||||
dstFolder + filepath.FromSlash("/user.go"),
|
||||
})
|
||||
|
||||
// contents
|
||||
testPath := gtest.DataPath("genservice", "service")
|
||||
expectFiles := []string{
|
||||
testPath + filepath.FromSlash("/article.go"),
|
||||
testPath + filepath.FromSlash("/delivery.go"),
|
||||
testPath + filepath.FromSlash("/user.go"),
|
||||
}
|
||||
for i := range files {
|
||||
t.Assert(gfile.GetContents(files[i]), gfile.GetContents(expectFiles[i]))
|
||||
|
||||
78
cmd/gf/internal/cmd/genctrl/genctrl_ast_parse.go
Normal file
78
cmd/gf/internal/cmd/genctrl/genctrl_ast_parse.go
Normal file
@ -0,0 +1,78 @@
|
||||
// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package genctrl
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"go/ast"
|
||||
"go/parser"
|
||||
"go/printer"
|
||||
"go/token"
|
||||
|
||||
"github.com/gogf/gf/v2/os/gfile"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
)
|
||||
|
||||
// getStructsNameInSrc retrieves all struct names
|
||||
// that end in "Req" and have "g.Meta" in their body.
|
||||
func (c CGenCtrl) getStructsNameInSrc(filePath string) (structsName []string, err error) {
|
||||
var (
|
||||
fileContent = gfile.GetContents(filePath)
|
||||
fileSet = token.NewFileSet()
|
||||
)
|
||||
|
||||
node, err := parser.ParseFile(fileSet, "", fileContent, parser.ParseComments)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
ast.Inspect(node, func(n ast.Node) bool {
|
||||
if typeSpec, ok := n.(*ast.TypeSpec); ok {
|
||||
methodName := typeSpec.Name.Name
|
||||
if !gstr.HasSuffix(methodName, "Req") {
|
||||
// ignore struct name that do not end in "Req"
|
||||
return true
|
||||
}
|
||||
if structType, ok := typeSpec.Type.(*ast.StructType); ok {
|
||||
var buf bytes.Buffer
|
||||
if err := printer.Fprint(&buf, fileSet, structType); err != nil {
|
||||
return false
|
||||
}
|
||||
// ignore struct name that match a request, but has no g.Meta in its body.
|
||||
if !gstr.Contains(buf.String(), `g.Meta`) {
|
||||
return true
|
||||
}
|
||||
structsName = append(structsName, methodName)
|
||||
}
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// getImportsInDst retrieves all import paths in the file.
|
||||
func (c CGenCtrl) getImportsInDst(filePath string) (imports []string, err error) {
|
||||
var (
|
||||
fileContent = gfile.GetContents(filePath)
|
||||
fileSet = token.NewFileSet()
|
||||
)
|
||||
|
||||
node, err := parser.ParseFile(fileSet, "", fileContent, parser.ParseComments)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
ast.Inspect(node, func(n ast.Node) bool {
|
||||
if imp, ok := n.(*ast.ImportSpec); ok {
|
||||
imports = append(imports, imp.Path.Value)
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
43
cmd/gf/internal/cmd/genctrl/genctrl_ast_parse_clear.go
Normal file
43
cmd/gf/internal/cmd/genctrl/genctrl_ast_parse_clear.go
Normal file
@ -0,0 +1,43 @@
|
||||
// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package genctrl
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"go/ast"
|
||||
"go/parser"
|
||||
"go/printer"
|
||||
"go/token"
|
||||
|
||||
"github.com/gogf/gf/v2/os/gfile"
|
||||
)
|
||||
|
||||
// getFuncInDst retrieves all function declarations and bodies in the file.
|
||||
func (c *controllerClearer) getFuncInDst(filePath string) (funcs []string, err error) {
|
||||
var (
|
||||
fileContent = gfile.GetContents(filePath)
|
||||
fileSet = token.NewFileSet()
|
||||
)
|
||||
|
||||
node, err := parser.ParseFile(fileSet, "", fileContent, parser.ParseComments)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
ast.Inspect(node, func(n ast.Node) bool {
|
||||
if fun, ok := n.(*ast.FuncDecl); ok {
|
||||
var buf bytes.Buffer
|
||||
if err := printer.Fprint(&buf, fileSet, fun); err != nil {
|
||||
return false
|
||||
}
|
||||
funcs = append(funcs, buf.String())
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
@ -7,12 +7,6 @@
|
||||
package genctrl
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"go/ast"
|
||||
"go/parser"
|
||||
"go/printer"
|
||||
"go/token"
|
||||
|
||||
"github.com/gogf/gf/cmd/gf/v2/internal/utility/utils"
|
||||
"github.com/gogf/gf/v2/os/gfile"
|
||||
"github.com/gogf/gf/v2/text/gregex"
|
||||
@ -139,63 +133,3 @@ func (c CGenCtrl) getApiItemsInDst(dstFolder string) (items []apiItem, err error
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// getStructsNameInSrc retrieves all struct names
|
||||
// that end in "Req" and have "g.Meta" in their body.
|
||||
func (c CGenCtrl) getStructsNameInSrc(filePath string) (structsName []string, err error) {
|
||||
var (
|
||||
fileContent = gfile.GetContents(filePath)
|
||||
fileSet = token.NewFileSet()
|
||||
)
|
||||
|
||||
node, err := parser.ParseFile(fileSet, "", fileContent, parser.ParseComments)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
ast.Inspect(node, func(n ast.Node) bool {
|
||||
if typeSpec, ok := n.(*ast.TypeSpec); ok {
|
||||
methodName := typeSpec.Name.Name
|
||||
if !gstr.HasSuffix(methodName, "Req") {
|
||||
// ignore struct name that do not end in "Req"
|
||||
return true
|
||||
}
|
||||
if structType, ok := typeSpec.Type.(*ast.StructType); ok {
|
||||
var buf bytes.Buffer
|
||||
if err := printer.Fprint(&buf, fileSet, structType); err != nil {
|
||||
return false
|
||||
}
|
||||
// ignore struct name that match a request, but has no g.Meta in its body.
|
||||
if !gstr.Contains(buf.String(), `g.Meta`) {
|
||||
return true
|
||||
}
|
||||
structsName = append(structsName, methodName)
|
||||
}
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// getImportsInDst retrieves all import paths in the file.
|
||||
func (c CGenCtrl) getImportsInDst(filePath string) (imports []string, err error) {
|
||||
var (
|
||||
fileContent = gfile.GetContents(filePath)
|
||||
fileSet = token.NewFileSet()
|
||||
)
|
||||
|
||||
node, err := parser.ParseFile(fileSet, "", fileContent, parser.ParseComments)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
ast.Inspect(node, func(n ast.Node) bool {
|
||||
if imp, ok := n.(*ast.ImportSpec); ok {
|
||||
imports = append(imports, imp.Path.Value)
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@ -11,7 +11,6 @@ import (
|
||||
|
||||
"github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog"
|
||||
"github.com/gogf/gf/v2/os/gfile"
|
||||
"github.com/gogf/gf/v2/text/gregex"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
)
|
||||
|
||||
@ -36,17 +35,16 @@ func (c *controllerClearer) doClear(dstModuleFolderPath string, item apiItem) (e
|
||||
methodFilePath = gfile.Join(dstModuleFolderPath, fmt.Sprintf(
|
||||
`%s_%s_%s.go`, item.Module, item.Version, methodNameSnake,
|
||||
))
|
||||
fileContent = gstr.Trim(gfile.GetContents(methodFilePath))
|
||||
)
|
||||
// retrieve it without using AST, because it's simple.
|
||||
match, err := gregex.MatchString(`.+?Req.+?Res.+?{([\s\S]+?)}`, fileContent)
|
||||
|
||||
funcs, err := c.getFuncInDst(methodFilePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(match) > 1 {
|
||||
implements := gstr.Trim(match[1])
|
||||
|
||||
if len(funcs) > 1 {
|
||||
// One line.
|
||||
if !gstr.Contains(implements, "\n") && gstr.Contains(implements, `CodeNotImplemented`) {
|
||||
if !gstr.Contains(funcs[0], "\n") && gstr.Contains(funcs[0], `CodeNotImplemented`) {
|
||||
mlog.Printf(
|
||||
`remove unimplemented and of no api definitions controller file: %s`,
|
||||
methodFilePath,
|
||||
|
||||
@ -118,6 +118,7 @@ generated json tag case for model struct, cases are as follows:
|
||||
tplVarGroupName = `{TplGroupName}`
|
||||
tplVarDatetimeStr = `{TplDatetimeStr}`
|
||||
tplVarCreatedAtDatetimeStr = `{TplCreatedAtDatetimeStr}`
|
||||
tplVarPackageName = `{TplPackageName}`
|
||||
)
|
||||
|
||||
var (
|
||||
|
||||
@ -58,8 +58,8 @@ func generateDaoSingle(ctx context.Context, in generateDaoSingleInput) {
|
||||
mlog.Fatalf(`fetching tables fields failed for table "%s": %+v`, in.TableName, err)
|
||||
}
|
||||
var (
|
||||
tableNameCamelCase = gstr.CaseCamel(in.NewTableName)
|
||||
tableNameCamelLowerCase = gstr.CaseCamelLower(in.NewTableName)
|
||||
tableNameCamelCase = gstr.CaseCamel(strings.ToLower(in.NewTableName))
|
||||
tableNameCamelLowerCase = gstr.CaseCamelLower(strings.ToLower(in.NewTableName))
|
||||
tableNameSnakeCase = gstr.CaseSnake(in.NewTableName)
|
||||
importPrefix = in.ImportPrefix
|
||||
)
|
||||
@ -116,6 +116,7 @@ func generateDaoIndex(in generateDaoIndexInput) {
|
||||
tplVarTableName: in.TableName,
|
||||
tplVarTableNameCamelCase: in.TableNameCamelCase,
|
||||
tplVarTableNameCamelLowerCase: in.TableNameCamelLowerCase,
|
||||
tplVarPackageName: filepath.Base(in.DaoPath),
|
||||
})
|
||||
indexContent = replaceDefaultVar(in.CGenDaoInternalInput, indexContent)
|
||||
if err := gfile.PutContents(path, strings.TrimSpace(indexContent)); err != nil {
|
||||
@ -178,7 +179,7 @@ func generateColumnNamesForDao(fieldMap map[string]*gdb.TableField, removeFieldP
|
||||
}
|
||||
|
||||
array[index] = []string{
|
||||
" #" + gstr.CaseCamel(newFiledName) + ":",
|
||||
" #" + gstr.CaseCamel(strings.ToLower(newFiledName)) + ":",
|
||||
fmt.Sprintf(` #"%s",`, field.Name),
|
||||
}
|
||||
}
|
||||
@ -218,7 +219,7 @@ func generateColumnDefinitionForDao(fieldMap map[string]*gdb.TableField, removeF
|
||||
newFiledName = gstr.TrimLeftStr(newFiledName, v, 1)
|
||||
}
|
||||
array[index] = []string{
|
||||
" #" + gstr.CaseCamel(newFiledName),
|
||||
" #" + gstr.CaseCamel(strings.ToLower(newFiledName)),
|
||||
" # " + "string",
|
||||
" #" + fmt.Sprintf(`// %s`, comment),
|
||||
}
|
||||
|
||||
@ -40,7 +40,7 @@ func generateDo(ctx context.Context, in CGenDaoInternalInput) {
|
||||
structDefinition, _ = generateStructDefinition(ctx, generateStructDefinitionInput{
|
||||
CGenDaoInternalInput: in,
|
||||
TableName: tableName,
|
||||
StructName: gstr.CaseCamel(newTableName),
|
||||
StructName: gstr.CaseCamel(strings.ToLower(newTableName)),
|
||||
FieldMap: fieldMap,
|
||||
IsDo: true,
|
||||
})
|
||||
@ -61,7 +61,7 @@ func generateDo(ctx context.Context, in CGenDaoInternalInput) {
|
||||
ctx,
|
||||
in,
|
||||
tableName,
|
||||
gstr.CaseCamel(newTableName),
|
||||
gstr.CaseCamel(strings.ToLower(newTableName)),
|
||||
structDefinition,
|
||||
)
|
||||
in.genItems.AppendGeneratedFilePath(doFilePath)
|
||||
@ -85,6 +85,7 @@ func generateDoContent(
|
||||
tplVarPackageImports: getImportPartContent(ctx, structDefine, true, nil),
|
||||
tplVarTableNameCamelCase: tableNameCamelCase,
|
||||
tplVarStructDefine: structDefine,
|
||||
tplVarPackageName: filepath.Base(in.DoPath),
|
||||
},
|
||||
)
|
||||
doContent = replaceDefaultVar(in, doContent)
|
||||
|
||||
@ -36,7 +36,7 @@ func generateEntity(ctx context.Context, in CGenDaoInternalInput) {
|
||||
structDefinition, appendImports = generateStructDefinition(ctx, generateStructDefinitionInput{
|
||||
CGenDaoInternalInput: in,
|
||||
TableName: tableName,
|
||||
StructName: gstr.CaseCamel(newTableName),
|
||||
StructName: gstr.CaseCamel(strings.ToLower(newTableName)),
|
||||
FieldMap: fieldMap,
|
||||
IsDo: false,
|
||||
})
|
||||
@ -44,7 +44,7 @@ func generateEntity(ctx context.Context, in CGenDaoInternalInput) {
|
||||
ctx,
|
||||
in,
|
||||
newTableName,
|
||||
gstr.CaseCamel(newTableName),
|
||||
gstr.CaseCamel(strings.ToLower(newTableName)),
|
||||
structDefinition,
|
||||
appendImports,
|
||||
)
|
||||
@ -70,6 +70,7 @@ func generateEntityContent(
|
||||
tplVarPackageImports: getImportPartContent(ctx, structDefine, false, appendImports),
|
||||
tplVarTableNameCamelCase: tableNameCamelCase,
|
||||
tplVarStructDefine: structDefine,
|
||||
tplVarPackageName: filepath.Base(in.EntityPath),
|
||||
},
|
||||
)
|
||||
entityContent = replaceDefaultVar(in, entityContent)
|
||||
|
||||
@ -140,7 +140,7 @@ func generateStructFieldDefinition(
|
||||
}
|
||||
|
||||
attrLines = []string{
|
||||
" #" + gstr.CaseCamel(newFiledName),
|
||||
" #" + gstr.CaseCamel(strings.ToLower(newFiledName)),
|
||||
" #" + localTypeNameStr,
|
||||
}
|
||||
attrLines = append(attrLines, fmt.Sprintf(` #%sjson:"%s"`, tagKey, jsonTag))
|
||||
|
||||
@ -10,7 +10,11 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog"
|
||||
"github.com/gogf/gf/cmd/gf/v2/internal/utility/utils"
|
||||
"github.com/gogf/gf/v2/container/garray"
|
||||
"github.com/gogf/gf/v2/container/gmap"
|
||||
"github.com/gogf/gf/v2/container/gset"
|
||||
@ -21,9 +25,6 @@ import (
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"github.com/gogf/gf/v2/util/gtag"
|
||||
|
||||
"github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog"
|
||||
"github.com/gogf/gf/cmd/gf/v2/internal/utility/utils"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -147,19 +148,22 @@ func (c CGenService) Service(ctx context.Context, in CGenServiceInput) (out *CGe
|
||||
}
|
||||
|
||||
var (
|
||||
isDirty bool // Temp boolean.
|
||||
isDirty atomic.Value // Temp boolean.
|
||||
files []string // Temp file array.
|
||||
fileContent string // Temp file content for handling go file.
|
||||
initImportSrcPackages []string // Used for generating logic.go.
|
||||
inputPackages = in.Packages // Custom packages.
|
||||
dstPackageName = gstr.ToLower(gfile.Basename(in.DstFolder)) // Package name for generated go files.
|
||||
generatedDstFilePathSet = gset.NewStrSet() // All generated file path set.
|
||||
)
|
||||
isDirty.Store(false)
|
||||
|
||||
// The first level folders.
|
||||
srcFolderPaths, err := gfile.ScanDir(in.SrcFolder, "*", false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// it will use goroutine to generate service files for each package.
|
||||
var wg = sync.WaitGroup{}
|
||||
for _, srcFolderPath := range srcFolderPaths {
|
||||
if !gfile.IsDir(srcFolderPath) {
|
||||
continue
|
||||
@ -173,111 +177,30 @@ func (c CGenService) Service(ctx context.Context, in CGenServiceInput) (out *CGe
|
||||
}
|
||||
// Parse single logic package folder.
|
||||
var (
|
||||
// StructName => FunctionDefinitions
|
||||
srcPkgInterfaceMap = gmap.NewListMap()
|
||||
srcImportedPackages = garray.NewSortedStrArray().SetUnique(true)
|
||||
importAliasToPathMap = gmap.NewStrStrMap() // for conflict imports check. alias => import path(with `"`)
|
||||
importPathToAliasMap = gmap.NewStrStrMap() // for conflict imports check. import path(with `"`) => alias
|
||||
srcPackageName = gfile.Basename(srcFolderPath)
|
||||
ok bool
|
||||
dstFilePath = gfile.Join(in.DstFolder,
|
||||
srcPackageName = gfile.Basename(srcFolderPath)
|
||||
srcImportedPackages = garray.NewSortedStrArray().SetUnique(true)
|
||||
srcStructFunctions = gmap.NewListMap()
|
||||
dstFilePath = gfile.Join(in.DstFolder,
|
||||
c.getDstFileNameCase(srcPackageName, in.DstFileNameCase)+".go",
|
||||
)
|
||||
srcCodeCommentedMap = make(map[string]string)
|
||||
)
|
||||
generatedDstFilePathSet.Add(dstFilePath)
|
||||
// if it were to use goroutine,
|
||||
// it would cause the order of the generated functions in the file to be disordered.
|
||||
for _, file := range files {
|
||||
var packageItems []packageItem
|
||||
fileContent = gfile.GetContents(file)
|
||||
// Calculate code comments in source Go files.
|
||||
err = c.calculateCodeCommented(in, fileContent, srcCodeCommentedMap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// remove all comments.
|
||||
fileContent, err = gregex.ReplaceString(`(//.*)|((?s)/\*.*?\*/)`, "", fileContent)
|
||||
pkgItems, funcItems, err := c.parseItemsInSrc(file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Calculate imported packages of source go files.
|
||||
packageItems, err = c.calculateImportedPackages(fileContent)
|
||||
// Calculate imported packages for service generating.
|
||||
err = c.calculateImportedItems(in, pkgItems, funcItems, srcImportedPackages)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// try finding the conflicts imports between files.
|
||||
for _, item := range packageItems {
|
||||
var alias = item.Alias
|
||||
if alias == "" {
|
||||
alias = gfile.Basename(gstr.Trim(item.Path, `"`))
|
||||
}
|
||||
|
||||
// ignore unused import paths, which do not exist in function definitions.
|
||||
if !gregex.IsMatchString(fmt.Sprintf(`func .+?([^\w])%s(\.\w+).+?{`, alias), fileContent) {
|
||||
mlog.Debugf(`ignore unused package: %s`, item.RawImport)
|
||||
continue
|
||||
}
|
||||
|
||||
// find the exist alias with the same import path.
|
||||
var existAlias = importPathToAliasMap.Get(item.Path)
|
||||
if existAlias != "" {
|
||||
fileContent, err = gregex.ReplaceStringFuncMatch(
|
||||
fmt.Sprintf(`([^\w])%s(\.\w+)`, alias), fileContent,
|
||||
func(match []string) string {
|
||||
return match[1] + existAlias + match[2]
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// resolve alias conflicts.
|
||||
var importPath = importAliasToPathMap.Get(alias)
|
||||
if importPath == "" {
|
||||
importAliasToPathMap.Set(alias, item.Path)
|
||||
importPathToAliasMap.Set(item.Path, alias)
|
||||
srcImportedPackages.Add(item.RawImport)
|
||||
continue
|
||||
}
|
||||
if importPath != item.Path {
|
||||
// update the conflicted alias for import path with suffix.
|
||||
// eg:
|
||||
// v1 -> v10
|
||||
// v11 -> v110
|
||||
for aliasIndex := 0; ; aliasIndex++ {
|
||||
item.Alias = fmt.Sprintf(`%s%d`, alias, aliasIndex)
|
||||
var existPathForAlias = importAliasToPathMap.Get(item.Alias)
|
||||
if existPathForAlias != "" {
|
||||
if existPathForAlias == item.Path {
|
||||
break
|
||||
}
|
||||
continue
|
||||
}
|
||||
break
|
||||
}
|
||||
importPathToAliasMap.Set(item.Path, item.Alias)
|
||||
importAliasToPathMap.Set(item.Alias, item.Path)
|
||||
// reformat the import path with alias.
|
||||
item.RawImport = fmt.Sprintf(`%s %s`, item.Alias, item.Path)
|
||||
|
||||
// update the file content with new alias import.
|
||||
fileContent, err = gregex.ReplaceStringFuncMatch(
|
||||
fmt.Sprintf(`([^\w])%s(\.\w+)`, alias), fileContent,
|
||||
func(match []string) string {
|
||||
return match[1] + item.Alias + match[2]
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
srcImportedPackages.Add(item.RawImport)
|
||||
}
|
||||
}
|
||||
|
||||
// Calculate functions and interfaces for service generating.
|
||||
err = c.calculateInterfaceFunctions(in, fileContent, srcPkgInterfaceMap)
|
||||
err = c.calculateFuncItems(in, funcItems, srcStructFunctions)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -295,22 +218,28 @@ func (c CGenService) Service(ctx context.Context, in CGenServiceInput) (out *CGe
|
||||
)
|
||||
continue
|
||||
}
|
||||
|
||||
// Generating service go file for single logic package.
|
||||
if ok, err = c.generateServiceFile(generateServiceFilesInput{
|
||||
wg.Add(1)
|
||||
go func(generateServiceFilesInput generateServiceFilesInput) {
|
||||
defer wg.Done()
|
||||
ok, err := c.generateServiceFile(generateServiceFilesInput)
|
||||
if err != nil {
|
||||
mlog.Printf(`error generating service file "%s": %v`, generateServiceFilesInput.DstFilePath, err)
|
||||
}
|
||||
if !isDirty.Load().(bool) && ok {
|
||||
isDirty.Store(true)
|
||||
}
|
||||
}(generateServiceFilesInput{
|
||||
CGenServiceInput: in,
|
||||
SrcStructFunctions: srcPkgInterfaceMap,
|
||||
SrcImportedPackages: srcImportedPackages.Slice(),
|
||||
SrcPackageName: srcPackageName,
|
||||
SrcImportedPackages: srcImportedPackages.Slice(),
|
||||
SrcStructFunctions: srcStructFunctions,
|
||||
DstPackageName: dstPackageName,
|
||||
DstFilePath: dstFilePath,
|
||||
SrcCodeCommentedMap: srcCodeCommentedMap,
|
||||
}); err != nil {
|
||||
return
|
||||
}
|
||||
if ok {
|
||||
isDirty = true
|
||||
}
|
||||
})
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
if in.Clear {
|
||||
files, err = gfile.ScanDirFile(in.DstFolder, "*.go", false)
|
||||
@ -320,7 +249,9 @@ func (c CGenService) Service(ctx context.Context, in CGenServiceInput) (out *CGe
|
||||
var relativeFilePath string
|
||||
for _, file := range files {
|
||||
relativeFilePath = gstr.SubStrFromR(file, in.DstFolder)
|
||||
if !generatedDstFilePathSet.Contains(relativeFilePath) && utils.IsFileDoNotEdit(relativeFilePath) {
|
||||
if !generatedDstFilePathSet.Contains(relativeFilePath) &&
|
||||
utils.IsFileDoNotEdit(relativeFilePath) {
|
||||
|
||||
mlog.Printf(`remove no longer used service file: %s`, relativeFilePath)
|
||||
if err = gfile.Remove(file); err != nil {
|
||||
return nil, err
|
||||
@ -329,7 +260,7 @@ func (c CGenService) Service(ctx context.Context, in CGenServiceInput) (out *CGe
|
||||
}
|
||||
}
|
||||
|
||||
if isDirty {
|
||||
if isDirty.Load().(bool) {
|
||||
// Generate initialization go file.
|
||||
if len(initImportSrcPackages) > 0 {
|
||||
if err = c.generateInitializationFile(in, initImportSrcPackages); err != nil {
|
||||
|
||||
186
cmd/gf/internal/cmd/genservice/genservice_ast_parse.go
Normal file
186
cmd/gf/internal/cmd/genservice/genservice_ast_parse.go
Normal file
@ -0,0 +1,186 @@
|
||||
// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package genservice
|
||||
|
||||
import (
|
||||
"go/ast"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
|
||||
"github.com/gogf/gf/v2/os/gfile"
|
||||
)
|
||||
|
||||
type pkgItem struct {
|
||||
Alias string `eg:"gdbas"`
|
||||
Path string `eg:"github.com/gogf/gf/v2/database/gdb"`
|
||||
RawImport string `eg:"gdbas github.com/gogf/gf/v2/database/gdb"`
|
||||
}
|
||||
|
||||
type funcItem struct {
|
||||
Receiver string `eg:"sUser"`
|
||||
MethodName string `eg:"GetList"`
|
||||
Params []map[string]string `eg:"ctx: context.Context, cond: *SearchInput"`
|
||||
Results []map[string]string `eg:"list: []*User, err: error"`
|
||||
Comment string `eg:"Get user list"`
|
||||
}
|
||||
|
||||
// parseItemsInSrc parses the pkgItem and funcItem from the specified file.
|
||||
// It can't skip the private methods.
|
||||
// It can't skip the imported packages of import alias equal to `_`.
|
||||
func (c CGenService) parseItemsInSrc(filePath string) (pkgItems []pkgItem, funcItems []funcItem, err error) {
|
||||
var (
|
||||
fileContent = gfile.GetContents(filePath)
|
||||
fileSet = token.NewFileSet()
|
||||
)
|
||||
|
||||
node, err := parser.ParseFile(fileSet, "", fileContent, parser.ParseComments)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
ast.Inspect(node, func(n ast.Node) bool {
|
||||
switch x := n.(type) {
|
||||
case *ast.ImportSpec:
|
||||
// parse the imported packages.
|
||||
pkgItems = append(pkgItems, c.parseImportPackages(x))
|
||||
|
||||
case *ast.FuncDecl:
|
||||
// parse the function items.
|
||||
if x.Recv == nil {
|
||||
return true
|
||||
}
|
||||
|
||||
var funcName = x.Name.Name
|
||||
funcItems = append(funcItems, funcItem{
|
||||
Receiver: c.parseFuncReceiverTypeName(x),
|
||||
MethodName: funcName,
|
||||
Params: c.parseFuncParams(x),
|
||||
Results: c.parseFuncResults(x),
|
||||
Comment: c.parseFuncComment(x),
|
||||
})
|
||||
}
|
||||
return true
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// parseImportPackages retrieves the imported packages from the specified ast.ImportSpec.
|
||||
func (c CGenService) parseImportPackages(node *ast.ImportSpec) (packages pkgItem) {
|
||||
if node.Path == nil {
|
||||
return
|
||||
}
|
||||
var (
|
||||
alias string
|
||||
path = node.Path.Value
|
||||
rawImport string
|
||||
)
|
||||
if node.Name != nil {
|
||||
alias = node.Name.Name
|
||||
rawImport = alias + " " + path
|
||||
} else {
|
||||
rawImport = path
|
||||
}
|
||||
return pkgItem{
|
||||
Alias: alias,
|
||||
Path: path,
|
||||
RawImport: rawImport,
|
||||
}
|
||||
}
|
||||
|
||||
// parseFuncReceiverTypeName retrieves the receiver type of the function.
|
||||
// For example:
|
||||
//
|
||||
// func(s *sArticle) -> *sArticle
|
||||
// func(s sArticle) -> sArticle
|
||||
func (c CGenService) parseFuncReceiverTypeName(node *ast.FuncDecl) (receiverType string) {
|
||||
if node.Recv == nil {
|
||||
return ""
|
||||
}
|
||||
receiverType, err := c.astExprToString(node.Recv.List[0].Type)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// parseFuncParams retrieves the input parameters of the function.
|
||||
// It returns the name and type of the input parameters.
|
||||
// For example:
|
||||
//
|
||||
// []map[string]string{paramName:ctx paramType:context.Context, paramName:info paramType:struct{}}
|
||||
func (c CGenService) parseFuncParams(node *ast.FuncDecl) (params []map[string]string) {
|
||||
if node.Type.Params == nil {
|
||||
return
|
||||
}
|
||||
for _, param := range node.Type.Params.List {
|
||||
if param.Names == nil {
|
||||
// No name for the return value.
|
||||
resultType, err := c.astExprToString(param.Type)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
params = append(params, map[string]string{
|
||||
"paramName": "",
|
||||
"paramType": resultType,
|
||||
})
|
||||
continue
|
||||
}
|
||||
for _, name := range param.Names {
|
||||
paramType, err := c.astExprToString(param.Type)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
params = append(params, map[string]string{
|
||||
"paramName": name.Name,
|
||||
"paramType": paramType,
|
||||
})
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// parseFuncResults retrieves the output parameters of the function.
|
||||
// It returns the name and type of the output parameters.
|
||||
// For example:
|
||||
//
|
||||
// []map[string]string{resultName:list resultType:[]*User, resultName:err resultType:error}
|
||||
// []map[string]string{resultName: "", resultType: error}
|
||||
func (c CGenService) parseFuncResults(node *ast.FuncDecl) (results []map[string]string) {
|
||||
if node.Type.Results == nil {
|
||||
return
|
||||
}
|
||||
for _, result := range node.Type.Results.List {
|
||||
if result.Names == nil {
|
||||
// No name for the return value.
|
||||
resultType, err := c.astExprToString(result.Type)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
results = append(results, map[string]string{
|
||||
"resultName": "",
|
||||
"resultType": resultType,
|
||||
})
|
||||
continue
|
||||
}
|
||||
for _, name := range result.Names {
|
||||
resultType, err := c.astExprToString(result.Type)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
results = append(results, map[string]string{
|
||||
"resultName": name.Name,
|
||||
"resultType": resultType,
|
||||
})
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// parseFuncComment retrieves the comment of the function.
|
||||
func (c CGenService) parseFuncComment(node *ast.FuncDecl) string {
|
||||
return c.astCommentToString(node.Doc)
|
||||
}
|
||||
48
cmd/gf/internal/cmd/genservice/genservice_ast_utils.go
Normal file
48
cmd/gf/internal/cmd/genservice/genservice_ast_utils.go
Normal file
@ -0,0 +1,48 @@
|
||||
// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package genservice
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"go/ast"
|
||||
"go/format"
|
||||
"go/token"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// exprToString converts ast.Expr to string.
|
||||
// For example:
|
||||
//
|
||||
// ast.Expr -> "context.Context"
|
||||
// ast.Expr -> "*v1.XxxReq"
|
||||
// ast.Expr -> "error"
|
||||
// ast.Expr -> "int"
|
||||
func (c CGenService) astExprToString(expr ast.Expr) (string, error) {
|
||||
var (
|
||||
buf bytes.Buffer
|
||||
err error
|
||||
)
|
||||
err = format.Node(&buf, token.NewFileSet(), expr)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return buf.String(), nil
|
||||
}
|
||||
|
||||
// astCommentToString returns the raw (original) text of the comment.
|
||||
// It includes the comment markers (//, /*, and */).
|
||||
// It adds a newline at the end of the comment.
|
||||
func (c CGenService) astCommentToString(node *ast.CommentGroup) string {
|
||||
if node == nil {
|
||||
return ""
|
||||
}
|
||||
var b strings.Builder
|
||||
for _, c := range node.List {
|
||||
b.WriteString(c.Text + "\n")
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
@ -8,168 +8,145 @@ package genservice
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog"
|
||||
"github.com/gogf/gf/v2/container/garray"
|
||||
"github.com/gogf/gf/v2/container/gmap"
|
||||
"github.com/gogf/gf/v2/os/gfile"
|
||||
"github.com/gogf/gf/v2/text/gregex"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
)
|
||||
|
||||
type packageItem struct {
|
||||
Alias string
|
||||
Path string
|
||||
RawImport string
|
||||
}
|
||||
|
||||
func (c CGenService) calculateImportedPackages(fileContent string) (packages []packageItem, err error) {
|
||||
f, err := parser.ParseFile(token.NewFileSet(), "", fileContent, parser.ImportsOnly)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
packages = make([]packageItem, 0)
|
||||
for _, s := range f.Imports {
|
||||
if s.Path != nil {
|
||||
if s.Name != nil {
|
||||
// If it has alias, and it is not `_`.
|
||||
if pkgAlias := s.Name.String(); pkgAlias != "_" {
|
||||
packages = append(packages, packageItem{
|
||||
Alias: pkgAlias,
|
||||
Path: s.Path.Value,
|
||||
RawImport: pkgAlias + " " + s.Path.Value,
|
||||
})
|
||||
}
|
||||
} else {
|
||||
// no alias
|
||||
packages = append(packages, packageItem{
|
||||
Alias: "",
|
||||
Path: s.Path.Value,
|
||||
RawImport: s.Path.Value,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
return packages, nil
|
||||
}
|
||||
|
||||
func (c CGenService) calculateCodeCommented(in CGenServiceInput, fileContent string, srcCodeCommentedMap map[string]string) error {
|
||||
matches, err := gregex.MatchAllString(`((((//.*)|(/\*[\s\S]*?\*/))\s)+)func \((.+?)\) ([\s\S]+?) {`, fileContent)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, match := range matches {
|
||||
var (
|
||||
structName string
|
||||
structMatch []string
|
||||
funcReceiver = gstr.Trim(match[1+5])
|
||||
receiverArray = gstr.SplitAndTrim(funcReceiver, " ")
|
||||
functionHead = gstr.Trim(gstr.Replace(match[2+5], "\n", ""))
|
||||
commentedInfo = ""
|
||||
)
|
||||
if len(receiverArray) > 1 {
|
||||
structName = receiverArray[1]
|
||||
} else if len(receiverArray) == 1 {
|
||||
structName = receiverArray[0]
|
||||
}
|
||||
structName = gstr.Trim(structName, "*")
|
||||
|
||||
// Case of:
|
||||
// Xxx(\n ctx context.Context, req *v1.XxxReq,\n) -> Xxx(ctx context.Context, req *v1.XxxReq)
|
||||
functionHead = gstr.Replace(functionHead, `,)`, `)`)
|
||||
functionHead, _ = gregex.ReplaceString(`\(\s+`, `(`, functionHead)
|
||||
functionHead, _ = gregex.ReplaceString(`\s{2,}`, ` `, functionHead)
|
||||
if !gstr.IsLetterUpper(functionHead[0]) {
|
||||
continue
|
||||
}
|
||||
// Match and pick the struct name from receiver.
|
||||
if structMatch, err = gregex.MatchString(in.StPattern, structName); err != nil {
|
||||
return err
|
||||
}
|
||||
if len(structMatch) < 1 {
|
||||
continue
|
||||
}
|
||||
structName = gstr.CaseCamel(structMatch[1])
|
||||
|
||||
commentedInfo = match[1]
|
||||
if len(commentedInfo) > 0 {
|
||||
srcCodeCommentedMap[fmt.Sprintf("%s-%s", structName, functionHead)] = commentedInfo
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c CGenService) calculateInterfaceFunctions(
|
||||
in CGenServiceInput, fileContent string, srcPkgInterfaceMap *gmap.ListMap,
|
||||
func (c CGenService) calculateImportedItems(
|
||||
in CGenServiceInput,
|
||||
pkgItems []pkgItem, funcItems []funcItem,
|
||||
srcImportedPackages *garray.SortedStrArray,
|
||||
) (err error) {
|
||||
var (
|
||||
matches [][]string
|
||||
srcPkgInterfaceFuncArray *garray.StrArray
|
||||
)
|
||||
// calculate struct name and its functions according function definitions.
|
||||
matches, err = gregex.MatchAllString(`func \((.+?)\) ([\s\S]+?) {`, fileContent)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, match := range matches {
|
||||
var (
|
||||
structName string
|
||||
structMatch []string
|
||||
funcReceiver = gstr.Trim(match[1])
|
||||
receiverArray = gstr.SplitAndTrim(funcReceiver, " ")
|
||||
functionHead = gstr.Trim(gstr.Replace(match[2], "\n", ""))
|
||||
)
|
||||
if len(receiverArray) > 1 {
|
||||
structName = receiverArray[1]
|
||||
} else if len(receiverArray) == 1 {
|
||||
structName = receiverArray[0]
|
||||
}
|
||||
structName = gstr.Trim(structName, "*")
|
||||
// allFuncParamType saves all the param and result types of the functions.
|
||||
var allFuncParamType strings.Builder
|
||||
|
||||
// Case of:
|
||||
// Xxx(\n ctx context.Context, req *v1.XxxReq,\n) -> Xxx(ctx context.Context, req *v1.XxxReq)
|
||||
functionHead = gstr.Replace(functionHead, `,)`, `)`)
|
||||
functionHead, _ = gregex.ReplaceString(`\(\s+`, `(`, functionHead)
|
||||
functionHead, _ = gregex.ReplaceString(`\s{2,}`, ` `, functionHead)
|
||||
if !gstr.IsLetterUpper(functionHead[0]) {
|
||||
continue
|
||||
for _, item := range funcItems {
|
||||
for _, param := range item.Params {
|
||||
allFuncParamType.WriteString(param["paramType"] + ",")
|
||||
}
|
||||
// Match and pick the struct name from receiver.
|
||||
if structMatch, err = gregex.MatchString(in.StPattern, structName); err != nil {
|
||||
return err
|
||||
for _, result := range item.Results {
|
||||
allFuncParamType.WriteString(result["resultType"] + ",")
|
||||
}
|
||||
if len(structMatch) < 1 {
|
||||
continue
|
||||
}
|
||||
structName = gstr.CaseCamel(structMatch[1])
|
||||
if !srcPkgInterfaceMap.Contains(structName) {
|
||||
srcPkgInterfaceFuncArray = garray.NewStrArray()
|
||||
srcPkgInterfaceMap.Set(structName, srcPkgInterfaceFuncArray)
|
||||
} else {
|
||||
srcPkgInterfaceFuncArray = srcPkgInterfaceMap.Get(structName).(*garray.StrArray)
|
||||
}
|
||||
srcPkgInterfaceFuncArray.Append(functionHead)
|
||||
}
|
||||
// calculate struct name according type definitions.
|
||||
matches, err = gregex.MatchAllString(`type (.+) struct\s*{`, fileContent)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, match := range matches {
|
||||
var (
|
||||
structName string
|
||||
structMatch []string
|
||||
)
|
||||
if structMatch, err = gregex.MatchString(in.StPattern, match[1]); err != nil {
|
||||
return err
|
||||
}
|
||||
if len(structMatch) < 1 {
|
||||
|
||||
for _, item := range pkgItems {
|
||||
alias := item.Alias
|
||||
|
||||
// If the alias is _, it means that the package is not generated.
|
||||
if alias == "_" {
|
||||
mlog.Debugf(`ignore anonymous package: %s`, item.RawImport)
|
||||
continue
|
||||
}
|
||||
structName = gstr.CaseCamel(structMatch[1])
|
||||
if !srcPkgInterfaceMap.Contains(structName) {
|
||||
srcPkgInterfaceMap.Set(structName, garray.NewStrArray())
|
||||
// If the alias is empty, it will use the package name as the alias.
|
||||
if alias == "" {
|
||||
alias = gfile.Basename(gstr.Trim(item.Path, `"`))
|
||||
}
|
||||
if !gstr.Contains(allFuncParamType.String(), alias) {
|
||||
mlog.Debugf(`ignore unused package: %s`, item.RawImport)
|
||||
continue
|
||||
}
|
||||
srcImportedPackages.Add(item.RawImport)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c CGenService) calculateFuncItems(
|
||||
in CGenServiceInput,
|
||||
funcItems []funcItem,
|
||||
srcPkgInterfaceMap *gmap.ListMap,
|
||||
) (err error) {
|
||||
var srcPkgInterfaceFunc []map[string]string
|
||||
|
||||
for _, item := range funcItems {
|
||||
var (
|
||||
// eg: "sArticle"
|
||||
receiverName string
|
||||
receiverMatch []string
|
||||
|
||||
// eg: "GetList(ctx context.Context, req *v1.ArticleListReq) (list []*v1.Article, err error)"
|
||||
funcHead string
|
||||
)
|
||||
|
||||
// handle the receiver name.
|
||||
if item.Receiver == "" {
|
||||
continue
|
||||
}
|
||||
receiverName = item.Receiver
|
||||
receiverName = gstr.Trim(receiverName, "*")
|
||||
// Match and pick the struct name from receiver.
|
||||
if receiverMatch, err = gregex.MatchString(in.StPattern, receiverName); err != nil {
|
||||
return err
|
||||
}
|
||||
if len(receiverMatch) < 1 {
|
||||
continue
|
||||
}
|
||||
receiverName = gstr.CaseCamel(receiverMatch[1])
|
||||
|
||||
// check if the func name is public.
|
||||
if !gstr.IsLetterUpper(item.MethodName[0]) {
|
||||
continue
|
||||
}
|
||||
|
||||
if !srcPkgInterfaceMap.Contains(receiverName) {
|
||||
srcPkgInterfaceFunc = make([]map[string]string, 0)
|
||||
srcPkgInterfaceMap.Set(receiverName, srcPkgInterfaceFunc)
|
||||
} else {
|
||||
srcPkgInterfaceFunc = srcPkgInterfaceMap.Get(receiverName).([]map[string]string)
|
||||
}
|
||||
|
||||
// make the func head.
|
||||
paramsStr := c.tidyParam(item.Params)
|
||||
resultsStr := c.tidyResult(item.Results)
|
||||
funcHead = fmt.Sprintf("%s(%s) (%s)", item.MethodName, paramsStr, resultsStr)
|
||||
|
||||
srcPkgInterfaceFunc = append(srcPkgInterfaceFunc, map[string]string{
|
||||
"funcHead": funcHead,
|
||||
"funcComment": item.Comment,
|
||||
})
|
||||
srcPkgInterfaceMap.Set(receiverName, srcPkgInterfaceFunc)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// tidyParam tidies the input parameters.
|
||||
// For example:
|
||||
//
|
||||
// []map[string]string{paramName:ctx paramType:context.Context, paramName:info paramType:struct{}}
|
||||
// -> ctx context.Context, info struct{}
|
||||
func (c CGenService) tidyParam(paramSlice []map[string]string) (paramStr string) {
|
||||
for i, param := range paramSlice {
|
||||
if i > 0 {
|
||||
paramStr += ", "
|
||||
}
|
||||
paramStr += fmt.Sprintf("%s %s", param["paramName"], param["paramType"])
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// tidyResult tidies the output parameters.
|
||||
// For example:
|
||||
//
|
||||
// []map[string]string{resultName:list resultType:[]*User, resultName:err resultType:error}
|
||||
// -> list []*User, err error
|
||||
//
|
||||
// []map[string]string{resultName: "", resultType: error}
|
||||
// -> error
|
||||
func (c CGenService) tidyResult(resultSlice []map[string]string) (resultStr string) {
|
||||
for i, result := range resultSlice {
|
||||
if i > 0 {
|
||||
resultStr += ", "
|
||||
}
|
||||
if result["resultName"] != "" {
|
||||
resultStr += fmt.Sprintf("%s %s", result["resultName"], result["resultType"])
|
||||
} else {
|
||||
resultStr += result["resultType"]
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@ -7,112 +7,34 @@
|
||||
package genservice
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/v2/container/garray"
|
||||
"github.com/gogf/gf/v2/container/gmap"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gfile"
|
||||
"github.com/gogf/gf/v2/text/gregex"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
|
||||
"github.com/gogf/gf/cmd/gf/v2/internal/consts"
|
||||
"github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog"
|
||||
"github.com/gogf/gf/cmd/gf/v2/internal/utility/utils"
|
||||
"github.com/gogf/gf/v2/container/gmap"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gfile"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
)
|
||||
|
||||
type generateServiceFilesInput struct {
|
||||
CGenServiceInput
|
||||
DstFilePath string // Absolute file path for generated service go file.
|
||||
SrcStructFunctions *gmap.ListMap
|
||||
SrcImportedPackages []string
|
||||
SrcPackageName string
|
||||
SrcImportedPackages []string
|
||||
SrcStructFunctions *gmap.ListMap
|
||||
DstPackageName string
|
||||
SrcCodeCommentedMap map[string]string
|
||||
DstFilePath string // Absolute file path for generated service go file.
|
||||
}
|
||||
|
||||
func (c CGenService) generateServiceFile(in generateServiceFilesInput) (ok bool, err error) {
|
||||
var (
|
||||
generatedContent string
|
||||
allFuncArray = garray.NewStrArray() // Used for check whether interface dirty, going to change file content.
|
||||
importedPackagesContent = fmt.Sprintf(
|
||||
"import (\n%s\n)", gstr.Join(in.SrcImportedPackages, "\n"),
|
||||
)
|
||||
)
|
||||
generatedContent += gstr.ReplaceByMap(consts.TemplateGenServiceContentHead, g.MapStrStr{
|
||||
"{Imports}": importedPackagesContent,
|
||||
"{PackageName}": in.DstPackageName,
|
||||
})
|
||||
var generatedContent bytes.Buffer
|
||||
|
||||
// Type definitions.
|
||||
generatedContent += "type("
|
||||
generatedContent += "\n"
|
||||
in.SrcStructFunctions.Iterator(func(key, value interface{}) bool {
|
||||
structName, funcArray := key.(string), value.(*garray.StrArray)
|
||||
allFuncArray.Append(funcArray.Slice()...)
|
||||
// Add comments to a method.
|
||||
for index, funcName := range funcArray.Slice() {
|
||||
if commentedInfo, exist := in.SrcCodeCommentedMap[fmt.Sprintf("%s-%s", structName, funcName)]; exist {
|
||||
funcName = commentedInfo + funcName
|
||||
_ = funcArray.Set(index, funcName)
|
||||
}
|
||||
}
|
||||
generatedContent += gstr.Trim(gstr.ReplaceByMap(consts.TemplateGenServiceContentInterface, g.MapStrStr{
|
||||
"{InterfaceName}": "I" + structName,
|
||||
"{FuncDefinition}": funcArray.Join("\n\t"),
|
||||
}))
|
||||
generatedContent += "\n"
|
||||
return true
|
||||
})
|
||||
generatedContent += ")"
|
||||
generatedContent += "\n"
|
||||
|
||||
// Generating variable and register definitions.
|
||||
var (
|
||||
variableContent string
|
||||
generatingInterfaceCheck string
|
||||
)
|
||||
// Variable definitions.
|
||||
in.SrcStructFunctions.Iterator(func(key, value interface{}) bool {
|
||||
structName := key.(string)
|
||||
generatingInterfaceCheck = fmt.Sprintf(`[^\w\d]+%s.I%s[^\w\d]`, in.DstPackageName, structName)
|
||||
if gregex.IsMatchString(generatingInterfaceCheck, generatedContent) {
|
||||
return true
|
||||
}
|
||||
variableContent += gstr.Trim(gstr.ReplaceByMap(consts.TemplateGenServiceContentVariable, g.MapStrStr{
|
||||
"{StructName}": structName,
|
||||
"{InterfaceName}": "I" + structName,
|
||||
}))
|
||||
variableContent += "\n"
|
||||
return true
|
||||
})
|
||||
if variableContent != "" {
|
||||
generatedContent += "var("
|
||||
generatedContent += "\n"
|
||||
generatedContent += variableContent
|
||||
generatedContent += ")"
|
||||
generatedContent += "\n"
|
||||
}
|
||||
// Variable register function definitions.
|
||||
in.SrcStructFunctions.Iterator(func(key, value interface{}) bool {
|
||||
structName := key.(string)
|
||||
generatingInterfaceCheck = fmt.Sprintf(`[^\w\d]+%s.I%s[^\w\d]`, in.DstPackageName, structName)
|
||||
if gregex.IsMatchString(generatingInterfaceCheck, generatedContent) {
|
||||
return true
|
||||
}
|
||||
generatedContent += gstr.Trim(gstr.ReplaceByMap(consts.TemplateGenServiceContentRegister, g.MapStrStr{
|
||||
"{StructName}": structName,
|
||||
"{InterfaceName}": "I" + structName,
|
||||
}))
|
||||
generatedContent += "\n\n"
|
||||
return true
|
||||
})
|
||||
|
||||
// Replace empty braces that have new line.
|
||||
generatedContent, _ = gregex.ReplaceString(`{[\s\t]+}`, `{}`, generatedContent)
|
||||
|
||||
// Remove package name calls of `dstPackageName` in produced codes.
|
||||
generatedContent, _ = gregex.ReplaceString(fmt.Sprintf(`\*{0,1}%s\.`, in.DstPackageName), ``, generatedContent)
|
||||
c.generatePackageImports(&generatedContent, in.DstPackageName, in.SrcImportedPackages)
|
||||
c.generateType(&generatedContent, in.SrcStructFunctions, in.DstPackageName)
|
||||
c.generateVar(&generatedContent, in.SrcStructFunctions)
|
||||
c.generateFunc(&generatedContent, in.SrcStructFunctions)
|
||||
|
||||
// Write file content to disk.
|
||||
if gfile.Exists(in.DstFilePath) {
|
||||
@ -120,59 +42,14 @@ func (c CGenService) generateServiceFile(in generateServiceFilesInput) (ok bool,
|
||||
mlog.Printf(`ignore file as it is manually maintained: %s`, in.DstFilePath)
|
||||
return false, nil
|
||||
}
|
||||
if !c.isToGenerateServiceGoFile(in.DstPackageName, in.DstFilePath, allFuncArray) {
|
||||
mlog.Printf(`not dirty, ignore generating service go file: %s`, in.DstFilePath)
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
mlog.Printf(`generating service go file: %s`, in.DstFilePath)
|
||||
if err = gfile.PutContents(in.DstFilePath, generatedContent); err != nil {
|
||||
if err = gfile.PutBytes(in.DstFilePath, generatedContent.Bytes()); err != nil {
|
||||
return true, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// isToGenerateServiceGoFile checks and returns whether the service content dirty.
|
||||
func (c CGenService) isToGenerateServiceGoFile(dstPackageName, filePath string, funcArray *garray.StrArray) bool {
|
||||
var (
|
||||
err error
|
||||
fileContent = gfile.GetContents(filePath)
|
||||
generatedFuncArray = garray.NewSortedStrArrayFrom(funcArray.Slice())
|
||||
contentFuncArray = garray.NewSortedStrArray()
|
||||
)
|
||||
if fileContent == "" {
|
||||
return true
|
||||
}
|
||||
// remove all comments.
|
||||
fileContent, err = gregex.ReplaceString(`(//.*)|((?s)/\*.*?\*/)`, "", fileContent)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return false
|
||||
}
|
||||
matches, _ := gregex.MatchAllString(`\s+interface\s+{([\s\S]+?)}`, fileContent)
|
||||
for _, match := range matches {
|
||||
contentFuncArray.Append(gstr.SplitAndTrim(match[1], "\n")...)
|
||||
}
|
||||
if generatedFuncArray.Len() != contentFuncArray.Len() {
|
||||
mlog.Debugf(
|
||||
`dirty, generatedFuncArray.Len()[%d] != contentFuncArray.Len()[%d]`,
|
||||
generatedFuncArray.Len(), contentFuncArray.Len(),
|
||||
)
|
||||
return true
|
||||
}
|
||||
var funcDefinition string
|
||||
for i := 0; i < generatedFuncArray.Len(); i++ {
|
||||
funcDefinition, _ = gregex.ReplaceString(
|
||||
fmt.Sprintf(`\*{0,1}%s\.`, dstPackageName), ``, generatedFuncArray.At(i),
|
||||
)
|
||||
if funcDefinition != contentFuncArray.At(i) {
|
||||
mlog.Debugf(`dirty, %s != %s`, funcDefinition, contentFuncArray.At(i))
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// generateInitializationFile generates `logic.go`.
|
||||
func (c CGenService) generateInitializationFile(in CGenServiceInput, importSrcPackages []string) (err error) {
|
||||
var (
|
||||
|
||||
104
cmd/gf/internal/cmd/genservice/genservice_generate_template.go
Normal file
104
cmd/gf/internal/cmd/genservice/genservice_generate_template.go
Normal file
@ -0,0 +1,104 @@
|
||||
// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package genservice
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/cmd/gf/v2/internal/consts"
|
||||
"github.com/gogf/gf/v2/container/gmap"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/text/gregex"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
)
|
||||
|
||||
func (c CGenService) generatePackageImports(generatedContent *bytes.Buffer, packageName string, imports []string) {
|
||||
generatedContent.WriteString(gstr.ReplaceByMap(consts.TemplateGenServiceContentHead, g.MapStrStr{
|
||||
"{PackageName}": packageName,
|
||||
"{Imports}": fmt.Sprintf(
|
||||
"import (\n%s\n)", gstr.Join(imports, "\n"),
|
||||
),
|
||||
}))
|
||||
}
|
||||
|
||||
// generateType type definitions.
|
||||
// See: const.TemplateGenServiceContentInterface
|
||||
func (c CGenService) generateType(generatedContent *bytes.Buffer, srcStructFunctions *gmap.ListMap, dstPackageName string) {
|
||||
generatedContent.WriteString("type(")
|
||||
generatedContent.WriteString("\n")
|
||||
|
||||
srcStructFunctions.Iterator(func(key, value interface{}) bool {
|
||||
var (
|
||||
funcContents = make([]string, 0)
|
||||
funcContent string
|
||||
)
|
||||
structName, funcSlice := key.(string), value.([]map[string]string)
|
||||
// Generating interface content.
|
||||
for _, funcInfo := range funcSlice {
|
||||
// Remove package name calls of `dstPackageName` in produced codes.
|
||||
funcHead, _ := gregex.ReplaceString(
|
||||
fmt.Sprintf(`\*{0,1}%s\.`, dstPackageName),
|
||||
``, funcInfo["funcHead"],
|
||||
)
|
||||
funcContent = funcInfo["funcComment"] + funcHead
|
||||
funcContents = append(funcContents, funcContent)
|
||||
}
|
||||
|
||||
// funcContents to string.
|
||||
generatedContent.WriteString(
|
||||
gstr.Trim(gstr.ReplaceByMap(consts.TemplateGenServiceContentInterface, g.MapStrStr{
|
||||
"{InterfaceName}": "I" + structName,
|
||||
"{FuncDefinition}": gstr.Join(funcContents, "\n\t"),
|
||||
})),
|
||||
)
|
||||
generatedContent.WriteString("\n")
|
||||
return true
|
||||
})
|
||||
|
||||
generatedContent.WriteString(")")
|
||||
generatedContent.WriteString("\n")
|
||||
}
|
||||
|
||||
// generateVar variable definitions.
|
||||
// See: const.TemplateGenServiceContentVariable
|
||||
func (c CGenService) generateVar(generatedContent *bytes.Buffer, srcStructFunctions *gmap.ListMap) {
|
||||
// Generating variable and register definitions.
|
||||
var variableContent string
|
||||
|
||||
srcStructFunctions.Iterator(func(key, value interface{}) bool {
|
||||
structName := key.(string)
|
||||
variableContent += gstr.Trim(gstr.ReplaceByMap(consts.TemplateGenServiceContentVariable, g.MapStrStr{
|
||||
"{StructName}": structName,
|
||||
"{InterfaceName}": "I" + structName,
|
||||
}))
|
||||
variableContent += "\n"
|
||||
return true
|
||||
})
|
||||
if variableContent != "" {
|
||||
generatedContent.WriteString("var(")
|
||||
generatedContent.WriteString("\n")
|
||||
generatedContent.WriteString(variableContent)
|
||||
generatedContent.WriteString(")")
|
||||
generatedContent.WriteString("\n")
|
||||
}
|
||||
}
|
||||
|
||||
// generateFunc function definitions.
|
||||
// See: const.TemplateGenServiceContentRegister
|
||||
func (c CGenService) generateFunc(generatedContent *bytes.Buffer, srcStructFunctions *gmap.ListMap) {
|
||||
// Variable register function definitions.
|
||||
srcStructFunctions.Iterator(func(key, value interface{}) bool {
|
||||
structName := key.(string)
|
||||
generatedContent.WriteString(gstr.Trim(gstr.ReplaceByMap(consts.TemplateGenServiceContentRegister, g.MapStrStr{
|
||||
"{StructName}": structName,
|
||||
"{InterfaceName}": "I" + structName,
|
||||
})))
|
||||
generatedContent.WriteString("\n\n")
|
||||
return true
|
||||
})
|
||||
}
|
||||
@ -1,6 +1,7 @@
|
||||
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
|
||||
github.com/clbanning/mxj/v2 v2.7.0 h1:WA/La7UGCanFe5NpHF0Q3DNtnCsVoxbPKuyBNHWRyME=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
|
||||
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
|
||||
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
|
||||
github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0=
|
||||
|
||||
@ -21,3 +21,11 @@ type UpdateReq struct {
|
||||
}
|
||||
|
||||
type UpdateRes struct{}
|
||||
|
||||
//type GetListReq struct {
|
||||
// g.Meta `path:"/article/list" method:"get" tags:"ArticleService"`
|
||||
//}
|
||||
//
|
||||
//type GetListRes struct {
|
||||
// list []struct{}
|
||||
//}
|
||||
|
||||
@ -8,8 +8,11 @@ package article
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go/ast"
|
||||
t "time"
|
||||
|
||||
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genservice/service"
|
||||
gdbalias "github.com/gogf/gf/v2/database/gdb"
|
||||
)
|
||||
|
||||
type sArticle struct {
|
||||
@ -30,5 +33,15 @@ func (s *sArticle) Get(ctx context.Context, id uint) (info struct{}, err error)
|
||||
* @author oldme
|
||||
*/
|
||||
func (s *sArticle) Create(ctx context.Context, info struct{}) (id uint, err error) {
|
||||
// Use time package to test alias import.
|
||||
t.Now()
|
||||
return id, err
|
||||
}
|
||||
|
||||
func (s *sArticle) A1o2(ctx context.Context, str string, a, b *ast.GoStmt) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *sArticle) B_2(ctx context.Context, db gdbalias.Raw) (err error) {
|
||||
return nil
|
||||
}
|
||||
|
||||
75
cmd/gf/internal/cmd/testdata/genservice/logic/article/article_extra.go
vendored
Normal file
75
cmd/gf/internal/cmd/testdata/genservice/logic/article/article_extra.go
vendored
Normal file
@ -0,0 +1,75 @@
|
||||
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package article
|
||||
|
||||
// import (
|
||||
// "context"
|
||||
//
|
||||
// "github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genservice/service"
|
||||
// )
|
||||
import (
|
||||
"context"
|
||||
|
||||
// This is a random comment
|
||||
gdbas "github.com/gogf/gf/v2/database/gdb"
|
||||
/**
|
||||
*
|
||||
*/
|
||||
_ "github.com/gogf/gf/v2/os/gfile"
|
||||
)
|
||||
|
||||
// T1 random comment
|
||||
func (s sArticle) T1(ctx context.Context, id, id2 uint) (gdb gdbas.Model, err error) {
|
||||
g := gdbas.Model{}
|
||||
return g, err
|
||||
}
|
||||
|
||||
// I'm a random comment
|
||||
|
||||
// t2 random comment
|
||||
func (s *sArticle) t2(ctx context.Context) (err error) {
|
||||
/**
|
||||
* random comment
|
||||
* i (1). func (s *sArticle) t2(ctx context.Context) (err error) { /** 1883
|
||||
*
|
||||
*/
|
||||
_ = func(ctx2 context.Context) {}
|
||||
return nil
|
||||
}
|
||||
|
||||
// T3
|
||||
/**
|
||||
* random comment @*4213hHY1&%##%><<Y
|
||||
* @param b
|
||||
* @return c, d
|
||||
* @return err
|
||||
* @author oldme
|
||||
*/
|
||||
func (s *sArticle) T3(ctx context.Context, b *gdbas.Model) (c, d *gdbas.Model, err error) {
|
||||
/* import (
|
||||
* "context"
|
||||
*
|
||||
* "github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genservice/service"
|
||||
*/
|
||||
return nil, nil, nil
|
||||
}
|
||||
|
||||
/**
|
||||
* random comment
|
||||
*/
|
||||
|
||||
// func (s *sArticle) T4(i interface{}) interface{}
|
||||
// # $ % ^ & * ( ) _ + - = { } | [ ] \ : " ; ' < > ? , . /
|
||||
func (s *sArticle) T4(i interface{}) interface{} {
|
||||
return nil
|
||||
}
|
||||
|
||||
/**
|
||||
* func (s *sArticle) T4(i interface{}) interface{} {
|
||||
* return nil
|
||||
* }
|
||||
*/
|
||||
38
cmd/gf/internal/cmd/testdata/genservice/logic/delivery/delivery_app.go
vendored
Normal file
38
cmd/gf/internal/cmd/testdata/genservice/logic/delivery/delivery_app.go
vendored
Normal file
@ -0,0 +1,38 @@
|
||||
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package delivery
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genservice/service"
|
||||
)
|
||||
|
||||
type sDeliveryApp struct{}
|
||||
|
||||
func NewDeliveryApp() *sDeliveryApp {
|
||||
return &sDeliveryApp{}
|
||||
}
|
||||
|
||||
func (s *sDeliveryApp) Create(ctx context.Context) (i service.IDeliveryCluster, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sDeliveryApp) GetList(ctx context.Context, i service.IDeliveryCluster) (err error) {
|
||||
service.Article().Get(ctx, 1)
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sDeliveryApp) GetOne(ctx context.Context) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sDeliveryApp) Delete(ctx context.Context) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sDeliveryApp) AA(ctx context.Context) (err error) { return }
|
||||
32
cmd/gf/internal/cmd/testdata/genservice/logic/delivery/delivery_cluster.go
vendored
Normal file
32
cmd/gf/internal/cmd/testdata/genservice/logic/delivery/delivery_cluster.go
vendored
Normal file
@ -0,0 +1,32 @@
|
||||
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package delivery
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
gdbas "github.com/gogf/gf/v2/database/gdb"
|
||||
)
|
||||
|
||||
type sDeliveryCluster struct{}
|
||||
|
||||
func NewDeliveryCluster() *sDeliveryCluster {
|
||||
return &sDeliveryCluster{}
|
||||
}
|
||||
|
||||
// Create 自动创建Cluster及Project.
|
||||
func (s *sDeliveryCluster) Create(ctx context.Context) (err error, gdb gdbas.Model) {
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sDeliveryCluster) Delete(ctx context.Context) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sDeliveryCluster) GetList(ctx context.Context) (err error) {
|
||||
return
|
||||
}
|
||||
@ -6,4 +6,6 @@ package logic
|
||||
|
||||
import (
|
||||
_ "github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genservice/logic/article"
|
||||
_ "github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genservice/logic/delivery"
|
||||
_ "github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genservice/logic/user"
|
||||
)
|
||||
|
||||
49
cmd/gf/internal/cmd/testdata/genservice/logic/user/user.go
vendored
Normal file
49
cmd/gf/internal/cmd/testdata/genservice/logic/user/user.go
vendored
Normal file
@ -0,0 +1,49 @@
|
||||
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genservice/service"
|
||||
)
|
||||
|
||||
func init() {
|
||||
service.RegisterUser(New())
|
||||
}
|
||||
|
||||
type sUser struct {
|
||||
}
|
||||
|
||||
func New() *sUser {
|
||||
return &sUser{}
|
||||
}
|
||||
|
||||
// Create creates a new user.
|
||||
func (s *sUser) Create(ctx context.Context, name string) (id int, err error) {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
// GetOne retrieves user by id.
|
||||
func (s *sUser) GetOne(ctx context.Context, id int) (name string, err error) {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
// GetList retrieves user list.
|
||||
func (s *sUser) GetList(ctx context.Context) (names []string, err error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// Update updates user by id.
|
||||
func (s *sUser) Update(ctx context.Context, id int) (name string, err error) {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
// Delete deletes user by id.
|
||||
func (s *sUser) Delete(ctx context.Context, id int) (err error) {
|
||||
return nil
|
||||
}
|
||||
@ -7,6 +7,10 @@ package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go/ast"
|
||||
|
||||
gdbalias "github.com/gogf/gf/v2/database/gdb"
|
||||
gdbas "github.com/gogf/gf/v2/database/gdb"
|
||||
)
|
||||
|
||||
type (
|
||||
@ -19,6 +23,22 @@ type (
|
||||
* @author oldme
|
||||
*/
|
||||
Create(ctx context.Context, info struct{}) (id uint, err error)
|
||||
A1o2(ctx context.Context, str string, a *ast.GoStmt, b *ast.GoStmt) error
|
||||
B_2(ctx context.Context, db gdbalias.Raw) (err error)
|
||||
// T1 random comment
|
||||
T1(ctx context.Context, id uint, id2 uint) (gdb gdbas.Model, err error)
|
||||
// T3
|
||||
/**
|
||||
* random comment @*4213hHY1&%##%><<Y
|
||||
* @param b
|
||||
* @return c, d
|
||||
* @return err
|
||||
* @author oldme
|
||||
*/
|
||||
T3(ctx context.Context, b *gdbas.Model) (c *gdbas.Model, d *gdbas.Model, err error)
|
||||
// func (s *sArticle) T4(i interface{}) interface{}
|
||||
// # $ % ^ & * ( ) _ + - = { } | [ ] \ : " ; ' < > ? , . /
|
||||
T4(i interface{}) interface{}
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
55
cmd/gf/internal/cmd/testdata/genservice/service/delivery.go
vendored
Normal file
55
cmd/gf/internal/cmd/testdata/genservice/service/delivery.go
vendored
Normal file
@ -0,0 +1,55 @@
|
||||
// ================================================================================
|
||||
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
|
||||
// You can delete these comments if you wish manually maintain this interface file.
|
||||
// ================================================================================
|
||||
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
gdbas "github.com/gogf/gf/v2/database/gdb"
|
||||
)
|
||||
|
||||
type (
|
||||
IDeliveryApp interface {
|
||||
Create(ctx context.Context) (i IDeliveryCluster, err error)
|
||||
GetList(ctx context.Context, i IDeliveryCluster) (err error)
|
||||
GetOne(ctx context.Context) (err error)
|
||||
Delete(ctx context.Context) (err error)
|
||||
AA(ctx context.Context) (err error)
|
||||
}
|
||||
IDeliveryCluster interface {
|
||||
// Create 自动创建Cluster及Project.
|
||||
Create(ctx context.Context) (err error, gdb gdbas.Model)
|
||||
Delete(ctx context.Context) (err error)
|
||||
GetList(ctx context.Context) (err error)
|
||||
}
|
||||
)
|
||||
|
||||
var (
|
||||
localDeliveryApp IDeliveryApp
|
||||
localDeliveryCluster IDeliveryCluster
|
||||
)
|
||||
|
||||
func DeliveryApp() IDeliveryApp {
|
||||
if localDeliveryApp == nil {
|
||||
panic("implement not found for interface IDeliveryApp, forgot register?")
|
||||
}
|
||||
return localDeliveryApp
|
||||
}
|
||||
|
||||
func RegisterDeliveryApp(i IDeliveryApp) {
|
||||
localDeliveryApp = i
|
||||
}
|
||||
|
||||
func DeliveryCluster() IDeliveryCluster {
|
||||
if localDeliveryCluster == nil {
|
||||
panic("implement not found for interface IDeliveryCluster, forgot register?")
|
||||
}
|
||||
return localDeliveryCluster
|
||||
}
|
||||
|
||||
func RegisterDeliveryCluster(i IDeliveryCluster) {
|
||||
localDeliveryCluster = i
|
||||
}
|
||||
40
cmd/gf/internal/cmd/testdata/genservice/service/user.go
vendored
Normal file
40
cmd/gf/internal/cmd/testdata/genservice/service/user.go
vendored
Normal file
@ -0,0 +1,40 @@
|
||||
// ================================================================================
|
||||
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
|
||||
// You can delete these comments if you wish manually maintain this interface file.
|
||||
// ================================================================================
|
||||
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
type (
|
||||
IUser interface {
|
||||
// Create creates a new user.
|
||||
Create(ctx context.Context, name string) (id int, err error)
|
||||
// GetOne retrieves user by id.
|
||||
GetOne(ctx context.Context, id int) (name string, err error)
|
||||
// GetList retrieves user list.
|
||||
GetList(ctx context.Context) (names []string, err error)
|
||||
// Update updates user by id.
|
||||
Update(ctx context.Context, id int) (name string, err error)
|
||||
// Delete deletes user by id.
|
||||
Delete(ctx context.Context, id int) (err error)
|
||||
}
|
||||
)
|
||||
|
||||
var (
|
||||
localUser IUser
|
||||
)
|
||||
|
||||
func User() IUser {
|
||||
if localUser == nil {
|
||||
panic("implement not found for interface IUser, forgot register?")
|
||||
}
|
||||
return localUser
|
||||
}
|
||||
|
||||
func RegisterUser(i IUser) {
|
||||
localUser = i
|
||||
}
|
||||
@ -11,7 +11,7 @@ const TemplateGenDaoIndexContent = `
|
||||
// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
|
||||
// =================================================================================
|
||||
|
||||
package dao
|
||||
package {TplPackageName}
|
||||
|
||||
import (
|
||||
"{TplImportPrefix}/internal"
|
||||
|
||||
@ -11,7 +11,7 @@ const TemplateGenDaoDoContent = `
|
||||
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT. {TplCreatedAtDatetimeStr}
|
||||
// =================================================================================
|
||||
|
||||
package do
|
||||
package {TplPackageName}
|
||||
|
||||
{TplPackageImports}
|
||||
|
||||
|
||||
@ -11,7 +11,7 @@ const TemplateGenDaoEntityContent = `
|
||||
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT. {TplCreatedAtDatetimeStr}
|
||||
// =================================================================================
|
||||
|
||||
package entity
|
||||
package {TplPackageName}
|
||||
|
||||
{TplPackageImports}
|
||||
|
||||
|
||||
9
cmd/gf/internal/packed/template-mono-app.go
Normal file
9
cmd/gf/internal/packed/template-mono-app.go
Normal file
File diff suppressed because one or more lines are too long
@ -45,9 +45,7 @@ func NewAnyAnyMapFrom(data map[interface{}]interface{}, safe ...bool) *AnyAnyMap
|
||||
// Iterator iterates the hash map readonly with custom callback function `f`.
|
||||
// If `f` returns true, then it continues iterating; or false to stop.
|
||||
func (m *AnyAnyMap) Iterator(f func(k interface{}, v interface{}) bool) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
for k, v := range m.data {
|
||||
for k, v := range m.Map() {
|
||||
if !f(k, v) {
|
||||
break
|
||||
}
|
||||
|
||||
@ -46,9 +46,7 @@ func NewIntAnyMapFrom(data map[int]interface{}, safe ...bool) *IntAnyMap {
|
||||
// Iterator iterates the hash map readonly with custom callback function `f`.
|
||||
// If `f` returns true, then it continues iterating; or false to stop.
|
||||
func (m *IntAnyMap) Iterator(f func(k int, v interface{}) bool) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
for k, v := range m.data {
|
||||
for k, v := range m.Map() {
|
||||
if !f(k, v) {
|
||||
break
|
||||
}
|
||||
|
||||
@ -42,9 +42,7 @@ func NewIntIntMapFrom(data map[int]int, safe ...bool) *IntIntMap {
|
||||
// Iterator iterates the hash map readonly with custom callback function `f`.
|
||||
// If `f` returns true, then it continues iterating; or false to stop.
|
||||
func (m *IntIntMap) Iterator(f func(k int, v int) bool) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
for k, v := range m.data {
|
||||
for k, v := range m.Map() {
|
||||
if !f(k, v) {
|
||||
break
|
||||
}
|
||||
|
||||
@ -42,9 +42,7 @@ func NewIntStrMapFrom(data map[int]string, safe ...bool) *IntStrMap {
|
||||
// Iterator iterates the hash map readonly with custom callback function `f`.
|
||||
// If `f` returns true, then it continues iterating; or false to stop.
|
||||
func (m *IntStrMap) Iterator(f func(k int, v string) bool) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
for k, v := range m.data {
|
||||
for k, v := range m.Map() {
|
||||
if !f(k, v) {
|
||||
break
|
||||
}
|
||||
|
||||
@ -47,9 +47,7 @@ func NewStrAnyMapFrom(data map[string]interface{}, safe ...bool) *StrAnyMap {
|
||||
// Iterator iterates the hash map readonly with custom callback function `f`.
|
||||
// If `f` returns true, then it continues iterating; or false to stop.
|
||||
func (m *StrAnyMap) Iterator(f func(k string, v interface{}) bool) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
for k, v := range m.data {
|
||||
for k, v := range m.Map() {
|
||||
if !f(k, v) {
|
||||
break
|
||||
}
|
||||
|
||||
@ -43,9 +43,7 @@ func NewStrIntMapFrom(data map[string]int, safe ...bool) *StrIntMap {
|
||||
// Iterator iterates the hash map readonly with custom callback function `f`.
|
||||
// If `f` returns true, then it continues iterating; or false to stop.
|
||||
func (m *StrIntMap) Iterator(f func(k string, v int) bool) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
for k, v := range m.data {
|
||||
for k, v := range m.Map() {
|
||||
if !f(k, v) {
|
||||
break
|
||||
}
|
||||
|
||||
@ -43,9 +43,7 @@ func NewStrStrMapFrom(data map[string]string, safe ...bool) *StrStrMap {
|
||||
// Iterator iterates the hash map readonly with custom callback function `f`.
|
||||
// If `f` returns true, then it continues iterating; or false to stop.
|
||||
func (m *StrStrMap) Iterator(f func(k string, v string) bool) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
for k, v := range m.data {
|
||||
for k, v := range m.Map() {
|
||||
if !f(k, v) {
|
||||
break
|
||||
}
|
||||
|
||||
@ -110,6 +110,22 @@ func Test_AnyAnyMap_Batch(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func Test_AnyAnyMap_Iterator_Deadlock(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewAnyAnyMapFrom(map[interface{}]interface{}{1: 1, 2: "2", "3": "3", "4": 4}, true)
|
||||
m.Iterator(func(k interface{}, _ interface{}) bool {
|
||||
if gconv.Int(k)%2 == 0 {
|
||||
m.Remove(k)
|
||||
}
|
||||
return true
|
||||
})
|
||||
t.Assert(m.Map(), map[interface{}]interface{}{
|
||||
1: 1,
|
||||
"3": "3",
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func Test_AnyAnyMap_Iterator(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect := map[interface{}]interface{}{1: 1, 2: "2"}
|
||||
|
||||
@ -118,6 +118,22 @@ func Test_IntAnyMap_Batch(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func Test_IntAnyMap_Iterator_Deadlock(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewIntAnyMapFrom(map[int]interface{}{1: 1, 2: 2, 3: "3", 4: 4}, true)
|
||||
m.Iterator(func(k int, _ interface{}) bool {
|
||||
if k%2 == 0 {
|
||||
m.Remove(k)
|
||||
}
|
||||
return true
|
||||
})
|
||||
t.Assert(m.Map(), map[int]interface{}{
|
||||
1: 1,
|
||||
3: "3",
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func Test_IntAnyMap_Iterator(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect := map[int]interface{}{1: 1, 2: "2"}
|
||||
|
||||
@ -127,6 +127,22 @@ func Test_IntIntMap_Batch(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func Test_IntIntMap_Iterator_Deadlock(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewIntIntMapFrom(map[int]int{1: 1, 2: 2, 3: 3, 4: 4}, true)
|
||||
m.Iterator(func(k int, _ int) bool {
|
||||
if k%2 == 0 {
|
||||
m.Remove(k)
|
||||
}
|
||||
return true
|
||||
})
|
||||
t.Assert(m.Map(), map[int]int{
|
||||
1: 1,
|
||||
3: 3,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func Test_IntIntMap_Iterator(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect := map[int]int{1: 1, 2: 2}
|
||||
|
||||
@ -153,6 +153,22 @@ func Test_IntStrMap_Batch(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func Test_IntStrMap_Iterator_Deadlock(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewIntStrMapFrom(map[int]string{1: "1", 2: "2", 3: "3", 4: "4"}, true)
|
||||
m.Iterator(func(k int, _ string) bool {
|
||||
if k%2 == 0 {
|
||||
m.Remove(k)
|
||||
}
|
||||
return true
|
||||
})
|
||||
t.Assert(m.Map(), map[int]string{
|
||||
1: "1",
|
||||
3: "3",
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func Test_IntStrMap_Iterator(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect := map[int]string{1: "a", 2: "b"}
|
||||
|
||||
@ -7,6 +7,7 @@
|
||||
package gmap_test
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/v2/container/garray"
|
||||
@ -109,6 +110,23 @@ func Test_StrAnyMap_Batch(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func Test_StrAnyMap_Iterator_Deadlock(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewStrAnyMapFrom(map[string]interface{}{"1": "1", "2": "2", "3": "3", "4": "4"}, true)
|
||||
m.Iterator(func(k string, _ interface{}) bool {
|
||||
kInt, _ := strconv.Atoi(k)
|
||||
if kInt%2 == 0 {
|
||||
m.Remove(k)
|
||||
}
|
||||
return true
|
||||
})
|
||||
t.Assert(m.Map(), map[string]interface{}{
|
||||
"1": "1",
|
||||
"3": "3",
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func Test_StrAnyMap_Iterator(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect := map[string]interface{}{"a": true, "b": false}
|
||||
|
||||
@ -7,6 +7,7 @@
|
||||
package gmap_test
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/v2/container/garray"
|
||||
@ -118,6 +119,20 @@ func Test_StrIntMap_Batch(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func Test_StrIntMap_Iterator_Deadlock(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewStrIntMapFrom(map[string]int{"1": 1, "2": 2, "3": 3, "4": 4}, true)
|
||||
m.Iterator(func(k string, _ int) bool {
|
||||
kInt, _ := strconv.Atoi(k)
|
||||
if kInt%2 == 0 {
|
||||
m.Remove(k)
|
||||
}
|
||||
return true
|
||||
})
|
||||
t.Assert(m.Size(), 2)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_StrIntMap_Iterator(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect := map[string]int{"a": 1, "b": 2}
|
||||
|
||||
@ -7,6 +7,7 @@
|
||||
package gmap_test
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/v2/container/garray"
|
||||
@ -117,6 +118,20 @@ func Test_StrStrMap_Batch(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func Test_StrStrMap_Iterator_Deadlock(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewStrStrMapFrom(map[string]string{"1": "1", "2": "2", "3": "3", "4": "4"}, true)
|
||||
m.Iterator(func(k string, _ string) bool {
|
||||
kInt, _ := strconv.Atoi(k)
|
||||
if kInt%2 == 0 {
|
||||
m.Remove(k)
|
||||
}
|
||||
return true
|
||||
})
|
||||
t.Assert(m.Size(), 2)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_StrStrMap_Iterator(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
expect := map[string]string{"a": "a", "b": "b"}
|
||||
|
||||
@ -53,9 +53,7 @@ func NewFrom(items interface{}, safe ...bool) *Set {
|
||||
// Iterator iterates the set readonly with given callback function `f`,
|
||||
// if `f` returns true then continue iterating; or false to stop.
|
||||
func (set *Set) Iterator(f func(v interface{}) bool) {
|
||||
set.mu.RLock()
|
||||
defer set.mu.RUnlock()
|
||||
for k := range set.data {
|
||||
for _, k := range set.Slice() {
|
||||
if !f(k) {
|
||||
break
|
||||
}
|
||||
|
||||
@ -45,9 +45,7 @@ func NewIntSetFrom(items []int, safe ...bool) *IntSet {
|
||||
// Iterator iterates the set readonly with given callback function `f`,
|
||||
// if `f` returns true then continue iterating; or false to stop.
|
||||
func (set *IntSet) Iterator(f func(v int) bool) {
|
||||
set.mu.RLock()
|
||||
defer set.mu.RUnlock()
|
||||
for k := range set.data {
|
||||
for _, k := range set.Slice() {
|
||||
if !f(k) {
|
||||
break
|
||||
}
|
||||
|
||||
@ -47,9 +47,7 @@ func NewStrSetFrom(items []string, safe ...bool) *StrSet {
|
||||
// Iterator iterates the set readonly with given callback function `f`,
|
||||
// if `f` returns true then continue iterating; or false to stop.
|
||||
func (set *StrSet) Iterator(f func(v string) bool) {
|
||||
set.mu.RLock()
|
||||
defer set.mu.RUnlock()
|
||||
for k := range set.data {
|
||||
for _, k := range set.Slice() {
|
||||
if !f(k) {
|
||||
break
|
||||
}
|
||||
|
||||
@ -82,6 +82,23 @@ func TestSet_Basic(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestSet_Iterator_Deadlock(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
set := gset.NewFrom([]interface{}{1, 2, 3, 4, 5}, true)
|
||||
set.Iterator(func(k interface{}) bool {
|
||||
if gconv.Int(k)%2 == 0 {
|
||||
set.Remove(k)
|
||||
}
|
||||
return true
|
||||
})
|
||||
t.Assert(set.Contains(1), true)
|
||||
t.Assert(set.Contains(2), false)
|
||||
t.Assert(set.Contains(3), true)
|
||||
t.Assert(set.Contains(4), false)
|
||||
t.Assert(set.Contains(5), true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSet_Iterator(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s := gset.NewSet()
|
||||
|
||||
@ -62,6 +62,23 @@ func TestIntSet_Basic(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntSet_Iterator_Deadlock(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
set := gset.NewIntSetFrom([]int{1, 2, 3, 4, 5}, true)
|
||||
set.Iterator(func(k int) bool {
|
||||
if k%2 == 0 {
|
||||
set.Remove(k)
|
||||
}
|
||||
return true
|
||||
})
|
||||
t.Assert(set.Contains(1), true)
|
||||
t.Assert(set.Contains(2), false)
|
||||
t.Assert(set.Contains(3), true)
|
||||
t.Assert(set.Contains(4), false)
|
||||
t.Assert(set.Contains(5), true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntSet_Iterator(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s := gset.NewIntSet()
|
||||
|
||||
@ -73,6 +73,23 @@ func TestStrSet_ContainsI(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrSet_Iterator_Deadlock(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
set := gset.NewStrSetFrom([]string{"1", "2", "3", "4", "5"}, true)
|
||||
set.Iterator(func(k string) bool {
|
||||
if gconv.Int(k)%2 == 0 {
|
||||
set.Remove(k)
|
||||
}
|
||||
return true
|
||||
})
|
||||
t.Assert(set.Contains("1"), true)
|
||||
t.Assert(set.Contains("2"), false)
|
||||
t.Assert(set.Contains("3"), true)
|
||||
t.Assert(set.Contains("4"), false)
|
||||
t.Assert(set.Contains("5"), true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrSet_Iterator(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s := gset.NewStrSet()
|
||||
|
||||
@ -7,32 +7,21 @@
|
||||
package gtree
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/emirpasic/gods/trees/btree"
|
||||
"github.com/gogf/gf/v2/container/gvar"
|
||||
"github.com/gogf/gf/v2/internal/intlog"
|
||||
"github.com/gogf/gf/v2/internal/json"
|
||||
"github.com/gogf/gf/v2/internal/rwmutex"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
// BTree holds elements of the B-tree.
|
||||
type BTree struct {
|
||||
mu rwmutex.RWMutex
|
||||
root *BTreeNode
|
||||
comparator func(v1, v2 interface{}) int
|
||||
size int // Total number of keys in the tree
|
||||
m int // order (maximum number of children)
|
||||
}
|
||||
|
||||
// BTreeNode is a single element within the tree.
|
||||
type BTreeNode struct {
|
||||
Parent *BTreeNode
|
||||
Entries []*BTreeEntry // Contained keys in node
|
||||
Children []*BTreeNode // Children nodes
|
||||
tree *btree.Tree
|
||||
}
|
||||
|
||||
// BTreeEntry represents the key-value pair contained within nodes.
|
||||
@ -46,13 +35,11 @@ type BTreeEntry struct {
|
||||
// which is false in default.
|
||||
// Note that the `m` must be greater or equal than 3, or else it panics.
|
||||
func NewBTree(m int, comparator func(v1, v2 interface{}) int, safe ...bool) *BTree {
|
||||
if m < 3 {
|
||||
panic("Invalid order, should be at least 3")
|
||||
}
|
||||
return &BTree{
|
||||
comparator: comparator,
|
||||
mu: rwmutex.Create(safe...),
|
||||
m: m,
|
||||
comparator: comparator,
|
||||
tree: btree.NewWith(m, comparator),
|
||||
}
|
||||
}
|
||||
|
||||
@ -81,21 +68,6 @@ func (tree *BTree) Set(key interface{}, value interface{}) {
|
||||
tree.doSet(key, value)
|
||||
}
|
||||
|
||||
// doSet inserts key-value pair node into the tree.
|
||||
// If key already exists, then its value is updated with the new value.
|
||||
func (tree *BTree) doSet(key interface{}, value interface{}) {
|
||||
entry := &BTreeEntry{Key: key, Value: value}
|
||||
if tree.root == nil {
|
||||
tree.root = &BTreeNode{Entries: []*BTreeEntry{entry}, Children: []*BTreeNode{}}
|
||||
tree.size++
|
||||
return
|
||||
}
|
||||
|
||||
if tree.insert(tree.root, entry) {
|
||||
tree.size++
|
||||
}
|
||||
}
|
||||
|
||||
// Sets batch sets key-values to the tree.
|
||||
func (tree *BTree) Sets(data map[interface{}]interface{}) {
|
||||
tree.mu.Lock()
|
||||
@ -107,39 +79,19 @@ func (tree *BTree) Sets(data map[interface{}]interface{}) {
|
||||
|
||||
// Get searches the node in the tree by `key` and returns its value or nil if key is not found in tree.
|
||||
func (tree *BTree) Get(key interface{}) (value interface{}) {
|
||||
value, _ = tree.Search(key)
|
||||
return
|
||||
}
|
||||
|
||||
// doSetWithLockCheck checks whether value of the key exists with mutex.Lock,
|
||||
// if not exists, set value to the map with given `key`,
|
||||
// or else just return the existing value.
|
||||
//
|
||||
// When setting value, if `value` is type of <func() interface {}>,
|
||||
// it will be executed with mutex.Lock of the hash map,
|
||||
// and its return value will be set to the map with `key`.
|
||||
//
|
||||
// It returns value with given `key`.
|
||||
func (tree *BTree) doSetWithLockCheck(key interface{}, value interface{}) interface{} {
|
||||
tree.mu.Lock()
|
||||
defer tree.mu.Unlock()
|
||||
if entry := tree.doSearch(key); entry != nil {
|
||||
return entry.Value
|
||||
}
|
||||
if f, ok := value.(func() interface{}); ok {
|
||||
value = f()
|
||||
}
|
||||
if value != nil {
|
||||
tree.doSet(key, value)
|
||||
}
|
||||
return value
|
||||
value, _ = tree.doGet(key)
|
||||
return
|
||||
}
|
||||
|
||||
// GetOrSet returns the value by key,
|
||||
// or sets value with given `value` if it does not exist and then returns this value.
|
||||
func (tree *BTree) GetOrSet(key interface{}, value interface{}) interface{} {
|
||||
if v, ok := tree.Search(key); !ok {
|
||||
return tree.doSetWithLockCheck(key, value)
|
||||
tree.mu.Lock()
|
||||
defer tree.mu.Unlock()
|
||||
if v, ok := tree.doGet(key); !ok {
|
||||
return tree.doSet(key, value)
|
||||
} else {
|
||||
return v
|
||||
}
|
||||
@ -149,8 +101,10 @@ func (tree *BTree) GetOrSet(key interface{}, value interface{}) interface{} {
|
||||
// or sets value with returned value of callback function `f` if it does not exist
|
||||
// and then returns this value.
|
||||
func (tree *BTree) GetOrSetFunc(key interface{}, f func() interface{}) interface{} {
|
||||
if v, ok := tree.Search(key); !ok {
|
||||
return tree.doSetWithLockCheck(key, f())
|
||||
tree.mu.Lock()
|
||||
defer tree.mu.Unlock()
|
||||
if v, ok := tree.doGet(key); !ok {
|
||||
return tree.doSet(key, f())
|
||||
} else {
|
||||
return v
|
||||
}
|
||||
@ -163,8 +117,10 @@ func (tree *BTree) GetOrSetFunc(key interface{}, f func() interface{}) interface
|
||||
// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f`
|
||||
// with mutex.Lock of the hash map.
|
||||
func (tree *BTree) GetOrSetFuncLock(key interface{}, f func() interface{}) interface{} {
|
||||
if v, ok := tree.Search(key); !ok {
|
||||
return tree.doSetWithLockCheck(key, f)
|
||||
tree.mu.Lock()
|
||||
defer tree.mu.Unlock()
|
||||
if v, ok := tree.doGet(key); !ok {
|
||||
return tree.doSet(key, f)
|
||||
} else {
|
||||
return v
|
||||
}
|
||||
@ -197,8 +153,10 @@ func (tree *BTree) GetVarOrSetFuncLock(key interface{}, f func() interface{}) *g
|
||||
// SetIfNotExist sets `value` to the map if the `key` does not exist, and then returns true.
|
||||
// It returns false if `key` exists, and `value` would be ignored.
|
||||
func (tree *BTree) SetIfNotExist(key interface{}, value interface{}) bool {
|
||||
if !tree.Contains(key) {
|
||||
tree.doSetWithLockCheck(key, value)
|
||||
tree.mu.Lock()
|
||||
defer tree.mu.Unlock()
|
||||
if _, ok := tree.doGet(key); !ok {
|
||||
tree.doSet(key, value)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
@ -207,8 +165,10 @@ func (tree *BTree) SetIfNotExist(key interface{}, value interface{}) bool {
|
||||
// SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true.
|
||||
// It returns false if `key` exists, and `value` would be ignored.
|
||||
func (tree *BTree) SetIfNotExistFunc(key interface{}, f func() interface{}) bool {
|
||||
if !tree.Contains(key) {
|
||||
tree.doSetWithLockCheck(key, f())
|
||||
tree.mu.Lock()
|
||||
defer tree.mu.Unlock()
|
||||
if _, ok := tree.doGet(key); !ok {
|
||||
tree.doSet(key, f())
|
||||
return true
|
||||
}
|
||||
return false
|
||||
@ -220,29 +180,29 @@ func (tree *BTree) SetIfNotExistFunc(key interface{}, f func() interface{}) bool
|
||||
// SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that
|
||||
// it executes function `f` with mutex.Lock of the hash map.
|
||||
func (tree *BTree) SetIfNotExistFuncLock(key interface{}, f func() interface{}) bool {
|
||||
if !tree.Contains(key) {
|
||||
tree.doSetWithLockCheck(key, f)
|
||||
tree.mu.Lock()
|
||||
defer tree.mu.Unlock()
|
||||
if _, ok := tree.doGet(key); !ok {
|
||||
tree.doSet(key, f)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Contains checks whether `key` exists in the tree.
|
||||
func (tree *BTree) Contains(key interface{}) bool {
|
||||
_, ok := tree.Search(key)
|
||||
return ok
|
||||
// Search searches the tree with given `key`.
|
||||
// Second return parameter `found` is true if key was found, otherwise false.
|
||||
func (tree *BTree) Search(key interface{}) (value interface{}, found bool) {
|
||||
tree.mu.RLock()
|
||||
defer tree.mu.RUnlock()
|
||||
return tree.tree.Get(key)
|
||||
}
|
||||
|
||||
// doRemove removes the node from the tree by key.
|
||||
// Key should adhere to the comparator's type assertion, otherwise method panics.
|
||||
func (tree *BTree) doRemove(key interface{}) (value interface{}) {
|
||||
node, index, found := tree.searchRecursively(tree.root, key)
|
||||
if found {
|
||||
value = node.Entries[index].Value
|
||||
tree.delete(node, index)
|
||||
tree.size--
|
||||
}
|
||||
return
|
||||
// Contains checks whether `key` exists in the tree.
|
||||
func (tree *BTree) Contains(key interface{}) bool {
|
||||
tree.mu.RLock()
|
||||
defer tree.mu.RUnlock()
|
||||
_, ok := tree.doGet(key)
|
||||
return ok
|
||||
}
|
||||
|
||||
// Remove removes the node from the tree by `key`.
|
||||
@ -263,42 +223,36 @@ func (tree *BTree) Removes(keys []interface{}) {
|
||||
|
||||
// IsEmpty returns true if tree does not contain any nodes
|
||||
func (tree *BTree) IsEmpty() bool {
|
||||
return tree.Size() == 0
|
||||
tree.mu.RLock()
|
||||
defer tree.mu.RUnlock()
|
||||
return tree.tree.Size() == 0
|
||||
}
|
||||
|
||||
// Size returns number of nodes in the tree.
|
||||
func (tree *BTree) Size() int {
|
||||
tree.mu.RLock()
|
||||
defer tree.mu.RUnlock()
|
||||
return tree.size
|
||||
return tree.tree.Size()
|
||||
}
|
||||
|
||||
// Keys returns all keys in asc order.
|
||||
func (tree *BTree) Keys() []interface{} {
|
||||
keys := make([]interface{}, tree.Size())
|
||||
index := 0
|
||||
tree.IteratorAsc(func(key, value interface{}) bool {
|
||||
keys[index] = key
|
||||
index++
|
||||
return true
|
||||
})
|
||||
return keys
|
||||
tree.mu.RLock()
|
||||
defer tree.mu.RUnlock()
|
||||
return tree.tree.Keys()
|
||||
}
|
||||
|
||||
// Values returns all values in asc order based on the key.
|
||||
func (tree *BTree) Values() []interface{} {
|
||||
values := make([]interface{}, tree.Size())
|
||||
index := 0
|
||||
tree.IteratorAsc(func(key, value interface{}) bool {
|
||||
values[index] = value
|
||||
index++
|
||||
return true
|
||||
})
|
||||
return values
|
||||
tree.mu.RLock()
|
||||
defer tree.mu.RUnlock()
|
||||
return tree.tree.Values()
|
||||
}
|
||||
|
||||
// Map returns all key-value items as map.
|
||||
func (tree *BTree) Map() map[interface{}]interface{} {
|
||||
tree.mu.RLock()
|
||||
defer tree.mu.RUnlock()
|
||||
m := make(map[interface{}]interface{}, tree.Size())
|
||||
tree.IteratorAsc(func(key, value interface{}) bool {
|
||||
m[key] = value
|
||||
@ -309,6 +263,8 @@ func (tree *BTree) Map() map[interface{}]interface{} {
|
||||
|
||||
// MapStrAny returns all key-value items as map[string]interface{}.
|
||||
func (tree *BTree) MapStrAny() map[string]interface{} {
|
||||
tree.mu.RLock()
|
||||
defer tree.mu.RUnlock()
|
||||
m := make(map[string]interface{}, tree.Size())
|
||||
tree.IteratorAsc(func(key, value interface{}) bool {
|
||||
m[gconv.String(key)] = value
|
||||
@ -321,16 +277,14 @@ func (tree *BTree) MapStrAny() map[string]interface{} {
|
||||
func (tree *BTree) Clear() {
|
||||
tree.mu.Lock()
|
||||
defer tree.mu.Unlock()
|
||||
tree.root = nil
|
||||
tree.size = 0
|
||||
tree.tree.Clear()
|
||||
}
|
||||
|
||||
// Replace the data of the tree with given `data`.
|
||||
func (tree *BTree) Replace(data map[interface{}]interface{}) {
|
||||
tree.mu.Lock()
|
||||
defer tree.mu.Unlock()
|
||||
tree.root = nil
|
||||
tree.size = 0
|
||||
tree.tree.Clear()
|
||||
for k, v := range data {
|
||||
tree.doSet(k, v)
|
||||
}
|
||||
@ -340,65 +294,42 @@ func (tree *BTree) Replace(data map[interface{}]interface{}) {
|
||||
func (tree *BTree) Height() int {
|
||||
tree.mu.RLock()
|
||||
defer tree.mu.RUnlock()
|
||||
return tree.root.height()
|
||||
return tree.tree.Height()
|
||||
}
|
||||
|
||||
// Left returns the left-most (min) entry or nil if tree is empty.
|
||||
func (tree *BTree) Left() *BTreeEntry {
|
||||
tree.mu.RLock()
|
||||
defer tree.mu.RUnlock()
|
||||
node := tree.left(tree.root)
|
||||
if node != nil {
|
||||
return node.Entries[0]
|
||||
node := tree.tree.Left()
|
||||
if node == nil || node.Entries == nil || len(node.Entries) == 0 {
|
||||
return nil
|
||||
}
|
||||
return &BTreeEntry{
|
||||
Key: node.Entries[0].Key,
|
||||
Value: node.Entries[0].Value,
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Right returns the right-most (max) entry or nil if tree is empty.
|
||||
func (tree *BTree) Right() *BTreeEntry {
|
||||
tree.mu.RLock()
|
||||
defer tree.mu.RUnlock()
|
||||
node := tree.right(tree.root)
|
||||
if node != nil {
|
||||
return node.Entries[len(node.Entries)-1]
|
||||
node := tree.tree.Right()
|
||||
if node == nil || node.Entries == nil || len(node.Entries) == 0 {
|
||||
return nil
|
||||
}
|
||||
return &BTreeEntry{
|
||||
Key: node.Entries[len(node.Entries)-1].Key,
|
||||
Value: node.Entries[len(node.Entries)-1].Value,
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// String returns a string representation of container (for debugging purposes)
|
||||
func (tree *BTree) String() string {
|
||||
if tree == nil {
|
||||
return ""
|
||||
}
|
||||
tree.mu.RLock()
|
||||
defer tree.mu.RUnlock()
|
||||
var buffer bytes.Buffer
|
||||
if tree.size != 0 {
|
||||
tree.output(&buffer, tree.root, 0, true)
|
||||
}
|
||||
return buffer.String()
|
||||
}
|
||||
|
||||
// Search searches the tree with given `key`.
|
||||
// Second return parameter `found` is true if key was found, otherwise false.
|
||||
func (tree *BTree) Search(key interface{}) (value interface{}, found bool) {
|
||||
tree.mu.RLock()
|
||||
defer tree.mu.RUnlock()
|
||||
node, index, found := tree.searchRecursively(tree.root, key)
|
||||
if found {
|
||||
return node.Entries[index].Value, true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// Search searches the tree with given `key` without mutex.
|
||||
// It returns the entry if found or otherwise nil.
|
||||
func (tree *BTree) doSearch(key interface{}) *BTreeEntry {
|
||||
node, index, found := tree.searchRecursively(tree.root, key)
|
||||
if found {
|
||||
return node.Entries[index]
|
||||
}
|
||||
return nil
|
||||
return gstr.Replace(tree.tree.String(), "BTree\n", "")
|
||||
}
|
||||
|
||||
// Print prints the tree to stdout.
|
||||
@ -421,11 +352,13 @@ func (tree *BTree) IteratorFrom(key interface{}, match bool, f func(key, value i
|
||||
func (tree *BTree) IteratorAsc(f func(key, value interface{}) bool) {
|
||||
tree.mu.RLock()
|
||||
defer tree.mu.RUnlock()
|
||||
node := tree.left(tree.root)
|
||||
if node == nil {
|
||||
return
|
||||
it := tree.tree.Iterator()
|
||||
for it.Begin(); it.Next(); {
|
||||
index, value := it.Key(), it.Value()
|
||||
if ok := f(index, value); !ok {
|
||||
break
|
||||
}
|
||||
}
|
||||
tree.doIteratorAsc(node, node.Entries[0], 0, f)
|
||||
}
|
||||
|
||||
// IteratorAscFrom iterates the tree readonly in ascending order with given callback function `f`.
|
||||
@ -435,59 +368,13 @@ func (tree *BTree) IteratorAsc(f func(key, value interface{}) bool) {
|
||||
func (tree *BTree) IteratorAscFrom(key interface{}, match bool, f func(key, value interface{}) bool) {
|
||||
tree.mu.RLock()
|
||||
defer tree.mu.RUnlock()
|
||||
node, index, found := tree.searchRecursively(tree.root, key)
|
||||
if match {
|
||||
if found {
|
||||
tree.doIteratorAsc(node, node.Entries[index], index, f)
|
||||
}
|
||||
} else {
|
||||
if index >= 0 && index < len(node.Entries) {
|
||||
tree.doIteratorAsc(node, node.Entries[index], index, f)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (tree *BTree) doIteratorAsc(node *BTreeNode, entry *BTreeEntry, index int, f func(key, value interface{}) bool) {
|
||||
first := true
|
||||
loop:
|
||||
if entry == nil {
|
||||
var keys = tree.tree.Keys()
|
||||
index, isIterator := tree.iteratorFromGetIndex(key, keys, match)
|
||||
if !isIterator {
|
||||
return
|
||||
}
|
||||
if !f(entry.Key, entry.Value) {
|
||||
return
|
||||
}
|
||||
// Find current entry position in current node
|
||||
if !first {
|
||||
index, _ = tree.search(node, entry.Key)
|
||||
} else {
|
||||
first = false
|
||||
}
|
||||
// Try to go down to the child right of the current entry
|
||||
if index+1 < len(node.Children) {
|
||||
node = node.Children[index+1]
|
||||
// Try to go down to the child left of the current node
|
||||
for len(node.Children) > 0 {
|
||||
node = node.Children[0]
|
||||
}
|
||||
// Return the left-most entry
|
||||
entry = node.Entries[0]
|
||||
goto loop
|
||||
}
|
||||
// Above assures that we have reached a leaf node, so return the next entry in current node (if any)
|
||||
if index+1 < len(node.Entries) {
|
||||
entry = node.Entries[index+1]
|
||||
goto loop
|
||||
}
|
||||
// Reached leaf node and there are no entries to the right of the current entry, so go up to the parent
|
||||
for node.Parent != nil {
|
||||
node = node.Parent
|
||||
// Find next entry position in current node (note: search returns the first equal or bigger than entry)
|
||||
index, _ = tree.search(node, entry.Key)
|
||||
// Check that there is a next entry position in current node
|
||||
if index < len(node.Entries) {
|
||||
entry = node.Entries[index]
|
||||
goto loop
|
||||
}
|
||||
for ; index < len(keys); index++ {
|
||||
f(keys[index], tree.Get(keys[index]))
|
||||
}
|
||||
}
|
||||
|
||||
@ -496,13 +383,13 @@ loop:
|
||||
func (tree *BTree) IteratorDesc(f func(key, value interface{}) bool) {
|
||||
tree.mu.RLock()
|
||||
defer tree.mu.RUnlock()
|
||||
node := tree.right(tree.root)
|
||||
if node == nil {
|
||||
return
|
||||
it := tree.tree.Iterator()
|
||||
for it.End(); it.Prev(); {
|
||||
index, value := it.Key(), it.Value()
|
||||
if ok := f(index, value); !ok {
|
||||
break
|
||||
}
|
||||
}
|
||||
index := len(node.Entries) - 1
|
||||
entry := node.Entries[index]
|
||||
tree.doIteratorDesc(node, entry, index, f)
|
||||
}
|
||||
|
||||
// IteratorDescFrom iterates the tree readonly in descending order with given callback function `f`.
|
||||
@ -512,468 +399,70 @@ func (tree *BTree) IteratorDesc(f func(key, value interface{}) bool) {
|
||||
func (tree *BTree) IteratorDescFrom(key interface{}, match bool, f func(key, value interface{}) bool) {
|
||||
tree.mu.RLock()
|
||||
defer tree.mu.RUnlock()
|
||||
node, index, found := tree.searchRecursively(tree.root, key)
|
||||
if match {
|
||||
if found {
|
||||
tree.doIteratorDesc(node, node.Entries[index], index, f)
|
||||
}
|
||||
} else {
|
||||
if index >= 0 && index < len(node.Entries) {
|
||||
tree.doIteratorDesc(node, node.Entries[index], index, f)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// IteratorDesc iterates the tree readonly in descending order with given callback function `f`.
|
||||
// If `f` returns true, then it continues iterating; or false to stop.
|
||||
func (tree *BTree) doIteratorDesc(node *BTreeNode, entry *BTreeEntry, index int, f func(key, value interface{}) bool) {
|
||||
first := true
|
||||
loop:
|
||||
if entry == nil {
|
||||
var keys = tree.tree.Keys()
|
||||
index, isIterator := tree.iteratorFromGetIndex(key, keys, match)
|
||||
if !isIterator {
|
||||
return
|
||||
}
|
||||
if !f(entry.Key, entry.Value) {
|
||||
return
|
||||
for ; index >= 0; index-- {
|
||||
f(keys[index], tree.Get(keys[index]))
|
||||
}
|
||||
// Find current entry position in current node
|
||||
if !first {
|
||||
index, _ = tree.search(node, entry.Key)
|
||||
} else {
|
||||
first = false
|
||||
}
|
||||
// Try to go down to the child left of the current entry
|
||||
if index < len(node.Children) {
|
||||
node = node.Children[index]
|
||||
// Try to go down to the child right of the current node
|
||||
for len(node.Children) > 0 {
|
||||
node = node.Children[len(node.Children)-1]
|
||||
}
|
||||
// Return the right-most entry
|
||||
entry = node.Entries[len(node.Entries)-1]
|
||||
goto loop
|
||||
}
|
||||
// Above assures that we have reached a leaf node, so return the previous entry in current node (if any)
|
||||
if index-1 >= 0 {
|
||||
entry = node.Entries[index-1]
|
||||
goto loop
|
||||
}
|
||||
|
||||
// Reached leaf node and there are no entries to the left of the current entry, so go up to the parent
|
||||
for node.Parent != nil {
|
||||
node = node.Parent
|
||||
// Find previous entry position in current node (note: search returns the first equal or bigger than entry)
|
||||
index, _ = tree.search(node, entry.Key)
|
||||
// Check that there is a previous entry position in current node
|
||||
if index-1 >= 0 {
|
||||
entry = node.Entries[index-1]
|
||||
goto loop
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (tree *BTree) output(buffer *bytes.Buffer, node *BTreeNode, level int, isTail bool) {
|
||||
for e := 0; e < len(node.Entries)+1; e++ {
|
||||
if e < len(node.Children) {
|
||||
tree.output(buffer, node.Children[e], level+1, true)
|
||||
}
|
||||
if e < len(node.Entries) {
|
||||
if _, err := buffer.WriteString(strings.Repeat(" ", level)); err != nil {
|
||||
intlog.Errorf(context.TODO(), `%+v`, err)
|
||||
}
|
||||
if _, err := buffer.WriteString(fmt.Sprintf("%v", node.Entries[e].Key) + "\n"); err != nil {
|
||||
intlog.Errorf(context.TODO(), `%+v`, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (node *BTreeNode) height() int {
|
||||
h := 0
|
||||
n := node
|
||||
for ; n != nil; n = n.Children[0] {
|
||||
h++
|
||||
if len(n.Children) == 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
return h
|
||||
}
|
||||
|
||||
func (tree *BTree) isLeaf(node *BTreeNode) bool {
|
||||
return len(node.Children) == 0
|
||||
}
|
||||
|
||||
// func (tree *BTree) isFull(node *BTreeNode) bool {
|
||||
// return len(node.Entries) == tree.maxEntries()
|
||||
// }
|
||||
|
||||
func (tree *BTree) shouldSplit(node *BTreeNode) bool {
|
||||
return len(node.Entries) > tree.maxEntries()
|
||||
}
|
||||
|
||||
func (tree *BTree) maxChildren() int {
|
||||
return tree.m
|
||||
}
|
||||
|
||||
func (tree *BTree) minChildren() int {
|
||||
return (tree.m + 1) / 2 // ceil(m/2)
|
||||
}
|
||||
|
||||
func (tree *BTree) maxEntries() int {
|
||||
return tree.maxChildren() - 1
|
||||
}
|
||||
|
||||
func (tree *BTree) minEntries() int {
|
||||
return tree.minChildren() - 1
|
||||
}
|
||||
|
||||
func (tree *BTree) middle() int {
|
||||
// "-1" to favor right nodes to have more keys when splitting
|
||||
return (tree.m - 1) / 2
|
||||
}
|
||||
|
||||
// search does search only within the single node among its entries
|
||||
func (tree *BTree) search(node *BTreeNode, key interface{}) (index int, found bool) {
|
||||
low, mid, high := 0, 0, len(node.Entries)-1
|
||||
for low <= high {
|
||||
mid = low + (high-low)/2
|
||||
compare := tree.getComparator()(key, node.Entries[mid].Key)
|
||||
switch {
|
||||
case compare > 0:
|
||||
low = mid + 1
|
||||
case compare < 0:
|
||||
high = mid - 1
|
||||
case compare == 0:
|
||||
return mid, true
|
||||
}
|
||||
}
|
||||
return low, false
|
||||
}
|
||||
|
||||
// searchRecursively searches recursively down the tree starting at the startNode
|
||||
func (tree *BTree) searchRecursively(startNode *BTreeNode, key interface{}) (node *BTreeNode, index int, found bool) {
|
||||
if tree.size == 0 {
|
||||
return nil, -1, false
|
||||
}
|
||||
node = startNode
|
||||
for {
|
||||
index, found = tree.search(node, key)
|
||||
if found {
|
||||
return node, index, true
|
||||
}
|
||||
if tree.isLeaf(node) {
|
||||
return node, index, false
|
||||
}
|
||||
node = node.Children[index]
|
||||
}
|
||||
}
|
||||
|
||||
func (tree *BTree) insert(node *BTreeNode, entry *BTreeEntry) (inserted bool) {
|
||||
if tree.isLeaf(node) {
|
||||
return tree.insertIntoLeaf(node, entry)
|
||||
}
|
||||
return tree.insertIntoInternal(node, entry)
|
||||
}
|
||||
|
||||
func (tree *BTree) insertIntoLeaf(node *BTreeNode, entry *BTreeEntry) (inserted bool) {
|
||||
insertPosition, found := tree.search(node, entry.Key)
|
||||
if found {
|
||||
node.Entries[insertPosition] = entry
|
||||
return false
|
||||
}
|
||||
// Insert entry's key in the middle of the node
|
||||
node.Entries = append(node.Entries, nil)
|
||||
copy(node.Entries[insertPosition+1:], node.Entries[insertPosition:])
|
||||
node.Entries[insertPosition] = entry
|
||||
tree.split(node)
|
||||
return true
|
||||
}
|
||||
|
||||
func (tree *BTree) insertIntoInternal(node *BTreeNode, entry *BTreeEntry) (inserted bool) {
|
||||
insertPosition, found := tree.search(node, entry.Key)
|
||||
if found {
|
||||
node.Entries[insertPosition] = entry
|
||||
return false
|
||||
}
|
||||
return tree.insert(node.Children[insertPosition], entry)
|
||||
}
|
||||
|
||||
func (tree *BTree) split(node *BTreeNode) {
|
||||
if !tree.shouldSplit(node) {
|
||||
return
|
||||
}
|
||||
|
||||
if node == tree.root {
|
||||
tree.splitRoot()
|
||||
return
|
||||
}
|
||||
|
||||
tree.splitNonRoot(node)
|
||||
}
|
||||
|
||||
func (tree *BTree) splitNonRoot(node *BTreeNode) {
|
||||
middle := tree.middle()
|
||||
parent := node.Parent
|
||||
|
||||
left := &BTreeNode{Entries: append([]*BTreeEntry(nil), node.Entries[:middle]...), Parent: parent}
|
||||
right := &BTreeNode{Entries: append([]*BTreeEntry(nil), node.Entries[middle+1:]...), Parent: parent}
|
||||
|
||||
// Move children from the node to be split into left and right nodes
|
||||
if !tree.isLeaf(node) {
|
||||
left.Children = append([]*BTreeNode(nil), node.Children[:middle+1]...)
|
||||
right.Children = append([]*BTreeNode(nil), node.Children[middle+1:]...)
|
||||
setParent(left.Children, left)
|
||||
setParent(right.Children, right)
|
||||
}
|
||||
|
||||
insertPosition, _ := tree.search(parent, node.Entries[middle].Key)
|
||||
|
||||
// Insert middle key into parent
|
||||
parent.Entries = append(parent.Entries, nil)
|
||||
copy(parent.Entries[insertPosition+1:], parent.Entries[insertPosition:])
|
||||
parent.Entries[insertPosition] = node.Entries[middle]
|
||||
|
||||
// Set child left of inserted key in parent to the created left node
|
||||
parent.Children[insertPosition] = left
|
||||
|
||||
// Set child right of inserted key in parent to the created right node
|
||||
parent.Children = append(parent.Children, nil)
|
||||
copy(parent.Children[insertPosition+2:], parent.Children[insertPosition+1:])
|
||||
parent.Children[insertPosition+1] = right
|
||||
|
||||
tree.split(parent)
|
||||
}
|
||||
|
||||
func (tree *BTree) splitRoot() {
|
||||
middle := tree.middle()
|
||||
left := &BTreeNode{Entries: append([]*BTreeEntry(nil), tree.root.Entries[:middle]...)}
|
||||
right := &BTreeNode{Entries: append([]*BTreeEntry(nil), tree.root.Entries[middle+1:]...)}
|
||||
|
||||
// Move children from the node to be split into left and right nodes
|
||||
if !tree.isLeaf(tree.root) {
|
||||
left.Children = append([]*BTreeNode(nil), tree.root.Children[:middle+1]...)
|
||||
right.Children = append([]*BTreeNode(nil), tree.root.Children[middle+1:]...)
|
||||
setParent(left.Children, left)
|
||||
setParent(right.Children, right)
|
||||
}
|
||||
|
||||
// Root is a node with one entry and two children (left and right)
|
||||
newRoot := &BTreeNode{
|
||||
Entries: []*BTreeEntry{tree.root.Entries[middle]},
|
||||
Children: []*BTreeNode{left, right},
|
||||
}
|
||||
|
||||
left.Parent = newRoot
|
||||
right.Parent = newRoot
|
||||
tree.root = newRoot
|
||||
}
|
||||
|
||||
func setParent(nodes []*BTreeNode, parent *BTreeNode) {
|
||||
for _, node := range nodes {
|
||||
node.Parent = parent
|
||||
}
|
||||
}
|
||||
|
||||
func (tree *BTree) left(node *BTreeNode) *BTreeNode {
|
||||
if tree.size == 0 {
|
||||
return nil
|
||||
}
|
||||
current := node
|
||||
for {
|
||||
if tree.isLeaf(current) {
|
||||
return current
|
||||
}
|
||||
current = current.Children[0]
|
||||
}
|
||||
}
|
||||
|
||||
func (tree *BTree) right(node *BTreeNode) *BTreeNode {
|
||||
if tree.size == 0 {
|
||||
return nil
|
||||
}
|
||||
current := node
|
||||
for {
|
||||
if tree.isLeaf(current) {
|
||||
return current
|
||||
}
|
||||
current = current.Children[len(current.Children)-1]
|
||||
}
|
||||
}
|
||||
|
||||
// leftSibling returns the node's left sibling and child index (in parent) if it exists, otherwise (nil,-1)
|
||||
// key is any of keys in node (could even be deleted).
|
||||
func (tree *BTree) leftSibling(node *BTreeNode, key interface{}) (*BTreeNode, int) {
|
||||
if node.Parent != nil {
|
||||
index, _ := tree.search(node.Parent, key)
|
||||
index--
|
||||
if index >= 0 && index < len(node.Parent.Children) {
|
||||
return node.Parent.Children[index], index
|
||||
}
|
||||
}
|
||||
return nil, -1
|
||||
}
|
||||
|
||||
// rightSibling returns the node's right sibling and child index (in parent) if it exists, otherwise (nil,-1)
|
||||
// key is any of keys in node (could even be deleted).
|
||||
func (tree *BTree) rightSibling(node *BTreeNode, key interface{}) (*BTreeNode, int) {
|
||||
if node.Parent != nil {
|
||||
index, _ := tree.search(node.Parent, key)
|
||||
index++
|
||||
if index < len(node.Parent.Children) {
|
||||
return node.Parent.Children[index], index
|
||||
}
|
||||
}
|
||||
return nil, -1
|
||||
}
|
||||
|
||||
// delete deletes an entry in node at entries' index
|
||||
// ref.: https://en.wikipedia.org/wiki/B-tree#Deletion
|
||||
func (tree *BTree) delete(node *BTreeNode, index int) {
|
||||
// deleting from a leaf node
|
||||
if tree.isLeaf(node) {
|
||||
deletedKey := node.Entries[index].Key
|
||||
tree.deleteEntry(node, index)
|
||||
tree.reBalance(node, deletedKey)
|
||||
if len(tree.root.Entries) == 0 {
|
||||
tree.root = nil
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// deleting from an internal node
|
||||
leftLargestNode := tree.right(node.Children[index]) // largest node in the left sub-tree (assumed to exist)
|
||||
leftLargestEntryIndex := len(leftLargestNode.Entries) - 1
|
||||
node.Entries[index] = leftLargestNode.Entries[leftLargestEntryIndex]
|
||||
deletedKey := leftLargestNode.Entries[leftLargestEntryIndex].Key
|
||||
tree.deleteEntry(leftLargestNode, leftLargestEntryIndex)
|
||||
tree.reBalance(leftLargestNode, deletedKey)
|
||||
}
|
||||
|
||||
// reBalance reBalances the tree after deletion if necessary and returns true, otherwise false.
|
||||
// Note that we first delete the entry and then call reBalance, thus the passed deleted key as reference.
|
||||
func (tree *BTree) reBalance(node *BTreeNode, deletedKey interface{}) {
|
||||
// check if re-balancing is needed
|
||||
if node == nil || len(node.Entries) >= tree.minEntries() {
|
||||
return
|
||||
}
|
||||
|
||||
// try to borrow from left sibling
|
||||
leftSibling, leftSiblingIndex := tree.leftSibling(node, deletedKey)
|
||||
if leftSibling != nil && len(leftSibling.Entries) > tree.minEntries() {
|
||||
// rotate right
|
||||
node.Entries = append([]*BTreeEntry{node.Parent.Entries[leftSiblingIndex]}, node.Entries...) // prepend parent's separator entry to node's entries
|
||||
node.Parent.Entries[leftSiblingIndex] = leftSibling.Entries[len(leftSibling.Entries)-1]
|
||||
tree.deleteEntry(leftSibling, len(leftSibling.Entries)-1)
|
||||
if !tree.isLeaf(leftSibling) {
|
||||
leftSiblingRightMostChild := leftSibling.Children[len(leftSibling.Children)-1]
|
||||
leftSiblingRightMostChild.Parent = node
|
||||
node.Children = append([]*BTreeNode{leftSiblingRightMostChild}, node.Children...)
|
||||
tree.deleteChild(leftSibling, len(leftSibling.Children)-1)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// try to borrow from right sibling
|
||||
rightSibling, rightSiblingIndex := tree.rightSibling(node, deletedKey)
|
||||
if rightSibling != nil && len(rightSibling.Entries) > tree.minEntries() {
|
||||
// rotate left
|
||||
node.Entries = append(node.Entries, node.Parent.Entries[rightSiblingIndex-1]) // append parent's separator entry to node's entries
|
||||
node.Parent.Entries[rightSiblingIndex-1] = rightSibling.Entries[0]
|
||||
tree.deleteEntry(rightSibling, 0)
|
||||
if !tree.isLeaf(rightSibling) {
|
||||
rightSiblingLeftMostChild := rightSibling.Children[0]
|
||||
rightSiblingLeftMostChild.Parent = node
|
||||
node.Children = append(node.Children, rightSiblingLeftMostChild)
|
||||
tree.deleteChild(rightSibling, 0)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// merge with siblings
|
||||
if rightSibling != nil {
|
||||
// merge with right sibling
|
||||
node.Entries = append(node.Entries, node.Parent.Entries[rightSiblingIndex-1])
|
||||
node.Entries = append(node.Entries, rightSibling.Entries...)
|
||||
deletedKey = node.Parent.Entries[rightSiblingIndex-1].Key
|
||||
tree.deleteEntry(node.Parent, rightSiblingIndex-1)
|
||||
tree.appendChildren(node.Parent.Children[rightSiblingIndex], node)
|
||||
tree.deleteChild(node.Parent, rightSiblingIndex)
|
||||
} else if leftSibling != nil {
|
||||
// merge with left sibling
|
||||
entries := append([]*BTreeEntry(nil), leftSibling.Entries...)
|
||||
entries = append(entries, node.Parent.Entries[leftSiblingIndex])
|
||||
node.Entries = append(entries, node.Entries...)
|
||||
deletedKey = node.Parent.Entries[leftSiblingIndex].Key
|
||||
tree.deleteEntry(node.Parent, leftSiblingIndex)
|
||||
tree.prependChildren(node.Parent.Children[leftSiblingIndex], node)
|
||||
tree.deleteChild(node.Parent, leftSiblingIndex)
|
||||
}
|
||||
|
||||
// make the merged node the root if its parent was the root and the root is empty
|
||||
if node.Parent == tree.root && len(tree.root.Entries) == 0 {
|
||||
tree.root = node
|
||||
node.Parent = nil
|
||||
return
|
||||
}
|
||||
|
||||
// parent might be underflow, so try to reBalance if necessary
|
||||
tree.reBalance(node.Parent, deletedKey)
|
||||
}
|
||||
|
||||
func (tree *BTree) prependChildren(fromNode *BTreeNode, toNode *BTreeNode) {
|
||||
children := append([]*BTreeNode(nil), fromNode.Children...)
|
||||
toNode.Children = append(children, toNode.Children...)
|
||||
setParent(fromNode.Children, toNode)
|
||||
}
|
||||
|
||||
func (tree *BTree) appendChildren(fromNode *BTreeNode, toNode *BTreeNode) {
|
||||
toNode.Children = append(toNode.Children, fromNode.Children...)
|
||||
setParent(fromNode.Children, toNode)
|
||||
}
|
||||
|
||||
func (tree *BTree) deleteEntry(node *BTreeNode, index int) {
|
||||
copy(node.Entries[index:], node.Entries[index+1:])
|
||||
node.Entries[len(node.Entries)-1] = nil
|
||||
node.Entries = node.Entries[:len(node.Entries)-1]
|
||||
}
|
||||
|
||||
func (tree *BTree) deleteChild(node *BTreeNode, index int) {
|
||||
if index >= len(node.Children) {
|
||||
return
|
||||
}
|
||||
copy(node.Children[index:], node.Children[index+1:])
|
||||
node.Children[len(node.Children)-1] = nil
|
||||
node.Children = node.Children[:len(node.Children)-1]
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
func (tree BTree) MarshalJSON() (jsonBytes []byte, err error) {
|
||||
if tree.root == nil {
|
||||
return []byte("null"), nil
|
||||
}
|
||||
buffer := bytes.NewBuffer(nil)
|
||||
buffer.WriteByte('{')
|
||||
tree.Iterator(func(key, value interface{}) bool {
|
||||
valueBytes, valueJsonErr := json.Marshal(value)
|
||||
if valueJsonErr != nil {
|
||||
err = valueJsonErr
|
||||
return false
|
||||
}
|
||||
if buffer.Len() > 1 {
|
||||
buffer.WriteByte(',')
|
||||
}
|
||||
buffer.WriteString(fmt.Sprintf(`"%v":%s`, key, valueBytes))
|
||||
return true
|
||||
})
|
||||
buffer.WriteByte('}')
|
||||
return buffer.Bytes(), nil
|
||||
func (tree *BTree) MarshalJSON() (jsonBytes []byte, err error) {
|
||||
tree.mu.RLock()
|
||||
defer tree.mu.RUnlock()
|
||||
return tree.tree.MarshalJSON()
|
||||
}
|
||||
|
||||
// getComparator returns the comparator if it's previously set,
|
||||
// or else it panics.
|
||||
func (tree *BTree) getComparator() func(a, b interface{}) int {
|
||||
if tree.comparator == nil {
|
||||
panic("comparator is missing for tree")
|
||||
// doSet inserts key-value pair node into the tree.
|
||||
// If key already exists, then its value is updated with the new value.
|
||||
// If `value` is type of <func() interface {}>,
|
||||
// it will be executed and its return value will be set to the map with `key`.
|
||||
//
|
||||
// It returns value with given `key`.
|
||||
func (tree *BTree) doSet(key interface{}, value interface{}) interface{} {
|
||||
if f, ok := value.(func() interface{}); ok {
|
||||
value = f()
|
||||
}
|
||||
return tree.comparator
|
||||
if value == nil {
|
||||
return value
|
||||
}
|
||||
tree.tree.Put(key, value)
|
||||
return value
|
||||
}
|
||||
|
||||
// doGet get the value from the tree by key.
|
||||
func (tree *BTree) doGet(key interface{}) (value interface{}, ok bool) {
|
||||
return tree.tree.Get(key)
|
||||
}
|
||||
|
||||
// doRemove removes the node from the tree by key.
|
||||
// Key should adhere to the comparator's type assertion, otherwise method panics.
|
||||
func (tree *BTree) doRemove(key interface{}) (value interface{}) {
|
||||
value, _ = tree.tree.Get(key)
|
||||
tree.tree.Remove(key)
|
||||
return
|
||||
}
|
||||
|
||||
// iteratorFromGetIndex returns the index of the key in the keys slice.
|
||||
// The parameter `match` specifies whether starting iterating if the `key` is fully matched,
|
||||
// or else using index searching iterating.
|
||||
// If `isIterator` is true, iterator is available; or else not.
|
||||
func (tree *BTree) iteratorFromGetIndex(key interface{}, keys []interface{}, match bool) (index int, isIterator bool) {
|
||||
if match {
|
||||
for i, k := range keys {
|
||||
if k == key {
|
||||
isIterator = true
|
||||
index = i
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if i, ok := key.(int); ok {
|
||||
isIterator = true
|
||||
index = i
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@ -453,7 +453,7 @@ func ExampleBTree_String() {
|
||||
fmt.Println(tree.String())
|
||||
|
||||
// Output:
|
||||
// key0
|
||||
// key0
|
||||
// key1
|
||||
// key2
|
||||
// key3
|
||||
@ -484,7 +484,7 @@ func ExampleBTree_Print() {
|
||||
tree.Print()
|
||||
|
||||
// Output:
|
||||
// key0
|
||||
// key0
|
||||
// key1
|
||||
// key2
|
||||
// key3
|
||||
|
||||
@ -4,12 +4,13 @@ go 1.18
|
||||
|
||||
require (
|
||||
github.com/apolloconfig/agollo/v4 v4.3.1
|
||||
github.com/gogf/gf/v2 v2.7.1
|
||||
github.com/gogf/gf/v2 v2.7.2
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/BurntSushi/toml v1.3.2 // indirect
|
||||
github.com/clbanning/mxj/v2 v2.7.0 // indirect
|
||||
github.com/emirpasic/gods v1.18.1 // indirect
|
||||
github.com/fatih/color v1.16.0 // indirect
|
||||
github.com/fsnotify/fsnotify v1.7.0 // indirect
|
||||
github.com/go-logr/logr v1.2.4 // indirect
|
||||
|
||||
@ -64,6 +64,8 @@ github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSV
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
|
||||
github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
|
||||
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
||||
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
||||
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
|
||||
|
||||
@ -11,13 +11,14 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/consul/api"
|
||||
"github.com/hashicorp/consul/api/watch"
|
||||
|
||||
"github.com/gogf/gf/v2/encoding/gjson"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gcfg"
|
||||
"github.com/gogf/gf/v2/os/glog"
|
||||
"github.com/hashicorp/consul/api"
|
||||
"github.com/hashicorp/consul/api/watch"
|
||||
)
|
||||
|
||||
// Config is the configuration object for consul client.
|
||||
@ -156,22 +157,28 @@ func (c *Client) addWatcher() (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := c.doUpdate(v.Value); err != nil {
|
||||
c.config.Logger.Errorf(context.Background(),
|
||||
if err = c.doUpdate(v.Value); err != nil {
|
||||
c.config.Logger.Errorf(
|
||||
context.Background(),
|
||||
"watch config from consul path %+v update failed: %s",
|
||||
c.config.Path, err)
|
||||
c.config.Path, err,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
plan.Datacenter = c.config.ConsulConfig.Datacenter
|
||||
plan.Token = c.config.ConsulConfig.Token
|
||||
|
||||
go func() {
|
||||
if err := plan.Run(c.config.ConsulConfig.Address); err != nil {
|
||||
c.config.Logger.Errorf(context.Background(),
|
||||
"watch config from consul path %+v plan start failed: %s",
|
||||
c.config.Path, err)
|
||||
}
|
||||
}()
|
||||
go c.startAsynchronousWatch(plan)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) startAsynchronousWatch(plan *watch.Plan) {
|
||||
if err := plan.Run(c.config.ConsulConfig.Address); err != nil {
|
||||
c.config.Logger.Errorf(
|
||||
context.Background(),
|
||||
"watch config from consul path %+v plan start failed: %s",
|
||||
c.config.Path, err,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/config/consul/v2
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/gogf/gf/v2 v2.7.1
|
||||
github.com/gogf/gf/v2 v2.7.2
|
||||
github.com/hashicorp/consul/api v1.24.0
|
||||
github.com/hashicorp/go-cleanhttp v0.5.2
|
||||
)
|
||||
@ -12,6 +12,7 @@ require (
|
||||
github.com/BurntSushi/toml v1.3.2 // indirect
|
||||
github.com/armon/go-metrics v0.4.1 // indirect
|
||||
github.com/clbanning/mxj/v2 v2.7.0 // indirect
|
||||
github.com/emirpasic/gods v1.18.1 // indirect
|
||||
github.com/fatih/color v1.16.0 // indirect
|
||||
github.com/fsnotify/fsnotify v1.7.0 // indirect
|
||||
github.com/go-logr/logr v1.2.4 // indirect
|
||||
|
||||
@ -23,6 +23,8 @@ github.com/clbanning/mxj/v2 v2.7.0/go.mod h1:hNiWqW14h+kc+MdF9C6/YoRfjEJoR3ou6tn
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
|
||||
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
|
||||
github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
|
||||
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
|
||||
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
|
||||
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
|
||||
|
||||
@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/config/kubecm/v2
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/gogf/gf/v2 v2.7.1
|
||||
github.com/gogf/gf/v2 v2.7.2
|
||||
k8s.io/api v0.27.4
|
||||
k8s.io/apimachinery v0.27.4
|
||||
k8s.io/client-go v0.27.4
|
||||
@ -14,6 +14,7 @@ require (
|
||||
github.com/clbanning/mxj/v2 v2.7.0 // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
|
||||
github.com/emirpasic/gods v1.18.1 // indirect
|
||||
github.com/fatih/color v1.16.0 // indirect
|
||||
github.com/fsnotify/fsnotify v1.7.0 // indirect
|
||||
github.com/go-logr/logr v1.2.4 // indirect
|
||||
|
||||
@ -50,6 +50,8 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
|
||||
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
|
||||
github.com/emicklei/go-restful/v3 v3.9.0 h1:XwGDlfxEnQZzuopoqxwSEllNcCOM9DhhFyhFIIGKwxE=
|
||||
github.com/emicklei/go-restful/v3 v3.9.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
|
||||
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
|
||||
github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
|
||||
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
||||
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
||||
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
|
||||
|
||||
@ -164,14 +164,16 @@ func (c *Client) doWatch(ctx context.Context, namespace string) (err error) {
|
||||
c.config.ConfigMap, namespace,
|
||||
)
|
||||
}
|
||||
go func() {
|
||||
for {
|
||||
event := <-watchHandler.ResultChan()
|
||||
switch event.Type {
|
||||
case watch.Modified:
|
||||
_ = c.doUpdate(ctx, namespace)
|
||||
}
|
||||
}
|
||||
}()
|
||||
go c.startAsynchronousWatch(ctx, namespace, watchHandler)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) startAsynchronousWatch(ctx context.Context, namespace string, watchHandler watch.Interface) {
|
||||
for {
|
||||
event := <-watchHandler.ResultChan()
|
||||
switch event.Type {
|
||||
case watch.Modified:
|
||||
_ = c.doUpdate(ctx, namespace)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/config/nacos/v2
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/gogf/gf/v2 v2.7.1
|
||||
github.com/gogf/gf/v2 v2.7.2
|
||||
github.com/nacos-group/nacos-sdk-go/v2 v2.2.5
|
||||
)
|
||||
|
||||
@ -19,6 +19,7 @@ require (
|
||||
github.com/buger/jsonparser v1.1.1 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.2.0 // indirect
|
||||
github.com/clbanning/mxj/v2 v2.7.0 // indirect
|
||||
github.com/emirpasic/gods v1.18.1 // indirect
|
||||
github.com/fatih/color v1.16.0 // indirect
|
||||
github.com/fsnotify/fsnotify v1.7.0 // indirect
|
||||
github.com/go-logr/logr v1.2.4 // indirect
|
||||
|
||||
@ -80,6 +80,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
|
||||
github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
|
||||
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
||||
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
||||
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
|
||||
|
||||
@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/config/polaris/v2
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/gogf/gf/v2 v2.7.1
|
||||
github.com/gogf/gf/v2 v2.7.2
|
||||
github.com/polarismesh/polaris-go v1.5.5
|
||||
)
|
||||
|
||||
@ -13,6 +13,7 @@ require (
|
||||
github.com/cespare/xxhash/v2 v2.2.0 // indirect
|
||||
github.com/clbanning/mxj/v2 v2.7.0 // indirect
|
||||
github.com/dlclark/regexp2 v1.7.0 // indirect
|
||||
github.com/emirpasic/gods v1.18.1 // indirect
|
||||
github.com/fatih/color v1.16.0 // indirect
|
||||
github.com/fsnotify/fsnotify v1.7.0 // indirect
|
||||
github.com/go-logr/logr v1.2.4 // indirect
|
||||
|
||||
@ -212,6 +212,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dlclark/regexp2 v1.7.0 h1:7lJfhqlPssTb1WQx4yvTHN0uElPEv52sbaECrAQxjAo=
|
||||
github.com/dlclark/regexp2 v1.7.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
|
||||
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
|
||||
github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
|
||||
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
||||
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
||||
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
|
||||
|
||||
@ -158,14 +158,18 @@ func (c *Client) doWatch(ctx context.Context) (err error) {
|
||||
if !c.config.Watch {
|
||||
return nil
|
||||
}
|
||||
var changeChan = c.client.AddChangeListenerWithChannel()
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-changeChan:
|
||||
_ = c.doUpdate(ctx)
|
||||
}
|
||||
}
|
||||
}()
|
||||
go c.startAsynchronousWatch(
|
||||
ctx,
|
||||
c.client.AddChangeListenerWithChannel(),
|
||||
)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) startAsynchronousWatch(ctx context.Context, changeChan <-chan model.ConfigFileChangeEvent) {
|
||||
for {
|
||||
select {
|
||||
case <-changeChan:
|
||||
_ = c.doUpdate(ctx)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,8 +1,10 @@
|
||||
# drivers
|
||||
[简体中文](README.zh_CN.MD)
|
||||
|
||||
# Database drivers
|
||||
|
||||
Powerful database drivers for package gdb.
|
||||
|
||||
# Installation
|
||||
## Installation
|
||||
|
||||
Let's take `mysql` for example.
|
||||
|
||||
@ -40,27 +42,29 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
# Supported Drivers
|
||||
## Supported Drivers
|
||||
|
||||
## MySQL/MariaDB/TiDB
|
||||
### MySQL/MariaDB/TiDB
|
||||
|
||||
```go
|
||||
import _ "github.com/gogf/gf/contrib/drivers/mysql/v2"
|
||||
```
|
||||
|
||||
## SQLite
|
||||
### SQLite
|
||||
|
||||
```go
|
||||
import _ "github.com/gogf/gf/contrib/drivers/sqlite/v2"
|
||||
```
|
||||
|
||||
### cgo version for 32-bit windows
|
||||
#### cgo version
|
||||
|
||||
When the target is a 32-bit Windows system, the cgo version needs to be used.
|
||||
|
||||
```go
|
||||
import _ "github.com/gogf/gf/contrib/drivers/sqlitecgo/v2"
|
||||
```
|
||||
|
||||
## PostgreSQL
|
||||
### PostgreSQL
|
||||
|
||||
```go
|
||||
import _ "github.com/gogf/gf/contrib/drivers/pgsql/v2"
|
||||
@ -70,7 +74,7 @@ Note:
|
||||
|
||||
- It does not support `Replace` features.
|
||||
|
||||
## SQL Server
|
||||
### SQL Server
|
||||
|
||||
```go
|
||||
import _ "github.com/gogf/gf/contrib/drivers/mssql/v2"
|
||||
@ -83,7 +87,7 @@ Note:
|
||||
- It supports server version >= `SQL Server2005`
|
||||
- It ONLY supports datetime2 and datetimeoffset types for auto handling created_at/updated_at/deleted_at columns, because datetime type does not support microseconds precision when column value is passed as string.
|
||||
|
||||
## Oracle
|
||||
### Oracle
|
||||
|
||||
```go
|
||||
import _ "github.com/gogf/gf/contrib/drivers/oracle/v2"
|
||||
@ -94,7 +98,7 @@ Note:
|
||||
- It does not support `Replace` features.
|
||||
- It does not support `LastInsertId`.
|
||||
|
||||
## ClickHouse
|
||||
### ClickHouse
|
||||
|
||||
```go
|
||||
import _ "github.com/gogf/gf/contrib/drivers/clickhouse/v2"
|
||||
@ -107,7 +111,7 @@ Note:
|
||||
- It does not support `Transaction` feature.
|
||||
- It does not support `RowsAffected` feature.
|
||||
|
||||
## DM
|
||||
### DM
|
||||
|
||||
```go
|
||||
import _ "github.com/gogf/gf/contrib/drivers/dm/v2"
|
||||
@ -117,7 +121,7 @@ Note:
|
||||
|
||||
- It does not support `Replace` features.
|
||||
|
||||
# Custom Drivers
|
||||
## Custom Drivers
|
||||
|
||||
It's quick and easy, please refer to current driver source.
|
||||
It's quite appreciated if any PR for new drivers support into current repo.
|
||||
|
||||
125
contrib/drivers/README.zh_CN.MD
Normal file
125
contrib/drivers/README.zh_CN.MD
Normal file
@ -0,0 +1,125 @@
|
||||
# 数据库驱动程序
|
||||
|
||||
用于gdb包的数据库驱动程序。
|
||||
|
||||
## 安装
|
||||
|
||||
以 `mysql` 为例。
|
||||
|
||||
```shell
|
||||
go get -u github.com/gogf/gf/contrib/drivers/mysql/v2
|
||||
# 方便复制
|
||||
go get -u github.com/gogf/gf/contrib/drivers/clickhouse/v2
|
||||
go get -u github.com/gogf/gf/contrib/drivers/dm/v2
|
||||
go get -u github.com/gogf/gf/contrib/drivers/mssql/v2
|
||||
go get -u github.com/gogf/gf/contrib/drivers/oracle/v2
|
||||
go get -u github.com/gogf/gf/contrib/drivers/pgsql/v2
|
||||
go get -u github.com/gogf/gf/contrib/drivers/sqlite/v2
|
||||
go get -u github.com/gogf/gf/contrib/drivers/sqlitecgo/v2
|
||||
```
|
||||
|
||||
选择并将驱动程序导入到您的项目中:
|
||||
|
||||
```go
|
||||
import _ "github.com/gogf/gf/contrib/drivers/mysql/v2"
|
||||
```
|
||||
|
||||
通常在 `main.go` 的顶部导入:
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
_ "github.com/gogf/gf/contrib/drivers/mysql/v2"
|
||||
|
||||
// 其他导入的包。
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 主要逻辑。
|
||||
}
|
||||
```
|
||||
|
||||
## 支持的驱动程序
|
||||
|
||||
### MySQL/MariaDB/TiDB
|
||||
|
||||
```go
|
||||
import _ "github.com/gogf/gf/contrib/drivers/mysql/v2"
|
||||
```
|
||||
|
||||
### SQLite
|
||||
|
||||
```go
|
||||
import _ "github.com/gogf/gf/contrib/drivers/sqlite/v2"
|
||||
```
|
||||
|
||||
#### cgo 版本
|
||||
|
||||
32位Windows请使用cgo版本
|
||||
|
||||
```go
|
||||
import _ "github.com/gogf/gf/contrib/drivers/sqlitecgo/v2"
|
||||
```
|
||||
|
||||
### PostgreSQL
|
||||
|
||||
```go
|
||||
import _ "github.com/gogf/gf/contrib/drivers/pgsql/v2"
|
||||
```
|
||||
|
||||
注意:
|
||||
|
||||
- 不支持 `Replace` 功能。
|
||||
|
||||
### SQL Server
|
||||
|
||||
```go
|
||||
import _ "github.com/gogf/gf/contrib/drivers/mssql/v2"
|
||||
```
|
||||
|
||||
注意:
|
||||
|
||||
- 不支持 `Replace` 功能。
|
||||
- 不支持 `LastInsertId`。
|
||||
- 仅支持服务器版本 >= `SQL Server2005`
|
||||
- 仅支持 datetime2 和 datetimeoffset 类型来自动处理 created_at/updated_at/deleted_at 列,因为 datetime 类型在将列值作为字符串传递时不支持微秒精度。
|
||||
|
||||
### Oracle
|
||||
|
||||
```go
|
||||
import _ "github.com/gogf/gf/contrib/drivers/oracle/v2"
|
||||
```
|
||||
|
||||
注意:
|
||||
|
||||
- 不支持 `Replace` 功能。
|
||||
- 不支持 `LastInsertId`。
|
||||
|
||||
### ClickHouse
|
||||
|
||||
```go
|
||||
import _ "github.com/gogf/gf/contrib/drivers/clickhouse/v2"
|
||||
```
|
||||
|
||||
注意:
|
||||
|
||||
- 不支持 `InsertIgnore/InsertGetId` 功能。
|
||||
- 不支持 `Save/Replace` 功能。
|
||||
- 不支持 `Transaction` 功能。
|
||||
- 不支持 `RowsAffected` 功能。
|
||||
|
||||
### DM
|
||||
|
||||
```go
|
||||
import _ "github.com/gogf/gf/contrib/drivers/dm/v2"
|
||||
```
|
||||
|
||||
注意:
|
||||
|
||||
- 不支持 `Replace` 功能。
|
||||
|
||||
## 自定义驱动程序
|
||||
|
||||
自定义驱动程序非常快速和简单,您可以参考当前驱动程序的源代码来进行开发。
|
||||
如果您有关于支持新驱动程序的PR(Pull Request),我们将非常感激地接受您的提交到当前仓库。
|
||||
@ -4,7 +4,7 @@ go 1.18
|
||||
|
||||
require (
|
||||
github.com/ClickHouse/clickhouse-go/v2 v2.0.15
|
||||
github.com/gogf/gf/v2 v2.7.1
|
||||
github.com/gogf/gf/v2 v2.7.2
|
||||
github.com/google/uuid v1.3.0
|
||||
github.com/shopspring/decimal v1.3.1
|
||||
)
|
||||
@ -12,6 +12,7 @@ require (
|
||||
require (
|
||||
github.com/BurntSushi/toml v1.3.2 // indirect
|
||||
github.com/clbanning/mxj/v2 v2.7.0 // indirect
|
||||
github.com/emirpasic/gods v1.18.1 // indirect
|
||||
github.com/fatih/color v1.16.0 // indirect
|
||||
github.com/fsnotify/fsnotify v1.7.0 // indirect
|
||||
github.com/go-logr/logr v1.2.4 // indirect
|
||||
|
||||
@ -8,6 +8,8 @@ github.com/clbanning/mxj/v2 v2.7.0/go.mod h1:hNiWqW14h+kc+MdF9C6/YoRfjEJoR3ou6tn
|
||||
github.com/cloudflare/golz4 v0.0.0-20150217214814-ef862a3cdc58/go.mod h1:EOBUe0h4xcZ5GoxqC5SDxFQ8gwyZPKQoEzownBlhI80=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
|
||||
github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
|
||||
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
|
||||
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
|
||||
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
|
||||
|
||||
@ -6,12 +6,13 @@ replace github.com/gogf/gf/v2 => ../../../
|
||||
|
||||
require (
|
||||
gitee.com/chunanyong/dm v1.8.12
|
||||
github.com/gogf/gf/v2 v2.7.1
|
||||
github.com/gogf/gf/v2 v2.7.2
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/BurntSushi/toml v1.3.2 // indirect
|
||||
github.com/clbanning/mxj/v2 v2.7.0 // indirect
|
||||
github.com/emirpasic/gods v1.18.1 // indirect
|
||||
github.com/fatih/color v1.16.0 // indirect
|
||||
github.com/fsnotify/fsnotify v1.7.0 // indirect
|
||||
github.com/go-logr/logr v1.2.4 // indirect
|
||||
|
||||
@ -5,6 +5,8 @@ github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbi
|
||||
github.com/clbanning/mxj/v2 v2.7.0 h1:WA/La7UGCanFe5NpHF0Q3DNtnCsVoxbPKuyBNHWRyME=
|
||||
github.com/clbanning/mxj/v2 v2.7.0/go.mod h1:hNiWqW14h+kc+MdF9C6/YoRfjEJoR3ou6tn/Qo+ve2s=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
|
||||
github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
|
||||
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
|
||||
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
|
||||
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
|
||||
|
||||
@ -3,18 +3,19 @@ module github.com/gogf/gf/contrib/drivers/mssql/v2
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/denisenkom/go-mssqldb v0.12.3
|
||||
github.com/gogf/gf/v2 v2.7.1
|
||||
github.com/gogf/gf/v2 v2.7.2
|
||||
github.com/microsoft/go-mssqldb v1.7.1
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/BurntSushi/toml v1.3.2 // indirect
|
||||
github.com/clbanning/mxj/v2 v2.7.0 // indirect
|
||||
github.com/emirpasic/gods v1.18.1 // indirect
|
||||
github.com/fatih/color v1.16.0 // indirect
|
||||
github.com/fsnotify/fsnotify v1.7.0 // indirect
|
||||
github.com/go-logr/logr v1.2.4 // indirect
|
||||
github.com/go-logr/stdr v1.2.2 // indirect
|
||||
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe // indirect
|
||||
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
|
||||
github.com/golang-sql/sqlexp v0.1.0 // indirect
|
||||
github.com/gorilla/websocket v1.5.1 // indirect
|
||||
github.com/grokify/html-strip-tags-go v0.1.0 // indirect
|
||||
|
||||
@ -1,16 +1,16 @@
|
||||
github.com/Azure/azure-sdk-for-go/sdk/azcore v0.19.0/go.mod h1:h6H6c8enJmmocHUbLiiGY6sx7f9i+X3m1CHdd5c6Rdw=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/azidentity v0.11.0/go.mod h1:HcM1YX14R7CJcghJGOYCgdezslRSVzqwLf/q+4Y2r/0=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/internal v0.7.0/go.mod h1:yqy467j36fJxcRV2TzfVZ1pCb5vxm4BtZPUdYWe/Xo8=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.9.1 h1:lGlwhPtrX6EVml1hO0ivjkUxsSyl4dsiw9qcA1k/3IQ=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.5.1 h1:sO0/P7g68FrryJzljemN+6GTssUXdANk6aJ7T1ZxnsQ=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/internal v1.5.1 h1:6oNBlSdi1QqM1PNW7FPA6xOGA5UNsXnkaYZz9vdPGhA=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/azkeys v1.0.1 h1:MyVTgWR8qd/Jw1Le0NZebGBUCLbtak3bJ3z1OlqZBpw=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/internal v1.0.0 h1:D3occbWoio4EBLkbkevetNMAVX197GkzbUMtqjGWn80=
|
||||
github.com/AzureAD/microsoft-authentication-library-for-go v1.2.1 h1:DzHpqpoJVaCgOUdVHxE8QB52S6NiVdDQvGlny1qvPqA=
|
||||
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
|
||||
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
||||
github.com/clbanning/mxj/v2 v2.7.0 h1:WA/La7UGCanFe5NpHF0Q3DNtnCsVoxbPKuyBNHWRyME=
|
||||
github.com/clbanning/mxj/v2 v2.7.0/go.mod h1:hNiWqW14h+kc+MdF9C6/YoRfjEJoR3ou6tn/Qo+ve2s=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/denisenkom/go-mssqldb v0.12.3 h1:pBSGx9Tq67pBOTLmxNuirNTeB8Vjmf886Kx+8Y+8shw=
|
||||
github.com/denisenkom/go-mssqldb v0.12.3/go.mod h1:k0mtMFOnU+AihqFxPMiF05rtiDrorD1Vrm1KEz5hxDo=
|
||||
github.com/dnaeon/go-vcr v1.2.0/go.mod h1:R4UdLID7HZT3taECzJs4YgbbH6PIGXB6W/sc5OLb6RQ=
|
||||
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
|
||||
github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
|
||||
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
|
||||
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
|
||||
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
|
||||
@ -20,15 +20,18 @@ github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ=
|
||||
github.com/go-logr/logr v1.2.4/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
|
||||
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
|
||||
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
|
||||
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZkZR4hgp4KJVfY3nMkvmwbVkpv1rVY=
|
||||
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0=
|
||||
github.com/golang-jwt/jwt/v5 v5.2.0 h1:d/ix8ftRUorsN+5eMIlF4T6J8CAt9rch3My2winC1Jw=
|
||||
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 h1:au07oEsX2xN0ktxqI+Sida1w446QrXBRJ0nee3SNZlA=
|
||||
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0=
|
||||
github.com/golang-sql/sqlexp v0.1.0 h1:ZCD6MBpcuOVfGVqsEmY5/4FtYiKz6tSyUv9LPEDei6A=
|
||||
github.com/golang-sql/sqlexp v0.1.0/go.mod h1:J4ad9Vo8ZCWQ2GMrC4UCQy1JpCbwU9m3EOqtpKwwwHI=
|
||||
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
|
||||
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
|
||||
github.com/grokify/html-strip-tags-go v0.1.0 h1:03UrQLjAny8xci+R+qjCce/MYnpNXCtgzltlQbOBae4=
|
||||
github.com/grokify/html-strip-tags-go v0.1.0/go.mod h1:ZdzgfHEzAfz9X6Xe5eBLVblWIxXfYSQ40S/VKrAOGpc=
|
||||
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
|
||||
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
|
||||
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
|
||||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
||||
@ -39,54 +42,33 @@ github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D
|
||||
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
|
||||
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
|
||||
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
||||
github.com/modocache/gover v0.0.0-20171022184752-b58185e213c5/go.mod h1:caMODM3PzxT8aQXRPkAt8xlV/e7d7w8GM5g0fa5F0D8=
|
||||
github.com/microsoft/go-mssqldb v1.7.1 h1:KU/g8aWeM3Hx7IMOFpiwYiUkU+9zeISb4+tx3ScVfsM=
|
||||
github.com/microsoft/go-mssqldb v1.7.1/go.mod h1:kOvZKUdrhhFQmxLZqbwUV0rHkNkZpthMITIb2Ko1IoA=
|
||||
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
|
||||
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
|
||||
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4/go.mod h1:4OwLy04Bl9Ef3GJJCoec+30X3LQs/0/m4HFRt/2LUSA=
|
||||
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmdv1U2eRNDiU2ErMBj1gwrq8eQ=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
|
||||
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
|
||||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||
go.opentelemetry.io/otel v1.14.0 h1:/79Huy8wbf5DnIPhemGB+zEPVwnN6fuQybr/SRXa6hM=
|
||||
go.opentelemetry.io/otel v1.14.0/go.mod h1:o4buv+dJzx8rohcUeRmWUZhqupFvzWis188WlggnNeU=
|
||||
go.opentelemetry.io/otel/sdk v1.14.0 h1:PDCppFRDq8A1jL9v6KMI6dYesaq+DFcDZvjsoGvxGzY=
|
||||
go.opentelemetry.io/otel/sdk v1.14.0/go.mod h1:bwIC5TjrNG6QDCHNWvW4HLHtUQ4I+VQDsnjhvyZCALM=
|
||||
go.opentelemetry.io/otel/trace v1.14.0 h1:wp2Mmvj41tDsyAJXiWDWpfNsOiIyd38fy85pyKcFq/M=
|
||||
go.opentelemetry.io/otel/trace v1.14.0/go.mod h1:8avnQLK+CG77yNLUae4ea2JDQ6iT+gozhnZjy/rw9G8=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
|
||||
golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30=
|
||||
golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20210610132358-84b48f89b13b/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w=
|
||||
golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
|
||||
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
|
||||
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
||||
@ -12,7 +12,7 @@
|
||||
package mssql
|
||||
|
||||
import (
|
||||
_ "github.com/denisenkom/go-mssqldb"
|
||||
_ "github.com/microsoft/go-mssqldb"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
)
|
||||
|
||||
@ -4,12 +4,13 @@ go 1.18
|
||||
|
||||
require (
|
||||
github.com/go-sql-driver/mysql v1.7.1
|
||||
github.com/gogf/gf/v2 v2.7.1
|
||||
github.com/gogf/gf/v2 v2.7.2
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/BurntSushi/toml v1.3.2 // indirect
|
||||
github.com/clbanning/mxj/v2 v2.7.0 // indirect
|
||||
github.com/emirpasic/gods v1.18.1 // indirect
|
||||
github.com/fatih/color v1.16.0 // indirect
|
||||
github.com/fsnotify/fsnotify v1.7.0 // indirect
|
||||
github.com/go-logr/logr v1.2.4 // indirect
|
||||
|
||||
@ -3,6 +3,8 @@ github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbi
|
||||
github.com/clbanning/mxj/v2 v2.7.0 h1:WA/La7UGCanFe5NpHF0Q3DNtnCsVoxbPKuyBNHWRyME=
|
||||
github.com/clbanning/mxj/v2 v2.7.0/go.mod h1:hNiWqW14h+kc+MdF9C6/YoRfjEJoR3ou6tn/Qo+ve2s=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
|
||||
github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
|
||||
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
|
||||
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
|
||||
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
|
||||
|
||||
@ -1267,3 +1267,133 @@ CREATE TABLE %s (
|
||||
t.Assert(one["delete_at"].Int64(), 1)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_SoftTime_CreateUpdateDelete_Specified(t *testing.T) {
|
||||
table := "soft_time_test_table_" + gtime.TimestampNanoStr()
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE %s (
|
||||
id int(11) NOT NULL,
|
||||
name varchar(45) DEFAULT NULL,
|
||||
create_at datetime(0) DEFAULT NULL,
|
||||
update_at datetime(0) DEFAULT NULL,
|
||||
delete_at datetime(0) DEFAULT NULL,
|
||||
PRIMARY KEY (id)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||
`, table)); err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Insert
|
||||
dataInsert := g.Map{
|
||||
"id": 1,
|
||||
"name": "name_1",
|
||||
"create_at": gtime.NewFromStr("2024-05-30 20:00:00"),
|
||||
"update_at": gtime.NewFromStr("2024-05-30 20:00:00"),
|
||||
}
|
||||
r, err := db.Model(table).Data(dataInsert).Insert()
|
||||
t.AssertNil(err)
|
||||
n, _ := r.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
|
||||
oneInsert, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(oneInsert["id"].Int(), 1)
|
||||
t.Assert(oneInsert["name"].String(), "name_1")
|
||||
t.Assert(oneInsert["delete_at"].String(), "")
|
||||
t.Assert(oneInsert["create_at"].String(), "2024-05-30 20:00:00")
|
||||
t.Assert(oneInsert["update_at"].String(), "2024-05-30 20:00:00")
|
||||
|
||||
// For time asserting purpose.
|
||||
time.Sleep(2 * time.Second)
|
||||
|
||||
// Save
|
||||
dataSave := g.Map{
|
||||
"id": 1,
|
||||
"name": "name_10",
|
||||
"update_at": gtime.NewFromStr("2024-05-30 20:15:00"),
|
||||
}
|
||||
r, err = db.Model(table).Data(dataSave).Save()
|
||||
t.AssertNil(err)
|
||||
n, _ = r.RowsAffected()
|
||||
t.Assert(n, 2)
|
||||
|
||||
oneSave, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(oneSave["id"].Int(), 1)
|
||||
t.Assert(oneSave["name"].String(), "name_10")
|
||||
t.Assert(oneSave["delete_at"].String(), "")
|
||||
t.Assert(oneSave["create_at"].String(), "2024-05-30 20:00:00")
|
||||
t.Assert(oneSave["update_at"].String(), "2024-05-30 20:15:00")
|
||||
|
||||
// For time asserting purpose.
|
||||
time.Sleep(2 * time.Second)
|
||||
|
||||
// Update
|
||||
dataUpdate := g.Map{
|
||||
"name": "name_1000",
|
||||
"update_at": gtime.NewFromStr("2024-05-30 20:30:00"),
|
||||
}
|
||||
r, err = db.Model(table).Data(dataUpdate).WherePri(1).Update()
|
||||
t.AssertNil(err)
|
||||
n, _ = r.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
|
||||
oneUpdate, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(oneUpdate["id"].Int(), 1)
|
||||
t.Assert(oneUpdate["name"].String(), "name_1000")
|
||||
t.Assert(oneUpdate["delete_at"].String(), "")
|
||||
t.Assert(oneUpdate["create_at"].String(), "2024-05-30 20:00:00")
|
||||
t.Assert(oneUpdate["update_at"].String(), "2024-05-30 20:30:00")
|
||||
|
||||
// Replace
|
||||
dataReplace := g.Map{
|
||||
"id": 1,
|
||||
"name": "name_100",
|
||||
"create_at": gtime.NewFromStr("2024-05-30 21:00:00"),
|
||||
"update_at": gtime.NewFromStr("2024-05-30 21:00:00"),
|
||||
}
|
||||
r, err = db.Model(table).Data(dataReplace).Replace()
|
||||
t.AssertNil(err)
|
||||
n, _ = r.RowsAffected()
|
||||
t.Assert(n, 2)
|
||||
|
||||
oneReplace, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(oneReplace["id"].Int(), 1)
|
||||
t.Assert(oneReplace["name"].String(), "name_100")
|
||||
t.Assert(oneReplace["delete_at"].String(), "")
|
||||
t.Assert(oneReplace["create_at"].String(), "2024-05-30 21:00:00")
|
||||
t.Assert(oneReplace["update_at"].String(), "2024-05-30 21:00:00")
|
||||
|
||||
// For time asserting purpose.
|
||||
time.Sleep(2 * time.Second)
|
||||
|
||||
// Insert with delete_at
|
||||
dataInsertDelete := g.Map{
|
||||
"id": 2,
|
||||
"name": "name_2",
|
||||
"create_at": gtime.NewFromStr("2024-05-30 20:00:00"),
|
||||
"update_at": gtime.NewFromStr("2024-05-30 20:00:00"),
|
||||
"delete_at": gtime.NewFromStr("2024-05-30 20:00:00"),
|
||||
}
|
||||
r, err = db.Model(table).Data(dataInsertDelete).Insert()
|
||||
t.AssertNil(err)
|
||||
n, _ = r.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
|
||||
// Delete Select
|
||||
oneDelete, err := db.Model(table).WherePri(2).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(oneDelete), 0)
|
||||
oneDeleteUnscoped, err := db.Model(table).Unscoped().WherePri(2).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(oneDeleteUnscoped["id"].Int(), 2)
|
||||
t.Assert(oneDeleteUnscoped["name"].String(), "name_2")
|
||||
t.Assert(oneDeleteUnscoped["delete_at"].String(), "2024-05-30 20:00:00")
|
||||
t.Assert(oneDeleteUnscoped["create_at"].String(), "2024-05-30 20:00:00")
|
||||
t.Assert(oneDeleteUnscoped["update_at"].String(), "2024-05-30 20:00:00")
|
||||
})
|
||||
}
|
||||
|
||||
@ -10,6 +10,8 @@ import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gfile"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
@ -1988,3 +1990,111 @@ PRIMARY KEY (id)
|
||||
t.Assert(user.UserScores[4].Score, 5)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Table_Relation_WithAll_Unscoped(t *testing.T) {
|
||||
var (
|
||||
tableUser = "user101"
|
||||
tableUserDetail = "user_detail101"
|
||||
)
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
id int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
name varchar(45) NOT NULL,
|
||||
PRIMARY KEY (id)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||
`, tableUser)); err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
defer dropTable(tableUser)
|
||||
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
user_id int(10) unsigned NOT NULL,
|
||||
address varchar(45) NOT NULL,
|
||||
deleted_at datetime default NULL ,
|
||||
PRIMARY KEY (user_id)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||
`, tableUserDetail)); err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
defer dropTable(tableUserDetail)
|
||||
|
||||
type UserDetail struct {
|
||||
gmeta.Meta `orm:"table:user_detail101"`
|
||||
UserID int `json:"user_id"`
|
||||
Address string `json:"address"`
|
||||
DeletedAt *gtime.Time `json:"deleted_at"`
|
||||
}
|
||||
|
||||
// For Test Only
|
||||
type UserEmbedded struct {
|
||||
ID int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type User struct {
|
||||
gmeta.Meta `orm:"table:user101"`
|
||||
UserEmbedded
|
||||
UserDetail *UserDetail `orm:"with:user_id=id"`
|
||||
}
|
||||
type UserWithDeletedDetail struct {
|
||||
gmeta.Meta `orm:"table:user101"`
|
||||
UserEmbedded
|
||||
UserDetail *UserDetail `orm:"with:user_id=id, unscoped:true"`
|
||||
}
|
||||
|
||||
// Initialize the data.
|
||||
var err error
|
||||
for i := 1; i <= 5; i++ {
|
||||
// User.
|
||||
_, err = db.Insert(ctx, tableUser, g.Map{
|
||||
"id": i,
|
||||
"name": fmt.Sprintf(`name_%d`, i),
|
||||
})
|
||||
gtest.AssertNil(err)
|
||||
// Detail.
|
||||
_, err = db.Insert(ctx, tableUserDetail, g.Map{
|
||||
"user_id": i,
|
||||
"address": fmt.Sprintf(`address_%d`, i),
|
||||
})
|
||||
// Delete detail where i = 3
|
||||
if i == 3 {
|
||||
_, err = db.Delete(ctx, tableUserDetail, g.Map{
|
||||
"user_id": i,
|
||||
})
|
||||
}
|
||||
gtest.AssertNil(err)
|
||||
}
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var user0 User
|
||||
err := db.Model(tableUser).WithAll().Where("id", 4).Scan(&user0)
|
||||
t.AssertNil(err)
|
||||
t.Assert(user0.ID, 4)
|
||||
t.AssertNE(user0.UserDetail, nil)
|
||||
t.AssertNil(user0.UserDetail.DeletedAt)
|
||||
t.Assert(user0.UserDetail.UserID, 4)
|
||||
t.Assert(user0.UserDetail.Address, `address_4`)
|
||||
|
||||
var user1 User
|
||||
err = db.Model(tableUser).WithAll().Where("id", 3).Scan(&user1)
|
||||
t.AssertNil(err)
|
||||
t.Assert(user1.ID, 3)
|
||||
t.AssertNil(user1.UserDetail)
|
||||
|
||||
var user2 UserWithDeletedDetail
|
||||
err = db.Model(tableUser).WithAll().Where("id", 3).Scan(&user2)
|
||||
t.AssertNil(err)
|
||||
t.Assert(user2.ID, 3)
|
||||
t.AssertNE(user2.UserDetail, nil)
|
||||
t.AssertNE(user2.UserDetail.DeletedAt, nil)
|
||||
t.Assert(user2.UserDetail.UserID, 3)
|
||||
t.Assert(user2.UserDetail.Address, `address_3`)
|
||||
|
||||
// Unscoped outside test
|
||||
var user3 User
|
||||
err = db.Model(tableUser).Unscoped().WithAll().Where("id", 3).Scan(&user3)
|
||||
t.AssertNil(err)
|
||||
t.Assert(user3.ID, 3)
|
||||
t.AssertNil(user3.UserDetail)
|
||||
})
|
||||
}
|
||||
|
||||
@ -3,13 +3,14 @@ module github.com/gogf/gf/contrib/drivers/oracle/v2
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/gogf/gf/v2 v2.7.1
|
||||
github.com/gogf/gf/v2 v2.7.2
|
||||
github.com/sijms/go-ora/v2 v2.7.10
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/BurntSushi/toml v1.3.2 // indirect
|
||||
github.com/clbanning/mxj/v2 v2.7.0 // indirect
|
||||
github.com/emirpasic/gods v1.18.1 // indirect
|
||||
github.com/fatih/color v1.16.0 // indirect
|
||||
github.com/fsnotify/fsnotify v1.7.0 // indirect
|
||||
github.com/go-logr/logr v1.2.4 // indirect
|
||||
|
||||
@ -3,6 +3,8 @@ github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbi
|
||||
github.com/clbanning/mxj/v2 v2.7.0 h1:WA/La7UGCanFe5NpHF0Q3DNtnCsVoxbPKuyBNHWRyME=
|
||||
github.com/clbanning/mxj/v2 v2.7.0/go.mod h1:hNiWqW14h+kc+MdF9C6/YoRfjEJoR3ou6tn/Qo+ve2s=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
|
||||
github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
|
||||
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
|
||||
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
|
||||
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
|
||||
|
||||
@ -3,13 +3,14 @@ module github.com/gogf/gf/contrib/drivers/pgsql/v2
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/gogf/gf/v2 v2.7.1
|
||||
github.com/gogf/gf/v2 v2.7.2
|
||||
github.com/lib/pq v1.10.9
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/BurntSushi/toml v1.3.2 // indirect
|
||||
github.com/clbanning/mxj/v2 v2.7.0 // indirect
|
||||
github.com/emirpasic/gods v1.18.1 // indirect
|
||||
github.com/fatih/color v1.16.0 // indirect
|
||||
github.com/fsnotify/fsnotify v1.7.0 // indirect
|
||||
github.com/go-logr/logr v1.2.4 // indirect
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user