From 45be48a4ee92d151318c9961475fc80fa9a97039 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Sat, 2 Mar 2024 22:45:24 +0800 Subject: [PATCH] chore: coding style (#3959) --- core/mr/mapreduce_fuzz_test.go | 2 +- core/mr/mapreduce_fuzzcase_test.go | 2 +- core/stores/redis/redislock.go | 2 +- core/stringx/node_fuzz_test.go | 2 +- core/threading/stablerunner_test.go | 4 ++-- tools/goctl/api/parser/parser.go | 2 +- tools/goctl/pkg/parser/api/ast/ast.go | 1 + tools/goctl/pkg/parser/api/parser/analyzer.go | 1 + tools/goctl/pkg/parser/api/parser/analyzer_test.go | 8 +++++--- tools/goctl/pkg/parser/api/parser/parser.go | 11 +++++++++-- 10 files changed, 23 insertions(+), 12 deletions(-) diff --git a/core/mr/mapreduce_fuzz_test.go b/core/mr/mapreduce_fuzz_test.go index 936d3623..a275a3ce 100644 --- a/core/mr/mapreduce_fuzz_test.go +++ b/core/mr/mapreduce_fuzz_test.go @@ -13,7 +13,7 @@ import ( ) func FuzzMapReduce(f *testing.F) { - rand.Seed(time.Now().UnixNano()) + rand.NewSource(time.Now().UnixNano()) f.Add(int64(10), runtime.NumCPU()) f.Fuzz(func(t *testing.T, n int64, workers int) { diff --git a/core/mr/mapreduce_fuzzcase_test.go b/core/mr/mapreduce_fuzzcase_test.go index 3b30c31b..ffa3515e 100644 --- a/core/mr/mapreduce_fuzzcase_test.go +++ b/core/mr/mapreduce_fuzzcase_test.go @@ -20,7 +20,7 @@ import ( // If Fuzz stuck, we don't know why, because it only returns hung or unexpected, // so we need to simulate the fuzz test in test mode. func TestMapReduceRandom(t *testing.T) { - rand.Seed(time.Now().UnixNano()) + rand.NewSource(time.Now().UnixNano()) const ( times = 10000 diff --git a/core/stores/redis/redislock.go b/core/stores/redis/redislock.go index 2979ee50..f335d6c8 100644 --- a/core/stores/redis/redislock.go +++ b/core/stores/redis/redislock.go @@ -41,7 +41,7 @@ type RedisLock struct { } func init() { - rand.Seed(time.Now().UnixNano()) + rand.NewSource(time.Now().UnixNano()) } // NewRedisLock returns a RedisLock. diff --git a/core/stringx/node_fuzz_test.go b/core/stringx/node_fuzz_test.go index 6d752100..d256b513 100644 --- a/core/stringx/node_fuzz_test.go +++ b/core/stringx/node_fuzz_test.go @@ -11,7 +11,7 @@ import ( ) func FuzzNodeFind(f *testing.F) { - rand.Seed(time.Now().UnixNano()) + rand.NewSource(time.Now().UnixNano()) f.Add(10) f.Fuzz(func(t *testing.T, keys int) { diff --git a/core/threading/stablerunner_test.go b/core/threading/stablerunner_test.go index b39e81bc..b2da997c 100644 --- a/core/threading/stablerunner_test.go +++ b/core/threading/stablerunner_test.go @@ -12,7 +12,7 @@ import ( func TestStableRunner(t *testing.T) { size := bufSize * 2 - rand.Seed(time.Now().UnixNano()) + rand.NewSource(time.Now().UnixNano()) runner := NewStableRunner(func(v int) float64 { if v == 0 { time.Sleep(time.Millisecond * 100) @@ -49,7 +49,7 @@ func TestStableRunner(t *testing.T) { } func FuzzStableRunner(f *testing.F) { - rand.Seed(time.Now().UnixNano()) + rand.NewSource(time.Now().UnixNano()) f.Add(uint64(bufSize)) f.Fuzz(func(t *testing.T, n uint64) { runner := NewStableRunner(func(v int) float64 { diff --git a/tools/goctl/api/parser/parser.go b/tools/goctl/api/parser/parser.go index 0eff38fc..d29be54a 100644 --- a/tools/goctl/api/parser/parser.go +++ b/tools/goctl/api/parser/parser.go @@ -17,9 +17,9 @@ type parser struct { spec *spec.ApiSpec } +// Parse parses the api file. // Depreacted: use tools/goctl/pkg/parser/api/parser/parser.go:18 instead, // it will be removed in the future. -// Parse parses the api file. func Parse(filename string) (*spec.ApiSpec, error) { if env.UseExperimental() { return apiParser.Parse(filename, "") diff --git a/tools/goctl/pkg/parser/api/ast/ast.go b/tools/goctl/pkg/parser/api/ast/ast.go index 9b06e14a..b077ab8a 100644 --- a/tools/goctl/pkg/parser/api/ast/ast.go +++ b/tools/goctl/pkg/parser/api/ast/ast.go @@ -94,6 +94,7 @@ func (t *TokenNode) RawText() string { text = strings.TrimPrefix(text, `"`) text = strings.TrimSuffix(text, `"`) } + return text } diff --git a/tools/goctl/pkg/parser/api/parser/analyzer.go b/tools/goctl/pkg/parser/api/parser/analyzer.go index 35566b93..fc90164b 100644 --- a/tools/goctl/pkg/parser/api/parser/analyzer.go +++ b/tools/goctl/pkg/parser/api/parser/analyzer.go @@ -158,6 +158,7 @@ func (a *Analyzer) convertKV(kv []*ast.KVExpr) map[string]string { ret[key] = v.Value.Token.Text } } + return ret } diff --git a/tools/goctl/pkg/parser/api/parser/analyzer_test.go b/tools/goctl/pkg/parser/api/parser/analyzer_test.go index fa46ecf2..9f7976e4 100644 --- a/tools/goctl/pkg/parser/api/parser/analyzer_test.go +++ b/tools/goctl/pkg/parser/api/parser/analyzer_test.go @@ -8,9 +8,8 @@ import ( "strings" "testing" - "github.com/zeromicro/go-zero/tools/goctl/api/spec" - "github.com/stretchr/testify/assert" + "github.com/zeromicro/go-zero/tools/goctl/api/spec" "github.com/zeromicro/go-zero/tools/goctl/pkg/parser/api/assertx" ) @@ -43,6 +42,7 @@ func Test_Parse(t *testing.T) { return false }()) }) + t.Run("invalid", func(t *testing.T) { data, err := os.ReadFile("./testdata/invalid.api") assert.NoError(t, err) @@ -64,15 +64,17 @@ func Test_Parse(t *testing.T) { assertx.Error(t, err) } }) + t.Run("circleImport", func(t *testing.T) { _, err := Parse("./testdata/base.api", nil) assertx.Error(t, err) }) + t.Run("link_import", func(t *testing.T) { _, err := Parse("./testdata/link_import.api", nil) assert.Nil(t, err) }) - + t.Run("duplicate_types", func(t *testing.T) { _, err := Parse("./testdata/duplicate_type.api", nil) assertx.Error(t, err) diff --git a/tools/goctl/pkg/parser/api/parser/parser.go b/tools/goctl/pkg/parser/api/parser/parser.go index 92806d52..2fe60ca6 100644 --- a/tools/goctl/pkg/parser/api/parser/parser.go +++ b/tools/goctl/pkg/parser/api/parser/parser.go @@ -1158,15 +1158,18 @@ func (p *Parser) parseAtServerKVExpression() *ast.KVExpr { if !p.advanceIfPeekTokenIs(token.IDENT) { return nil } + pathText += p.curTok.Text - if p.peekTokenIs(token.SUB) { // 解析 abc-efg 格式 + if p.peekTokenIs(token.SUB) { // parse abc-efg format if !p.nextToken() { return nil } + pathText += p.curTok.Text if !p.advanceIfPeekTokenIs(token.IDENT) { return nil } + pathText += p.curTok.Text } @@ -1203,6 +1206,7 @@ func (p *Parser) parseAtServerKVExpression() *ast.KVExpr { return nil } } + if !p.nextToken() { return nil } @@ -1263,15 +1267,18 @@ func (p *Parser) parseAtServerKVExpression() *ast.KVExpr { if !p.advanceIfPeekTokenIs(token.IDENT) { return nil } + pathText += p.curTok.Text - if p.peekTokenIs(token.SUB) { // 解析 abc-efg 格式 + if p.peekTokenIs(token.SUB) { // parse abc-efg format if !p.nextToken() { return nil } + pathText += p.curTok.Text if !p.advanceIfPeekTokenIs(token.IDENT) { return nil } + pathText += p.curTok.Text }