Compare commits
55 Commits
tools/goct
...
v1.4.2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
06e4914e41 | ||
|
|
9cadab2684 | ||
|
|
7fe2492009 | ||
|
|
22bdf0bbd5 | ||
|
|
c92a2d1b77 | ||
|
|
b21162d638 | ||
|
|
7c9ef3ca67 | ||
|
|
bbadbe0175 | ||
|
|
f9beab1095 | ||
|
|
de5c59aad3 | ||
|
|
36d3765c5c | ||
|
|
d326e6f813 | ||
|
|
ea52fe2e0d | ||
|
|
05a5de7c6d | ||
|
|
d4c9fd2aff | ||
|
|
776673d57d | ||
|
|
1b87f5e30d | ||
|
|
bc47959384 | ||
|
|
9f6d926455 | ||
|
|
f7a4e3a19e | ||
|
|
a515a3c735 | ||
|
|
6f6f1ae21f | ||
|
|
10f94ffcc2 | ||
|
|
f068062b13 | ||
|
|
799c118d95 | ||
|
|
74cc6b55e8 | ||
|
|
fc59aec2e7 | ||
|
|
7868667b4f | ||
|
|
773b59106b | ||
|
|
97f8667b71 | ||
|
|
b51339b69b | ||
|
|
38a73d7fbe | ||
|
|
e50689beed | ||
|
|
1bc138bd34 | ||
|
|
4b9066eda6 | ||
|
|
0c66e041b5 | ||
|
|
aa2be0163a | ||
|
|
ada2941e87 | ||
|
|
59c0013cd1 | ||
|
|
05737f6519 | ||
|
|
4f6a900fd4 | ||
|
|
63cfe60f1a | ||
|
|
e7acadb15d | ||
|
|
111e626a73 | ||
|
|
1a6d7b3ef6 | ||
|
|
2e1e4f3574 | ||
|
|
22d0a2120a | ||
|
|
68e15360c2 | ||
|
|
1b344a8851 | ||
|
|
d640544a40 | ||
|
|
e6aa6fc361 | ||
|
|
4c927624b0 | ||
|
|
0ea92b7280 | ||
|
|
2cde970c9e | ||
|
|
5061158bd6 |
6
.github/workflows/go.yml
vendored
6
.github/workflows/go.yml
vendored
@@ -12,7 +12,7 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Set up Go 1.x
|
||||
uses: actions/setup-go@v2
|
||||
uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version: ^1.16
|
||||
id: go
|
||||
@@ -34,14 +34,14 @@ jobs:
|
||||
run: go test -race -coverprofile=coverage.txt -covermode=atomic ./...
|
||||
|
||||
- name: Codecov
|
||||
uses: codecov/codecov-action@v2
|
||||
uses: codecov/codecov-action@v3
|
||||
|
||||
test-win:
|
||||
name: Windows
|
||||
runs-on: windows-latest
|
||||
steps:
|
||||
- name: Set up Go 1.x
|
||||
uses: actions/setup-go@v2
|
||||
uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version: ^1.16
|
||||
|
||||
|
||||
2
.github/workflows/issue-translator.yml
vendored
2
.github/workflows/issue-translator.yml
vendored
@@ -9,7 +9,7 @@ jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: tomsun28/issues-translate-action@v2.6
|
||||
- uses: usthe/issues-translate-action@v2.7
|
||||
with:
|
||||
IS_MODIFY_TITLE: true
|
||||
# not require, default false, . Decide whether to modify the issue title
|
||||
|
||||
2
.github/workflows/issues.yml
vendored
2
.github/workflows/issues.yml
vendored
@@ -7,7 +7,7 @@ jobs:
|
||||
close-issues:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/stale@v3
|
||||
- uses: actions/stale@v6
|
||||
with:
|
||||
days-before-issue-stale: 365
|
||||
days-before-issue-close: 90
|
||||
|
||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -22,6 +22,7 @@ go.work.sum
|
||||
|
||||
# gitlab ci
|
||||
.cache
|
||||
.golangci.yml
|
||||
|
||||
# vim auto backup file
|
||||
*~
|
||||
|
||||
@@ -105,7 +105,6 @@ d = "abcd!@#112"
|
||||
assert.Equal(t, 1, val.B)
|
||||
assert.Equal(t, "2", val.C)
|
||||
assert.Equal(t, "abcd!@#112", val.D)
|
||||
|
||||
}
|
||||
|
||||
func TestConfigJsonEnv(t *testing.T) {
|
||||
|
||||
@@ -297,7 +297,8 @@ func (c *cluster) watch(cli EtcdClient, key string, rev int64) {
|
||||
func (c *cluster) watchStream(cli EtcdClient, key string, rev int64) bool {
|
||||
var rch clientv3.WatchChan
|
||||
if rev != 0 {
|
||||
rch = cli.Watch(clientv3.WithRequireLeader(c.context(cli)), makeKeyPrefix(key), clientv3.WithPrefix(), clientv3.WithRev(rev+1))
|
||||
rch = cli.Watch(clientv3.WithRequireLeader(c.context(cli)), makeKeyPrefix(key), clientv3.WithPrefix(),
|
||||
clientv3.WithRev(rev+1))
|
||||
} else {
|
||||
rch = cli.Watch(clientv3.WithRequireLeader(c.context(cli)), makeKeyPrefix(key), clientv3.WithPrefix())
|
||||
}
|
||||
@@ -342,6 +343,7 @@ func DialClient(endpoints []string) (EtcdClient, error) {
|
||||
DialKeepAliveTime: dialKeepAliveTime,
|
||||
DialKeepAliveTimeout: DialTimeout,
|
||||
RejectOldCluster: true,
|
||||
PermitWithoutStream: true,
|
||||
}
|
||||
if account, ok := GetAccount(endpoints); ok {
|
||||
cfg.Username = account.User
|
||||
|
||||
@@ -2,7 +2,6 @@ package internal
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go.etcd.io/etcd/api/v3/etcdserverpb"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
@@ -12,6 +11,7 @@ import (
|
||||
"github.com/zeromicro/go-zero/core/lang"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/core/stringx"
|
||||
"go.etcd.io/etcd/api/v3/etcdserverpb"
|
||||
"go.etcd.io/etcd/api/v3/mvccpb"
|
||||
clientv3 "go.etcd.io/etcd/client/v3"
|
||||
)
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package limit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"sync"
|
||||
@@ -58,8 +60,8 @@ type TokenLimiter struct {
|
||||
timestampKey string
|
||||
rescueLock sync.Mutex
|
||||
redisAlive uint32
|
||||
rescueLimiter *xrate.Limiter
|
||||
monitorStarted bool
|
||||
rescueLimiter *xrate.Limiter
|
||||
}
|
||||
|
||||
// NewTokenLimiter returns a new TokenLimiter that allows events up to rate and permits
|
||||
@@ -84,19 +86,31 @@ func (lim *TokenLimiter) Allow() bool {
|
||||
return lim.AllowN(time.Now(), 1)
|
||||
}
|
||||
|
||||
// AllowCtx is shorthand for AllowNCtx(ctx,time.Now(), 1) with incoming context.
|
||||
func (lim *TokenLimiter) AllowCtx(ctx context.Context) bool {
|
||||
return lim.AllowNCtx(ctx, time.Now(), 1)
|
||||
}
|
||||
|
||||
// AllowN reports whether n events may happen at time now.
|
||||
// Use this method if you intend to drop / skip events that exceed the rate.
|
||||
// Otherwise, use Reserve or Wait.
|
||||
func (lim *TokenLimiter) AllowN(now time.Time, n int) bool {
|
||||
return lim.reserveN(now, n)
|
||||
return lim.reserveN(context.Background(), now, n)
|
||||
}
|
||||
|
||||
func (lim *TokenLimiter) reserveN(now time.Time, n int) bool {
|
||||
// AllowNCtx reports whether n events may happen at time now with incoming context.
|
||||
// Use this method if you intend to drop / skip events that exceed the rate.
|
||||
// Otherwise, use Reserve or Wait.
|
||||
func (lim *TokenLimiter) AllowNCtx(ctx context.Context, now time.Time, n int) bool {
|
||||
return lim.reserveN(ctx, now, n)
|
||||
}
|
||||
|
||||
func (lim *TokenLimiter) reserveN(ctx context.Context, now time.Time, n int) bool {
|
||||
if atomic.LoadUint32(&lim.redisAlive) == 0 {
|
||||
return lim.rescueLimiter.AllowN(now, n)
|
||||
}
|
||||
|
||||
resp, err := lim.store.Eval(
|
||||
resp, err := lim.store.EvalCtx(ctx,
|
||||
script,
|
||||
[]string{
|
||||
lim.tokenKey,
|
||||
@@ -113,6 +127,10 @@ func (lim *TokenLimiter) reserveN(now time.Time, n int) bool {
|
||||
if err == redis.Nil {
|
||||
return false
|
||||
}
|
||||
if errors.Is(err, context.DeadlineExceeded) || errors.Is(err, context.Canceled) {
|
||||
logx.Errorf("fail to use rate limiter: %s", err)
|
||||
return false
|
||||
}
|
||||
if err != nil {
|
||||
logx.Errorf("fail to use rate limiter: %s, use in-process limiter for rescue", err)
|
||||
lim.startMonitor()
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package limit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -15,6 +16,30 @@ func init() {
|
||||
logx.Disable()
|
||||
}
|
||||
|
||||
func TestTokenLimit_WithCtx(t *testing.T) {
|
||||
s, err := miniredis.Run()
|
||||
assert.Nil(t, err)
|
||||
|
||||
const (
|
||||
total = 100
|
||||
rate = 5
|
||||
burst = 10
|
||||
)
|
||||
l := NewTokenLimiter(rate, burst, redis.New(s.Addr()), "tokenlimit")
|
||||
defer s.Close()
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
ok := l.AllowCtx(ctx)
|
||||
assert.True(t, ok)
|
||||
|
||||
cancel()
|
||||
for i := 0; i < total; i++ {
|
||||
ok := l.AllowCtx(ctx)
|
||||
assert.False(t, ok)
|
||||
assert.False(t, l.monitorStarted)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTokenLimit_Rescue(t *testing.T) {
|
||||
s, err := miniredis.Run()
|
||||
assert.Nil(t, err)
|
||||
|
||||
@@ -17,7 +17,7 @@ import (
|
||||
const (
|
||||
defaultBuckets = 50
|
||||
defaultWindow = time.Second * 5
|
||||
// using 1000m notation, 900m is like 80%, keep it as var for unit test
|
||||
// using 1000m notation, 900m is like 90%, keep it as var for unit test
|
||||
defaultCpuThreshold = 900
|
||||
defaultMinRt = float64(time.Second / time.Millisecond)
|
||||
// moving average hyperparameter beta for calculating requests on the fly
|
||||
@@ -70,7 +70,7 @@ type (
|
||||
flying int64
|
||||
avgFlying float64
|
||||
avgFlyingLock syncx.SpinLock
|
||||
dropTime *syncx.AtomicDuration
|
||||
overloadTime *syncx.AtomicDuration
|
||||
droppedRecently *syncx.AtomicBool
|
||||
passCounter *collection.RollingWindow
|
||||
rtCounter *collection.RollingWindow
|
||||
@@ -106,7 +106,7 @@ func NewAdaptiveShedder(opts ...ShedderOption) Shedder {
|
||||
return &adaptiveShedder{
|
||||
cpuThreshold: options.cpuThreshold,
|
||||
windows: int64(time.Second / bucketDuration),
|
||||
dropTime: syncx.NewAtomicDuration(),
|
||||
overloadTime: syncx.NewAtomicDuration(),
|
||||
droppedRecently: syncx.NewAtomicBool(),
|
||||
passCounter: collection.NewRollingWindow(options.buckets, bucketDuration,
|
||||
collection.IgnoreCurrentBucket()),
|
||||
@@ -118,7 +118,6 @@ func NewAdaptiveShedder(opts ...ShedderOption) Shedder {
|
||||
// Allow implements Shedder.Allow.
|
||||
func (as *adaptiveShedder) Allow() (Promise, error) {
|
||||
if as.shouldDrop() {
|
||||
as.dropTime.Set(timex.Now())
|
||||
as.droppedRecently.Set(true)
|
||||
|
||||
return nil, ErrServiceOverloaded
|
||||
@@ -215,21 +214,26 @@ func (as *adaptiveShedder) stillHot() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
dropTime := as.dropTime.Load()
|
||||
if dropTime == 0 {
|
||||
overloadTime := as.overloadTime.Load()
|
||||
if overloadTime == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
hot := timex.Since(dropTime) < coolOffDuration
|
||||
if !hot {
|
||||
as.droppedRecently.Set(false)
|
||||
if timex.Since(overloadTime) < coolOffDuration {
|
||||
return true
|
||||
}
|
||||
|
||||
return hot
|
||||
as.droppedRecently.Set(false)
|
||||
return false
|
||||
}
|
||||
|
||||
func (as *adaptiveShedder) systemOverloaded() bool {
|
||||
return systemOverloadChecker(as.cpuThreshold)
|
||||
if !systemOverloadChecker(as.cpuThreshold) {
|
||||
return false
|
||||
}
|
||||
|
||||
as.overloadTime.Set(timex.Now())
|
||||
return true
|
||||
}
|
||||
|
||||
// WithBuckets customizes the Shedder with given number of buckets.
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
"github.com/zeromicro/go-zero/core/mathx"
|
||||
"github.com/zeromicro/go-zero/core/stat"
|
||||
"github.com/zeromicro/go-zero/core/syncx"
|
||||
"github.com/zeromicro/go-zero/core/timex"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -136,7 +137,7 @@ func TestAdaptiveShedderShouldDrop(t *testing.T) {
|
||||
passCounter: passCounter,
|
||||
rtCounter: rtCounter,
|
||||
windows: buckets,
|
||||
dropTime: syncx.NewAtomicDuration(),
|
||||
overloadTime: syncx.NewAtomicDuration(),
|
||||
droppedRecently: syncx.NewAtomicBool(),
|
||||
}
|
||||
// cpu >= 800, inflight < maxPass
|
||||
@@ -190,12 +191,15 @@ func TestAdaptiveShedderStillHot(t *testing.T) {
|
||||
passCounter: passCounter,
|
||||
rtCounter: rtCounter,
|
||||
windows: buckets,
|
||||
dropTime: syncx.NewAtomicDuration(),
|
||||
overloadTime: syncx.NewAtomicDuration(),
|
||||
droppedRecently: syncx.ForAtomicBool(true),
|
||||
}
|
||||
assert.False(t, shedder.stillHot())
|
||||
shedder.dropTime.Set(-coolOffDuration * 2)
|
||||
shedder.overloadTime.Set(-coolOffDuration * 2)
|
||||
assert.False(t, shedder.stillHot())
|
||||
shedder.droppedRecently.Set(true)
|
||||
shedder.overloadTime.Set(timex.Now())
|
||||
assert.True(t, shedder.stillHot())
|
||||
}
|
||||
|
||||
func BenchmarkAdaptiveShedder_Allow(b *testing.B) {
|
||||
|
||||
122
core/logc/logs.go
Normal file
122
core/logc/logs.go
Normal file
@@ -0,0 +1,122 @@
|
||||
package logc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type (
|
||||
LogConf = logx.LogConf
|
||||
LogField = logx.LogField
|
||||
)
|
||||
|
||||
// AddGlobalFields adds global fields.
|
||||
func AddGlobalFields(fields ...LogField) {
|
||||
logx.AddGlobalFields(fields...)
|
||||
}
|
||||
|
||||
// Alert alerts v in alert level, and the message is written to error log.
|
||||
func Alert(_ context.Context, v string) {
|
||||
logx.Alert(v)
|
||||
}
|
||||
|
||||
// Close closes the logging.
|
||||
func Close() error {
|
||||
return logx.Close()
|
||||
}
|
||||
|
||||
// Error writes v into error log.
|
||||
func Error(ctx context.Context, v ...interface{}) {
|
||||
getLogger(ctx).Error(v...)
|
||||
}
|
||||
|
||||
// Errorf writes v with format into error log.
|
||||
func Errorf(ctx context.Context, format string, v ...interface{}) {
|
||||
getLogger(ctx).Errorf(fmt.Errorf(format, v...).Error())
|
||||
}
|
||||
|
||||
// Errorv writes v into error log with json content.
|
||||
// No call stack attached, because not elegant to pack the messages.
|
||||
func Errorv(ctx context.Context, v interface{}) {
|
||||
getLogger(ctx).Errorv(v)
|
||||
}
|
||||
|
||||
// Errorw writes msg along with fields into error log.
|
||||
func Errorw(ctx context.Context, msg string, fields ...LogField) {
|
||||
getLogger(ctx).Errorw(msg, fields...)
|
||||
}
|
||||
|
||||
// Field returns a LogField for the given key and value.
|
||||
func Field(key string, value interface{}) LogField {
|
||||
return logx.Field(key, value)
|
||||
}
|
||||
|
||||
// Info writes v into access log.
|
||||
func Info(ctx context.Context, v ...interface{}) {
|
||||
getLogger(ctx).Info(v...)
|
||||
}
|
||||
|
||||
// Infof writes v with format into access log.
|
||||
func Infof(ctx context.Context, format string, v ...interface{}) {
|
||||
getLogger(ctx).Infof(format, v...)
|
||||
}
|
||||
|
||||
// Infov writes v into access log with json content.
|
||||
func Infov(ctx context.Context, v interface{}) {
|
||||
getLogger(ctx).Infov(v)
|
||||
}
|
||||
|
||||
// Infow writes msg along with fields into access log.
|
||||
func Infow(ctx context.Context, msg string, fields ...LogField) {
|
||||
getLogger(ctx).Infow(msg, fields...)
|
||||
}
|
||||
|
||||
// Must checks if err is nil, otherwise logs the error and exits.
|
||||
func Must(err error) {
|
||||
logx.Must(err)
|
||||
}
|
||||
|
||||
// MustSetup sets up logging with given config c. It exits on error.
|
||||
func MustSetup(c logx.LogConf) {
|
||||
logx.MustSetup(c)
|
||||
}
|
||||
|
||||
// SetLevel sets the logging level. It can be used to suppress some logs.
|
||||
func SetLevel(level uint32) {
|
||||
logx.SetLevel(level)
|
||||
}
|
||||
|
||||
// SetUp sets up the logx. If already set up, just return nil.
|
||||
// we allow SetUp to be called multiple times, because for example
|
||||
// we need to allow different service frameworks to initialize logx respectively.
|
||||
// the same logic for SetUp
|
||||
func SetUp(c LogConf) error {
|
||||
return logx.SetUp(c)
|
||||
}
|
||||
|
||||
// Slow writes v into slow log.
|
||||
func Slow(ctx context.Context, v ...interface{}) {
|
||||
getLogger(ctx).Slow(v...)
|
||||
}
|
||||
|
||||
// Slowf writes v with format into slow log.
|
||||
func Slowf(ctx context.Context, format string, v ...interface{}) {
|
||||
getLogger(ctx).Slowf(format, v...)
|
||||
}
|
||||
|
||||
// Slowv writes v into slow log with json content.
|
||||
func Slowv(ctx context.Context, v interface{}) {
|
||||
getLogger(ctx).Slowv(v)
|
||||
}
|
||||
|
||||
// Sloww writes msg along with fields into slow log.
|
||||
func Sloww(ctx context.Context, msg string, fields ...LogField) {
|
||||
getLogger(ctx).Sloww(msg, fields...)
|
||||
}
|
||||
|
||||
// getLogger returns the logx.Logger with the given ctx and correct caller.
|
||||
func getLogger(ctx context.Context) logx.Logger {
|
||||
return logx.WithContext(ctx).WithCallerSkip(1)
|
||||
}
|
||||
218
core/logc/logs_test.go
Normal file
218
core/logc/logs_test.go
Normal file
@@ -0,0 +1,218 @@
|
||||
package logc
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
func TestAddGlobalFields(t *testing.T) {
|
||||
var buf bytes.Buffer
|
||||
writer := logx.NewWriter(&buf)
|
||||
old := logx.Reset()
|
||||
logx.SetWriter(writer)
|
||||
defer logx.SetWriter(old)
|
||||
|
||||
Info(context.Background(), "hello")
|
||||
buf.Reset()
|
||||
|
||||
AddGlobalFields(Field("a", "1"), Field("b", "2"))
|
||||
AddGlobalFields(Field("c", "3"))
|
||||
Info(context.Background(), "world")
|
||||
var m map[string]interface{}
|
||||
assert.NoError(t, json.Unmarshal(buf.Bytes(), &m))
|
||||
assert.Equal(t, "1", m["a"])
|
||||
assert.Equal(t, "2", m["b"])
|
||||
assert.Equal(t, "3", m["c"])
|
||||
}
|
||||
|
||||
func TestAlert(t *testing.T) {
|
||||
var buf strings.Builder
|
||||
writer := logx.NewWriter(&buf)
|
||||
old := logx.Reset()
|
||||
logx.SetWriter(writer)
|
||||
defer logx.SetWriter(old)
|
||||
|
||||
Alert(context.Background(), "foo")
|
||||
assert.True(t, strings.Contains(buf.String(), "foo"), buf.String())
|
||||
}
|
||||
|
||||
func TestError(t *testing.T) {
|
||||
var buf strings.Builder
|
||||
writer := logx.NewWriter(&buf)
|
||||
old := logx.Reset()
|
||||
logx.SetWriter(writer)
|
||||
defer logx.SetWriter(old)
|
||||
|
||||
file, line := getFileLine()
|
||||
Error(context.Background(), "foo")
|
||||
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
|
||||
}
|
||||
|
||||
func TestErrorf(t *testing.T) {
|
||||
var buf strings.Builder
|
||||
writer := logx.NewWriter(&buf)
|
||||
old := logx.Reset()
|
||||
logx.SetWriter(writer)
|
||||
defer logx.SetWriter(old)
|
||||
|
||||
file, line := getFileLine()
|
||||
Errorf(context.Background(), "foo %s", "bar")
|
||||
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
|
||||
}
|
||||
|
||||
func TestErrorv(t *testing.T) {
|
||||
var buf strings.Builder
|
||||
writer := logx.NewWriter(&buf)
|
||||
old := logx.Reset()
|
||||
logx.SetWriter(writer)
|
||||
defer logx.SetWriter(old)
|
||||
|
||||
file, line := getFileLine()
|
||||
Errorv(context.Background(), "foo")
|
||||
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
|
||||
}
|
||||
|
||||
func TestErrorw(t *testing.T) {
|
||||
var buf strings.Builder
|
||||
writer := logx.NewWriter(&buf)
|
||||
old := logx.Reset()
|
||||
logx.SetWriter(writer)
|
||||
defer logx.SetWriter(old)
|
||||
|
||||
file, line := getFileLine()
|
||||
Errorw(context.Background(), "foo", Field("a", "b"))
|
||||
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
|
||||
}
|
||||
|
||||
func TestInfo(t *testing.T) {
|
||||
var buf strings.Builder
|
||||
writer := logx.NewWriter(&buf)
|
||||
old := logx.Reset()
|
||||
logx.SetWriter(writer)
|
||||
defer logx.SetWriter(old)
|
||||
|
||||
file, line := getFileLine()
|
||||
Info(context.Background(), "foo")
|
||||
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
|
||||
}
|
||||
|
||||
func TestInfof(t *testing.T) {
|
||||
var buf strings.Builder
|
||||
writer := logx.NewWriter(&buf)
|
||||
old := logx.Reset()
|
||||
logx.SetWriter(writer)
|
||||
defer logx.SetWriter(old)
|
||||
|
||||
file, line := getFileLine()
|
||||
Infof(context.Background(), "foo %s", "bar")
|
||||
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
|
||||
}
|
||||
|
||||
func TestInfov(t *testing.T) {
|
||||
var buf strings.Builder
|
||||
writer := logx.NewWriter(&buf)
|
||||
old := logx.Reset()
|
||||
logx.SetWriter(writer)
|
||||
defer logx.SetWriter(old)
|
||||
|
||||
file, line := getFileLine()
|
||||
Infov(context.Background(), "foo")
|
||||
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
|
||||
}
|
||||
|
||||
func TestInfow(t *testing.T) {
|
||||
var buf strings.Builder
|
||||
writer := logx.NewWriter(&buf)
|
||||
old := logx.Reset()
|
||||
logx.SetWriter(writer)
|
||||
defer logx.SetWriter(old)
|
||||
|
||||
file, line := getFileLine()
|
||||
Infow(context.Background(), "foo", Field("a", "b"))
|
||||
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
|
||||
}
|
||||
|
||||
func TestMust(t *testing.T) {
|
||||
assert.NotPanics(t, func() {
|
||||
Must(nil)
|
||||
})
|
||||
assert.NotPanics(t, func() {
|
||||
MustSetup(LogConf{})
|
||||
})
|
||||
}
|
||||
|
||||
func TestMisc(t *testing.T) {
|
||||
SetLevel(logx.DebugLevel)
|
||||
assert.NoError(t, SetUp(LogConf{}))
|
||||
assert.NoError(t, Close())
|
||||
}
|
||||
|
||||
func TestSlow(t *testing.T) {
|
||||
var buf strings.Builder
|
||||
writer := logx.NewWriter(&buf)
|
||||
old := logx.Reset()
|
||||
logx.SetWriter(writer)
|
||||
defer logx.SetWriter(old)
|
||||
|
||||
file, line := getFileLine()
|
||||
Slow(context.Background(), "foo")
|
||||
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)), buf.String())
|
||||
}
|
||||
|
||||
func TestSlowf(t *testing.T) {
|
||||
var buf strings.Builder
|
||||
writer := logx.NewWriter(&buf)
|
||||
old := logx.Reset()
|
||||
logx.SetWriter(writer)
|
||||
defer logx.SetWriter(old)
|
||||
|
||||
file, line := getFileLine()
|
||||
Slowf(context.Background(), "foo %s", "bar")
|
||||
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)), buf.String())
|
||||
}
|
||||
|
||||
func TestSlowv(t *testing.T) {
|
||||
var buf strings.Builder
|
||||
writer := logx.NewWriter(&buf)
|
||||
old := logx.Reset()
|
||||
logx.SetWriter(writer)
|
||||
defer logx.SetWriter(old)
|
||||
|
||||
file, line := getFileLine()
|
||||
Slowv(context.Background(), "foo")
|
||||
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)), buf.String())
|
||||
}
|
||||
|
||||
func TestSloww(t *testing.T) {
|
||||
var buf strings.Builder
|
||||
writer := logx.NewWriter(&buf)
|
||||
old := logx.Reset()
|
||||
logx.SetWriter(writer)
|
||||
defer logx.SetWriter(old)
|
||||
|
||||
file, line := getFileLine()
|
||||
Sloww(context.Background(), "foo", Field("a", "b"))
|
||||
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)), buf.String())
|
||||
}
|
||||
|
||||
func getFileLine() (string, int) {
|
||||
_, file, line, _ := runtime.Caller(1)
|
||||
short := file
|
||||
|
||||
for i := len(file) - 1; i > 0; i-- {
|
||||
if file[i] == '/' {
|
||||
short = file[i+1:]
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return short, line
|
||||
}
|
||||
@@ -7,7 +7,7 @@ type LogConf struct {
|
||||
Encoding string `json:",default=json,options=[json,plain]"`
|
||||
TimeFormat string `json:",optional"`
|
||||
Path string `json:",default=logs"`
|
||||
Level string `json:",default=info,options=[info,error,severe]"`
|
||||
Level string `json:",default=info,options=[debug,info,error,severe]"`
|
||||
Compress bool `json:",optional"`
|
||||
KeepDays int `json:",optional"`
|
||||
StackCooldownMillis int `json:",default=100"`
|
||||
|
||||
@@ -1,11 +1,32 @@
|
||||
package logx
|
||||
|
||||
import "context"
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
var fieldsContextKey contextKey
|
||||
var (
|
||||
fieldsContextKey contextKey
|
||||
globalFields atomic.Value
|
||||
globalFieldsLock sync.Mutex
|
||||
)
|
||||
|
||||
type contextKey struct{}
|
||||
|
||||
// AddGlobalFields adds global fields.
|
||||
func AddGlobalFields(fields ...LogField) {
|
||||
globalFieldsLock.Lock()
|
||||
defer globalFieldsLock.Unlock()
|
||||
|
||||
old := globalFields.Load()
|
||||
if old == nil {
|
||||
globalFields.Store(append([]LogField(nil), fields...))
|
||||
} else {
|
||||
globalFields.Store(append(old.([]LogField), fields...))
|
||||
}
|
||||
}
|
||||
|
||||
// ContextWithFields returns a new context with the given fields.
|
||||
func ContextWithFields(ctx context.Context, fields ...LogField) context.Context {
|
||||
if val := ctx.Value(fieldsContextKey); val != nil {
|
||||
|
||||
@@ -1,12 +1,36 @@
|
||||
package logx
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestAddGlobalFields(t *testing.T) {
|
||||
var buf bytes.Buffer
|
||||
writer := NewWriter(&buf)
|
||||
old := Reset()
|
||||
SetWriter(writer)
|
||||
defer SetWriter(old)
|
||||
|
||||
Info("hello")
|
||||
buf.Reset()
|
||||
|
||||
AddGlobalFields(Field("a", "1"), Field("b", "2"))
|
||||
AddGlobalFields(Field("c", "3"))
|
||||
Info("world")
|
||||
var m map[string]interface{}
|
||||
assert.NoError(t, json.Unmarshal(buf.Bytes(), &m))
|
||||
assert.Equal(t, "1", m["a"])
|
||||
assert.Equal(t, "2", m["b"])
|
||||
assert.Equal(t, "3", m["c"])
|
||||
}
|
||||
|
||||
func TestContextWithFields(t *testing.T) {
|
||||
ctx := ContextWithFields(context.Background(), Field("a", 1), Field("b", 2))
|
||||
vals := ctx.Value(fieldsContextKey)
|
||||
@@ -16,6 +40,15 @@ func TestContextWithFields(t *testing.T) {
|
||||
assert.EqualValues(t, []LogField{Field("a", 1), Field("b", 2)}, fields)
|
||||
}
|
||||
|
||||
func TestWithFields(t *testing.T) {
|
||||
ctx := WithFields(context.Background(), Field("a", 1), Field("b", 2))
|
||||
vals := ctx.Value(fieldsContextKey)
|
||||
assert.NotNil(t, vals)
|
||||
fields, ok := vals.([]LogField)
|
||||
assert.True(t, ok)
|
||||
assert.EqualValues(t, []LogField{Field("a", 1), Field("b", 2)}, fields)
|
||||
}
|
||||
|
||||
func TestWithFieldsAppend(t *testing.T) {
|
||||
var dummyKey struct{}
|
||||
ctx := context.WithValue(context.Background(), dummyKey, "dummy")
|
||||
@@ -33,3 +66,39 @@ func TestWithFieldsAppend(t *testing.T) {
|
||||
Field("d", 4),
|
||||
}, fields)
|
||||
}
|
||||
|
||||
func BenchmarkAtomicValue(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
|
||||
var container atomic.Value
|
||||
vals := []LogField{
|
||||
Field("a", "b"),
|
||||
Field("c", "d"),
|
||||
Field("e", "f"),
|
||||
}
|
||||
container.Store(&vals)
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
val := container.Load()
|
||||
if val != nil {
|
||||
_ = *val.(*[]LogField)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkRWMutex(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
|
||||
var lock sync.RWMutex
|
||||
vals := []LogField{
|
||||
Field("a", "b"),
|
||||
Field("c", "d"),
|
||||
Field("e", "f"),
|
||||
}
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
lock.RLock()
|
||||
_ = vals
|
||||
lock.RUnlock()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,14 @@ import (
|
||||
|
||||
// A Logger represents a logger.
|
||||
type Logger interface {
|
||||
// Debug logs a message at info level.
|
||||
Debug(...interface{})
|
||||
// Debugf logs a message at info level.
|
||||
Debugf(string, ...interface{})
|
||||
// Debugv logs a message at info level.
|
||||
Debugv(interface{})
|
||||
// Debugw logs a message at info level.
|
||||
Debugw(string, ...LogField)
|
||||
// Error logs a message at error level.
|
||||
Error(...interface{})
|
||||
// Errorf logs a message at error level.
|
||||
@@ -37,4 +45,6 @@ type Logger interface {
|
||||
WithContext(ctx context.Context) Logger
|
||||
// WithDuration returns a new logger with the given duration.
|
||||
WithDuration(d time.Duration) Logger
|
||||
// WithFields returns a new logger with the given fields.
|
||||
WithFields(fields ...LogField) Logger
|
||||
}
|
||||
|
||||
@@ -64,6 +64,26 @@ func Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Debug writes v into access log.
|
||||
func Debug(v ...interface{}) {
|
||||
writeDebug(fmt.Sprint(v...))
|
||||
}
|
||||
|
||||
// Debugf writes v with format into access log.
|
||||
func Debugf(format string, v ...interface{}) {
|
||||
writeDebug(fmt.Sprintf(format, v...))
|
||||
}
|
||||
|
||||
// Debugv writes v into access log with json content.
|
||||
func Debugv(v interface{}) {
|
||||
writeDebug(v)
|
||||
}
|
||||
|
||||
// Debugw writes msg along with fields into access log.
|
||||
func Debugw(msg string, fields ...LogField) {
|
||||
writeDebug(msg, fields...)
|
||||
}
|
||||
|
||||
// Disable disables the logging.
|
||||
func Disable() {
|
||||
atomic.StoreUint32(&disableLog, 1)
|
||||
@@ -352,6 +372,8 @@ func handleOptions(opts []LogOption) {
|
||||
|
||||
func setupLogLevel(c LogConf) {
|
||||
switch c.Level {
|
||||
case levelDebug:
|
||||
SetLevel(DebugLevel)
|
||||
case levelInfo:
|
||||
SetLevel(InfoLevel)
|
||||
case levelError:
|
||||
@@ -392,6 +414,12 @@ func shallLogStat() bool {
|
||||
return atomic.LoadUint32(&disableStat) == 0
|
||||
}
|
||||
|
||||
func writeDebug(val interface{}, fields ...LogField) {
|
||||
if shallLog(DebugLevel) {
|
||||
getWriter().Debug(val, addCaller(fields...)...)
|
||||
}
|
||||
}
|
||||
|
||||
func writeError(val interface{}, fields ...LogField) {
|
||||
if shallLog(ErrorLevel) {
|
||||
getWriter().Error(val, addCaller(fields...)...)
|
||||
|
||||
@@ -35,6 +35,12 @@ func (mw *mockWriter) Alert(v interface{}) {
|
||||
output(&mw.builder, levelAlert, v)
|
||||
}
|
||||
|
||||
func (mw *mockWriter) Debug(v interface{}, fields ...LogField) {
|
||||
mw.lock.Lock()
|
||||
defer mw.lock.Unlock()
|
||||
output(&mw.builder, levelDebug, v, fields...)
|
||||
}
|
||||
|
||||
func (mw *mockWriter) Error(v interface{}, fields ...LogField) {
|
||||
mw.lock.Lock()
|
||||
defer mw.lock.Unlock()
|
||||
@@ -212,6 +218,46 @@ func TestStructedLogAlert(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestStructedLogDebug(t *testing.T) {
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
defer writer.Store(old)
|
||||
|
||||
doTestStructedLog(t, levelDebug, w, func(v ...interface{}) {
|
||||
Debug(v...)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStructedLogDebugf(t *testing.T) {
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
defer writer.Store(old)
|
||||
|
||||
doTestStructedLog(t, levelDebug, w, func(v ...interface{}) {
|
||||
Debugf(fmt.Sprint(v...))
|
||||
})
|
||||
}
|
||||
|
||||
func TestStructedLogDebugv(t *testing.T) {
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
defer writer.Store(old)
|
||||
|
||||
doTestStructedLog(t, levelDebug, w, func(v ...interface{}) {
|
||||
Debugv(fmt.Sprint(v...))
|
||||
})
|
||||
}
|
||||
|
||||
func TestStructedLogDebugw(t *testing.T) {
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
defer writer.Store(old)
|
||||
|
||||
doTestStructedLog(t, levelDebug, w, func(v ...interface{}) {
|
||||
Debugw(fmt.Sprint(v...), Field("foo", time.Second))
|
||||
})
|
||||
}
|
||||
|
||||
func TestStructedLogError(t *testing.T) {
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
@@ -569,6 +615,8 @@ func TestSetup(t *testing.T) {
|
||||
Path: os.TempDir(),
|
||||
Compress: true,
|
||||
KeepDays: 1,
|
||||
MaxBackups: 3,
|
||||
MaxSize: 1024 * 1024,
|
||||
}))
|
||||
setupLogLevel(LogConf{
|
||||
Level: levelInfo,
|
||||
|
||||
@@ -40,6 +40,22 @@ type richLogger struct {
|
||||
fields []LogField
|
||||
}
|
||||
|
||||
func (l *richLogger) Debug(v ...interface{}) {
|
||||
l.debug(fmt.Sprint(v...))
|
||||
}
|
||||
|
||||
func (l *richLogger) Debugf(format string, v ...interface{}) {
|
||||
l.debug(fmt.Sprintf(format, v...))
|
||||
}
|
||||
|
||||
func (l *richLogger) Debugv(v interface{}) {
|
||||
l.debug(v)
|
||||
}
|
||||
|
||||
func (l *richLogger) Debugw(msg string, fields ...LogField) {
|
||||
l.debug(msg, fields...)
|
||||
}
|
||||
|
||||
func (l *richLogger) Error(v ...interface{}) {
|
||||
l.err(fmt.Sprint(v...))
|
||||
}
|
||||
@@ -107,6 +123,11 @@ func (l *richLogger) WithDuration(duration time.Duration) Logger {
|
||||
return l
|
||||
}
|
||||
|
||||
func (l *richLogger) WithFields(fields ...LogField) Logger {
|
||||
l.fields = append(l.fields, fields...)
|
||||
return l
|
||||
}
|
||||
|
||||
func (l *richLogger) buildFields(fields ...LogField) []LogField {
|
||||
fields = append(l.fields, fields...)
|
||||
fields = append(fields, Field(callerKey, getCaller(callerDepth+l.callerSkip)))
|
||||
@@ -135,6 +156,12 @@ func (l *richLogger) buildFields(fields ...LogField) []LogField {
|
||||
return fields
|
||||
}
|
||||
|
||||
func (l *richLogger) debug(v interface{}, fields ...LogField) {
|
||||
if shallLog(DebugLevel) {
|
||||
getWriter().Debug(v, l.buildFields(fields...)...)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *richLogger) err(v interface{}, fields ...LogField) {
|
||||
if shallLog(ErrorLevel) {
|
||||
getWriter().Error(v, l.buildFields(fields...)...)
|
||||
|
||||
@@ -37,6 +37,41 @@ func TestTraceLog(t *testing.T) {
|
||||
validate(t, w.String(), true, true)
|
||||
}
|
||||
|
||||
func TestTraceDebug(t *testing.T) {
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
writer.lock.RLock()
|
||||
defer func() {
|
||||
writer.lock.RUnlock()
|
||||
writer.Store(old)
|
||||
}()
|
||||
|
||||
otp := otel.GetTracerProvider()
|
||||
tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()))
|
||||
otel.SetTracerProvider(tp)
|
||||
defer otel.SetTracerProvider(otp)
|
||||
|
||||
ctx, span := tp.Tracer("foo").Start(context.Background(), "bar")
|
||||
defer span.End()
|
||||
|
||||
l := WithContext(ctx)
|
||||
SetLevel(DebugLevel)
|
||||
l.WithDuration(time.Second).Debug(testlog)
|
||||
assert.True(t, strings.Contains(w.String(), traceKey))
|
||||
assert.True(t, strings.Contains(w.String(), spanKey))
|
||||
w.Reset()
|
||||
l.WithDuration(time.Second).Debugf(testlog)
|
||||
validate(t, w.String(), true, true)
|
||||
w.Reset()
|
||||
l.WithDuration(time.Second).Debugv(testlog)
|
||||
validate(t, w.String(), true, true)
|
||||
w.Reset()
|
||||
l.WithDuration(time.Second).Debugw(testlog, Field("foo", "bar"))
|
||||
validate(t, w.String(), true, true)
|
||||
assert.True(t, strings.Contains(w.String(), "foo"), w.String())
|
||||
assert.True(t, strings.Contains(w.String(), "bar"), w.String())
|
||||
}
|
||||
|
||||
func TestTraceError(t *testing.T) {
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
@@ -237,6 +272,23 @@ func TestLogWithCallerSkip(t *testing.T) {
|
||||
assert.True(t, w.Contains(fmt.Sprintf("%s:%d", file, line+1)))
|
||||
}
|
||||
|
||||
func TestLoggerWithFields(t *testing.T) {
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
writer.lock.RLock()
|
||||
defer func() {
|
||||
writer.lock.RUnlock()
|
||||
writer.Store(old)
|
||||
}()
|
||||
|
||||
l := WithContext(context.Background()).WithFields(Field("foo", "bar"))
|
||||
l.Info(testlog)
|
||||
|
||||
var val mockValue
|
||||
assert.Nil(t, json.Unmarshal([]byte(w.String()), &val))
|
||||
assert.Equal(t, "bar", val.Foo)
|
||||
}
|
||||
|
||||
func validate(t *testing.T, body string, expectedTrace, expectedSpan bool) {
|
||||
var val mockValue
|
||||
dec := json.NewDecoder(strings.NewReader(body))
|
||||
|
||||
@@ -115,7 +115,9 @@ func (r *DailyRotateRule) OutdatedFiles() []string {
|
||||
|
||||
var buf strings.Builder
|
||||
boundary := time.Now().Add(-time.Hour * time.Duration(hoursPerDay*r.days)).Format(dateFormat)
|
||||
fmt.Fprintf(&buf, "%s%s%s", r.filename, r.delimiter, boundary)
|
||||
buf.WriteString(r.filename)
|
||||
buf.WriteString(r.delimiter)
|
||||
buf.WriteString(boundary)
|
||||
if r.gzip {
|
||||
buf.WriteString(gzipExt)
|
||||
}
|
||||
|
||||
@@ -3,8 +3,10 @@ package logx
|
||||
import "errors"
|
||||
|
||||
const (
|
||||
// InfoLevel logs everything
|
||||
InfoLevel uint32 = iota
|
||||
// DebugLevel logs everything
|
||||
DebugLevel uint32 = iota
|
||||
// InfoLevel does not include debugs
|
||||
InfoLevel
|
||||
// ErrorLevel includes errors, slows, stacks
|
||||
ErrorLevel
|
||||
// SevereLevel only log severe messages
|
||||
@@ -37,6 +39,7 @@ const (
|
||||
levelFatal = "fatal"
|
||||
levelSlow = "slow"
|
||||
levelStat = "stat"
|
||||
levelDebug = "debug"
|
||||
|
||||
backupFileDelimiter = "-"
|
||||
flags = 0x0
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
package logx
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"path"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
|
||||
@@ -18,6 +18,7 @@ type (
|
||||
Writer interface {
|
||||
Alert(v interface{})
|
||||
Close() error
|
||||
Debug(v interface{}, fields ...LogField)
|
||||
Error(v interface{}, fields ...LogField)
|
||||
Info(v interface{}, fields ...LogField)
|
||||
Severe(v interface{})
|
||||
@@ -194,6 +195,10 @@ func (w *concreteWriter) Close() error {
|
||||
return w.statLog.Close()
|
||||
}
|
||||
|
||||
func (w *concreteWriter) Debug(v interface{}, fields ...LogField) {
|
||||
output(w.infoLog, levelDebug, v, fields...)
|
||||
}
|
||||
|
||||
func (w *concreteWriter) Error(v interface{}, fields ...LogField) {
|
||||
output(w.errorLog, levelError, v, fields...)
|
||||
}
|
||||
@@ -227,6 +232,9 @@ func (n nopWriter) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n nopWriter) Debug(_ interface{}, _ ...LogField) {
|
||||
}
|
||||
|
||||
func (n nopWriter) Error(_ interface{}, _ ...LogField) {
|
||||
}
|
||||
|
||||
@@ -255,7 +263,18 @@ func buildFields(fields ...LogField) []string {
|
||||
return items
|
||||
}
|
||||
|
||||
func combineGlobalFields(fields []LogField) []LogField {
|
||||
globals := globalFields.Load()
|
||||
if globals == nil {
|
||||
return fields
|
||||
}
|
||||
|
||||
return append(globals.([]LogField), fields...)
|
||||
}
|
||||
|
||||
func output(writer io.Writer, level string, val interface{}, fields ...LogField) {
|
||||
fields = combineGlobalFields(fields)
|
||||
|
||||
switch atomic.LoadUint32(&encoding) {
|
||||
case plainEncodingType:
|
||||
writePlainAny(writer, level, val, buildFields(fields...)...)
|
||||
@@ -284,6 +303,8 @@ func wrapLevelWithColor(level string) string {
|
||||
colour = color.FgBlue
|
||||
case levelSlow:
|
||||
colour = color.FgYellow
|
||||
case levelDebug:
|
||||
colour = color.FgYellow
|
||||
case levelStat:
|
||||
colour = color.FgGreen
|
||||
}
|
||||
@@ -321,7 +342,7 @@ func writePlainAny(writer io.Writer, level string, val interface{}, fields ...st
|
||||
}
|
||||
|
||||
func writePlainText(writer io.Writer, level, msg string, fields ...string) {
|
||||
var buf strings.Builder
|
||||
var buf bytes.Buffer
|
||||
buf.WriteString(getTimestamp())
|
||||
buf.WriteByte(plainEncodingSep)
|
||||
buf.WriteString(level)
|
||||
@@ -337,13 +358,13 @@ func writePlainText(writer io.Writer, level, msg string, fields ...string) {
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := fmt.Fprint(writer, buf.String()); err != nil {
|
||||
if _, err := writer.Write(buf.Bytes()); err != nil {
|
||||
log.Println(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func writePlainValue(writer io.Writer, level string, val interface{}, fields ...string) {
|
||||
var buf strings.Builder
|
||||
var buf bytes.Buffer
|
||||
buf.WriteString(getTimestamp())
|
||||
buf.WriteByte(plainEncodingSep)
|
||||
buf.WriteString(level)
|
||||
@@ -363,7 +384,7 @@ func writePlainValue(writer io.Writer, level string, val interface{}, fields ...
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := fmt.Fprint(writer, buf.String()); err != nil {
|
||||
if _, err := writer.Write(buf.Bytes()); err != nil {
|
||||
log.Println(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,9 @@ func TestNewWriter(t *testing.T) {
|
||||
w := NewWriter(&buf)
|
||||
w.Info(literal)
|
||||
assert.Contains(t, buf.String(), literal)
|
||||
buf.Reset()
|
||||
w.Debug(literal)
|
||||
assert.Contains(t, buf.String(), literal)
|
||||
}
|
||||
|
||||
func TestConsoleWriter(t *testing.T) {
|
||||
@@ -97,6 +100,7 @@ func TestNopWriter(t *testing.T) {
|
||||
assert.NotPanics(t, func() {
|
||||
var w nopWriter
|
||||
w.Alert("foo")
|
||||
w.Debug("foo")
|
||||
w.Error("foo")
|
||||
w.Info("foo")
|
||||
w.Severe("foo")
|
||||
@@ -123,6 +127,12 @@ func TestWritePlainAny(t *testing.T) {
|
||||
writePlainAny(nil, levelInfo, "foo")
|
||||
assert.Contains(t, buf.String(), "foo")
|
||||
|
||||
buf.Reset()
|
||||
writePlainAny(nil, levelDebug, make(chan int))
|
||||
assert.Contains(t, buf.String(), "unsupported type")
|
||||
writePlainAny(nil, levelDebug, 100)
|
||||
assert.Contains(t, buf.String(), "100")
|
||||
|
||||
buf.Reset()
|
||||
writePlainAny(nil, levelError, make(chan int))
|
||||
assert.Contains(t, buf.String(), "unsupported type")
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package mapping
|
||||
|
||||
import (
|
||||
"encoding"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -25,9 +26,9 @@ var (
|
||||
errValueNotStruct = errors.New("value type is not struct")
|
||||
keyUnmarshaler = NewUnmarshaler(defaultKeyName)
|
||||
durationType = reflect.TypeOf(time.Duration(0))
|
||||
cacheKeys map[string][]string
|
||||
cacheKeys = make(map[string][]string)
|
||||
cacheKeysLock sync.Mutex
|
||||
defaultCache map[string]interface{}
|
||||
defaultCache = make(map[string]interface{})
|
||||
defaultCacheLock sync.Mutex
|
||||
emptyMap = map[string]interface{}{}
|
||||
emptyValue = reflect.ValueOf(lang.Placeholder)
|
||||
@@ -49,11 +50,6 @@ type (
|
||||
}
|
||||
)
|
||||
|
||||
func init() {
|
||||
cacheKeys = make(map[string][]string)
|
||||
defaultCache = make(map[string]interface{})
|
||||
}
|
||||
|
||||
// NewUnmarshaler returns a Unmarshaler.
|
||||
func NewUnmarshaler(key string, opts ...UnmarshalOption) *Unmarshaler {
|
||||
unmarshaler := Unmarshaler{
|
||||
@@ -144,7 +140,6 @@ func (u *Unmarshaler) processAnonymousFieldOptional(field reflect.StructField, v
|
||||
filled = true
|
||||
maybeNewValue(field, value)
|
||||
indirectValue = reflect.Indirect(value)
|
||||
|
||||
}
|
||||
if err = u.processField(subField, indirectValue.Field(i), m, fullName); err != nil {
|
||||
return err
|
||||
@@ -205,6 +200,8 @@ func (u *Unmarshaler) processFieldNotFromString(field reflect.StructField, value
|
||||
return u.processFieldStruct(field, value, mapValue, fullName)
|
||||
case valueKind == reflect.Map && typeKind == reflect.Map:
|
||||
return u.fillMap(field, value, mapValue)
|
||||
case valueKind == reflect.String && typeKind == reflect.Map:
|
||||
return u.fillMapFromString(value, mapValue)
|
||||
case valueKind == reflect.String && typeKind == reflect.Slice:
|
||||
return u.fillSliceFromString(fieldType, value, mapValue)
|
||||
case valueKind == reflect.String && derefedFieldType == durationType:
|
||||
@@ -322,6 +319,28 @@ func (u *Unmarshaler) processFieldStructWithMap(field reflect.StructField, value
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *Unmarshaler) processFieldTextUnmarshaler(field reflect.StructField, value reflect.Value,
|
||||
mapValue interface{}) (bool, error) {
|
||||
var tval encoding.TextUnmarshaler
|
||||
var ok bool
|
||||
|
||||
if field.Type.Kind() == reflect.Ptr {
|
||||
tval, ok = value.Interface().(encoding.TextUnmarshaler)
|
||||
} else {
|
||||
tval, ok = value.Addr().Interface().(encoding.TextUnmarshaler)
|
||||
}
|
||||
if ok {
|
||||
switch mv := mapValue.(type) {
|
||||
case string:
|
||||
return true, tval.UnmarshalText([]byte(mv))
|
||||
case []byte:
|
||||
return true, tval.UnmarshalText(mv)
|
||||
}
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (u *Unmarshaler) processNamedField(field reflect.StructField, value reflect.Value,
|
||||
m Valuer, fullName string) error {
|
||||
key, opts, err := u.parseOptionsWithContext(field, m, fullName)
|
||||
@@ -352,8 +371,16 @@ func (u *Unmarshaler) processNamedFieldWithValue(field reflect.StructField, valu
|
||||
return fmt.Errorf("field %s mustn't be nil", key)
|
||||
}
|
||||
|
||||
if !value.CanSet() {
|
||||
return fmt.Errorf("field %s is not settable", key)
|
||||
}
|
||||
|
||||
maybeNewValue(field, value)
|
||||
|
||||
if yes, err := u.processFieldTextUnmarshaler(field, value, mapValue); yes {
|
||||
return err
|
||||
}
|
||||
|
||||
fieldKind := Deref(field.Type).Kind()
|
||||
switch fieldKind {
|
||||
case reflect.Array, reflect.Map, reflect.Slice, reflect.Struct:
|
||||
@@ -442,6 +469,27 @@ func (u *Unmarshaler) fillMap(field reflect.StructField, value reflect.Value, ma
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *Unmarshaler) fillMapFromString(value reflect.Value, mapValue interface{}) error {
|
||||
if !value.CanSet() {
|
||||
return errValueNotSettable
|
||||
}
|
||||
|
||||
switch v := mapValue.(type) {
|
||||
case fmt.Stringer:
|
||||
if err := jsonx.UnmarshalFromString(v.String(), value.Addr().Interface()); err != nil {
|
||||
return err
|
||||
}
|
||||
case string:
|
||||
if err := jsonx.UnmarshalFromString(v, value.Addr().Interface()); err != nil {
|
||||
return err
|
||||
}
|
||||
default:
|
||||
return errUnsupportedType
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *Unmarshaler) fillSlice(fieldType reflect.Type, value reflect.Value, mapValue interface{}) error {
|
||||
if !value.CanSet() {
|
||||
return errValueNotSettable
|
||||
|
||||
@@ -2,11 +2,13 @@ package mapping
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/zeromicro/go-zero/core/stringx"
|
||||
)
|
||||
@@ -467,6 +469,146 @@ func TestUnmarshalIntSliceFromString(t *testing.T) {
|
||||
ast.Equal(2, v.Values[1])
|
||||
}
|
||||
|
||||
func TestUnmarshalIntMapFromString(t *testing.T) {
|
||||
var v struct {
|
||||
Sort map[string]int `key:"sort"`
|
||||
}
|
||||
m := map[string]interface{}{
|
||||
"sort": `{"value":12345,"zeroVal":0,"nullVal":null}`,
|
||||
}
|
||||
|
||||
ast := assert.New(t)
|
||||
ast.Nil(UnmarshalKey(m, &v))
|
||||
ast.Equal(3, len(v.Sort))
|
||||
ast.Equal(12345, v.Sort["value"])
|
||||
ast.Equal(0, v.Sort["zeroVal"])
|
||||
ast.Equal(0, v.Sort["nullVal"])
|
||||
}
|
||||
|
||||
func TestUnmarshalBoolMapFromString(t *testing.T) {
|
||||
var v struct {
|
||||
Sort map[string]bool `key:"sort"`
|
||||
}
|
||||
m := map[string]interface{}{
|
||||
"sort": `{"value":true,"zeroVal":false,"nullVal":null}`,
|
||||
}
|
||||
|
||||
ast := assert.New(t)
|
||||
ast.Nil(UnmarshalKey(m, &v))
|
||||
ast.Equal(3, len(v.Sort))
|
||||
ast.Equal(true, v.Sort["value"])
|
||||
ast.Equal(false, v.Sort["zeroVal"])
|
||||
ast.Equal(false, v.Sort["nullVal"])
|
||||
}
|
||||
|
||||
type CustomStringer string
|
||||
|
||||
type UnsupportedStringer string
|
||||
|
||||
func (c CustomStringer) String() string {
|
||||
return fmt.Sprintf("{%s}", string(c))
|
||||
}
|
||||
|
||||
func TestUnmarshalStringMapFromStringer(t *testing.T) {
|
||||
var v struct {
|
||||
Sort map[string]string `key:"sort"`
|
||||
}
|
||||
m := map[string]interface{}{
|
||||
"sort": CustomStringer(`"value":"ascend","emptyStr":""`),
|
||||
}
|
||||
|
||||
ast := assert.New(t)
|
||||
ast.Nil(UnmarshalKey(m, &v))
|
||||
ast.Equal(2, len(v.Sort))
|
||||
ast.Equal("ascend", v.Sort["value"])
|
||||
ast.Equal("", v.Sort["emptyStr"])
|
||||
}
|
||||
|
||||
func TestUnmarshalStringMapFromUnsupportedType(t *testing.T) {
|
||||
var v struct {
|
||||
Sort map[string]string `key:"sort"`
|
||||
}
|
||||
m := map[string]interface{}{
|
||||
"sort": UnsupportedStringer(`{"value":"ascend","emptyStr":""}`),
|
||||
}
|
||||
|
||||
ast := assert.New(t)
|
||||
ast.NotNil(UnmarshalKey(m, &v))
|
||||
}
|
||||
|
||||
func TestUnmarshalStringMapFromNotSettableValue(t *testing.T) {
|
||||
var v struct {
|
||||
sort map[string]string `key:"sort"`
|
||||
psort *map[string]string `key:"sort"`
|
||||
}
|
||||
m := map[string]interface{}{
|
||||
"sort": `{"value":"ascend","emptyStr":""}`,
|
||||
"psort": `{"value":"ascend","emptyStr":""}`,
|
||||
}
|
||||
|
||||
ast := assert.New(t)
|
||||
ast.NotNil(UnmarshalKey(m, &v))
|
||||
}
|
||||
|
||||
func TestUnmarshalStringMapFromString(t *testing.T) {
|
||||
var v struct {
|
||||
Sort map[string]string `key:"sort"`
|
||||
}
|
||||
m := map[string]interface{}{
|
||||
"sort": `{"value":"ascend","emptyStr":""}`,
|
||||
}
|
||||
|
||||
ast := assert.New(t)
|
||||
ast.Nil(UnmarshalKey(m, &v))
|
||||
ast.Equal(2, len(v.Sort))
|
||||
ast.Equal("ascend", v.Sort["value"])
|
||||
ast.Equal("", v.Sort["emptyStr"])
|
||||
}
|
||||
|
||||
func TestUnmarshalStructMapFromString(t *testing.T) {
|
||||
var v struct {
|
||||
Filter map[string]struct {
|
||||
Field1 bool `json:"field1"`
|
||||
Field2 int64 `json:"field2,string"`
|
||||
Field3 string `json:"field3"`
|
||||
Field4 *string `json:"field4"`
|
||||
Field5 []string `json:"field5"`
|
||||
} `key:"filter"`
|
||||
}
|
||||
m := map[string]interface{}{
|
||||
"filter": `{"obj":{"field1":true,"field2":"1573570455447539712","field3":"this is a string",
|
||||
"field4":"this is a string pointer","field5":["str1","str2"]}}`,
|
||||
}
|
||||
|
||||
ast := assert.New(t)
|
||||
ast.Nil(UnmarshalKey(m, &v))
|
||||
ast.Equal(1, len(v.Filter))
|
||||
ast.NotNil(v.Filter["obj"])
|
||||
ast.Equal(true, v.Filter["obj"].Field1)
|
||||
ast.Equal(int64(1573570455447539712), v.Filter["obj"].Field2)
|
||||
ast.Equal("this is a string", v.Filter["obj"].Field3)
|
||||
ast.Equal("this is a string pointer", *v.Filter["obj"].Field4)
|
||||
ast.ElementsMatch([]string{"str1", "str2"}, v.Filter["obj"].Field5)
|
||||
}
|
||||
|
||||
func TestUnmarshalStringSliceMapFromString(t *testing.T) {
|
||||
var v struct {
|
||||
Filter map[string][]string `key:"filter"`
|
||||
}
|
||||
m := map[string]interface{}{
|
||||
"filter": `{"assignType":null,"status":["process","comment"],"rate":[]}`,
|
||||
}
|
||||
|
||||
ast := assert.New(t)
|
||||
ast.Nil(UnmarshalKey(m, &v))
|
||||
ast.Equal(3, len(v.Filter))
|
||||
ast.Equal([]string(nil), v.Filter["assignType"])
|
||||
ast.Equal(2, len(v.Filter["status"]))
|
||||
ast.Equal("process", v.Filter["status"][0])
|
||||
ast.Equal("comment", v.Filter["status"][1])
|
||||
ast.Equal(0, len(v.Filter["rate"]))
|
||||
}
|
||||
|
||||
func TestUnmarshalStruct(t *testing.T) {
|
||||
type address struct {
|
||||
City string `key:"city"`
|
||||
@@ -2879,6 +3021,24 @@ func TestUnmarshalJsonReaderArrayString(t *testing.T) {
|
||||
assert.NotNil(t, err)
|
||||
}
|
||||
|
||||
func TestGoogleUUID(t *testing.T) {
|
||||
var val struct {
|
||||
Uid uuid.UUID `json:"uid,optional"`
|
||||
Uidp *uuid.UUID `json:"uidp,optional"`
|
||||
}
|
||||
assert.NoError(t, UnmarshalJsonBytes([]byte(`{
|
||||
"uid": "6ba7b810-9dad-11d1-80b4-00c04fd430c8",
|
||||
"uidp": "6ba7b810-9dad-11d1-80b4-00c04fd430c9"}`), &val))
|
||||
assert.Equal(t, "6ba7b810-9dad-11d1-80b4-00c04fd430c8", val.Uid.String())
|
||||
assert.Equal(t, "6ba7b810-9dad-11d1-80b4-00c04fd430c9", val.Uidp.String())
|
||||
assert.NoError(t, UnmarshalJsonMap(map[string]interface{}{
|
||||
"uid": []byte("6ba7b810-9dad-11d1-80b4-00c04fd430c1"),
|
||||
"uidp": []byte("6ba7b810-9dad-11d1-80b4-00c04fd430c2"),
|
||||
}, &val))
|
||||
assert.Equal(t, "6ba7b810-9dad-11d1-80b4-00c04fd430c1", val.Uid.String())
|
||||
assert.Equal(t, "6ba7b810-9dad-11d1-80b4-00c04fd430c2", val.Uidp.String())
|
||||
}
|
||||
|
||||
func BenchmarkDefaultValue(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
var a struct {
|
||||
|
||||
@@ -63,7 +63,7 @@ func cleanupMapValue(v interface{}) interface{} {
|
||||
}
|
||||
}
|
||||
|
||||
func unmarshal(unmarshaler *Unmarshaler, o interface{}, v interface{}) error {
|
||||
func unmarshal(unmarshaler *Unmarshaler, o, v interface{}) error {
|
||||
if m, ok := o.(map[string]interface{}); ok {
|
||||
return unmarshaler.Unmarshal(m, v)
|
||||
}
|
||||
|
||||
@@ -145,7 +145,7 @@ func MapReduceChan(source <-chan interface{}, mapper MapperFunc, reducer Reducer
|
||||
return mapReduceWithPanicChan(source, panicChan, mapper, reducer, opts...)
|
||||
}
|
||||
|
||||
// MapReduceChan maps all elements from source, and reduce the output elements with given reducer.
|
||||
// mapReduceWithPanicChan maps all elements from source, and reduce the output elements with given reducer.
|
||||
func mapReduceWithPanicChan(source <-chan interface{}, panicChan *onceChan, mapper MapperFunc,
|
||||
reducer ReducerFunc, opts ...Option) (interface{}, error) {
|
||||
options := buildOptions(opts...)
|
||||
|
||||
@@ -19,7 +19,7 @@ func FuzzMapReduce(f *testing.F) {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
|
||||
f.Add(uint(10), uint(runtime.NumCPU()))
|
||||
f.Fuzz(func(t *testing.T, num uint, workers uint) {
|
||||
f.Fuzz(func(t *testing.T, num, workers uint) {
|
||||
n := int64(num)%5000 + 5000
|
||||
genPanic := rand.Intn(100) == 0
|
||||
mapperPanic := rand.Intn(100) == 0
|
||||
|
||||
@@ -46,14 +46,14 @@ func Report(msg string) {
|
||||
if fn != nil {
|
||||
reported := lessExecutor.DoOrDiscard(func() {
|
||||
var builder strings.Builder
|
||||
fmt.Fprintf(&builder, "%s\n", time.Now().Format(timeFormat))
|
||||
builder.WriteString(fmt.Sprintln(time.Now().Format(timeFormat)))
|
||||
if len(clusterName) > 0 {
|
||||
fmt.Fprintf(&builder, "cluster: %s\n", clusterName)
|
||||
builder.WriteString(fmt.Sprintf("cluster: %s\n", clusterName))
|
||||
}
|
||||
fmt.Fprintf(&builder, "host: %s\n", sysx.Hostname())
|
||||
builder.WriteString(fmt.Sprintf("host: %s\n", sysx.Hostname()))
|
||||
dp := atomic.SwapInt32(&dropped, 0)
|
||||
if dp > 0 {
|
||||
fmt.Fprintf(&builder, "dropped: %d\n", dp)
|
||||
builder.WriteString(fmt.Sprintf("dropped: %d\n", dp))
|
||||
}
|
||||
builder.WriteString(strings.TrimSpace(msg))
|
||||
fn(builder.String())
|
||||
|
||||
@@ -83,12 +83,12 @@ type (
|
||||
// FindOneAndReplace returns at most one document that matches the filter. If the filter
|
||||
// matches multiple documents, FindOneAndReplace returns the first document in the
|
||||
// collection that matches the filter.
|
||||
FindOneAndReplace(ctx context.Context, filter interface{}, replacement interface{},
|
||||
FindOneAndReplace(ctx context.Context, filter, replacement interface{},
|
||||
opts ...*mopt.FindOneAndReplaceOptions) (*mongo.SingleResult, error)
|
||||
// FindOneAndUpdate returns at most one document that matches the filter. If the filter
|
||||
// matches multiple documents, FindOneAndUpdate returns the first document in the
|
||||
// collection that matches the filter.
|
||||
FindOneAndUpdate(ctx context.Context, filter interface{}, update interface{},
|
||||
FindOneAndUpdate(ctx context.Context, filter, update interface{},
|
||||
opts ...*mopt.FindOneAndUpdateOptions) (*mongo.SingleResult, error)
|
||||
// Indexes returns the index view for this collection.
|
||||
Indexes() mongo.IndexView
|
||||
@@ -99,16 +99,16 @@ type (
|
||||
InsertOne(ctx context.Context, document interface{}, opts ...*mopt.InsertOneOptions) (
|
||||
*mongo.InsertOneResult, error)
|
||||
// ReplaceOne replaces at most one document that matches the filter.
|
||||
ReplaceOne(ctx context.Context, filter interface{}, replacement interface{},
|
||||
ReplaceOne(ctx context.Context, filter, replacement interface{},
|
||||
opts ...*mopt.ReplaceOptions) (*mongo.UpdateResult, error)
|
||||
// UpdateByID updates a single document matching the provided filter.
|
||||
UpdateByID(ctx context.Context, id interface{}, update interface{},
|
||||
UpdateByID(ctx context.Context, id, update interface{},
|
||||
opts ...*mopt.UpdateOptions) (*mongo.UpdateResult, error)
|
||||
// UpdateMany updates the provided documents.
|
||||
UpdateMany(ctx context.Context, filter interface{}, update interface{},
|
||||
UpdateMany(ctx context.Context, filter, update interface{},
|
||||
opts ...*mopt.UpdateOptions) (*mongo.UpdateResult, error)
|
||||
// UpdateOne updates a single document matching the provided filter.
|
||||
UpdateOne(ctx context.Context, filter interface{}, update interface{},
|
||||
UpdateOne(ctx context.Context, filter, update interface{},
|
||||
opts ...*mopt.UpdateOptions) (*mongo.UpdateResult, error)
|
||||
// Watch returns a change stream cursor used to receive notifications of changes to the collection.
|
||||
Watch(ctx context.Context, pipeline interface{}, opts ...*mopt.ChangeStreamOptions) (
|
||||
@@ -359,7 +359,7 @@ func (c *decoratedCollection) FindOneAndReplace(ctx context.Context, filter inte
|
||||
return
|
||||
}
|
||||
|
||||
func (c *decoratedCollection) FindOneAndUpdate(ctx context.Context, filter interface{}, update interface{},
|
||||
func (c *decoratedCollection) FindOneAndUpdate(ctx context.Context, filter, update interface{},
|
||||
opts ...*mopt.FindOneAndUpdateOptions) (res *mongo.SingleResult, err error) {
|
||||
ctx, span := startSpan(ctx, findOneAndUpdate)
|
||||
defer func() {
|
||||
@@ -420,7 +420,7 @@ func (c *decoratedCollection) InsertOne(ctx context.Context, document interface{
|
||||
return
|
||||
}
|
||||
|
||||
func (c *decoratedCollection) ReplaceOne(ctx context.Context, filter interface{}, replacement interface{},
|
||||
func (c *decoratedCollection) ReplaceOne(ctx context.Context, filter, replacement interface{},
|
||||
opts ...*mopt.ReplaceOptions) (res *mongo.UpdateResult, err error) {
|
||||
ctx, span := startSpan(ctx, replaceOne)
|
||||
defer func() {
|
||||
@@ -440,7 +440,7 @@ func (c *decoratedCollection) ReplaceOne(ctx context.Context, filter interface{}
|
||||
return
|
||||
}
|
||||
|
||||
func (c *decoratedCollection) UpdateByID(ctx context.Context, id interface{}, update interface{},
|
||||
func (c *decoratedCollection) UpdateByID(ctx context.Context, id, update interface{},
|
||||
opts ...*mopt.UpdateOptions) (res *mongo.UpdateResult, err error) {
|
||||
ctx, span := startSpan(ctx, updateByID)
|
||||
defer func() {
|
||||
@@ -460,7 +460,7 @@ func (c *decoratedCollection) UpdateByID(ctx context.Context, id interface{}, up
|
||||
return
|
||||
}
|
||||
|
||||
func (c *decoratedCollection) UpdateMany(ctx context.Context, filter interface{}, update interface{},
|
||||
func (c *decoratedCollection) UpdateMany(ctx context.Context, filter, update interface{},
|
||||
opts ...*mopt.UpdateOptions) (res *mongo.UpdateResult, err error) {
|
||||
ctx, span := startSpan(ctx, updateMany)
|
||||
defer func() {
|
||||
@@ -480,7 +480,7 @@ func (c *decoratedCollection) UpdateMany(ctx context.Context, filter interface{}
|
||||
return
|
||||
}
|
||||
|
||||
func (c *decoratedCollection) UpdateOne(ctx context.Context, filter interface{}, update interface{},
|
||||
func (c *decoratedCollection) UpdateOne(ctx context.Context, filter, update interface{},
|
||||
opts ...*mopt.UpdateOptions) (res *mongo.UpdateResult, err error) {
|
||||
ctx, span := startSpan(ctx, updateOne)
|
||||
defer func() {
|
||||
|
||||
@@ -106,14 +106,14 @@ func TestCollection_BulkWrite(t *testing.T) {
|
||||
}
|
||||
mt.AddMockResponses(mtest.CreateSuccessResponse(bson.D{{Key: "ok", Value: 1}}...))
|
||||
res, err := c.BulkWrite(context.Background(), []mongo.WriteModel{
|
||||
mongo.NewInsertOneModel().SetDocument(bson.D{{Key: "foo", Value: 1}})},
|
||||
)
|
||||
mongo.NewInsertOneModel().SetDocument(bson.D{{Key: "foo", Value: 1}}),
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
assert.NotNil(t, res)
|
||||
c.brk = new(dropBreaker)
|
||||
_, err = c.BulkWrite(context.Background(), []mongo.WriteModel{
|
||||
mongo.NewInsertOneModel().SetDocument(bson.D{{Key: "foo", Value: 1}})},
|
||||
)
|
||||
mongo.NewInsertOneModel().SetDocument(bson.D{{Key: "foo", Value: 1}}),
|
||||
})
|
||||
assert.Equal(t, errDummy, err)
|
||||
})
|
||||
}
|
||||
@@ -204,7 +204,7 @@ func TestCollection_EstimatedDocumentCount(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestCollectionFind(t *testing.T) {
|
||||
func TestCollection_Find(t *testing.T) {
|
||||
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
|
||||
defer mt.Close()
|
||||
|
||||
@@ -252,7 +252,7 @@ func TestCollectionFind(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestCollectionFindOne(t *testing.T) {
|
||||
func TestCollection_FindOne(t *testing.T) {
|
||||
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
|
||||
defer mt.Close()
|
||||
|
||||
@@ -436,7 +436,7 @@ func TestCollection_InsertMany(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestCollection_Remove(t *testing.T) {
|
||||
func TestCollection_DeleteOne(t *testing.T) {
|
||||
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
|
||||
defer mt.Close()
|
||||
|
||||
@@ -456,7 +456,7 @@ func TestCollection_Remove(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestCollectionRemoveAll(t *testing.T) {
|
||||
func TestCollection_DeleteMany(t *testing.T) {
|
||||
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
|
||||
defer mt.Close()
|
||||
|
||||
@@ -565,7 +565,7 @@ func TestCollection_UpdateMany(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func Test_DecoratedCollectionLogDuration(t *testing.T) {
|
||||
func TestDecoratedCollection_LogDuration(t *testing.T) {
|
||||
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
|
||||
defer mt.Close()
|
||||
c := decoratedCollection{
|
||||
|
||||
@@ -159,7 +159,7 @@ func (m *Model) FindOneAndDelete(ctx context.Context, v, filter interface{},
|
||||
}
|
||||
|
||||
// FindOneAndReplace finds a single document and replaces it.
|
||||
func (m *Model) FindOneAndReplace(ctx context.Context, v, filter interface{}, replacement interface{},
|
||||
func (m *Model) FindOneAndReplace(ctx context.Context, v, filter, replacement interface{},
|
||||
opts ...*mopt.FindOneAndReplaceOptions) error {
|
||||
res, err := m.Collection.FindOneAndReplace(ctx, filter, replacement, opts...)
|
||||
if err != nil {
|
||||
@@ -170,7 +170,7 @@ func (m *Model) FindOneAndReplace(ctx context.Context, v, filter interface{}, re
|
||||
}
|
||||
|
||||
// FindOneAndUpdate finds a single document and updates it.
|
||||
func (m *Model) FindOneAndUpdate(ctx context.Context, v, filter interface{}, update interface{},
|
||||
func (m *Model) FindOneAndUpdate(ctx context.Context, v, filter, update interface{},
|
||||
opts ...*mopt.FindOneAndUpdateOptions) error {
|
||||
res, err := m.Collection.FindOneAndUpdate(ctx, filter, update, opts...)
|
||||
if err != nil {
|
||||
|
||||
@@ -192,7 +192,7 @@ func (mm *Model) InsertOneNoCache(ctx context.Context, document interface{},
|
||||
}
|
||||
|
||||
// ReplaceOne replaces a single document in the collection, and remove the cache.
|
||||
func (mm *Model) ReplaceOne(ctx context.Context, key string, filter interface{}, replacement interface{},
|
||||
func (mm *Model) ReplaceOne(ctx context.Context, key string, filter, replacement interface{},
|
||||
opts ...*mopt.ReplaceOptions) (*mongo.UpdateResult, error) {
|
||||
res, err := mm.Model.ReplaceOne(ctx, filter, replacement, opts...)
|
||||
if err != nil {
|
||||
@@ -207,7 +207,7 @@ func (mm *Model) ReplaceOne(ctx context.Context, key string, filter interface{},
|
||||
}
|
||||
|
||||
// ReplaceOneNoCache replaces a single document in the collection.
|
||||
func (mm *Model) ReplaceOneNoCache(ctx context.Context, filter interface{}, replacement interface{},
|
||||
func (mm *Model) ReplaceOneNoCache(ctx context.Context, filter, replacement interface{},
|
||||
opts ...*mopt.ReplaceOptions) (*mongo.UpdateResult, error) {
|
||||
return mm.Model.ReplaceOne(ctx, filter, replacement, opts...)
|
||||
}
|
||||
@@ -218,7 +218,7 @@ func (mm *Model) SetCache(key string, v interface{}) error {
|
||||
}
|
||||
|
||||
// UpdateByID updates the document with given id with update, and remove the cache.
|
||||
func (mm *Model) UpdateByID(ctx context.Context, key string, id interface{}, update interface{},
|
||||
func (mm *Model) UpdateByID(ctx context.Context, key string, id, update interface{},
|
||||
opts ...*mopt.UpdateOptions) (*mongo.UpdateResult, error) {
|
||||
res, err := mm.Model.UpdateByID(ctx, id, update, opts...)
|
||||
if err != nil {
|
||||
@@ -233,13 +233,13 @@ func (mm *Model) UpdateByID(ctx context.Context, key string, id interface{}, upd
|
||||
}
|
||||
|
||||
// UpdateByIDNoCache updates the document with given id with update.
|
||||
func (mm *Model) UpdateByIDNoCache(ctx context.Context, id interface{}, update interface{},
|
||||
func (mm *Model) UpdateByIDNoCache(ctx context.Context, id, update interface{},
|
||||
opts ...*mopt.UpdateOptions) (*mongo.UpdateResult, error) {
|
||||
return mm.Model.UpdateByID(ctx, id, update, opts...)
|
||||
}
|
||||
|
||||
// UpdateMany updates the documents that match filter with update, and remove the cache.
|
||||
func (mm *Model) UpdateMany(ctx context.Context, keys []string, filter interface{}, update interface{},
|
||||
func (mm *Model) UpdateMany(ctx context.Context, keys []string, filter, update interface{},
|
||||
opts ...*mopt.UpdateOptions) (*mongo.UpdateResult, error) {
|
||||
res, err := mm.Model.UpdateMany(ctx, filter, update, opts...)
|
||||
if err != nil {
|
||||
@@ -254,13 +254,13 @@ func (mm *Model) UpdateMany(ctx context.Context, keys []string, filter interface
|
||||
}
|
||||
|
||||
// UpdateManyNoCache updates the documents that match filter with update.
|
||||
func (mm *Model) UpdateManyNoCache(ctx context.Context, filter interface{}, update interface{},
|
||||
func (mm *Model) UpdateManyNoCache(ctx context.Context, filter, update interface{},
|
||||
opts ...*mopt.UpdateOptions) (*mongo.UpdateResult, error) {
|
||||
return mm.Model.UpdateMany(ctx, filter, update, opts...)
|
||||
}
|
||||
|
||||
// UpdateOne updates the first document that matches filter with update, and remove the cache.
|
||||
func (mm *Model) UpdateOne(ctx context.Context, key string, filter interface{}, update interface{},
|
||||
func (mm *Model) UpdateOne(ctx context.Context, key string, filter, update interface{},
|
||||
opts ...*mopt.UpdateOptions) (*mongo.UpdateResult, error) {
|
||||
res, err := mm.Model.UpdateOne(ctx, filter, update, opts...)
|
||||
if err != nil {
|
||||
@@ -275,7 +275,7 @@ func (mm *Model) UpdateOne(ctx context.Context, key string, filter interface{},
|
||||
}
|
||||
|
||||
// UpdateOneNoCache updates the first document that matches filter with update.
|
||||
func (mm *Model) UpdateOneNoCache(ctx context.Context, filter interface{}, update interface{},
|
||||
func (mm *Model) UpdateOneNoCache(ctx context.Context, filter, update interface{},
|
||||
opts ...*mopt.UpdateOptions) (*mongo.UpdateResult, error) {
|
||||
return mm.Model.UpdateOne(ctx, filter, update, opts...)
|
||||
}
|
||||
|
||||
@@ -2,10 +2,13 @@ package redis
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
red "github.com/go-redis/redis/v8"
|
||||
"github.com/zeromicro/go-zero/core/breaker"
|
||||
"github.com/zeromicro/go-zero/core/errorx"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/core/mapping"
|
||||
@@ -53,7 +56,7 @@ func (h hook) AfterProcess(ctx context.Context, cmd red.Cmder) error {
|
||||
|
||||
duration := timex.Since(start)
|
||||
if duration > slowThreshold.Load() {
|
||||
logDuration(ctx, cmd, duration)
|
||||
logDuration(ctx, []red.Cmder{cmd}, duration)
|
||||
}
|
||||
|
||||
metricReqDur.Observe(int64(duration/time.Millisecond), cmd.Name())
|
||||
@@ -100,7 +103,7 @@ func (h hook) AfterProcessPipeline(ctx context.Context, cmds []red.Cmder) error
|
||||
|
||||
duration := timex.Since(start)
|
||||
if duration > slowThreshold.Load()*time.Duration(len(cmds)) {
|
||||
logDuration(ctx, cmds[0], duration)
|
||||
logDuration(ctx, cmds, duration)
|
||||
}
|
||||
|
||||
metricReqDur.Observe(int64(duration/time.Millisecond), "Pipeline")
|
||||
@@ -116,37 +119,37 @@ func formatError(err error) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
es := err.Error()
|
||||
switch {
|
||||
case strings.HasPrefix(es, "read"):
|
||||
return "read timeout"
|
||||
case strings.HasPrefix(es, "dial"):
|
||||
if strings.Contains(es, "connection refused") {
|
||||
return "connection refused"
|
||||
}
|
||||
return "dial timeout"
|
||||
case strings.HasPrefix(es, "write"):
|
||||
return "write timeout"
|
||||
case strings.Contains(es, "EOF"):
|
||||
opErr, ok := err.(*net.OpError)
|
||||
if ok && opErr.Timeout() {
|
||||
return "timeout"
|
||||
}
|
||||
|
||||
switch err {
|
||||
case io.EOF:
|
||||
return "eof"
|
||||
case strings.Contains(es, "reset"):
|
||||
return "reset"
|
||||
case strings.Contains(es, "broken"):
|
||||
return "broken pipe"
|
||||
case strings.Contains(es, "breaker"):
|
||||
case context.DeadlineExceeded:
|
||||
return "context deadline"
|
||||
case breaker.ErrServiceUnavailable:
|
||||
return "breaker"
|
||||
default:
|
||||
return "unexpected error"
|
||||
}
|
||||
}
|
||||
|
||||
func logDuration(ctx context.Context, cmd red.Cmder, duration time.Duration) {
|
||||
func logDuration(ctx context.Context, cmds []red.Cmder, duration time.Duration) {
|
||||
var buf strings.Builder
|
||||
for i, arg := range cmd.Args() {
|
||||
if i > 0 {
|
||||
buf.WriteByte(' ')
|
||||
for k, cmd := range cmds {
|
||||
if k > 0 {
|
||||
buf.WriteByte('\n')
|
||||
}
|
||||
buf.WriteString(mapping.Repr(arg))
|
||||
var build strings.Builder
|
||||
for i, arg := range cmd.Args() {
|
||||
if i > 0 {
|
||||
build.WriteByte(' ')
|
||||
}
|
||||
build.WriteString(mapping.Repr(arg))
|
||||
}
|
||||
buf.WriteString(build.String())
|
||||
}
|
||||
logx.WithContext(ctx).WithDuration(duration).Slowf("[REDIS] slowcall on executing: %s", buf.String())
|
||||
}
|
||||
|
||||
@@ -91,10 +91,10 @@ func TestHookProcessPipelineCase1(t *testing.T) {
|
||||
log.SetOutput(&buf)
|
||||
defer log.SetOutput(writer)
|
||||
|
||||
ctx, err := durationHook.BeforeProcessPipeline(context.Background(), []red.Cmder{red.NewCmd(context.Background())})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx, err := durationHook.BeforeProcessPipeline(context.Background(), []red.Cmder{
|
||||
red.NewCmd(context.Background()),
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "redis", tracesdk.SpanFromContext(ctx).(interface{ Name() string }).Name())
|
||||
|
||||
assert.Nil(t, durationHook.AfterProcessPipeline(ctx, []red.Cmder{
|
||||
@@ -115,10 +115,10 @@ func TestHookProcessPipelineCase2(t *testing.T) {
|
||||
w, restore := injectLog()
|
||||
defer restore()
|
||||
|
||||
ctx, err := durationHook.BeforeProcessPipeline(context.Background(), []red.Cmder{red.NewCmd(context.Background())})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx, err := durationHook.BeforeProcessPipeline(context.Background(), []red.Cmder{
|
||||
red.NewCmd(context.Background()),
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "redis", tracesdk.SpanFromContext(ctx).(interface{ Name() string }).Name())
|
||||
|
||||
time.Sleep(slowThreshold.Load() + time.Millisecond)
|
||||
@@ -159,10 +159,28 @@ func TestHookProcessPipelineCase5(t *testing.T) {
|
||||
defer log.SetOutput(writer)
|
||||
|
||||
ctx := context.WithValue(context.Background(), startTimeKey, "foo")
|
||||
assert.Nil(t, durationHook.AfterProcessPipeline(ctx, []red.Cmder{red.NewCmd(context.Background())}))
|
||||
assert.Nil(t, durationHook.AfterProcessPipeline(ctx, []red.Cmder{
|
||||
red.NewCmd(context.Background()),
|
||||
}))
|
||||
assert.True(t, buf.Len() == 0)
|
||||
}
|
||||
|
||||
func TestLogDuration(t *testing.T) {
|
||||
w, restore := injectLog()
|
||||
defer restore()
|
||||
|
||||
logDuration(context.Background(), []red.Cmder{
|
||||
red.NewCmd(context.Background(), "get", "foo"),
|
||||
}, 1*time.Second)
|
||||
assert.True(t, strings.Contains(w.String(), "get foo"))
|
||||
|
||||
logDuration(context.Background(), []red.Cmder{
|
||||
red.NewCmd(context.Background(), "get", "foo"),
|
||||
red.NewCmd(context.Background(), "set", "bar", 0),
|
||||
}, 1*time.Second)
|
||||
assert.True(t, strings.Contains(w.String(), `get foo\nset bar 0`))
|
||||
}
|
||||
|
||||
func injectLog() (r *strings.Builder, restore func()) {
|
||||
var buf strings.Builder
|
||||
w := logx.NewWriter(&buf)
|
||||
|
||||
@@ -226,20 +226,7 @@ func (s *Redis) Blpop(node RedisNode, key string) (string, error) {
|
||||
// BlpopCtx uses passed in redis connection to execute blocking queries.
|
||||
// Doesn't benefit from pooling redis connections of blocking queries
|
||||
func (s *Redis) BlpopCtx(ctx context.Context, node RedisNode, key string) (string, error) {
|
||||
if node == nil {
|
||||
return "", ErrNilNode
|
||||
}
|
||||
|
||||
vals, err := node.BLPop(ctx, blockingQueryTimeout, key).Result()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if len(vals) < 2 {
|
||||
return "", fmt.Errorf("no value on key: %s", key)
|
||||
}
|
||||
|
||||
return vals[1], nil
|
||||
return s.BlpopWithTimeoutCtx(ctx, node, blockingQueryTimeout, key)
|
||||
}
|
||||
|
||||
// BlpopEx uses passed in redis connection to execute blpop command.
|
||||
@@ -267,6 +254,32 @@ func (s *Redis) BlpopExCtx(ctx context.Context, node RedisNode, key string) (str
|
||||
return vals[1], true, nil
|
||||
}
|
||||
|
||||
// BlpopWithTimeout uses passed in redis connection to execute blpop command.
|
||||
// Control blocking query timeout
|
||||
func (s *Redis) BlpopWithTimeout(node RedisNode, timeout time.Duration, key string) (string, error) {
|
||||
return s.BlpopWithTimeoutCtx(context.Background(), node, timeout, key)
|
||||
}
|
||||
|
||||
// BlpopWithTimeoutCtx uses passed in redis connection to execute blpop command.
|
||||
// Control blocking query timeout
|
||||
func (s *Redis) BlpopWithTimeoutCtx(ctx context.Context, node RedisNode, timeout time.Duration,
|
||||
key string) (string, error) {
|
||||
if node == nil {
|
||||
return "", ErrNilNode
|
||||
}
|
||||
|
||||
vals, err := node.BLPop(ctx, timeout, key).Result()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if len(vals) < 2 {
|
||||
return "", fmt.Errorf("no value on key: %s", key)
|
||||
}
|
||||
|
||||
return vals[1], nil
|
||||
}
|
||||
|
||||
// Decr is the implementation of redis decr command.
|
||||
func (s *Redis) Decr(key string) (int64, error) {
|
||||
return s.DecrCtx(context.Background(), key)
|
||||
|
||||
@@ -1117,6 +1117,17 @@ func TestRedisBlpopEx(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestRedisBlpopWithTimeout(t *testing.T) {
|
||||
runOnRedis(t, func(client *Redis) {
|
||||
client.Ping()
|
||||
var node mockedNode
|
||||
_, err := client.BlpopWithTimeout(nil, 10*time.Second, "foo")
|
||||
assert.NotNil(t, err)
|
||||
_, err = client.BlpopWithTimeout(node, 10*time.Second, "foo")
|
||||
assert.NotNil(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestRedisGeo(t *testing.T) {
|
||||
runOnRedis(t, func(client *Redis) {
|
||||
client.Ping()
|
||||
|
||||
@@ -8,7 +8,7 @@ var (
|
||||
metricReqDur = metric.NewHistogramVec(&metric.HistogramVecOpts{
|
||||
Namespace: namespace,
|
||||
Subsystem: "requests",
|
||||
Name: "durations_ms",
|
||||
Name: "duration_ms",
|
||||
Help: "mysql client requests duration(ms).",
|
||||
Labels: []string{"command"},
|
||||
Buckets: []float64{5, 10, 25, 50, 100, 250, 500, 1000, 2500},
|
||||
|
||||
@@ -27,15 +27,6 @@ func NewEventHandler(writer io.Writer, resolver jsonpb.AnyResolver) *EventHandle
|
||||
}
|
||||
}
|
||||
|
||||
func (h *EventHandler) OnResolveMethod(_ *desc.MethodDescriptor) {
|
||||
}
|
||||
|
||||
func (h *EventHandler) OnSendHeaders(_ metadata.MD) {
|
||||
}
|
||||
|
||||
func (h *EventHandler) OnReceiveHeaders(_ metadata.MD) {
|
||||
}
|
||||
|
||||
func (h *EventHandler) OnReceiveResponse(message proto.Message) {
|
||||
if err := h.marshaler.Marshal(h.writer, message); err != nil {
|
||||
logx.Error(err)
|
||||
@@ -45,3 +36,12 @@ func (h *EventHandler) OnReceiveResponse(message proto.Message) {
|
||||
func (h *EventHandler) OnReceiveTrailers(status *status.Status, _ metadata.MD) {
|
||||
h.Status = status
|
||||
}
|
||||
|
||||
func (h *EventHandler) OnResolveMethod(_ *desc.MethodDescriptor) {
|
||||
}
|
||||
|
||||
func (h *EventHandler) OnSendHeaders(_ metadata.MD) {
|
||||
}
|
||||
|
||||
func (h *EventHandler) OnReceiveHeaders(_ metadata.MD) {
|
||||
}
|
||||
|
||||
20
gateway/internal/eventhandler_test.go
Normal file
20
gateway/internal/eventhandler_test.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"io"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
func TestEventHandler(t *testing.T) {
|
||||
h := NewEventHandler(io.Discard, nil)
|
||||
h.OnResolveMethod(nil)
|
||||
h.OnSendHeaders(nil)
|
||||
h.OnReceiveHeaders(nil)
|
||||
h.OnReceiveTrailers(status.New(codes.OK, ""), nil)
|
||||
assert.Equal(t, codes.OK, h.Status.Code())
|
||||
h.OnReceiveResponse(nil)
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
@@ -8,13 +9,13 @@ import (
|
||||
)
|
||||
|
||||
func TestBuildHeadersNoValue(t *testing.T) {
|
||||
req := httptest.NewRequest("GET", "/", nil)
|
||||
req := httptest.NewRequest("GET", "/", http.NoBody)
|
||||
req.Header.Add("a", "b")
|
||||
assert.Nil(t, ProcessHeaders(req.Header))
|
||||
}
|
||||
|
||||
func TestBuildHeadersWithValues(t *testing.T) {
|
||||
req := httptest.NewRequest("GET", "/", nil)
|
||||
req := httptest.NewRequest("GET", "/", http.NoBody)
|
||||
req.Header.Add("grpc-metadata-a", "b")
|
||||
req.Header.Add("grpc-metadata-b", "b")
|
||||
assert.ElementsMatch(t, []string{"gateway-A:b", "gateway-B:b"}, ProcessHeaders(req.Header))
|
||||
|
||||
@@ -3,6 +3,7 @@ package internal
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/fullstorydev/grpcurl"
|
||||
@@ -22,16 +23,18 @@ func NewRequestParser(r *http.Request, resolver jsonpb.AnyResolver) (grpcurl.Req
|
||||
for k, v := range vars {
|
||||
params[k] = v
|
||||
}
|
||||
if len(params) == 0 {
|
||||
return grpcurl.NewJSONRequestParser(r.Body, resolver), nil
|
||||
}
|
||||
|
||||
if r.ContentLength == 0 {
|
||||
body, ok := getBody(r)
|
||||
if !ok {
|
||||
return buildJsonRequestParser(params, resolver)
|
||||
}
|
||||
|
||||
if len(params) == 0 {
|
||||
return grpcurl.NewJSONRequestParser(body, resolver), nil
|
||||
}
|
||||
|
||||
m := make(map[string]interface{})
|
||||
if err := json.NewDecoder(r.Body).Decode(&m); err != nil {
|
||||
if err := json.NewDecoder(body).Decode(&m); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -51,3 +54,28 @@ func buildJsonRequestParser(m map[string]interface{}, resolver jsonpb.AnyResolve
|
||||
|
||||
return grpcurl.NewJSONRequestParser(&buf, resolver), nil
|
||||
}
|
||||
|
||||
func getBody(r *http.Request) (io.Reader, bool) {
|
||||
if r.Body == nil {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
if r.ContentLength == 0 {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
if r.ContentLength > 0 {
|
||||
return r.Body, true
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
if _, err := io.Copy(&buf, r.Body); err != nil {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
if buf.Len() > 0 {
|
||||
return &buf, true
|
||||
}
|
||||
|
||||
return nil, false
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -10,14 +12,14 @@ import (
|
||||
)
|
||||
|
||||
func TestNewRequestParserNoVar(t *testing.T) {
|
||||
req := httptest.NewRequest("GET", "/", nil)
|
||||
req := httptest.NewRequest("GET", "/", http.NoBody)
|
||||
parser, err := NewRequestParser(req, nil)
|
||||
assert.Nil(t, err)
|
||||
assert.NotNil(t, parser)
|
||||
}
|
||||
|
||||
func TestNewRequestParserWithVars(t *testing.T) {
|
||||
req := httptest.NewRequest("GET", "/", nil)
|
||||
req := httptest.NewRequest("GET", "/", http.NoBody)
|
||||
req = pathvar.WithVars(req, map[string]string{"a": "b"})
|
||||
parser, err := NewRequestParser(req, nil)
|
||||
assert.Nil(t, err)
|
||||
@@ -31,6 +33,14 @@ func TestNewRequestParserNoVarWithBody(t *testing.T) {
|
||||
assert.NotNil(t, parser)
|
||||
}
|
||||
|
||||
func TestNewRequestParserWithNegativeContentLength(t *testing.T) {
|
||||
req := httptest.NewRequest("GET", "/", strings.NewReader(`{"a": "b"}`))
|
||||
req.ContentLength = -1
|
||||
parser, err := NewRequestParser(req, nil)
|
||||
assert.Nil(t, err)
|
||||
assert.NotNil(t, parser)
|
||||
}
|
||||
|
||||
func TestNewRequestParserWithVarsWithBody(t *testing.T) {
|
||||
req := httptest.NewRequest("GET", "/", strings.NewReader(`{"a": "b"}`))
|
||||
req = pathvar.WithVars(req, map[string]string{"c": "d"})
|
||||
@@ -53,3 +63,37 @@ func TestNewRequestParserWithForm(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
assert.NotNil(t, parser)
|
||||
}
|
||||
|
||||
func TestNewRequestParserWithNilBody(t *testing.T) {
|
||||
req := httptest.NewRequest("GET", "/val?a=b", http.NoBody)
|
||||
req.Body = nil
|
||||
parser, err := NewRequestParser(req, nil)
|
||||
assert.Nil(t, err)
|
||||
assert.NotNil(t, parser)
|
||||
}
|
||||
|
||||
func TestNewRequestParserWithBadBody(t *testing.T) {
|
||||
req := httptest.NewRequest("GET", "/val?a=b", badBody{})
|
||||
req.Body = badBody{}
|
||||
parser, err := NewRequestParser(req, nil)
|
||||
assert.Nil(t, err)
|
||||
assert.NotNil(t, parser)
|
||||
}
|
||||
|
||||
func TestNewRequestParserWithBadForm(t *testing.T) {
|
||||
req := httptest.NewRequest("GET", "/val?a%1=b", http.NoBody)
|
||||
parser, err := NewRequestParser(req, nil)
|
||||
assert.NotNil(t, err)
|
||||
assert.Nil(t, parser)
|
||||
}
|
||||
|
||||
func TestRequestParser_buildJsonRequestParser(t *testing.T) {
|
||||
parser, err := buildJsonRequestParser(map[string]interface{}{"a": make(chan int)}, nil)
|
||||
assert.NotNil(t, err)
|
||||
assert.Nil(t, parser)
|
||||
}
|
||||
|
||||
type badBody struct{}
|
||||
|
||||
func (badBody) Read([]byte) (int, error) { return 0, errors.New("something bad") }
|
||||
func (badBody) Close() error { return nil }
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -9,14 +10,14 @@ import (
|
||||
)
|
||||
|
||||
func TestGetTimeout(t *testing.T) {
|
||||
req := httptest.NewRequest("GET", "/", nil)
|
||||
req := httptest.NewRequest("GET", "/", http.NoBody)
|
||||
req.Header.Set(grpcTimeoutHeader, "1s")
|
||||
timeout := GetTimeout(req.Header, time.Second*5)
|
||||
assert.Equal(t, time.Second, timeout)
|
||||
}
|
||||
|
||||
func TestGetTimeoutDefault(t *testing.T) {
|
||||
req := httptest.NewRequest("GET", "/", nil)
|
||||
req := httptest.NewRequest("GET", "/", http.NoBody)
|
||||
timeout := GetTimeout(req.Header, time.Second*5)
|
||||
assert.Equal(t, time.Second*5, timeout)
|
||||
}
|
||||
|
||||
18
go.mod
18
go.mod
@@ -24,18 +24,18 @@ require (
|
||||
github.com/stretchr/testify v1.8.0
|
||||
go.etcd.io/etcd/api/v3 v3.5.5
|
||||
go.etcd.io/etcd/client/v3 v3.5.5
|
||||
go.mongodb.org/mongo-driver v1.10.2
|
||||
go.opentelemetry.io/otel v1.10.0
|
||||
go.opentelemetry.io/otel/exporters/jaeger v1.10.0
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.10.0
|
||||
go.opentelemetry.io/otel/exporters/zipkin v1.10.0
|
||||
go.opentelemetry.io/otel/sdk v1.10.0
|
||||
go.opentelemetry.io/otel/trace v1.10.0
|
||||
go.mongodb.org/mongo-driver v1.10.3
|
||||
go.opentelemetry.io/otel v1.11.0
|
||||
go.opentelemetry.io/otel/exporters/jaeger v1.11.0
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.11.0
|
||||
go.opentelemetry.io/otel/exporters/zipkin v1.11.0
|
||||
go.opentelemetry.io/otel/sdk v1.11.0
|
||||
go.opentelemetry.io/otel/trace v1.11.0
|
||||
go.uber.org/automaxprocs v1.5.1
|
||||
go.uber.org/goleak v1.2.0
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a
|
||||
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8
|
||||
golang.org/x/time v0.0.0-20220411224347-583f2d630306
|
||||
google.golang.org/grpc v1.49.0
|
||||
google.golang.org/grpc v1.50.1
|
||||
google.golang.org/protobuf v1.28.1
|
||||
gopkg.in/cheggaaa/pb.v1 v1.0.28
|
||||
gopkg.in/h2non/gock.v1 v1.1.2
|
||||
|
||||
44
go.sum
44
go.sum
@@ -476,31 +476,31 @@ go.etcd.io/etcd/client/pkg/v3 v3.5.5 h1:9S0JUVvmrVl7wCF39iTQthdaaNIiAaQbmK75ogO6
|
||||
go.etcd.io/etcd/client/pkg/v3 v3.5.5/go.mod h1:ggrwbk069qxpKPq8/FKkQ3Xq9y39kbFR4LnKszpRXeQ=
|
||||
go.etcd.io/etcd/client/v3 v3.5.5 h1:q++2WTJbUgpQu4B6hCuT7VkdwaTP7Qz6Daak3WzbrlI=
|
||||
go.etcd.io/etcd/client/v3 v3.5.5/go.mod h1:aApjR4WGlSumpnJ2kloS75h6aHUmAyaPLjHMxpc7E7c=
|
||||
go.mongodb.org/mongo-driver v1.10.2 h1:4Wk3cnqOrQCn0P92L3/mmurMxzdvWWs5J9jinAVKD+k=
|
||||
go.mongodb.org/mongo-driver v1.10.2/go.mod h1:z4XpeoU6w+9Vht+jAFyLgVrD+jGSQQe0+CBWFHNiHt8=
|
||||
go.mongodb.org/mongo-driver v1.10.3 h1:XDQEvmh6z1EUsXuIkXE9TaVeqHw6SwS1uf93jFs0HBA=
|
||||
go.mongodb.org/mongo-driver v1.10.3/go.mod h1:z4XpeoU6w+9Vht+jAFyLgVrD+jGSQQe0+CBWFHNiHt8=
|
||||
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
|
||||
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
|
||||
go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
|
||||
go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
|
||||
go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
|
||||
go.opentelemetry.io/otel v1.7.0/go.mod h1:5BdUoMIz5WEs0vt0CUEMtSSaTSHBBVwrhnz7+nrD5xk=
|
||||
go.opentelemetry.io/otel v1.10.0 h1:Y7DTJMR6zs1xkS/upamJYk0SxxN4C9AqRd77jmZnyY4=
|
||||
go.opentelemetry.io/otel v1.10.0/go.mod h1:NbvWjCthWHKBEUMpf0/v8ZRZlni86PpGFEMA9pnQSnQ=
|
||||
go.opentelemetry.io/otel/exporters/jaeger v1.10.0 h1:7W3aVVjEYayu/GOqOVF4mbTvnCuxF1wWu3eRxFGQXvw=
|
||||
go.opentelemetry.io/otel/exporters/jaeger v1.10.0/go.mod h1:n9IGyx0fgyXXZ/i0foLHNxtET9CzXHzZeKCucvRBFgA=
|
||||
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.10.0 h1:TaB+1rQhddO1sF71MpZOZAuSPW1klK2M8XxfrBMfK7Y=
|
||||
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.10.0/go.mod h1:78XhIg8Ht9vR4tbLNUhXsiOnE2HOuSeKAiAcoVQEpOY=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.10.0 h1:pDDYmo0QadUPal5fwXoY1pmMpFcdyhXOmL5drCrI3vU=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.10.0/go.mod h1:Krqnjl22jUJ0HgMzw5eveuCvFDXY4nSYb4F8t5gdrag=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.10.0 h1:KtiUEhQmj/Pa874bVYKGNVdq8NPKiacPbaRRtgXi+t4=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.10.0/go.mod h1:OfUCyyIiDvNXHWpcWgbF+MWvqPZiNa3YDEnivcnYsV0=
|
||||
go.opentelemetry.io/otel/exporters/zipkin v1.10.0 h1:HcPAFsFpEBKF+G5NIOA+gBsxifd3Ej+wb+KsdBLa15E=
|
||||
go.opentelemetry.io/otel/exporters/zipkin v1.10.0/go.mod h1:HdfvgwcOoCB0+zzrTHycW6btjK0zNpkz2oTGO815SCI=
|
||||
go.opentelemetry.io/otel/sdk v1.10.0 h1:jZ6K7sVn04kk/3DNUdJ4mqRlGDiXAVuIG+MMENpTNdY=
|
||||
go.opentelemetry.io/otel/sdk v1.10.0/go.mod h1:vO06iKzD5baltJz1zarxMCNHFpUlUiOy4s65ECtn6kE=
|
||||
go.opentelemetry.io/otel v1.11.0 h1:kfToEGMDq6TrVrJ9Vht84Y8y9enykSZzDDZglV0kIEk=
|
||||
go.opentelemetry.io/otel v1.11.0/go.mod h1:H2KtuEphyMvlhZ+F7tg9GRhAOe60moNx61Ex+WmiKkk=
|
||||
go.opentelemetry.io/otel/exporters/jaeger v1.11.0 h1:Sv2valcFfMlfu6g8USSS+ZUN5vwbuGj1aY/CFtMG33w=
|
||||
go.opentelemetry.io/otel/exporters/jaeger v1.11.0/go.mod h1:nRgyJbgJ0hmaUdHwyDpTTfBYz61cTTeeGhVzfQc+FsI=
|
||||
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.11.0 h1:0dly5et1i/6Th3WHn0M6kYiJfFNzhhxanrJ0bOfnjEo=
|
||||
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.11.0/go.mod h1:+Lq4/WkdCkjbGcBMVHHg2apTbv8oMBf29QCnyCCJjNQ=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.11.0 h1:eyJ6njZmH16h9dOKCi7lMswAnGsSOwgTqWzfxqcuNr8=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.11.0/go.mod h1:FnDp7XemjN3oZ3xGunnfOUTVwd2XcvLbtRAuOSU3oc8=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.11.0 h1:j2RFV0Qdt38XQ2Jvi4WIsQ56w8T7eSirYbMw19VXRDg=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.11.0/go.mod h1:pILgiTEtrqvZpoiuGdblDgS5dbIaTgDrkIuKfEFkt+A=
|
||||
go.opentelemetry.io/otel/exporters/zipkin v1.11.0 h1:v/Abo5REOWrCj4zcEIUHFZtXpsCVjrwZj28iyX2rHXE=
|
||||
go.opentelemetry.io/otel/exporters/zipkin v1.11.0/go.mod h1:unWnsLCMYfINP8ue0aXVrB/GYHoXNn/lbTnupvLekGQ=
|
||||
go.opentelemetry.io/otel/sdk v1.11.0 h1:ZnKIL9V9Ztaq+ME43IUi/eo22mNsb6a7tGfzaOWB5fo=
|
||||
go.opentelemetry.io/otel/sdk v1.11.0/go.mod h1:REusa8RsyKaq0OlyangWXaw97t2VogoO4SSEeKkSTAk=
|
||||
go.opentelemetry.io/otel/trace v1.7.0/go.mod h1:fzLSB9nqR2eXzxPXb2JW9IKE+ScyXA48yyE4TNvoHqU=
|
||||
go.opentelemetry.io/otel/trace v1.10.0 h1:npQMbR8o7mum8uF95yFbOEJffhs1sbCOfDh8zAJiH5E=
|
||||
go.opentelemetry.io/otel/trace v1.10.0/go.mod h1:Sij3YYczqAdz+EhmGhE6TpTxUO5/F/AzrK+kxfGqySM=
|
||||
go.opentelemetry.io/otel/trace v1.11.0 h1:20U/Vj42SX+mASlXLmSGBg6jpI1jQtv682lZtTAOVFI=
|
||||
go.opentelemetry.io/otel/trace v1.11.0/go.mod h1:nyYjis9jy0gytE9LXGU+/m1sHTKbRY0fX0hulNNDP1U=
|
||||
go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI=
|
||||
go.opentelemetry.io/proto/otlp v0.19.0 h1:IVN6GR+mhC4s5yfcTbmzHYODqvWAp3ZedA2SJPI1Nnw=
|
||||
go.opentelemetry.io/proto/otlp v0.19.0/go.mod h1:H7XAot3MsfNsj7EXtrA2q5xSNQ10UqI405h3+duxN4U=
|
||||
@@ -677,7 +677,6 @@ golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7w
|
||||
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
@@ -687,8 +686,9 @@ golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBc
|
||||
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/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-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k=
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 h1:h+EGohizhe9XlX18rfpa8k8RAc5XyaeamM+0VHRd4lc=
|
||||
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
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/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
@@ -838,8 +838,8 @@ google.golang.org/grpc v1.41.0/go.mod h1:U3l9uK9J0sini8mHphKoXyaqDA/8VyGnDee1zzI
|
||||
google.golang.org/grpc v1.42.0/go.mod h1:k+4IHHFw41K8+bbowsex27ge2rCb65oeWqe4jJ590SU=
|
||||
google.golang.org/grpc v1.46.2/go.mod h1:vN9eftEi1UMyUsIF80+uQXhHjbXYbm0uXoFCACuMGWk=
|
||||
google.golang.org/grpc v1.48.0/go.mod h1:vN9eftEi1UMyUsIF80+uQXhHjbXYbm0uXoFCACuMGWk=
|
||||
google.golang.org/grpc v1.49.0 h1:WTLtQzmQori5FUH25Pq4WT22oCsv8USpQ+F6rqtsmxw=
|
||||
google.golang.org/grpc v1.49.0/go.mod h1:ZgQEeidpAuNRZ8iRrlBKXZQP1ghovWIVhdJRyCDK+GI=
|
||||
google.golang.org/grpc v1.50.1 h1:DS/BukOZWp8s6p4Dt/tOaJaTQyPyOoCcrjroHuCeLzY=
|
||||
google.golang.org/grpc v1.50.1/go.mod h1:ZgQEeidpAuNRZ8iRrlBKXZQP1ghovWIVhdJRyCDK+GI=
|
||||
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
|
||||
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
|
||||
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
|
||||
|
||||
69
readme.md
69
readme.md
@@ -1,8 +1,11 @@
|
||||
<img align="right" width="150px" src="https://raw.githubusercontent.com/zeromicro/zero-doc/main/doc/images/go-zero.png">
|
||||
|
||||
# go-zero
|
||||
<p align="center">
|
||||
<img align="center" width="150px" src="https://raw.githubusercontent.com/zeromicro/zero-doc/main/doc/images/go-zero.png">
|
||||
</p>
|
||||
|
||||
English | [简体中文](readme-cn.md)
|
||||
go-zero is a web and rpc framework with lots of builtin engineering practices. It’s born to ensure the stability of the busy services with resilience design and has been serving sites with tens of millions of users for years.
|
||||
|
||||
<div align=center>
|
||||
|
||||
[](https://github.com/zeromicro/go-zero/actions)
|
||||
[](https://codecov.io/gh/zeromicro/go-zero)
|
||||
@@ -13,23 +16,20 @@ English | [简体中文](readme-cn.md)
|
||||
[](https://opensource.org/licenses/MIT)
|
||||
[](https://discord.gg/4JQvC5A4Fe)
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
## 🤷 What is go-zero?
|
||||
English | [简体中文](readme-cn.md)
|
||||
|
||||
<a href="https://www.producthunt.com/posts/go-zero?utm_source=badge-featured&utm_medium=badge&utm_souce=badge-go-zero" target="_blank"><img src="https://api.producthunt.com/widgets/embed-image/v1/featured.svg?post_id=334030&theme=light" alt="go-zero - A web & rpc framework written in Go. | Product Hunt" style="width: 250px; height: 54px;" width="250" height="54" /></a>
|
||||
|
||||
> ***Important!***
|
||||
>
|
||||
> To upgrade from versions eariler than v1.3.0, run the following commands.
|
||||
>
|
||||
> `go install github.com/zeromicro/go-zero/tools/goctl@latest`
|
||||
>
|
||||
> `goctl migrate —verbose —version v1.4.0`
|
||||
|
||||
## 0. what is go-zero
|
||||
|
||||
go-zero (listed in CNCF Landscape: [https://landscape.cncf.io/?selected=go-zero](https://landscape.cncf.io/?selected=go-zero)) is a web and rpc framework with lots of builtin engineering practices. It’s born to ensure the stability of the busy services with resilience design and has been serving sites with tens of millions of users for years.
|
||||
|
||||
go-zero contains simple API description syntax and code generation tool called `goctl`. You can generate Go, iOS, Android, Kotlin, Dart, TypeScript, JavaScript from .api files with `goctl`.
|
||||
|
||||
Advantages of go-zero:
|
||||
#### Advantages of go-zero:
|
||||
|
||||
* improve the stability of the services with tens of millions of daily active users
|
||||
* builtin chained timeout control, concurrency control, rate limit, adaptive circuit breaker, adaptive load shedding, even no configuration needed
|
||||
@@ -40,7 +40,7 @@ Advantages of go-zero:
|
||||
|
||||
<img src="https://raw.githubusercontent.com/zeromicro/zero-doc/main/doc/images/architecture-en.png" alt="Architecture" width="1500" />
|
||||
|
||||
## 1. Backgrounds of go-zero
|
||||
## Backgrounds of go-zero
|
||||
|
||||
At the beginning of 2018, we decided to re-design our system, from monolithic architecture with Java+MongoDB to microservice architecture. After research and comparison, we chose to:
|
||||
|
||||
@@ -55,7 +55,7 @@ At the beginning of 2018, we decided to re-design our system, from monolithic ar
|
||||
* easy to locate the problems
|
||||
* easy to extend the features
|
||||
|
||||
## 2. Design considerations on go-zero
|
||||
## Design considerations on go-zero
|
||||
|
||||
By designing the microservice architecture, we expected to ensure stability, as well as productivity. And from just the beginning, we have the following design principles:
|
||||
|
||||
@@ -69,7 +69,7 @@ By designing the microservice architecture, we expected to ensure stability, as
|
||||
|
||||
After almost half a year, we finished the transfer from a monolithic system to microservice system and deployed on August 2018. The new system guaranteed business growth and system stability.
|
||||
|
||||
## 3. The implementation and features of go-zero
|
||||
## The implementation and features of go-zero
|
||||
|
||||
go-zero is a web and rpc framework that integrates lots of engineering practices. The features are mainly listed below:
|
||||
|
||||
@@ -91,27 +91,38 @@ As below, go-zero protects the system with a couple of layers and mechanisms:
|
||||
|
||||

|
||||
|
||||
## 4. The simplified architecture that we use with go-zero
|
||||
## The simplified architecture that we use with go-zero
|
||||
|
||||
<img width="1067" alt="image" src="https://user-images.githubusercontent.com/1918356/171880372-5010d846-e8b1-4942-8fe2-e2bbb584f762.png">
|
||||
|
||||
## 5. Installation
|
||||
## Installation
|
||||
|
||||
Run the following command under your project:
|
||||
|
||||
```shell
|
||||
go get -u github.com/zeromicro/go-zero
|
||||
```
|
||||
## Upgrade
|
||||
|
||||
## 6. Quick Start
|
||||
To upgrade from versions eariler than v1.3.0, run the following commands.
|
||||
|
||||
0. full examples can be checked out from below:
|
||||
```shell
|
||||
go install github.com/zeromicro/go-zero/tools/goctl@latest
|
||||
```
|
||||
|
||||
```shell
|
||||
goctl migrate —verbose —version v1.4.0
|
||||
```
|
||||
|
||||
## Quick Start
|
||||
|
||||
1. full examples can be checked out from below:
|
||||
|
||||
[Rapid development of microservice systems](https://github.com/zeromicro/zero-doc/blob/main/doc/shorturl-en.md)
|
||||
|
||||
[Rapid development of microservice systems - multiple RPCs](https://github.com/zeromicro/zero-doc/blob/main/docs/zero/bookstore-en.md)
|
||||
|
||||
1. install goctl
|
||||
2. install goctl
|
||||
|
||||
`goctl`can be read as `go control`. `goctl` means not to be controlled by code, instead, we control it. The inside `go` is not `golang`. At the very beginning, I was expecting it to help us improve productivity, and make our lives easier.
|
||||
|
||||
@@ -138,7 +149,7 @@ go get -u github.com/zeromicro/go-zero
|
||||
|
||||
make sure goctl is executable.
|
||||
|
||||
2. create the API file, like greet.api, you can install the plugin of goctl in vs code, api syntax is supported.
|
||||
3. create the API file, like greet.api, you can install the plugin of goctl in vs code, api syntax is supported.
|
||||
|
||||
```go
|
||||
type (
|
||||
@@ -163,7 +174,7 @@ go get -u github.com/zeromicro/go-zero
|
||||
goctl api -o greet.api
|
||||
```
|
||||
|
||||
3. generate the go server-side code
|
||||
4. generate the go server-side code
|
||||
|
||||
```shell
|
||||
goctl api go -api greet.api -dir greet
|
||||
@@ -216,12 +227,12 @@ go get -u github.com/zeromicro/go-zero
|
||||
Content-Length: 0
|
||||
```
|
||||
|
||||
4. Write the business logic code
|
||||
5. Write the business logic code
|
||||
|
||||
* the dependencies can be passed into the logic within servicecontext.go, like mysql, reds, etc.
|
||||
* add the logic code in a logic package according to .api file
|
||||
|
||||
5. Generate code like Java, TypeScript, Dart, JavaScript, etc. just from the api file
|
||||
6. Generate code like Java, TypeScript, Dart, JavaScript, etc. just from the api file
|
||||
|
||||
```shell
|
||||
goctl api java -api greet.api -dir greet
|
||||
@@ -229,24 +240,24 @@ go get -u github.com/zeromicro/go-zero
|
||||
...
|
||||
```
|
||||
|
||||
## 7. Benchmark
|
||||
## Benchmark
|
||||
|
||||

|
||||
|
||||
[Checkout the test code](https://github.com/smallnest/go-web-framework-benchmark)
|
||||
|
||||
## 8. Documents
|
||||
## Documents
|
||||
|
||||
* [Documents](https://go-zero.dev/)
|
||||
* [Rapid development of microservice systems](https://github.com/zeromicro/zero-doc/blob/main/doc/shorturl-en.md)
|
||||
* [Rapid development of microservice systems - multiple RPCs](https://github.com/zeromicro/zero-doc/blob/main/docs/zero/bookstore-en.md)
|
||||
* [Examples](https://github.com/zeromicro/zero-examples)
|
||||
|
||||
## 9. Chat group
|
||||
## Chat group
|
||||
|
||||
Join the chat via https://discord.gg/4JQvC5A4Fe
|
||||
|
||||
## 10. Cloud Native Landscape
|
||||
## Cloud Native Landscape
|
||||
|
||||
<p float="left">
|
||||
<img src="https://landscape.cncf.io/images/left-logo.svg" width="150"/>
|
||||
|
||||
@@ -86,7 +86,7 @@ func TestThenOrdersHandlersCorrectly(t *testing.T) {
|
||||
chained := New(t1, t2, t3).Then(testApp)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
r, err := http.NewRequest("GET", "/", nil)
|
||||
r, err := http.NewRequest("GET", "/", http.NoBody)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -103,7 +103,7 @@ func TestAppendAddsHandlersCorrectly(t *testing.T) {
|
||||
h := c.Then(testApp)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
r, err := http.NewRequest("GET", "/", nil)
|
||||
r, err := http.NewRequest("GET", "/", http.NoBody)
|
||||
assert.Nil(t, err)
|
||||
|
||||
h.ServeHTTP(w, r)
|
||||
|
||||
@@ -237,8 +237,8 @@ func TestEngine_notFoundHandler(t *testing.T) {
|
||||
defer ts.Close()
|
||||
|
||||
client := ts.Client()
|
||||
err := func(ctx context.Context) error {
|
||||
req, err := http.NewRequest("GET", ts.URL+"/bad", nil)
|
||||
err := func(_ context.Context) error {
|
||||
req, err := http.NewRequest("GET", ts.URL+"/bad", http.NoBody)
|
||||
assert.Nil(t, err)
|
||||
res, err := client.Do(req)
|
||||
assert.Nil(t, err)
|
||||
@@ -260,8 +260,8 @@ func TestEngine_notFoundHandlerNotNil(t *testing.T) {
|
||||
defer ts.Close()
|
||||
|
||||
client := ts.Client()
|
||||
err := func(ctx context.Context) error {
|
||||
req, err := http.NewRequest("GET", ts.URL+"/bad", nil)
|
||||
err := func(_ context.Context) error {
|
||||
req, err := http.NewRequest("GET", ts.URL+"/bad", http.NoBody)
|
||||
assert.Nil(t, err)
|
||||
res, err := client.Do(req)
|
||||
assert.Nil(t, err)
|
||||
@@ -285,8 +285,8 @@ func TestEngine_notFoundHandlerNotNilWriteHeader(t *testing.T) {
|
||||
defer ts.Close()
|
||||
|
||||
client := ts.Client()
|
||||
err := func(ctx context.Context) error {
|
||||
req, err := http.NewRequest("GET", ts.URL+"/bad", nil)
|
||||
err := func(_ context.Context) error {
|
||||
req, err := http.NewRequest("GET", ts.URL+"/bad", http.NoBody)
|
||||
assert.Nil(t, err)
|
||||
res, err := client.Do(req)
|
||||
assert.Nil(t, err)
|
||||
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
)
|
||||
|
||||
func TestAuthHandlerFailed(t *testing.T) {
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
|
||||
handler := Authorize("B63F477D-BBA3-4E52-96D3-C0034C27694A", WithUnauthorizedCallback(
|
||||
func(w http.ResponseWriter, r *http.Request, err error) {
|
||||
assert.NotNil(t, err)
|
||||
@@ -33,7 +33,7 @@ func TestAuthHandlerFailed(t *testing.T) {
|
||||
|
||||
func TestAuthHandler(t *testing.T) {
|
||||
const key = "B63F477D-BBA3-4E52-96D3-C0034C27694A"
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
|
||||
token, err := buildToken(key, map[string]interface{}{
|
||||
"key": "value",
|
||||
}, 3600)
|
||||
@@ -62,7 +62,7 @@ func TestAuthHandlerWithPrevSecret(t *testing.T) {
|
||||
key = "14F17379-EB8F-411B-8F12-6929002DCA76"
|
||||
prevKey = "B63F477D-BBA3-4E52-96D3-C0034C27694A"
|
||||
)
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
|
||||
token, err := buildToken(key, map[string]interface{}{
|
||||
"key": "value",
|
||||
}, 3600)
|
||||
@@ -83,7 +83,7 @@ func TestAuthHandlerWithPrevSecret(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestAuthHandler_NilError(t *testing.T) {
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
|
||||
resp := httptest.NewRecorder()
|
||||
assert.NotPanics(t, func() {
|
||||
unauthorized(resp, req, nil, nil)
|
||||
|
||||
@@ -25,7 +25,7 @@ func TestBreakerHandlerAccept(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
}))
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
|
||||
req.Header.Set("X-Test", "test")
|
||||
resp := httptest.NewRecorder()
|
||||
handler.ServeHTTP(resp, req)
|
||||
@@ -41,7 +41,7 @@ func TestBreakerHandlerFail(t *testing.T) {
|
||||
w.WriteHeader(http.StatusBadGateway)
|
||||
}))
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
|
||||
resp := httptest.NewRecorder()
|
||||
handler.ServeHTTP(resp, req)
|
||||
assert.Equal(t, http.StatusBadGateway, resp.Code)
|
||||
@@ -55,7 +55,7 @@ func TestBreakerHandler_4XX(t *testing.T) {
|
||||
}))
|
||||
|
||||
for i := 0; i < 1000; i++ {
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
|
||||
resp := httptest.NewRecorder()
|
||||
handler.ServeHTTP(resp, req)
|
||||
}
|
||||
@@ -63,7 +63,7 @@ func TestBreakerHandler_4XX(t *testing.T) {
|
||||
const tries = 100
|
||||
var pass int
|
||||
for i := 0; i < tries; i++ {
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
|
||||
resp := httptest.NewRecorder()
|
||||
handler.ServeHTTP(resp, req)
|
||||
if resp.Code == http.StatusBadRequest {
|
||||
@@ -82,14 +82,14 @@ func TestBreakerHandlerReject(t *testing.T) {
|
||||
}))
|
||||
|
||||
for i := 0; i < 1000; i++ {
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
|
||||
resp := httptest.NewRecorder()
|
||||
handler.ServeHTTP(resp, req)
|
||||
}
|
||||
|
||||
var drops int
|
||||
for i := 0; i < 100; i++ {
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
|
||||
resp := httptest.NewRecorder()
|
||||
handler.ServeHTTP(resp, req)
|
||||
if resp.Code == http.StatusServiceUnavailable {
|
||||
|
||||
@@ -26,7 +26,7 @@ func init() {
|
||||
}
|
||||
|
||||
func TestCryptionHandlerGet(t *testing.T) {
|
||||
req := httptest.NewRequest(http.MethodGet, "/any", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "/any", http.NoBody)
|
||||
handler := CryptionHandler(aesKey)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
_, err := w.Write([]byte(respText))
|
||||
w.Header().Set("X-Test", "test")
|
||||
@@ -80,7 +80,7 @@ func TestCryptionHandlerPostBadEncryption(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCryptionHandlerWriteHeader(t *testing.T) {
|
||||
req := httptest.NewRequest(http.MethodGet, "/any", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "/any", http.NoBody)
|
||||
handler := CryptionHandler(aesKey)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
}))
|
||||
@@ -90,7 +90,7 @@ func TestCryptionHandlerWriteHeader(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCryptionHandlerFlush(t *testing.T) {
|
||||
req := httptest.NewRequest(http.MethodGet, "/any", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "/any", http.NoBody)
|
||||
handler := CryptionHandler(aesKey)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte(respText))
|
||||
flusher, ok := w.(http.Flusher)
|
||||
|
||||
@@ -170,9 +170,9 @@ func logBrief(r *http.Request, code int, timer *utils.ElapsedTimer, logs *intern
|
||||
buf.WriteString(fmt.Sprintf("[HTTP] %s - %s %s - %s - %s",
|
||||
wrapStatusCode(code), wrapMethod(r.Method), r.RequestURI, httpx.GetRemoteAddr(r), r.UserAgent()))
|
||||
if duration > slowThreshold.Load() {
|
||||
logger.Slowf("[HTTP] %s - %s - %s %s - %s - slowcall(%s)",
|
||||
logger.Slowf("[HTTP] %s - %s %s - %s - %s - slowcall(%s)",
|
||||
wrapStatusCode(code), wrapMethod(r.Method), r.RequestURI, httpx.GetRemoteAddr(r), r.UserAgent(),
|
||||
fmt.Sprintf("slowcall(%s)", timex.ReprOfDuration(duration)))
|
||||
timex.ReprOfDuration(duration))
|
||||
}
|
||||
|
||||
ok := isOkResponse(code)
|
||||
|
||||
@@ -24,7 +24,7 @@ func TestLogHandler(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, logHandler := range handlers {
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
|
||||
handler := logHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
r.Context().Value(internal.LogContext).(*internal.LogCollector).Append("anything")
|
||||
w.Header().Set("X-Test", "test")
|
||||
@@ -79,7 +79,7 @@ func TestLogHandlerSlow(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, logHandler := range handlers {
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
|
||||
handler := logHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
time.Sleep(defaultSlowThreshold + time.Millisecond*50)
|
||||
}))
|
||||
@@ -159,7 +159,7 @@ func TestWrapStatusCodeWithColor(t *testing.T) {
|
||||
func BenchmarkLogHandler(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
|
||||
handler := LogHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
|
||||
@@ -32,13 +32,13 @@ func TestMaxConnsHandler(t *testing.T) {
|
||||
|
||||
for i := 0; i < conns; i++ {
|
||||
go func() {
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
|
||||
handler.ServeHTTP(httptest.NewRecorder(), req)
|
||||
}()
|
||||
}
|
||||
|
||||
waitGroup.Wait()
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
|
||||
resp := httptest.NewRecorder()
|
||||
handler.ServeHTTP(resp, req)
|
||||
assert.Equal(t, http.StatusServiceUnavailable, resp.Code)
|
||||
@@ -65,14 +65,14 @@ func TestWithoutMaxConnsHandler(t *testing.T) {
|
||||
|
||||
for i := 0; i < conns; i++ {
|
||||
go func() {
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
|
||||
req.Header.Set(key, value)
|
||||
handler.ServeHTTP(httptest.NewRecorder(), req)
|
||||
}()
|
||||
}
|
||||
|
||||
waitGroup.Wait()
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
|
||||
resp := httptest.NewRecorder()
|
||||
handler.ServeHTTP(resp, req)
|
||||
assert.Equal(t, http.StatusOK, resp.Code)
|
||||
|
||||
@@ -16,7 +16,7 @@ func TestMetricHandler(t *testing.T) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
|
||||
resp := httptest.NewRecorder()
|
||||
handler.ServeHTTP(resp, req)
|
||||
assert.Equal(t, http.StatusOK, resp.Code)
|
||||
|
||||
@@ -15,7 +15,7 @@ func TestPromMetricHandler_Disabled(t *testing.T) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
|
||||
resp := httptest.NewRecorder()
|
||||
handler.ServeHTTP(resp, req)
|
||||
assert.Equal(t, http.StatusOK, resp.Code)
|
||||
@@ -31,7 +31,7 @@ func TestPromMetricHandler_Enabled(t *testing.T) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
|
||||
resp := httptest.NewRecorder()
|
||||
handler.ServeHTTP(resp, req)
|
||||
assert.Equal(t, http.StatusOK, resp.Code)
|
||||
|
||||
@@ -19,7 +19,7 @@ func TestWithPanic(t *testing.T) {
|
||||
panic("whatever")
|
||||
}))
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
|
||||
resp := httptest.NewRecorder()
|
||||
handler.ServeHTTP(resp, req)
|
||||
assert.Equal(t, http.StatusInternalServerError, resp.Code)
|
||||
@@ -29,7 +29,7 @@ func TestWithoutPanic(t *testing.T) {
|
||||
handler := RecoverHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
}))
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
|
||||
resp := httptest.NewRecorder()
|
||||
handler.ServeHTTP(resp, req)
|
||||
assert.Equal(t, http.StatusOK, resp.Code)
|
||||
|
||||
@@ -28,7 +28,7 @@ func TestSheddingHandlerAccept(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
}))
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
|
||||
req.Header.Set("X-Test", "test")
|
||||
resp := httptest.NewRecorder()
|
||||
handler.ServeHTTP(resp, req)
|
||||
@@ -47,7 +47,7 @@ func TestSheddingHandlerFail(t *testing.T) {
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
}))
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
|
||||
resp := httptest.NewRecorder()
|
||||
handler.ServeHTTP(resp, req)
|
||||
assert.Equal(t, http.StatusServiceUnavailable, resp.Code)
|
||||
@@ -63,7 +63,7 @@ func TestSheddingHandlerReject(t *testing.T) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
|
||||
resp := httptest.NewRecorder()
|
||||
handler.ServeHTTP(resp, req)
|
||||
assert.Equal(t, http.StatusServiceUnavailable, resp.Code)
|
||||
@@ -76,7 +76,7 @@ func TestSheddingHandlerNoShedding(t *testing.T) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
|
||||
resp := httptest.NewRecorder()
|
||||
handler.ServeHTTP(resp, req)
|
||||
assert.Equal(t, http.StatusOK, resp.Code)
|
||||
|
||||
@@ -22,7 +22,7 @@ func TestTimeout(t *testing.T) {
|
||||
time.Sleep(time.Minute)
|
||||
}))
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
|
||||
resp := httptest.NewRecorder()
|
||||
handler.ServeHTTP(resp, req)
|
||||
assert.Equal(t, http.StatusServiceUnavailable, resp.Code)
|
||||
@@ -34,7 +34,7 @@ func TestWithinTimeout(t *testing.T) {
|
||||
time.Sleep(time.Millisecond)
|
||||
}))
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
|
||||
resp := httptest.NewRecorder()
|
||||
handler.ServeHTTP(resp, req)
|
||||
assert.Equal(t, http.StatusOK, resp.Code)
|
||||
@@ -48,7 +48,7 @@ func TestWithTimeoutTimedout(t *testing.T) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
|
||||
resp := httptest.NewRecorder()
|
||||
handler.ServeHTTP(resp, req)
|
||||
assert.Equal(t, http.StatusServiceUnavailable, resp.Code)
|
||||
@@ -60,7 +60,7 @@ func TestWithoutTimeout(t *testing.T) {
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
}))
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
|
||||
resp := httptest.NewRecorder()
|
||||
handler.ServeHTTP(resp, req)
|
||||
assert.Equal(t, http.StatusOK, resp.Code)
|
||||
@@ -72,7 +72,7 @@ func TestTimeoutPanic(t *testing.T) {
|
||||
panic("foo")
|
||||
}))
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
|
||||
resp := httptest.NewRecorder()
|
||||
assert.Panics(t, func() {
|
||||
handler.ServeHTTP(resp, req)
|
||||
@@ -85,7 +85,7 @@ func TestTimeoutWebsocket(t *testing.T) {
|
||||
time.Sleep(time.Millisecond * 10)
|
||||
}))
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
|
||||
req.Header.Set(headerUpgrade, valueWebsocket)
|
||||
resp := httptest.NewRecorder()
|
||||
handler.ServeHTTP(resp, req)
|
||||
@@ -100,7 +100,7 @@ func TestTimeoutWroteHeaderTwice(t *testing.T) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
|
||||
resp := httptest.NewRecorder()
|
||||
handler.ServeHTTP(resp, req)
|
||||
assert.Equal(t, http.StatusOK, resp.Code)
|
||||
@@ -112,7 +112,7 @@ func TestTimeoutWriteBadCode(t *testing.T) {
|
||||
w.WriteHeader(1000)
|
||||
}))
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
|
||||
resp := httptest.NewRecorder()
|
||||
assert.Panics(t, func() {
|
||||
handler.ServeHTTP(resp, req)
|
||||
@@ -125,7 +125,7 @@ func TestTimeoutClientClosed(t *testing.T) {
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
}))
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
req = req.WithContext(ctx)
|
||||
cancel()
|
||||
|
||||
@@ -2,7 +2,9 @@ package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/lang"
|
||||
"github.com/zeromicro/go-zero/core/trace"
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/propagation"
|
||||
@@ -10,6 +12,13 @@ import (
|
||||
oteltrace "go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
var notTracingSpans sync.Map
|
||||
|
||||
// DontTraceSpan disable tracing for the specified span name.
|
||||
func DontTraceSpan(spanName string) {
|
||||
notTracingSpans.Store(spanName, lang.Placeholder)
|
||||
}
|
||||
|
||||
// TracingHandler return a middleware that process the opentelemetry.
|
||||
func TracingHandler(serviceName, path string) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
@@ -17,11 +26,20 @@ func TracingHandler(serviceName, path string) func(http.Handler) http.Handler {
|
||||
tracer := otel.GetTracerProvider().Tracer(trace.TraceName)
|
||||
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
defer func() {
|
||||
next.ServeHTTP(w, r)
|
||||
}()
|
||||
|
||||
ctx := propagator.Extract(r.Context(), propagation.HeaderCarrier(r.Header))
|
||||
spanName := path
|
||||
if len(spanName) == 0 {
|
||||
spanName = r.URL.Path
|
||||
}
|
||||
|
||||
if _, ok := notTracingSpans.Load(spanName); ok {
|
||||
return
|
||||
}
|
||||
|
||||
spanCtx, span := tracer.Start(
|
||||
ctx,
|
||||
spanName,
|
||||
@@ -33,7 +51,7 @@ func TracingHandler(serviceName, path string) func(http.Handler) http.Handler {
|
||||
|
||||
// convenient for tracking error messages
|
||||
propagator.Inject(spanCtx, propagation.HeaderCarrier(w.Header()))
|
||||
next.ServeHTTP(w, r.WithContext(spanCtx))
|
||||
r = r.WithContext(spanCtx)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,3 +51,46 @@ func TestOtelHandler(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDontTracingSpanName(t *testing.T) {
|
||||
ztrace.StartAgent(ztrace.Config{
|
||||
Name: "go-zero-test",
|
||||
Endpoint: "http://localhost:14268/api/traces",
|
||||
Batcher: "jaeger",
|
||||
Sampler: 1.0,
|
||||
})
|
||||
|
||||
DontTraceSpan("bar")
|
||||
|
||||
for _, test := range []string{"", "bar", "foo"} {
|
||||
t.Run(test, func(t *testing.T) {
|
||||
h := chain.New(TracingHandler("foo", test)).Then(
|
||||
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
spanCtx := trace.SpanContextFromContext(r.Context())
|
||||
if test == "bar" {
|
||||
assert.False(t, spanCtx.IsValid())
|
||||
return
|
||||
}
|
||||
|
||||
assert.True(t, spanCtx.IsValid())
|
||||
}))
|
||||
ts := httptest.NewServer(h)
|
||||
defer ts.Close()
|
||||
|
||||
client := ts.Client()
|
||||
err := func(ctx context.Context) error {
|
||||
ctx, span := otel.Tracer("httptrace/client").Start(ctx, "test")
|
||||
defer span.End()
|
||||
|
||||
req, _ := http.NewRequest("GET", ts.URL, nil)
|
||||
otel.GetTextMapPropagator().Inject(ctx, propagation.HeaderCarrier(req.Header))
|
||||
|
||||
res, err := client.Do(req)
|
||||
assert.Nil(t, err)
|
||||
return res.Body.Close()
|
||||
}(context.Background())
|
||||
|
||||
assert.Nil(t, err)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ func TestParseForm(t *testing.T) {
|
||||
Percent float64 `form:"percent,optional"`
|
||||
}
|
||||
|
||||
r, err := http.NewRequest(http.MethodGet, "/a?name=hello&age=18&percent=3.4", nil)
|
||||
r, err := http.NewRequest(http.MethodGet, "/a?name=hello&age=18&percent=3.4", http.NoBody)
|
||||
assert.Nil(t, err)
|
||||
assert.Nil(t, Parse(r, &v))
|
||||
assert.Equal(t, "hello", v.Name)
|
||||
@@ -33,7 +33,7 @@ func TestParseForm_Error(t *testing.T) {
|
||||
Age int `form:"age"`
|
||||
}
|
||||
|
||||
r := httptest.NewRequest(http.MethodGet, "/a?name=hello;", nil)
|
||||
r := httptest.NewRequest(http.MethodGet, "/a?name=hello;", http.NoBody)
|
||||
assert.NotNil(t, ParseForm(r, &v))
|
||||
}
|
||||
|
||||
@@ -82,7 +82,7 @@ func TestParsePath(t *testing.T) {
|
||||
Age int `path:"age"`
|
||||
}
|
||||
|
||||
r := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
r := httptest.NewRequest(http.MethodGet, "/", http.NoBody)
|
||||
r = pathvar.WithVars(r, map[string]string{
|
||||
"name": "foo",
|
||||
"age": "18",
|
||||
@@ -99,7 +99,7 @@ func TestParsePath_Error(t *testing.T) {
|
||||
Age int `path:"age"`
|
||||
}
|
||||
|
||||
r := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
r := httptest.NewRequest(http.MethodGet, "/", http.NoBody)
|
||||
r = pathvar.WithVars(r, map[string]string{
|
||||
"name": "foo",
|
||||
})
|
||||
@@ -138,7 +138,7 @@ func TestParseFormOutOfRange(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
r, err := http.NewRequest(http.MethodGet, test.url, nil)
|
||||
r, err := http.NewRequest(http.MethodGet, test.url, http.NoBody)
|
||||
assert.Nil(t, err)
|
||||
|
||||
err = Parse(r, &v)
|
||||
@@ -218,7 +218,7 @@ func TestParseJsonBody(t *testing.T) {
|
||||
Age int `json:"age,optional"`
|
||||
}
|
||||
|
||||
r := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
r := httptest.NewRequest(http.MethodGet, "/", http.NoBody)
|
||||
assert.Nil(t, Parse(r, &v))
|
||||
assert.Equal(t, "", v.Name)
|
||||
assert.Equal(t, 0, v.Age)
|
||||
@@ -231,7 +231,7 @@ func TestParseRequired(t *testing.T) {
|
||||
Percent float64 `form:"percent"`
|
||||
}{}
|
||||
|
||||
r, err := http.NewRequest(http.MethodGet, "/a?name=hello", nil)
|
||||
r, err := http.NewRequest(http.MethodGet, "/a?name=hello", http.NoBody)
|
||||
assert.Nil(t, err)
|
||||
assert.NotNil(t, Parse(r, &v))
|
||||
}
|
||||
@@ -241,7 +241,7 @@ func TestParseOptions(t *testing.T) {
|
||||
Position int8 `form:"pos,options=1|2"`
|
||||
}{}
|
||||
|
||||
r, err := http.NewRequest(http.MethodGet, "/a?pos=4", nil)
|
||||
r, err := http.NewRequest(http.MethodGet, "/a?pos=4", http.NoBody)
|
||||
assert.Nil(t, err)
|
||||
assert.NotNil(t, Parse(r, &v))
|
||||
}
|
||||
@@ -258,7 +258,7 @@ func TestParseHeaders(t *testing.T) {
|
||||
XForwardedFor string `header:"X-Forwarded-For,optional"`
|
||||
AnonymousStruct
|
||||
}{}
|
||||
request, err := http.NewRequest("POST", "/", nil)
|
||||
request, err := http.NewRequest("POST", "/", http.NoBody)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -287,13 +287,13 @@ func TestParseHeaders_Error(t *testing.T) {
|
||||
Age int `header:"age"`
|
||||
}{}
|
||||
|
||||
r := httptest.NewRequest("POST", "/", nil)
|
||||
r := httptest.NewRequest("POST", "/", http.NoBody)
|
||||
r.Header.Set("name", "foo")
|
||||
assert.NotNil(t, Parse(r, &v))
|
||||
}
|
||||
|
||||
func BenchmarkParseRaw(b *testing.B) {
|
||||
r, err := http.NewRequest(http.MethodGet, "http://hello.com/a?name=hello&age=18&percent=3.4", nil)
|
||||
r, err := http.NewRequest(http.MethodGet, "http://hello.com/a?name=hello&age=18&percent=3.4", http.NoBody)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
@@ -318,7 +318,7 @@ func BenchmarkParseRaw(b *testing.B) {
|
||||
}
|
||||
|
||||
func BenchmarkParseAuto(b *testing.B) {
|
||||
r, err := http.NewRequest(http.MethodGet, "http://hello.com/a?name=hello&age=18&percent=3.4", nil)
|
||||
r, err := http.NewRequest(http.MethodGet, "http://hello.com/a?name=hello&age=18&percent=3.4", http.NoBody)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -200,7 +200,7 @@ func (w *tracedResponseWriter) Write(bytes []byte) (n int, err error) {
|
||||
|
||||
n, err = w.builder.Write(bytes)
|
||||
if w.lessWritten {
|
||||
n -= 1
|
||||
n--
|
||||
}
|
||||
w.hasBody = true
|
||||
|
||||
|
||||
@@ -65,7 +65,7 @@ func TestCorsHandlerWithOrigins(t *testing.T) {
|
||||
for _, method := range methods {
|
||||
test := test
|
||||
t.Run(test.name+"-handler", func(t *testing.T) {
|
||||
r := httptest.NewRequest(method, "http://localhost", nil)
|
||||
r := httptest.NewRequest(method, "http://localhost", http.NoBody)
|
||||
r.Header.Set(originHeader, test.reqOrigin)
|
||||
w := httptest.NewRecorder()
|
||||
handler := NotAllowedHandler(nil, test.origins...)
|
||||
@@ -78,7 +78,7 @@ func TestCorsHandlerWithOrigins(t *testing.T) {
|
||||
assert.Equal(t, test.expect, w.Header().Get(allowOrigin))
|
||||
})
|
||||
t.Run(test.name+"-handler-custom", func(t *testing.T) {
|
||||
r := httptest.NewRequest(method, "http://localhost", nil)
|
||||
r := httptest.NewRequest(method, "http://localhost", http.NoBody)
|
||||
r.Header.Set(originHeader, test.reqOrigin)
|
||||
w := httptest.NewRecorder()
|
||||
handler := NotAllowedHandler(func(w http.ResponseWriter) {
|
||||
@@ -100,7 +100,7 @@ func TestCorsHandlerWithOrigins(t *testing.T) {
|
||||
for _, method := range methods {
|
||||
test := test
|
||||
t.Run(test.name+"-middleware", func(t *testing.T) {
|
||||
r := httptest.NewRequest(method, "http://localhost", nil)
|
||||
r := httptest.NewRequest(method, "http://localhost", http.NoBody)
|
||||
r.Header.Set(originHeader, test.reqOrigin)
|
||||
w := httptest.NewRecorder()
|
||||
handler := Middleware(nil, test.origins...)(func(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -115,7 +115,7 @@ func TestCorsHandlerWithOrigins(t *testing.T) {
|
||||
assert.Equal(t, test.expect, w.Header().Get(allowOrigin))
|
||||
})
|
||||
t.Run(test.name+"-middleware-custom", func(t *testing.T) {
|
||||
r := httptest.NewRequest(method, "http://localhost", nil)
|
||||
r := httptest.NewRequest(method, "http://localhost", http.NoBody)
|
||||
r.Header.Set(originHeader, test.reqOrigin)
|
||||
w := httptest.NewRecorder()
|
||||
handler := Middleware(func(header http.Header) {
|
||||
|
||||
@@ -14,7 +14,7 @@ func TestParseHeaders(t *testing.T) {
|
||||
Baz int `header:"baz"`
|
||||
Qux bool `header:"qux,default=true"`
|
||||
}
|
||||
r := httptest.NewRequest(http.MethodGet, "/any", nil)
|
||||
r := httptest.NewRequest(http.MethodGet, "/any", http.NoBody)
|
||||
r.Header.Set("foo", "bar")
|
||||
r.Header.Set("baz", "1")
|
||||
assert.Nil(t, ParseHeaders(r.Header, &val))
|
||||
@@ -29,7 +29,7 @@ func TestParseHeadersMulti(t *testing.T) {
|
||||
Baz int `header:"baz"`
|
||||
Qux bool `header:"qux,default=true"`
|
||||
}
|
||||
r := httptest.NewRequest(http.MethodGet, "/any", nil)
|
||||
r := httptest.NewRequest(http.MethodGet, "/any", http.NoBody)
|
||||
r.Header.Set("foo", "bar")
|
||||
r.Header.Add("foo", "bar1")
|
||||
r.Header.Set("baz", "1")
|
||||
@@ -43,9 +43,9 @@ func TestParseHeadersArrayInt(t *testing.T) {
|
||||
var val struct {
|
||||
Foo []int `header:"foo"`
|
||||
}
|
||||
r := httptest.NewRequest(http.MethodGet, "/any", nil)
|
||||
r := httptest.NewRequest(http.MethodGet, "/any", http.NoBody)
|
||||
r.Header.Set("foo", "1")
|
||||
r.Header.Add("foo", "2")
|
||||
assert.Nil(t, ParseHeaders(r.Header, &val))
|
||||
assert.Equal(t, []int{1, 2}, val.Foo)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
|
||||
func TestInfo(t *testing.T) {
|
||||
collector := new(LogCollector)
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
|
||||
req = req.WithContext(context.WithValue(req.Context(), LogContext, collector))
|
||||
Info(req, "first")
|
||||
Infof(req, "second %s", "third")
|
||||
@@ -35,7 +35,7 @@ func TestError(t *testing.T) {
|
||||
logx.SetWriter(o)
|
||||
}()
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
|
||||
Error(req, "first")
|
||||
Errorf(req, "second %s", "third")
|
||||
val := buf.String()
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
)
|
||||
|
||||
func TestHeaderOnceResponseWriter_Flush(t *testing.T) {
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
|
||||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
cw := NewHeaderOnceResponseWriter(w)
|
||||
cw.Header().Set("X-Test", "test")
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
)
|
||||
|
||||
func TestWithCodeResponseWriter(t *testing.T) {
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
|
||||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
cw := &WithCodeResponseWriter{Writer: w}
|
||||
|
||||
|
||||
@@ -255,7 +255,7 @@ func TestWithPrefix(t *testing.T) {
|
||||
},
|
||||
}
|
||||
WithPrefix("/api")(&fr)
|
||||
var vals []string
|
||||
vals := make([]string, 0, len(fr.routes))
|
||||
for _, r := range fr.routes {
|
||||
vals = append(vals, r.Path)
|
||||
}
|
||||
@@ -510,7 +510,7 @@ func TestServer_WithChain(t *testing.T) {
|
||||
)
|
||||
rt := router.NewRouter()
|
||||
assert.Nil(t, server.ngin.bindRoutes(rt))
|
||||
req, err := http.NewRequest(http.MethodGet, "/", nil)
|
||||
req, err := http.NewRequest(http.MethodGet, "/", http.NoBody)
|
||||
assert.Nil(t, err)
|
||||
rt.ServeHTTP(httptest.NewRecorder(), req)
|
||||
assert.Equal(t, int32(5), atomic.LoadInt32(&called))
|
||||
@@ -531,7 +531,7 @@ func TestServer_WithCors(t *testing.T) {
|
||||
Router: r,
|
||||
middleware: cors.Middleware(nil, "*"),
|
||||
}
|
||||
req := httptest.NewRequest(http.MethodOptions, "/", nil)
|
||||
req := httptest.NewRequest(http.MethodOptions, "/", http.NoBody)
|
||||
cr.ServeHTTP(httptest.NewRecorder(), req)
|
||||
assert.Equal(t, int32(0), atomic.LoadInt32(&called))
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ func TestTokenParser(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, pair := range keys {
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
|
||||
token, err := buildToken(key, map[string]interface{}{
|
||||
"key": "value",
|
||||
}, 3600)
|
||||
@@ -50,7 +50,7 @@ func TestTokenParser_Expired(t *testing.T) {
|
||||
key = "14F17379-EB8F-411B-8F12-6929002DCA76"
|
||||
prevKey = "B63F477D-BBA3-4E52-96D3-C0034C27694A"
|
||||
)
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
|
||||
token, err := buildToken(key, map[string]interface{}{
|
||||
"key": "value",
|
||||
}, 3600)
|
||||
|
||||
@@ -43,7 +43,7 @@ var (
|
||||
|
||||
goCmd = &cobra.Command{
|
||||
Use: "go",
|
||||
Short: "Generate go files for provided api in yaml file",
|
||||
Short: "Generate go files for provided api in api file",
|
||||
RunE: gogen.GoCommand,
|
||||
}
|
||||
|
||||
|
||||
@@ -107,7 +107,7 @@ func associatedTypes(tp spec.DefineStruct, tps *[]spec.Type) {
|
||||
}
|
||||
|
||||
// buildTypes gen types to string
|
||||
func buildTypes(types []spec.Type, all []spec.Type) (string, error) {
|
||||
func buildTypes(types, all []spec.Type) (string, error) {
|
||||
var builder strings.Builder
|
||||
first := true
|
||||
for _, tp := range types {
|
||||
|
||||
15
tools/goctl/api/spec/example_test.go
Normal file
15
tools/goctl/api/spec/example_test.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package spec_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/zeromicro/go-zero/tools/goctl/api/spec"
|
||||
)
|
||||
|
||||
func ExampleMember_GetEnumOptions() {
|
||||
member := spec.Member{
|
||||
Tag: `json:"foo,options=foo|bar|options|123"`,
|
||||
}
|
||||
fmt.Println(member.GetEnumOptions())
|
||||
// Output:
|
||||
// [foo bar options 123]
|
||||
}
|
||||
@@ -13,10 +13,11 @@ const (
|
||||
bodyTagKey = "json"
|
||||
formTagKey = "form"
|
||||
pathTagKey = "path"
|
||||
headerTagKey = "header"
|
||||
defaultSummaryKey = "summary"
|
||||
)
|
||||
|
||||
var definedKeys = []string{bodyTagKey, formTagKey, pathTagKey}
|
||||
var definedKeys = []string{bodyTagKey, formTagKey, pathTagKey, headerTagKey}
|
||||
|
||||
func (s Service) JoinPrefix() Service {
|
||||
var groups []Group
|
||||
@@ -138,6 +139,42 @@ func (m Member) IsFormMember() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// IsTagMember returns true if contains given tag
|
||||
func (m Member) IsTagMember(tagKey string) bool {
|
||||
if m.IsInline {
|
||||
return true
|
||||
}
|
||||
|
||||
tags := m.Tags()
|
||||
for _, tag := range tags {
|
||||
if tag.Key == tagKey {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// GetEnumOptions return a slice contains all enumeration options
|
||||
func (m Member) GetEnumOptions() []string {
|
||||
if !m.IsBodyMember() {
|
||||
return nil
|
||||
}
|
||||
|
||||
tags := m.Tags()
|
||||
for _, tag := range tags {
|
||||
if tag.Key == bodyTagKey {
|
||||
options := tag.Options
|
||||
for _, option := range options {
|
||||
if strings.Index(option, "options=") == 0 {
|
||||
option = strings.TrimPrefix(option, "options=")
|
||||
return strings.Split(option, "|")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetBodyMembers returns all json fields
|
||||
func (t DefineStruct) GetBodyMembers() []Member {
|
||||
var result []Member
|
||||
@@ -171,6 +208,17 @@ func (t DefineStruct) GetNonBodyMembers() []Member {
|
||||
return result
|
||||
}
|
||||
|
||||
// GetTagMembers returns all given key fields
|
||||
func (t DefineStruct) GetTagMembers(tagKey string) []Member {
|
||||
var result []Member
|
||||
for _, member := range t.Members {
|
||||
if member.IsTagMember(tagKey) {
|
||||
result = append(result, member)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// JoinedDoc joins comments and summary value in AtDoc
|
||||
func (r Route) JoinedDoc() string {
|
||||
doc := r.AtDoc.Text
|
||||
|
||||
@@ -105,20 +105,43 @@ func paramsForRoute(route spec.Route) string {
|
||||
}
|
||||
hasParams := pathHasParams(route)
|
||||
hasBody := hasRequestBody(route)
|
||||
hasHeader := hasRequestHeader(route)
|
||||
hasPath := hasRequestPath(route)
|
||||
rt, err := goTypeToTs(route.RequestType, true)
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
return ""
|
||||
}
|
||||
|
||||
if hasParams && hasBody {
|
||||
return fmt.Sprintf("params: %s, req: %s", rt+"Params", rt)
|
||||
} else if hasParams {
|
||||
return fmt.Sprintf("params: %s", rt+"Params")
|
||||
} else if hasBody {
|
||||
return fmt.Sprintf("req: %s", rt)
|
||||
var params []string
|
||||
if hasParams {
|
||||
params = append(params, fmt.Sprintf("params: %s", rt+"Params"))
|
||||
}
|
||||
return ""
|
||||
if hasBody {
|
||||
params = append(params, fmt.Sprintf("req: %s", rt))
|
||||
}
|
||||
if hasHeader {
|
||||
params = append(params, fmt.Sprintf("headers: %s", rt+"Headers"))
|
||||
}
|
||||
if hasPath {
|
||||
ds, ok := route.RequestType.(spec.DefineStruct)
|
||||
if !ok {
|
||||
fmt.Printf("invalid route.RequestType: {%v}\n", route.RequestType)
|
||||
}
|
||||
members := ds.GetTagMembers(pathTagKey)
|
||||
for _, member := range members {
|
||||
tags := member.Tags()
|
||||
|
||||
if len(tags) > 0 && tags[0].Key == pathTagKey {
|
||||
valueType, err := goTypeToTs(member.Type, false)
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
return ""
|
||||
}
|
||||
params = append(params, fmt.Sprintf("%s: %s", tags[0].Name, valueType))
|
||||
}
|
||||
}
|
||||
}
|
||||
return strings.Join(params, ", ")
|
||||
}
|
||||
|
||||
func commentForRoute(route spec.Route) string {
|
||||
@@ -128,13 +151,15 @@ func commentForRoute(route spec.Route) string {
|
||||
builder.WriteString("\n * @description " + comment)
|
||||
hasParams := pathHasParams(route)
|
||||
hasBody := hasRequestBody(route)
|
||||
if hasParams && hasBody {
|
||||
hasHeader := hasRequestHeader(route)
|
||||
if hasParams {
|
||||
builder.WriteString("\n * @param params")
|
||||
}
|
||||
if hasBody {
|
||||
builder.WriteString("\n * @param req")
|
||||
} else if hasParams {
|
||||
builder.WriteString("\n * @param params")
|
||||
} else if hasBody {
|
||||
builder.WriteString("\n * @param req")
|
||||
}
|
||||
if hasHeader {
|
||||
builder.WriteString("\n * @param headers")
|
||||
}
|
||||
builder.WriteString("\n */")
|
||||
return builder.String()
|
||||
@@ -143,26 +168,42 @@ func commentForRoute(route spec.Route) string {
|
||||
func callParamsForRoute(route spec.Route, group spec.Group) string {
|
||||
hasParams := pathHasParams(route)
|
||||
hasBody := hasRequestBody(route)
|
||||
if hasParams && hasBody {
|
||||
return fmt.Sprintf("%s, %s, %s", pathForRoute(route, group), "params", "req")
|
||||
} else if hasParams {
|
||||
return fmt.Sprintf("%s, %s", pathForRoute(route, group), "params")
|
||||
} else if hasBody {
|
||||
return fmt.Sprintf("%s, %s", pathForRoute(route, group), "req")
|
||||
hasHeader := hasRequestHeader(route)
|
||||
|
||||
var params = []string{pathForRoute(route, group)}
|
||||
if hasParams {
|
||||
params = append(params, "params")
|
||||
}
|
||||
if hasBody {
|
||||
params = append(params, "req")
|
||||
}
|
||||
if hasHeader {
|
||||
params = append(params, "headers")
|
||||
}
|
||||
|
||||
return pathForRoute(route, group)
|
||||
return strings.Join(params, ", ")
|
||||
}
|
||||
|
||||
func pathForRoute(route spec.Route, group spec.Group) string {
|
||||
prefix := group.GetAnnotation(pathPrefix)
|
||||
|
||||
routePath := route.Path
|
||||
if strings.Contains(routePath, ":") {
|
||||
pathSlice := strings.Split(routePath, "/")
|
||||
for i, part := range pathSlice {
|
||||
if strings.Contains(part, ":") {
|
||||
pathSlice[i] = fmt.Sprintf("${%s}", part[1:])
|
||||
}
|
||||
}
|
||||
routePath = strings.Join(pathSlice, "/")
|
||||
}
|
||||
if len(prefix) == 0 {
|
||||
return "\"" + route.Path + "\""
|
||||
return "`" + routePath + "`"
|
||||
}
|
||||
|
||||
prefix = strings.TrimPrefix(prefix, `"`)
|
||||
prefix = strings.TrimSuffix(prefix, `"`)
|
||||
return fmt.Sprintf(`"%s/%s"`, prefix, strings.TrimPrefix(route.Path, "/"))
|
||||
return fmt.Sprintf("`%s/%s`", prefix, strings.TrimPrefix(routePath, "/"))
|
||||
}
|
||||
|
||||
func pathHasParams(route spec.Route) bool {
|
||||
@@ -182,3 +223,21 @@ func hasRequestBody(route spec.Route) bool {
|
||||
|
||||
return len(route.RequestTypeName()) > 0 && len(ds.GetBodyMembers()) > 0
|
||||
}
|
||||
|
||||
func hasRequestPath(route spec.Route) bool {
|
||||
ds, ok := route.RequestType.(spec.DefineStruct)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
return len(route.RequestTypeName()) > 0 && len(ds.GetTagMembers(pathTagKey)) > 0
|
||||
}
|
||||
|
||||
func hasRequestHeader(route spec.Route) bool {
|
||||
ds, ok := route.RequestType.(spec.DefineStruct)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
return len(route.RequestTypeName()) > 0 && len(ds.GetTagMembers(headerTagKey)) > 0
|
||||
}
|
||||
|
||||
@@ -11,9 +11,15 @@ import (
|
||||
"github.com/zeromicro/go-zero/tools/goctl/util"
|
||||
)
|
||||
|
||||
const (
|
||||
formTagKey = "form"
|
||||
pathTagKey = "path"
|
||||
headerTagKey = "header"
|
||||
)
|
||||
|
||||
func writeProperty(writer io.Writer, member spec.Member, indent int) error {
|
||||
writeIndent(writer, indent)
|
||||
ty, err := goTypeToTs(member.Type, false)
|
||||
ty, err := genTsType(member)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -46,6 +52,19 @@ func writeIndent(writer io.Writer, indent int) {
|
||||
}
|
||||
}
|
||||
|
||||
func genTsType(m spec.Member) (ty string, err error) {
|
||||
ty, err = goTypeToTs(m.Type, false)
|
||||
if enums := m.GetEnumOptions(); enums != nil {
|
||||
if ty == "string" {
|
||||
for i := range enums {
|
||||
enums[i] = "'" + enums[i] + "'"
|
||||
}
|
||||
}
|
||||
ty = strings.Join(enums, " | ")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func goTypeToTs(tp spec.Type, fromPacket bool) (string, error) {
|
||||
switch v := tp.(type) {
|
||||
case spec.DefineStruct:
|
||||
@@ -129,13 +148,21 @@ func genParamsTypesIfNeed(writer io.Writer, tp spec.Type) error {
|
||||
if len(members) == 0 {
|
||||
return nil
|
||||
}
|
||||
fmt.Fprintf(writer, "\n")
|
||||
|
||||
fmt.Fprintf(writer, "export interface %sParams {\n", util.Title(tp.Name()))
|
||||
if err := writeMembers(writer, tp, true); err != nil {
|
||||
if err := writeTagMembers(writer, tp, formTagKey); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Fprintf(writer, "}\n")
|
||||
|
||||
if len(definedType.GetTagMembers(headerTagKey)) > 0 {
|
||||
fmt.Fprintf(writer, "export interface %sHeaders {\n", util.Title(tp.Name()))
|
||||
if err := writeTagMembers(writer, tp, headerTagKey); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(writer, "}\n")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -168,3 +195,30 @@ func writeMembers(writer io.Writer, tp spec.Type, isParam bool) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func writeTagMembers(writer io.Writer, tp spec.Type, tagKey string) error {
|
||||
definedType, ok := tp.(spec.DefineStruct)
|
||||
if !ok {
|
||||
pointType, ok := tp.(spec.PointerType)
|
||||
if ok {
|
||||
return writeTagMembers(writer, pointType.Type, tagKey)
|
||||
}
|
||||
|
||||
return fmt.Errorf("type %s not supported", tp.Name())
|
||||
}
|
||||
|
||||
members := definedType.GetTagMembers(tagKey)
|
||||
for _, member := range members {
|
||||
if member.IsInline {
|
||||
if err := writeTagMembers(writer, member.Type, tagKey); err != nil {
|
||||
return err
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
if err := writeProperty(writer, member, 1); err != nil {
|
||||
return apiutil.WrapErr(err, " type "+tp.Name())
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
38
tools/goctl/api/tsgen/util_test.go
Normal file
38
tools/goctl/api/tsgen/util_test.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package tsgen
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/zeromicro/go-zero/tools/goctl/api/spec"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGenTsType(t *testing.T) {
|
||||
member := spec.Member{
|
||||
Name: "foo",
|
||||
Type: spec.PrimitiveType{RawName: "string"},
|
||||
Tag: `json:"foo,options=foo|bar|options|123"`,
|
||||
Comment: "",
|
||||
Docs: nil,
|
||||
IsInline: false,
|
||||
}
|
||||
ty, err := genTsType(member)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assert.Equal(t, `'foo' | 'bar' | 'options' | '123'`, ty)
|
||||
|
||||
member.IsInline = true
|
||||
ty, err = genTsType(member)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assert.Equal(t, `'foo' | 'bar' | 'options' | '123'`, ty)
|
||||
|
||||
member.Type = spec.PrimitiveType{RawName: "int"}
|
||||
member.Tag = `json:"foo,options=1|3|4|123"`
|
||||
ty, err = genTsType(member)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assert.Equal(t, `1 | 3 | 4 | 123`, ty)
|
||||
}
|
||||
@@ -8,7 +8,7 @@
|
||||
`namingFormat`可以用于对生成代码的文件名称进行格式化,和日期格式化符(yyyy-MM-dd)类似,在代码生成时可以根据这些配置项的格式化符进行格式化。
|
||||
|
||||
## 格式化符(gozero)
|
||||
格式化符有`go`,`zero`组成,如常见的三种格式化风格你可以这样编写:
|
||||
格式化符由`go`,`zero`组成,如常见的三种格式化风格你可以这样编写:
|
||||
* lower: `gozero`
|
||||
* camel: `goZero`
|
||||
* snake: `go_zero`
|
||||
@@ -47,4 +47,4 @@ goctl model mysql datasource -url="" -table="*" -dir ./snake -style GoZero
|
||||
```
|
||||
|
||||
# 默认值
|
||||
当不指定-style时默认值为`gozero`
|
||||
当不指定-style时默认值为`gozero`
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
)
|
||||
|
||||
// BuildVersion is the version of goctl.
|
||||
const BuildVersion = "1.4.1"
|
||||
const BuildVersion = "1.4.2"
|
||||
|
||||
var tag = map[string]int{"pre-alpha": 0, "alpha": 1, "pre-bata": 2, "beta": 3, "released": 4, "": 5}
|
||||
|
||||
|
||||
@@ -49,7 +49,7 @@ type Deployment struct {
|
||||
ImagePullPolicy string
|
||||
}
|
||||
|
||||
// DeploymentCommand is used to generate the kubernetes deployment yaml files.
|
||||
// deploymentCommand is used to generate the kubernetes deployment yaml files.
|
||||
func deploymentCommand(_ *cobra.Command, _ []string) error {
|
||||
nodePort := varIntNodePort
|
||||
home := varStringHome
|
||||
|
||||
@@ -93,6 +93,7 @@ func init() {
|
||||
mongoCmd.Flags().StringVar(&mongo.VarStringBranch, "branch", "", "The branch of the remote repo, it does work with --remote")
|
||||
|
||||
mysqlCmd.PersistentFlags().BoolVar(&command.VarBoolStrict, "strict", false, "Generate model in strict mode")
|
||||
mysqlCmd.PersistentFlags().StringSliceVarP(&command.VarStringSliceIgnoreColumns, "ignore-columns", "i", []string{"create_at", "created_at", "create_time", "update_at", "updated_at", "update_time"}, "Ignore columns while creating or updating rows")
|
||||
|
||||
mysqlCmd.AddCommand(datasourceCmd)
|
||||
mysqlCmd.AddCommand(ddlCmd)
|
||||
|
||||
@@ -97,7 +97,7 @@ func generateCustomModel(ctx *Context) error {
|
||||
|
||||
func generateTypes(ctx *Context) error {
|
||||
for _, t := range ctx.Types {
|
||||
fn, err := format.FileNamingFormat(ctx.Cfg.NamingFormat, t+"types")
|
||||
fn, err := format.FileNamingFormat(ctx.Cfg.NamingFormat, t+"_types")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -105,7 +105,7 @@ func (m *defaultUserModel) FindOne(ctx context.Context, id string) (*User, error
|
||||
}
|
||||
|
||||
var data User
|
||||
key := prefixUserCacheKey + data.ID.Hex()
|
||||
key := prefixUserCacheKey + id
|
||||
err = m.conn.FindOne(ctx, key, &data, bson.M{"_id": oid})
|
||||
switch err {
|
||||
case nil:
|
||||
|
||||
@@ -29,7 +29,7 @@ func newDefault{{.Type}}Model(conn {{if .Cache}}*monc.Model{{else}}*mon.Model{{e
|
||||
|
||||
|
||||
func (m *default{{.Type}}Model) Insert(ctx context.Context, data *{{.Type}}) error {
|
||||
if !data.ID.IsZero() {
|
||||
if data.ID.IsZero() {
|
||||
data.ID = primitive.NewObjectID()
|
||||
data.CreateAt = time.Now()
|
||||
data.UpdateAt = time.Now()
|
||||
@@ -47,7 +47,7 @@ func (m *default{{.Type}}Model) FindOne(ctx context.Context, id string) (*{{.Typ
|
||||
}
|
||||
|
||||
var data {{.Type}}
|
||||
{{if .Cache}}key := prefix{{.Type}}CacheKey + data.ID.Hex(){{end}}
|
||||
{{if .Cache}}key := prefix{{.Type}}CacheKey + id{{end}}
|
||||
err = m.conn.FindOne(ctx, {{if .Cache}}key, {{end}}&data, bson.M{"_id": oid})
|
||||
switch err {
|
||||
case nil:
|
||||
|
||||
@@ -35,16 +35,16 @@ goctl model 为go-zero下的工具模块中的组件之一,目前支持识别m
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
|
||||
"github.com/zeromicro/go-zero/core/stores/builder"
|
||||
"github.com/zeromicro/go-zero/core/stores/cache"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlc"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
"github.com/zeromicro/go-zero/core/stringx"
|
||||
"github.com/zeromicro/go-zero/tools/goctl/model/sql/builderx"
|
||||
)
|
||||
|
||||
var (
|
||||
userFieldNames = builderx.FieldNames(&User{})
|
||||
userFieldNames = builder.FieldNames(&User{})
|
||||
userRows = strings.Join(userFieldNames, ",")
|
||||
userRowsExpectAutoSet = strings.Join(stringx.Remove(userFieldNames, "id", "create_time", "update_time"), ",")
|
||||
userRowsWithPlaceHolder = strings.Join(stringx.Remove(userFieldNames, "id", "create_time", "update_time"), "=?,") + "=?"
|
||||
|
||||
@@ -7,10 +7,11 @@ import (
|
||||
|
||||
"github.com/go-sql-driver/mysql"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/collection"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/core/stores/postgres"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
|
||||
"github.com/zeromicro/go-zero/tools/goctl/config"
|
||||
"github.com/zeromicro/go-zero/tools/goctl/model/sql/command/migrationnotes"
|
||||
"github.com/zeromicro/go-zero/tools/goctl/model/sql/gen"
|
||||
@@ -50,6 +51,8 @@ var (
|
||||
VarStringBranch string
|
||||
// VarBoolStrict describes whether the strict mode is enabled.
|
||||
VarBoolStrict bool
|
||||
// VarStringSliceIgnoreColumns represents the columns which are ignored.
|
||||
VarStringSliceIgnoreColumns []string
|
||||
)
|
||||
|
||||
var errNotMatched = errors.New("sql not matched")
|
||||
@@ -81,13 +84,14 @@ func MysqlDDL(_ *cobra.Command, _ []string) error {
|
||||
}
|
||||
|
||||
arg := ddlArg{
|
||||
src: src,
|
||||
dir: dir,
|
||||
cfg: cfg,
|
||||
cache: cache,
|
||||
idea: idea,
|
||||
database: database,
|
||||
strict: VarBoolStrict,
|
||||
src: src,
|
||||
dir: dir,
|
||||
cfg: cfg,
|
||||
cache: cache,
|
||||
idea: idea,
|
||||
database: database,
|
||||
strict: VarBoolStrict,
|
||||
ignoreColumns: mergeColumns(VarStringSliceIgnoreColumns),
|
||||
}
|
||||
return fromDDL(arg)
|
||||
}
|
||||
@@ -121,17 +125,29 @@ func MySqlDataSource(_ *cobra.Command, _ []string) error {
|
||||
}
|
||||
|
||||
arg := dataSourceArg{
|
||||
url: url,
|
||||
dir: dir,
|
||||
tablePat: patterns,
|
||||
cfg: cfg,
|
||||
cache: cache,
|
||||
idea: idea,
|
||||
strict: VarBoolStrict,
|
||||
url: url,
|
||||
dir: dir,
|
||||
tablePat: patterns,
|
||||
cfg: cfg,
|
||||
cache: cache,
|
||||
idea: idea,
|
||||
strict: VarBoolStrict,
|
||||
ignoreColumns: mergeColumns(VarStringSliceIgnoreColumns),
|
||||
}
|
||||
return fromMysqlDataSource(arg)
|
||||
}
|
||||
|
||||
func mergeColumns(columns []string) []string {
|
||||
set := collection.NewSet()
|
||||
for _, v := range columns {
|
||||
fields := strings.FieldsFunc(v, func(r rune) bool {
|
||||
return r == ','
|
||||
})
|
||||
set.AddStr(fields...)
|
||||
}
|
||||
return set.KeysStr()
|
||||
}
|
||||
|
||||
type pattern map[string]struct{}
|
||||
|
||||
func (p pattern) Match(s string) bool {
|
||||
@@ -205,11 +221,12 @@ func PostgreSqlDataSource(_ *cobra.Command, _ []string) error {
|
||||
}
|
||||
|
||||
type ddlArg struct {
|
||||
src, dir string
|
||||
cfg *config.Config
|
||||
cache, idea bool
|
||||
database string
|
||||
strict bool
|
||||
src, dir string
|
||||
cfg *config.Config
|
||||
cache, idea bool
|
||||
database string
|
||||
strict bool
|
||||
ignoreColumns []string
|
||||
}
|
||||
|
||||
func fromDDL(arg ddlArg) error {
|
||||
@@ -228,7 +245,8 @@ func fromDDL(arg ddlArg) error {
|
||||
return errNotMatched
|
||||
}
|
||||
|
||||
generator, err := gen.NewDefaultGenerator(arg.dir, arg.cfg, gen.WithConsoleOption(log))
|
||||
generator, err := gen.NewDefaultGenerator(arg.dir, arg.cfg,
|
||||
gen.WithConsoleOption(log), gen.WithIgnoreColumns(arg.ignoreColumns))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -244,11 +262,12 @@ func fromDDL(arg ddlArg) error {
|
||||
}
|
||||
|
||||
type dataSourceArg struct {
|
||||
url, dir string
|
||||
tablePat pattern
|
||||
cfg *config.Config
|
||||
cache, idea bool
|
||||
strict bool
|
||||
url, dir string
|
||||
tablePat pattern
|
||||
cfg *config.Config
|
||||
cache, idea bool
|
||||
strict bool
|
||||
ignoreColumns []string
|
||||
}
|
||||
|
||||
func fromMysqlDataSource(arg dataSourceArg) error {
|
||||
@@ -301,7 +320,8 @@ func fromMysqlDataSource(arg dataSourceArg) error {
|
||||
return errors.New("no tables matched")
|
||||
}
|
||||
|
||||
generator, err := gen.NewDefaultGenerator(arg.dir, arg.cfg, gen.WithConsoleOption(log))
|
||||
generator, err := gen.NewDefaultGenerator(arg.dir, arg.cfg,
|
||||
gen.WithConsoleOption(log), gen.WithIgnoreColumns(arg.ignoreColumns))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -77,6 +77,7 @@ var commonMysqlDataTypeMapString = map[string]string{
|
||||
// For consistency, all integer types are converted to int64
|
||||
// bool
|
||||
"bool": "bool",
|
||||
"_bool": "pq.BoolArray",
|
||||
"boolean": "bool",
|
||||
// number
|
||||
"tinyint": "int64",
|
||||
@@ -85,14 +86,20 @@ var commonMysqlDataTypeMapString = map[string]string{
|
||||
"int": "int64",
|
||||
"int1": "int64",
|
||||
"int2": "int64",
|
||||
"_int2": "pq.Int64Array",
|
||||
"int3": "int64",
|
||||
"int4": "int64",
|
||||
"_int4": "pq.Int64Array",
|
||||
"int8": "int64",
|
||||
"_int8": "pq.Int64Array",
|
||||
"integer": "int64",
|
||||
"_integer": "pq.Int64Array",
|
||||
"bigint": "int64",
|
||||
"float": "float64",
|
||||
"float4": "float64",
|
||||
"_float4": "pq.Float64Array",
|
||||
"float8": "float64",
|
||||
"_float8": "pq.Float64Array",
|
||||
"double": "float64",
|
||||
"decimal": "float64",
|
||||
"dec": "float64",
|
||||
@@ -111,14 +118,17 @@ var commonMysqlDataTypeMapString = map[string]string{
|
||||
"nvarchar": "string",
|
||||
"nchar": "string",
|
||||
"char": "string",
|
||||
"_char": "pq.StringArray",
|
||||
"character": "string",
|
||||
"varchar": "string",
|
||||
"_varchar": "pq.StringArray",
|
||||
"binary": "string",
|
||||
"bytea": "string",
|
||||
"longvarbinary": "string",
|
||||
"varbinary": "string",
|
||||
"tinytext": "string",
|
||||
"text": "string",
|
||||
"_text": "pq.StringArray",
|
||||
"mediumtext": "string",
|
||||
"longtext": "string",
|
||||
"enum": "string",
|
||||
@@ -129,6 +139,7 @@ var commonMysqlDataTypeMapString = map[string]string{
|
||||
"longblob": "string",
|
||||
"mediumblob": "string",
|
||||
"tinyblob": "string",
|
||||
"ltree": "[]byte",
|
||||
}
|
||||
|
||||
// ConvertDataType converts mysql column type into golang type
|
||||
@@ -148,6 +159,10 @@ func ConvertStringDataType(dataBaseType string, isDefaultNull, unsigned, strict
|
||||
return "", fmt.Errorf("unsupported database type: %s", dataBaseType)
|
||||
}
|
||||
|
||||
if strings.HasPrefix(dataBaseType, "_") {
|
||||
return tp, nil
|
||||
}
|
||||
|
||||
return mayConvertNullType(tp, isDefaultNull, unsigned, strict), nil
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,10 @@ fromDDLWithCache:
|
||||
goctl template clean
|
||||
goctl model mysql ddl -src="./sql/*.sql" -dir="./sql/model/cache" -cache
|
||||
|
||||
fromDDLWithCacheAndIgnoreColumns:
|
||||
goctl template clean
|
||||
goctl model mysql ddl -src="./sql/*.sql" -dir="./sql/model/ignore_columns/cache" -cache -i 'gmt_create,create_at' -i 'gmt_modified,update_at'
|
||||
|
||||
fromDDLWithCacheAndDb:
|
||||
goctl template clean
|
||||
goctl model mysql ddl -src="./sql/*.sql" -dir="./sql/model/cache_db" -database="1gozero" -cache
|
||||
|
||||
@@ -26,10 +26,11 @@ type (
|
||||
defaultGenerator struct {
|
||||
console.Console
|
||||
// source string
|
||||
dir string
|
||||
pkg string
|
||||
cfg *config.Config
|
||||
isPostgreSql bool
|
||||
dir string
|
||||
pkg string
|
||||
cfg *config.Config
|
||||
isPostgreSql bool
|
||||
ignoreColumns []string
|
||||
}
|
||||
|
||||
// Option defines a function with argument defaultGenerator
|
||||
@@ -82,14 +83,21 @@ func NewDefaultGenerator(dir string, cfg *config.Config, opt ...Option) (*defaul
|
||||
return generator, nil
|
||||
}
|
||||
|
||||
// WithConsoleOption creates a console option
|
||||
// WithConsoleOption creates a console option.
|
||||
func WithConsoleOption(c console.Console) Option {
|
||||
return func(generator *defaultGenerator) {
|
||||
generator.Console = c
|
||||
}
|
||||
}
|
||||
|
||||
// WithPostgreSql marks defaultGenerator.isPostgreSql true
|
||||
// WithIgnoreColumns ignores the columns while insert or update rows.
|
||||
func WithIgnoreColumns(ignoreColumns []string) Option {
|
||||
return func(generator *defaultGenerator) {
|
||||
generator.ignoreColumns = ignoreColumns
|
||||
}
|
||||
}
|
||||
|
||||
// WithPostgreSql marks defaultGenerator.isPostgreSql true.
|
||||
func WithPostgreSql() Option {
|
||||
return func(generator *defaultGenerator) {
|
||||
generator.isPostgreSql = true
|
||||
@@ -235,6 +243,16 @@ type Table struct {
|
||||
PrimaryCacheKey Key
|
||||
UniqueCacheKey []Key
|
||||
ContainsUniqueCacheKey bool
|
||||
ignoreColumns []string
|
||||
}
|
||||
|
||||
func (t Table) isIgnoreColumns(columnName string) bool {
|
||||
for _, v := range t.ignoreColumns {
|
||||
if v == columnName {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, error) {
|
||||
@@ -249,6 +267,7 @@ func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, er
|
||||
table.PrimaryCacheKey = primaryKey
|
||||
table.UniqueCacheKey = uniqueKey
|
||||
table.ContainsUniqueCacheKey = len(uniqueKey) > 0
|
||||
table.ignoreColumns = g.ignoreColumns
|
||||
|
||||
importsCode, err := genImports(table, withCache, in.ContainsTime())
|
||||
if err != nil {
|
||||
|
||||
@@ -13,11 +13,11 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/core/stringx"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/core/stores/builder"
|
||||
"github.com/zeromicro/go-zero/core/stringx"
|
||||
"github.com/zeromicro/go-zero/tools/goctl/config"
|
||||
"github.com/zeromicro/go-zero/tools/goctl/model/sql/builderx"
|
||||
"github.com/zeromicro/go-zero/tools/goctl/model/sql/parser"
|
||||
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
|
||||
)
|
||||
@@ -119,7 +119,7 @@ func TestFolderName(t *testing.T) {
|
||||
|
||||
pkg := g.pkg
|
||||
|
||||
err = g.StartFromDDL(sqlFile, true, "go_zero")
|
||||
err = g.StartFromDDL(sqlFile, true, true, "go_zero")
|
||||
assert.Nil(t, err)
|
||||
assert.True(t, func() bool {
|
||||
_, err := os.Stat(filepath.Join(camelDir, "TestUserModel.go"))
|
||||
@@ -132,7 +132,7 @@ func TestFolderName(t *testing.T) {
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
|
||||
err = g.StartFromDDL(sqlFile, true, "go_zero")
|
||||
err = g.StartFromDDL(sqlFile, true, true, "go_zero")
|
||||
assert.Nil(t, err)
|
||||
assert.True(t, func() bool {
|
||||
_, err := os.Stat(filepath.Join(snakeDir, "test_user_model.go"))
|
||||
@@ -158,7 +158,7 @@ func TestFields(t *testing.T) {
|
||||
UpdateTime sql.NullTime `db:"update_time"`
|
||||
}
|
||||
var (
|
||||
studentFieldNames = builderx.RawFieldNames(&Student{})
|
||||
studentFieldNames = builder.RawFieldNames(&Student{})
|
||||
studentRows = strings.Join(studentFieldNames, ",")
|
||||
studentRowsExpectAutoSet = strings.Join(stringx.Remove(studentFieldNames, "`id`", "`create_time`", "`update_time`"), ",")
|
||||
studentRowsWithPlaceHolder = strings.Join(stringx.Remove(studentFieldNames, "`id`", "`create_time`", "`update_time`"), "=?,") + "=?"
|
||||
|
||||
@@ -31,7 +31,7 @@ func genInsert(table Table, withCache, postgreSql bool) (string, string, error)
|
||||
var count int
|
||||
for _, field := range table.Fields {
|
||||
camel := util.SafeString(field.Name.ToCamel())
|
||||
if camel == "CreateTime" || camel == "UpdateTime" || camel == "CreateAt" || camel == "UpdateAt" {
|
||||
if table.isIgnoreColumns(field.Name.Source()) {
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ func genUpdate(table Table, withCache, postgreSql bool) (
|
||||
}
|
||||
for _, field := range table.Fields {
|
||||
camel := util.SafeString(field.Name.ToCamel())
|
||||
if camel == "CreateTime" || camel == "UpdateTime" || camel == "CreateAt" || camel == "UpdateAt" {
|
||||
if table.isIgnoreColumns(field.Name.Source()) {
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package gen
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/collection"
|
||||
"github.com/zeromicro/go-zero/tools/goctl/model/sql/template"
|
||||
"github.com/zeromicro/go-zero/tools/goctl/util"
|
||||
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
|
||||
@@ -32,6 +34,17 @@ func genVars(table Table, withCache, postgreSql bool) (string, error) {
|
||||
"withCache": withCache,
|
||||
"postgreSql": postgreSql,
|
||||
"data": table,
|
||||
"ignoreColumns": func() string {
|
||||
var set = collection.NewSet()
|
||||
for _, c := range table.ignoreColumns {
|
||||
if postgreSql {
|
||||
set.AddStr(fmt.Sprintf(`"%s"`, c))
|
||||
} else {
|
||||
set.AddStr(fmt.Sprintf("\"`%s`\"", c))
|
||||
}
|
||||
}
|
||||
return strings.Join(set.KeysStr(), ", ")
|
||||
}(),
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
||||
@@ -10,11 +10,16 @@ import (
|
||||
const ModelCustom = `package {{.pkg}}
|
||||
{{if .withCache}}
|
||||
import (
|
||||
"github.com/lib/pq"
|
||||
"github.com/zeromicro/go-zero/core/stores/cache"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
)
|
||||
{{else}}
|
||||
import "github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
|
||||
import (
|
||||
"github.com/lib/pq"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
)
|
||||
{{end}}
|
||||
var _ {{.upperStartCamelObject}}Model = (*custom{{.upperStartCamelObject}}Model)(nil)
|
||||
|
||||
|
||||
@@ -1,20 +1,7 @@
|
||||
package template
|
||||
|
||||
import "fmt"
|
||||
import _ "embed"
|
||||
|
||||
// Vars defines a template for var block in model
|
||||
var Vars = fmt.Sprintf(
|
||||
`
|
||||
var (
|
||||
{{.lowerStartCamelObject}}FieldNames = builder.RawFieldNames(&{{.upperStartCamelObject}}{}{{if .postgreSql}},true{{end}})
|
||||
{{.lowerStartCamelObject}}Rows = strings.Join({{.lowerStartCamelObject}}FieldNames, ",")
|
||||
{{.lowerStartCamelObject}}RowsExpectAutoSet = {{if .postgreSql}}strings.Join(stringx.Remove({{.lowerStartCamelObject}}FieldNames, {{if .autoIncrement}}"{{.originalPrimaryKey}}",{{end}} "%screate_time%s", "%supdate_time%s", "%screate_at%s", "%supdate_at%s"), ","){{else}}strings.Join(stringx.Remove({{.lowerStartCamelObject}}FieldNames, {{if .autoIncrement}}"{{.originalPrimaryKey}}",{{end}} "%screate_time%s", "%supdate_time%s", "%screate_at%s", "%supdate_at%s"), ","){{end}}
|
||||
{{.lowerStartCamelObject}}RowsWithPlaceHolder = {{if .postgreSql}}builder.PostgreSqlJoin(stringx.Remove({{.lowerStartCamelObject}}FieldNames, "{{.originalPrimaryKey}}", "%screate_time%s", "%supdate_time%s", "%screate_at%s", "%supdate_at%s")){{else}}strings.Join(stringx.Remove({{.lowerStartCamelObject}}FieldNames, "{{.originalPrimaryKey}}", "%screate_time%s", "%supdate_time%s", "%screate_at%s", "%supdate_at%s"), "=?,") + "=?"{{end}}
|
||||
|
||||
{{if .withCache}}{{.cacheKeys}}{{end}}
|
||||
)
|
||||
`, "", "", "", "", "", "", "", "", // postgreSql mode
|
||||
"`", "`", "`", "`", "`", "`", "`", "`",
|
||||
"", "", "", "", "", "", "", "", // postgreSql mode
|
||||
"`", "`", "`", "`", "`", "`", "`", "`",
|
||||
)
|
||||
//go:embed vars.tpl
|
||||
var Vars string
|
||||
|
||||
8
tools/goctl/model/sql/template/vars.tpl
Normal file
8
tools/goctl/model/sql/template/vars.tpl
Normal file
@@ -0,0 +1,8 @@
|
||||
var (
|
||||
{{.lowerStartCamelObject}}FieldNames = builder.RawFieldNames(&{{.upperStartCamelObject}}{}{{if .postgreSql}}, true{{end}})
|
||||
{{.lowerStartCamelObject}}Rows = strings.Join({{.lowerStartCamelObject}}FieldNames, ",")
|
||||
{{.lowerStartCamelObject}}RowsExpectAutoSet = {{if .postgreSql}}strings.Join(stringx.Remove({{.lowerStartCamelObject}}FieldNames, {{if .autoIncrement}}"{{.originalPrimaryKey}}", {{end}} {{.ignoreColumns}}), ","){{else}}strings.Join(stringx.Remove({{.lowerStartCamelObject}}FieldNames, {{if .autoIncrement}}"{{.originalPrimaryKey}}", {{end}} {{.ignoreColumns}}), ","){{end}}
|
||||
{{.lowerStartCamelObject}}RowsWithPlaceHolder = {{if .postgreSql}}builder.PostgreSqlJoin(stringx.Remove({{.lowerStartCamelObject}}FieldNames, "{{.originalPrimaryKey}}", {{.ignoreColumns}})){{else}}strings.Join(stringx.Remove({{.lowerStartCamelObject}}FieldNames, "{{.originalPrimaryKey}}", {{.ignoreColumns}}), "=?,") + "=?"{{end}}
|
||||
|
||||
{{if .withCache}}{{.cacheKeys}}{{end}}
|
||||
)
|
||||
@@ -6,15 +6,15 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/stores/builder"
|
||||
"github.com/zeromicro/go-zero/core/stores/cache"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlc"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
"github.com/zeromicro/go-zero/core/stringx"
|
||||
"github.com/zeromicro/go-zero/tools/goctl/model/sql/builderx"
|
||||
)
|
||||
|
||||
var (
|
||||
studentFieldNames = builderx.RawFieldNames(&Student{})
|
||||
studentFieldNames = builder.RawFieldNames(&Student{})
|
||||
studentRows = strings.Join(studentFieldNames, ",")
|
||||
studentRowsExpectAutoSet = strings.Join(stringx.Remove(studentFieldNames, "`id`", "`create_time`", "`update_time`"), ",")
|
||||
studentRowsWithPlaceHolder = strings.Join(stringx.Remove(studentFieldNames, "`id`", "`create_time`", "`update_time`"), "=?,") + "=?"
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user