Skip to content

Commit

Permalink
Merge pull request #219 from wubin1989/main
Browse files Browse the repository at this point in the history
v2.3.8
  • Loading branch information
wubin1989 authored Jun 22, 2024
2 parents 30958dd + 4bc2685 commit 923ed28
Show file tree
Hide file tree
Showing 27 changed files with 111 additions and 75 deletions.
23 changes: 13 additions & 10 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var dbGrpc bool
var dbService bool
var dbTablePrefix string
var dbTableGlob string
var dbTableExcludeGlob string
var module bool
var dbGenGenGo bool
var dbOmitempty bool
Expand All @@ -39,16 +40,17 @@ var initCmd = &cobra.Command{
}
options := []svc.SvcOption{svc.WithModName(modName), svc.WithDocPath(docfile), svc.WithModule(module)}
dbConf := svc.DbConfig{
Driver: dbDriver,
Dsn: dbDsn,
TablePrefix: dbTablePrefix,
TableGlob: dbTableGlob,
GenGenGo: dbGenGenGo,
Orm: dbOrm,
Soft: dbSoft,
Grpc: dbGrpc,
Service: dbService,
Omitempty: dbOmitempty,
Driver: dbDriver,
Dsn: dbDsn,
TablePrefix: dbTablePrefix,
TableGlob: dbTableGlob,
TableExcludeGlob: dbTableExcludeGlob,
GenGenGo: dbGenGenGo,
Orm: dbOrm,
Soft: dbSoft,
Grpc: dbGrpc,
Service: dbService,
Omitempty: dbOmitempty,
}
if stringutils.IsNotEmpty(dbConf.Driver) && stringutils.IsNotEmpty(dbConf.Dsn) {
options = append(options, svc.WithDbConfig(&dbConf))
Expand Down Expand Up @@ -79,6 +81,7 @@ func init() {
initCmd.Flags().BoolVar(&dbGenGenGo, "db_gen_gen", false, `whether generate gen.go file`)
initCmd.Flags().StringVar(&dbTablePrefix, "db_table_prefix", "", `table prefix or schema name for pg`)
initCmd.Flags().StringVar(&dbTableGlob, "db_table_glob", "", `used to filter glob-matched tables`)
initCmd.Flags().StringVar(&dbTableExcludeGlob, "db_table_exclude_glob", "", `used to filter glob-matched tables`)
initCmd.Flags().StringVar(&naming, "case", "lowerCamel", `protobuf message field and json tag case, only support "lowerCamel" and "snake"`)
initCmd.Flags().BoolVar(&dbOmitempty, "db_omitempty", false, `whether add omitempty json tag to generated model field"`)
}
22 changes: 12 additions & 10 deletions cmd/internal/svc/codegen/database/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,17 @@ func GetOrmGenerator(kind OrmKind) IOrmGenerator {
}

type OrmGeneratorConfig struct {
Driver string
Dsn string
TablePrefix string
TableGlob string
GenGenGo bool
CaseConverter func(string) string
Dir string
Soft string
Grpc bool
Omitempty bool
Driver string
Dsn string
TablePrefix string
TableGlob string
TableExcludeGlob string
GenGenGo bool
CaseConverter func(string) string
Dir string
Soft string
Grpc bool
Omitempty bool
}

type IOrmGenerator interface {
Expand All @@ -64,6 +65,7 @@ type AbstractBaseGenerator struct {
Dsn string
TablePrefix string
TableGlob string
TableExcludeGlob string
GenGenGo bool
Dir string
g *gormgen.Generator
Expand Down
15 changes: 11 additions & 4 deletions cmd/internal/svc/codegen/database/gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func (gg *GormGenerator) Initialize(conf OrmGeneratorConfig) {
gg.Grpc = conf.Grpc
gg.TablePrefix = strings.TrimSuffix(conf.TablePrefix, ".")
gg.TableGlob = conf.TableGlob
gg.TableExcludeGlob = conf.TableExcludeGlob
gg.GenGenGo = conf.GenGenGo
gg.CaseConverter = conf.CaseConverter
var db *gorm.DB
Expand Down Expand Up @@ -158,14 +159,20 @@ func (gg *GormGenerator) Initialize(conf OrmGeneratorConfig) {
var models []interface{}
if stringutils.IsNotEmpty(gg.TableGlob) {
g.FilterTableGlob = glob.MustCompile(gg.TableGlob)
models = g.GenerateFilteredTables(
gormgen.FieldType(conf.Soft, "gorm.DeletedAt"),
gormgen.FieldGenType(conf.Soft, "Time"))
} else {
}
if stringutils.IsNotEmpty(gg.TableExcludeGlob) {
g.ExcludeTableGlob = glob.MustCompile(gg.TableExcludeGlob)
}

if stringutils.IsEmpty(gg.TableGlob) && stringutils.IsEmpty(gg.TableExcludeGlob) {
models = g.GenerateAllTable(
gormgen.FieldType(conf.Soft, "gorm.DeletedAt"),
gormgen.FieldGenType(conf.Soft, "Time"),
)
} else {
models = g.GenerateFilteredTables(
gormgen.FieldType(conf.Soft, "gorm.DeletedAt"),
gormgen.FieldGenType(conf.Soft, "Time"))
}
g.ApplyBasic(models...)
gg.g = g
Expand Down
4 changes: 3 additions & 1 deletion cmd/internal/svc/codegen/http2grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ var initHttp2GrpcTmpl = templates.EditableHeaderTmpl + `package httpsrv
import ()
var json = jsoniter.ConfigCompatibleWithStandardLibrary
type {{.Meta.Name}}Http2Grpc struct{
{{.Meta.Name | toLowerCamel}} pb.{{.Meta.Name}}ServiceServer
}
Expand All @@ -128,7 +130,7 @@ func New{{.Meta.Name}}Http2Grpc({{.Meta.Name | toLowerCamel}} pb.{{.Meta.Name}}S

var importHttp2GrpcTmpl = `
"context"
"encoding/json"
jsoniter "github.com/json-iterator/go"
"github.com/unionj-cloud/go-doudou/v2/framework/rest"
"net/http"
pb "{{.TransportGrpcPackage}}"
Expand Down
4 changes: 3 additions & 1 deletion cmd/internal/svc/codegen/httpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ package client
import (
"context"
"encoding/json"
jsoniter "github.com/json-iterator/go"
"github.com/go-resty/resty/v2"
"github.com/pkg/errors"
"github.com/klauspost/compress/gzip"
Expand All @@ -44,6 +44,8 @@ import (
"{{.DtoPackage}}"
)
var json = jsoniter.ConfigCompatibleWithStandardLibrary
type {{.Meta.Name}}Client struct {
provider registry.IServiceProvider
client *resty.Client
Expand Down
4 changes: 3 additions & 1 deletion cmd/internal/svc/codegen/httphandlerimpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ var appendHttpHandlerImplTmpl = `

var importTmpl = `
"context"
"encoding/json"
jsoniter "github.com/json-iterator/go"
"fmt"
"github.com/sirupsen/logrus"
v3 "github.com/unionj-cloud/go-doudou/v2/toolkit/openapi/v3"
Expand All @@ -410,6 +410,8 @@ var initHttpHandlerImplTmpl = templates.EditableHeaderTmpl + `package httpsrv
import ()
var json = jsoniter.ConfigCompatibleWithStandardLibrary
type {{.Meta.Name}}HandlerImpl struct{
{{.Meta.Name | toLowerCamel}} {{.ServiceAlias}}.{{.Meta.Name}}
}
Expand Down
26 changes: 14 additions & 12 deletions cmd/internal/svc/svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,9 @@ type DbConfig struct {
Driver string
Dsn string
// or schema for pg
TablePrefix string
TableGlob string
TablePrefix string
TableGlob string
TableExcludeGlob string
// whether generate gen.go file
GenGenGo bool
Orm string
Expand Down Expand Up @@ -187,16 +188,17 @@ func (receiver *Svc) Init() {
gen := database.GetOrmGenerator(database.OrmKind(receiver.DbConfig.Orm))
assert.NotNil(gen, "Unknown orm kind")
gen.Initialize(database.OrmGeneratorConfig{
Driver: receiver.DbConfig.Driver,
Dsn: receiver.DbConfig.Dsn,
TablePrefix: receiver.DbConfig.TablePrefix,
TableGlob: receiver.DbConfig.TableGlob,
GenGenGo: receiver.DbConfig.GenGenGo,
CaseConverter: receiver.CaseConverter,
Dir: receiver.dir,
Soft: receiver.DbConfig.Soft,
Grpc: receiver.DbConfig.Grpc,
Omitempty: receiver.DbConfig.Omitempty,
Driver: receiver.DbConfig.Driver,
Dsn: receiver.DbConfig.Dsn,
TablePrefix: receiver.DbConfig.TablePrefix,
TableGlob: receiver.DbConfig.TableGlob,
TableExcludeGlob: receiver.DbConfig.TableExcludeGlob,
GenGenGo: receiver.DbConfig.GenGenGo,
CaseConverter: receiver.CaseConverter,
Dir: receiver.dir,
Soft: receiver.DbConfig.Soft,
Grpc: receiver.DbConfig.Grpc,
Omitempty: receiver.DbConfig.Omitempty,
})
if receiver.DbConfig.Service {
gen.GenService()
Expand Down
4 changes: 3 additions & 1 deletion framework/database/marshaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package database

import (
"context"
"encoding/json"
jsoniter "github.com/json-iterator/go"

"github.com/unionj-cloud/go-doudou/v2/toolkit/gocache/lib/cache"
"github.com/unionj-cloud/go-doudou/v2/toolkit/gocache/lib/store"
)

var json = jsoniter.ConfigCompatibleWithStandardLibrary

// Marshaler is the struct that marshal and unmarshal cache values
type Marshaler struct {
cache cache.CacheInterface[any]
Expand Down
4 changes: 3 additions & 1 deletion framework/registry/memberlist/eventdelegate_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package memberlist

import (
"encoding/json"
jsoniter "github.com/json-iterator/go"
"github.com/unionj-cloud/go-doudou/v2/toolkit/memberlist"
"testing"
)

var json = jsoniter.ConfigCompatibleWithStandardLibrary

func Test_eventDelegate_NotifyJoin(t *testing.T) {
mm := NodeMeta{
Services: []Service{
Expand Down
4 changes: 3 additions & 1 deletion framework/registry/serversets/endpoint.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package serversets

import (
"encoding/json"
"fmt"
jsoniter "github.com/json-iterator/go"
"github.com/unionj-cloud/go-doudou/v2/framework/config"
"github.com/unionj-cloud/go-doudou/v2/toolkit/cast"
"net/url"
Expand All @@ -13,6 +13,8 @@ import (
"github.com/go-zookeeper/zk"
)

var json = jsoniter.ConfigCompatibleWithStandardLibrary

// An Endpoint is a service (host and port) registered on Zookeeper
// to be discovered by clients/watchers.
type Endpoint struct {
Expand Down
1 change: 0 additions & 1 deletion framework/registry/serversets/watch.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package serversets

import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
"github.com/unionj-cloud/go-doudou/v2/toolkit/caller"
Expand Down
1 change: 0 additions & 1 deletion framework/rest/dochandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package rest

import (
"bytes"
"encoding/json"
"fmt"
"github.com/rs/cors"
"github.com/unionj-cloud/go-doudou/v2/toolkit/stringutils"
Expand Down
1 change: 0 additions & 1 deletion framework/rest/memberlistuihandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package rest

import (
"bytes"
"encoding/json"
"github.com/hako/durafmt"
registry "github.com/unionj-cloud/go-doudou/v2/framework/registry/memberlist"
"github.com/unionj-cloud/go-doudou/v2/toolkit/memberlist"
Expand Down
4 changes: 3 additions & 1 deletion framework/rest/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package rest
import (
"context"
"crypto/subtle"
"encoding/json"
jsoniter "github.com/json-iterator/go"
"fmt"
"github.com/apolloconfig/agollo/v4/storage"
"github.com/ascarter/requestid"
Expand All @@ -29,6 +29,8 @@ import (
"time"
)

var json = jsoniter.ConfigCompatibleWithStandardLibrary

var (
Tracing = tracing
Metrics = metrics
Expand Down
6 changes: 4 additions & 2 deletions framework/rest/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ package rest_test

import (
"context"
"encoding/json"
"fmt"
"github.com/apolloconfig/agollo/v4"
"github.com/apolloconfig/agollo/v4/agcache/memory"
apolloConfig "github.com/apolloconfig/agollo/v4/env/config"
"github.com/go-resty/resty/v2"
"github.com/golang/mock/gomock"
jsoniter "github.com/json-iterator/go"
"github.com/opentracing-contrib/go-stdlib/nethttp"
"github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
"github.com/slok/goresilience"
. "github.com/smartystreets/goconvey/convey"
"github.com/unionj-cloud/go-doudou/v2/framework/config"
"github.com/unionj-cloud/go-doudou/v2/framework/configmgr"
"github.com/unionj-cloud/go-doudou/v2/framework/configmgr/mock"
"github.com/unionj-cloud/go-doudou/v2/framework/config"
"github.com/unionj-cloud/go-doudou/v2/framework/registry"
"github.com/unionj-cloud/go-doudou/v2/framework/rest"
httpMock "github.com/unionj-cloud/go-doudou/v2/framework/rest/mock"
Expand All @@ -31,6 +31,8 @@ import (
"time"
)

var json = jsoniter.ConfigCompatibleWithStandardLibrary

type IMocksvcHandler interface {
GetUser(w http.ResponseWriter, r *http.Request)
SaveUser(w http.ResponseWriter, r *http.Request)
Expand Down
1 change: 0 additions & 1 deletion framework/rest/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package rest

import (
"bytes"
"encoding/json"
"github.com/pkg/errors"
logger "github.com/unionj-cloud/go-doudou/v2/toolkit/zlogger"
"io"
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ require (
github.com/jinzhu/now v1.1.5 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/json-iterator/go v1.1.12
github.com/jtolds/gls v4.20.0+incompatible // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/klauspost/compress v1.17.8
Expand Down
4 changes: 3 additions & 1 deletion toolkit/copier/copier.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package copier

import (
"encoding/json"
jsoniter "github.com/json-iterator/go"
"github.com/pkg/errors"
"reflect"
)

var json = jsoniter.ConfigCompatibleWithStandardLibrary

// DeepCopy src to target with json marshal and unmarshal
func DeepCopy(src, target interface{}) error {
if src == nil || target == nil {
Expand Down
1 change: 0 additions & 1 deletion toolkit/copier/copier_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package copier

import (
"encoding/json"
"fmt"
"testing"
)
Expand Down
1 change: 1 addition & 0 deletions toolkit/gormgen/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type Config struct {
ConfigPackage string

FilterTableGlob glob.Glob
ExcludeTableGlob glob.Glob
GenGenGo bool
}

Expand Down
Loading

0 comments on commit 923ed28

Please sign in to comment.