Skip to content

Commit 1cd1449

Browse files
authored
add package contrib/rpc/grpcx (#2169)
1 parent 55690f3 commit 1cd1449

File tree

112 files changed

+5130
-212
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

112 files changed

+5130
-212
lines changed

.github/workflows/gf.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ jobs:
3535

3636
# Service containers to run with `code-test`
3737
services:
38+
# Etcd service.
39+
# docker run -d --name etcd -p 2379:2379 -e ALLOW_NONE_AUTHENTICATION=yes loads/etcd:3.4.24
40+
etcd:
41+
image: loads/etcd:3.4.24
42+
env:
43+
ALLOW_NONE_AUTHENTICATION: yes
44+
ports:
45+
- 2379:2379
46+
3847
# Redis backend server.
3948
redis:
4049
image : loads/redis:7.0
@@ -54,7 +63,6 @@ jobs:
5463
MYSQL_DATABASE : test
5564
MYSQL_ROOT_PASSWORD: 12345678
5665
ports:
57-
# Maps tcp port 3306 on service container to the host
5866
- 3306:3306
5967

6068
# PostgreSQL backend server.

cmd/gf/internal/cmd/cmd_gen_pb.go

Lines changed: 27 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@ package cmd
22

33
import (
44
"context"
5-
"fmt"
65

76
"github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog"
8-
"github.com/gogf/gf/v2/container/gset"
97
"github.com/gogf/gf/v2/frame/g"
10-
"github.com/gogf/gf/v2/os/genv"
118
"github.com/gogf/gf/v2/os/gfile"
129
"github.com/gogf/gf/v2/os/gproc"
1310
)
@@ -16,64 +13,53 @@ type (
1613
cGenPb struct{}
1714
cGenPbInput struct {
1815
g.Meta `name:"pb" brief:"parse proto files and generate protobuf go files"`
16+
Path string `name:"path" short:"p" dc:"protobuf file folder path" d:"manifest/protobuf"`
17+
Output string `name:"output" short:"o" dc:"output folder path storing generated go files" d:"api"`
1918
}
2019
cGenPbOutput struct{}
2120
)
2221

2322
func (c cGenPb) Pb(ctx context.Context, in cGenPbInput) (out *cGenPbOutput, err error) {
2423
// Necessary check.
25-
if gproc.SearchBinary("protoc") == "" {
24+
protoc := gproc.SearchBinary("protoc")
25+
if protoc == "" {
2626
mlog.Fatalf(`command "protoc" not found in your environment, please install protoc first to proceed this command`)
2727
}
2828

2929
// protocol fold checks.
30-
protoFolder := "protocol"
31-
if !gfile.Exists(protoFolder) {
32-
mlog.Fatalf(`proto files folder "%s" does not exist`, protoFolder)
30+
protoPath := gfile.RealPath(in.Path)
31+
if protoPath == "" {
32+
mlog.Fatalf(`proto files folder "%s" does not exist`, in.Path)
3333
}
34+
// output path checks.
35+
outputPath := gfile.RealPath(in.Output)
36+
if outputPath == "" {
37+
mlog.Fatalf(`output folder "%s" does not exist`, in.Output)
38+
}
39+
3440
// folder scanning.
35-
files, err := gfile.ScanDirFile(protoFolder, "*.proto", true)
41+
files, err := gfile.ScanDirFile(protoPath, "*.proto", true)
3642
if err != nil {
3743
mlog.Fatal(err)
3844
}
3945
if len(files) == 0 {
40-
mlog.Fatalf(`no proto files found in folder "%s"`, protoFolder)
46+
mlog.Fatalf(`no proto files found in folder "%s"`, in.Path)
4147
}
42-
dirSet := gset.NewStrSet()
43-
for _, file := range files {
44-
dirSet.Add(gfile.Dir(file))
48+
49+
if err = gfile.Chdir(protoPath); err != nil {
50+
mlog.Fatal(err)
4551
}
46-
var (
47-
servicePath = gfile.RealPath(".")
48-
goPathSrc = gfile.RealPath(gfile.Join(genv.Get("GOPATH").String(), "src"))
49-
)
50-
dirSet.Iterator(func(protoDirPath string) bool {
51-
parsingCommand := fmt.Sprintf(
52-
"protoc --gofast_out=plugins=grpc:. %s/*.proto -I%s",
53-
protoDirPath,
54-
servicePath,
55-
)
56-
if goPathSrc != "" {
57-
parsingCommand += " -I" + goPathSrc
58-
}
59-
mlog.Print(parsingCommand)
60-
if output, err := gproc.ShellExec(ctx, parsingCommand); err != nil {
61-
mlog.Print(output)
52+
for _, file := range files {
53+
var command = gproc.NewProcess(protoc, nil)
54+
command.Args = append(command.Args, "--proto_path="+gfile.Pwd())
55+
command.Args = append(command.Args, "--go_out=paths=source_relative:"+outputPath)
56+
command.Args = append(command.Args, "--go-grpc_out=paths=source_relative:"+outputPath)
57+
command.Args = append(command.Args, file)
58+
mlog.Print(command.String())
59+
if err = command.Run(ctx); err != nil {
6260
mlog.Fatal(err)
6361
}
64-
return true
65-
})
66-
// Custom replacement.
67-
//pbFolder := "protobuf"
68-
//_, _ = gfile.ScanDirFileFunc(pbFolder, "*.go", true, func(path string) string {
69-
// content := gfile.GetContents(path)
70-
// content = gstr.ReplaceByArray(content, g.SliceStr{
71-
// `gtime "gtime"`, `gtime "github.com/gogf/gf/v2/os/gtime"`,
72-
// })
73-
// _ = gfile.PutContents(path, content)
74-
// utils.GoFmt(path)
75-
// return path
76-
//})
62+
}
7763
mlog.Print("done!")
7864
return
7965
}

container/gmap/gmap_hash_any_any_map.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,3 +514,24 @@ func (m *AnyAnyMap) DeepCopy() interface{} {
514514
}
515515
return NewFrom(data, m.mu.IsSafe())
516516
}
517+
518+
// IsSubOf checks whether the current map is a sub-map of `other`.
519+
func (m *AnyAnyMap) IsSubOf(other *AnyAnyMap) bool {
520+
if m == other {
521+
return true
522+
}
523+
m.mu.RLock()
524+
defer m.mu.RUnlock()
525+
other.mu.RLock()
526+
defer other.mu.RUnlock()
527+
for key, value := range m.data {
528+
otherValue, ok := other.data[key]
529+
if !ok {
530+
return false
531+
}
532+
if otherValue != value {
533+
return false
534+
}
535+
}
536+
return true
537+
}

container/gmap/gmap_hash_int_any_map.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/gogf/gf/v2/util/gconv"
1717
)
1818

19+
// IntAnyMap implements map[int]interface{} with RWMutex that has switch.
1920
type IntAnyMap struct {
2021
mu rwmutex.RWMutex
2122
data map[int]interface{}
@@ -514,3 +515,24 @@ func (m *IntAnyMap) DeepCopy() interface{} {
514515
}
515516
return NewIntAnyMapFrom(data, m.mu.IsSafe())
516517
}
518+
519+
// IsSubOf checks whether the current map is a sub-map of `other`.
520+
func (m *IntAnyMap) IsSubOf(other *IntAnyMap) bool {
521+
if m == other {
522+
return true
523+
}
524+
m.mu.RLock()
525+
defer m.mu.RUnlock()
526+
other.mu.RLock()
527+
defer other.mu.RUnlock()
528+
for key, value := range m.data {
529+
otherValue, ok := other.data[key]
530+
if !ok {
531+
return false
532+
}
533+
if otherValue != value {
534+
return false
535+
}
536+
}
537+
return true
538+
}

container/gmap/gmap_hash_int_int_map.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/gogf/gf/v2/util/gconv"
1414
)
1515

16+
// IntIntMap implements map[int]int with RWMutex that has switch.
1617
type IntIntMap struct {
1718
mu rwmutex.RWMutex
1819
data map[int]int
@@ -484,3 +485,24 @@ func (m *IntIntMap) DeepCopy() interface{} {
484485
}
485486
return NewIntIntMapFrom(data, m.mu.IsSafe())
486487
}
488+
489+
// IsSubOf checks whether the current map is a sub-map of `other`.
490+
func (m *IntIntMap) IsSubOf(other *IntIntMap) bool {
491+
if m == other {
492+
return true
493+
}
494+
m.mu.RLock()
495+
defer m.mu.RUnlock()
496+
other.mu.RLock()
497+
defer other.mu.RUnlock()
498+
for key, value := range m.data {
499+
otherValue, ok := other.data[key]
500+
if !ok {
501+
return false
502+
}
503+
if otherValue != value {
504+
return false
505+
}
506+
}
507+
return true
508+
}

container/gmap/gmap_hash_int_str_map.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/gogf/gf/v2/util/gconv"
1414
)
1515

16+
// IntStrMap implements map[int]string with RWMutex that has switch.
1617
type IntStrMap struct {
1718
mu rwmutex.RWMutex
1819
data map[int]string
@@ -484,3 +485,24 @@ func (m *IntStrMap) DeepCopy() interface{} {
484485
}
485486
return NewIntStrMapFrom(data, m.mu.IsSafe())
486487
}
488+
489+
// IsSubOf checks whether the current map is a sub-map of `other`.
490+
func (m *IntStrMap) IsSubOf(other *IntStrMap) bool {
491+
if m == other {
492+
return true
493+
}
494+
m.mu.RLock()
495+
defer m.mu.RUnlock()
496+
other.mu.RLock()
497+
defer other.mu.RUnlock()
498+
for key, value := range m.data {
499+
otherValue, ok := other.data[key]
500+
if !ok {
501+
return false
502+
}
503+
if otherValue != value {
504+
return false
505+
}
506+
}
507+
return true
508+
}

container/gmap/gmap_hash_str_any_map.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,3 +501,24 @@ func (m *StrAnyMap) DeepCopy() interface{} {
501501
}
502502
return NewStrAnyMapFrom(data, m.mu.IsSafe())
503503
}
504+
505+
// IsSubOf checks whether the current map is a sub-map of `other`.
506+
func (m *StrAnyMap) IsSubOf(other *StrAnyMap) bool {
507+
if m == other {
508+
return true
509+
}
510+
m.mu.RLock()
511+
defer m.mu.RUnlock()
512+
other.mu.RLock()
513+
defer other.mu.RUnlock()
514+
for key, value := range m.data {
515+
otherValue, ok := other.data[key]
516+
if !ok {
517+
return false
518+
}
519+
if otherValue != value {
520+
return false
521+
}
522+
}
523+
return true
524+
}

container/gmap/gmap_hash_str_int_map.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/gogf/gf/v2/util/gconv"
1515
)
1616

17+
// StrIntMap implements map[string]int with RWMutex that has switch.
1718
type StrIntMap struct {
1819
mu rwmutex.RWMutex
1920
data map[string]int
@@ -488,3 +489,24 @@ func (m *StrIntMap) DeepCopy() interface{} {
488489
}
489490
return NewStrIntMapFrom(data, m.mu.IsSafe())
490491
}
492+
493+
// IsSubOf checks whether the current map is a sub-map of `other`.
494+
func (m *StrIntMap) IsSubOf(other *StrIntMap) bool {
495+
if m == other {
496+
return true
497+
}
498+
m.mu.RLock()
499+
defer m.mu.RUnlock()
500+
other.mu.RLock()
501+
defer other.mu.RUnlock()
502+
for key, value := range m.data {
503+
otherValue, ok := other.data[key]
504+
if !ok {
505+
return false
506+
}
507+
if otherValue != value {
508+
return false
509+
}
510+
}
511+
return true
512+
}

container/gmap/gmap_hash_str_str_map.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/gogf/gf/v2/util/gconv"
1515
)
1616

17+
// StrStrMap implements map[string]string with RWMutex that has switch.
1718
type StrStrMap struct {
1819
mu rwmutex.RWMutex
1920
data map[string]string
@@ -477,3 +478,24 @@ func (m *StrStrMap) DeepCopy() interface{} {
477478
}
478479
return NewStrStrMapFrom(data, m.mu.IsSafe())
479480
}
481+
482+
// IsSubOf checks whether the current map is a sub-map of `other`.
483+
func (m *StrStrMap) IsSubOf(other *StrStrMap) bool {
484+
if m == other {
485+
return true
486+
}
487+
m.mu.RLock()
488+
defer m.mu.RUnlock()
489+
other.mu.RLock()
490+
defer other.mu.RUnlock()
491+
for key, value := range m.data {
492+
otherValue, ok := other.data[key]
493+
if !ok {
494+
return false
495+
}
496+
if otherValue != value {
497+
return false
498+
}
499+
}
500+
return true
501+
}

container/gmap/gmap_z_unit_hash_any_any_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,3 +391,18 @@ func Test_AnyAnyMap_DeepCopy(t *testing.T) {
391391
t.AssertNE(m.Get("k1"), n.Get("k1"))
392392
})
393393
}
394+
395+
func Test_AnyAnyMap_IsSubOf(t *testing.T) {
396+
gtest.C(t, func(t *gtest.T) {
397+
m1 := gmap.NewAnyAnyMapFrom(g.MapAnyAny{
398+
"k1": "v1",
399+
"k2": "v2",
400+
})
401+
m2 := gmap.NewAnyAnyMapFrom(g.MapAnyAny{
402+
"k2": "v2",
403+
})
404+
t.Assert(m1.IsSubOf(m2), false)
405+
t.Assert(m2.IsSubOf(m1), true)
406+
t.Assert(m2.IsSubOf(m2), true)
407+
})
408+
}

container/gmap/gmap_z_unit_hash_int_any_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,3 +375,18 @@ func Test_IntAnyMap_DeepCopy(t *testing.T) {
375375
t.AssertNE(m.Get(1), n.Get(1))
376376
})
377377
}
378+
379+
func Test_IntAnyMap_IsSubOf(t *testing.T) {
380+
gtest.C(t, func(t *gtest.T) {
381+
m1 := gmap.NewIntAnyMapFrom(g.MapIntAny{
382+
1: "v1",
383+
2: "v2",
384+
})
385+
m2 := gmap.NewIntAnyMapFrom(g.MapIntAny{
386+
2: "v2",
387+
})
388+
t.Assert(m1.IsSubOf(m2), false)
389+
t.Assert(m2.IsSubOf(m1), true)
390+
t.Assert(m2.IsSubOf(m2), true)
391+
})
392+
}

0 commit comments

Comments
 (0)