refactor(transport): format import and init slice cap (#2741)

* fix(transport): format import and init slice cap

* fix lint: local-prefixes: github.com/go-kratos

---------

Co-authored-by: haiyux <haiyux@foxmail.com>
pull/2612/head
jessetang 2 years ago committed by GitHub
parent 446774f9e5
commit d470886977
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      .golangci.yml
  2. 4
      contrib/opensergo/opensergo.go
  3. 10
      transport/grpc/client.go
  4. 4
      transport/grpc/resolver/discovery/builder.go
  5. 21
      transport/grpc/resolver/discovery/resolver.go
  6. 18
      transport/grpc/server.go
  7. 3
      transport/http/binding/bind.go
  8. 88
      transport/http/binding/bind_test.go
  9. 7
      transport/http/binding/encode.go
  10. 4
      transport/http/client_test.go
  11. 4
      transport/http/codec.go
  12. 8
      transport/http/resolver.go
  13. 7
      transport/http/server.go

@ -68,4 +68,4 @@ linters-settings:
# recommend 10-20 # recommend 10-20
min-complexity: 50 min-complexity: 50
goimports: goimports:
local-prefixes: github.com/go-kratos/kratos # Put imports beginning with prefix after 3rd-party packages local-prefixes: github.com/go-kratos # Put imports beginning with prefix after 3rd-party packages

@ -9,8 +9,6 @@ import (
"strconv" "strconv"
"time" "time"
"github.com/go-kratos/kratos/v2"
v1 "github.com/opensergo/opensergo-go/proto/service_contract/v1" v1 "github.com/opensergo/opensergo-go/proto/service_contract/v1"
"golang.org/x/net/context" "golang.org/x/net/context"
"google.golang.org/genproto/googleapis/api/annotations" "google.golang.org/genproto/googleapis/api/annotations"
@ -19,6 +17,8 @@ import (
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect" "google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry" "google.golang.org/protobuf/reflect/protoregistry"
"github.com/go-kratos/kratos/v2"
) )
type Option func(*options) type Option func(*options)

@ -6,6 +6,11 @@ import (
"fmt" "fmt"
"time" "time"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
grpcinsecure "google.golang.org/grpc/credentials/insecure"
grpcmd "google.golang.org/grpc/metadata"
"github.com/go-kratos/kratos/v2/log" "github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/middleware" "github.com/go-kratos/kratos/v2/middleware"
"github.com/go-kratos/kratos/v2/registry" "github.com/go-kratos/kratos/v2/registry"
@ -16,11 +21,6 @@ import (
// init resolver // init resolver
_ "github.com/go-kratos/kratos/v2/transport/grpc/resolver/direct" _ "github.com/go-kratos/kratos/v2/transport/grpc/resolver/direct"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
grpcinsecure "google.golang.org/grpc/credentials/insecure"
grpcmd "google.golang.org/grpc/metadata"
) )
func init() { func init() {

@ -6,10 +6,10 @@ import (
"strings" "strings"
"time" "time"
"github.com/go-kratos/kratos/v2/registry"
"github.com/google/uuid" "github.com/google/uuid"
"google.golang.org/grpc/resolver" "google.golang.org/grpc/resolver"
"github.com/go-kratos/kratos/v2/registry"
) )
const name = "discovery" const name = "discovery"

@ -6,13 +6,13 @@ import (
"errors" "errors"
"time" "time"
"google.golang.org/grpc/attributes"
"google.golang.org/grpc/resolver"
"github.com/go-kratos/aegis/subset"
"github.com/go-kratos/kratos/v2/internal/endpoint" "github.com/go-kratos/kratos/v2/internal/endpoint"
"github.com/go-kratos/kratos/v2/log" "github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/registry" "github.com/go-kratos/kratos/v2/registry"
"github.com/go-kratos/aegis/subset"
"google.golang.org/grpc/attributes"
"google.golang.org/grpc/resolver"
) )
type discoveryResolver struct { type discoveryResolver struct {
@ -49,9 +49,10 @@ func (r *discoveryResolver) watch() {
} }
func (r *discoveryResolver) update(ins []*registry.ServiceInstance) { func (r *discoveryResolver) update(ins []*registry.ServiceInstance) {
addrs := make([]resolver.Address, 0) var (
endpoints := make(map[string]struct{}) endpoints = make(map[string]struct{})
filtered := make([]*registry.ServiceInstance, 0, len(ins)) filtered = make([]*registry.ServiceInstance, 0, len(ins))
)
for _, in := range ins { for _, in := range ins {
ept, err := endpoint.ParseEndpoint(in.Endpoints, endpoint.Scheme("grpc", !r.insecure)) ept, err := endpoint.ParseEndpoint(in.Endpoints, endpoint.Scheme("grpc", !r.insecure))
if err != nil { if err != nil {
@ -70,15 +71,16 @@ func (r *discoveryResolver) update(ins []*registry.ServiceInstance) {
if r.subsetSize != 0 { if r.subsetSize != 0 {
filtered = subset.Subset(r.selecterKey, filtered, r.subsetSize) filtered = subset.Subset(r.selecterKey, filtered, r.subsetSize)
} }
addrs := make([]resolver.Address, 0, len(filtered))
for _, in := range filtered { for _, in := range filtered {
ept, _ := endpoint.ParseEndpoint(in.Endpoints, endpoint.Scheme("grpc", !r.insecure)) ept, _ := endpoint.ParseEndpoint(in.Endpoints, endpoint.Scheme("grpc", !r.insecure))
endpoints[ept] = struct{}{} endpoints[ept] = struct{}{}
addr := resolver.Address{ addr := resolver.Address{
ServerName: in.Name, ServerName: in.Name,
Attributes: parseAttributes(in.Metadata), Attributes: parseAttributes(in.Metadata).WithValue("rawServiceInstance", in),
Addr: ept, Addr: ept,
} }
addr.Attributes = addr.Attributes.WithValue("rawServiceInstance", in)
addrs = append(addrs, addr) addrs = append(addrs, addr)
} }
if len(addrs) == 0 { if len(addrs) == 0 {
@ -89,7 +91,6 @@ func (r *discoveryResolver) update(ins []*registry.ServiceInstance) {
if err != nil { if err != nil {
log.Errorf("[resolver] failed to update state: %s", err) log.Errorf("[resolver] failed to update state: %s", err)
} }
if r.debugLog { if r.debugLog {
b, _ := json.Marshal(filtered) b, _ := json.Marshal(filtered)
log.Infof("[resolver] update instances: %s", b) log.Infof("[resolver] update instances: %s", b)

@ -7,22 +7,20 @@ import (
"net/url" "net/url"
"time" "time"
"github.com/go-kratos/kratos/v2/internal/endpoint"
"github.com/go-kratos/kratos/v2/internal/matcher"
apimd "github.com/go-kratos/kratos/v2/api/metadata"
"github.com/go-kratos/kratos/v2/internal/host"
"github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/middleware"
"github.com/go-kratos/kratos/v2/transport"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/admin" "google.golang.org/grpc/admin"
"google.golang.org/grpc/credentials" "google.golang.org/grpc/credentials"
"google.golang.org/grpc/health" "google.golang.org/grpc/health"
"google.golang.org/grpc/health/grpc_health_v1" "google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/reflection" "google.golang.org/grpc/reflection"
apimd "github.com/go-kratos/kratos/v2/api/metadata"
"github.com/go-kratos/kratos/v2/internal/endpoint"
"github.com/go-kratos/kratos/v2/internal/host"
"github.com/go-kratos/kratos/v2/internal/matcher"
"github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/middleware"
"github.com/go-kratos/kratos/v2/transport"
) )
var ( var (

@ -4,10 +4,9 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"github.com/go-kratos/kratos/v2/errors"
"github.com/go-kratos/kratos/v2/encoding" "github.com/go-kratos/kratos/v2/encoding"
"github.com/go-kratos/kratos/v2/encoding/form" "github.com/go-kratos/kratos/v2/encoding/form"
"github.com/go-kratos/kratos/v2/errors"
) )
// BindQuery bind vars parameters to target. // BindQuery bind vars parameters to target.

@ -1,6 +1,7 @@
package binding package binding
import ( import (
"errors"
"io" "io"
"net/http" "net/http"
"net/url" "net/url"
@ -8,47 +9,47 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/go-kratos/kratos/v2/errors" kratoserror "github.com/go-kratos/kratos/v2/errors"
) )
func TestBindQuery(t *testing.T) { type (
type TestBind struct { TestBind struct {
Name string `json:"name"` Name string `json:"name"`
URL string `json:"url"` URL string `json:"url"`
} }
TestBind2 struct {
type TestBind2 struct {
Age int `json:"age"` Age int `json:"age"`
} }
p1 := TestBind{} )
p2 := TestBind2{}
func TestBindQuery(t *testing.T) {
type args struct { type args struct {
vars url.Values vars url.Values
target interface{} target interface{}
} }
tests := []struct { tests := []struct {
name string name string
args args args args
wantErr bool err error
want interface{} want interface{}
}{ }{
{ {
name: "test", name: "test",
args: args{ args: args{
vars: map[string][]string{"name": {"kratos"}, "url": {"https://go-kratos.dev/"}}, vars: map[string][]string{"name": {"kratos"}, "url": {"https://go-kratos.dev/"}},
target: &p1, target: &TestBind{},
}, },
wantErr: false, err: nil,
want: &TestBind{"kratos", "https://go-kratos.dev/"}, want: &TestBind{"kratos", "https://go-kratos.dev/"},
}, },
{ {
name: "test1", name: "test1",
args: args{ args: args{
vars: map[string][]string{"age": {"kratos"}, "url": {"https://go-kratos.dev/"}}, vars: map[string][]string{"age": {"kratos"}, "url": {"https://go-kratos.dev/"}},
target: &p2, target: &TestBind2{},
}, },
wantErr: true, err: kratoserror.BadRequest("CODEC", "Field Namespace:age ERROR:Invalid Integer Value 'kratos' Type 'int' Namespace 'age'"),
want: errors.BadRequest("CODEC", ""),
}, },
{ {
name: "test2", name: "test2",
@ -56,18 +57,17 @@ func TestBindQuery(t *testing.T) {
vars: map[string][]string{"age": {"1"}, "url": {"https://go-kratos.dev/"}}, vars: map[string][]string{"age": {"1"}, "url": {"https://go-kratos.dev/"}},
target: &TestBind2{}, target: &TestBind2{},
}, },
wantErr: false, err: nil,
want: &TestBind2{Age: 1}, want: &TestBind2{Age: 1},
}, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if err := BindQuery(tt.args.vars, tt.args.target); (err != nil) != tt.wantErr { err := BindQuery(tt.args.vars, tt.args.target)
t.Errorf("BindQuery() error = %v, wantErr %v", err, tt.wantErr) if !kratoserror.Is(err, tt.err) {
} else { t.Fatalf("BindQuery() error = %v, err %v", err, tt.err)
t.Log(err)
} }
if !tt.wantErr && !reflect.DeepEqual(tt.args.target, tt.want) { if err == nil && !reflect.DeepEqual(tt.args.target, tt.want) {
t.Errorf("BindQuery() target = %v, want %v", tt.args.target, tt.want) t.Errorf("BindQuery() target = %v, want %v", tt.args.target, tt.want)
} }
}) })
@ -75,32 +75,25 @@ func TestBindQuery(t *testing.T) {
} }
func TestBindForm(t *testing.T) { func TestBindForm(t *testing.T) {
type TestBind struct {
Name string `json:"name"`
URL string `json:"url"`
}
type TestBind2 struct {
Age int `json:"age"`
}
p1 := TestBind{}
type args struct { type args struct {
req *http.Request req *http.Request
target interface{} target interface{}
} }
tests := []struct { tests := []struct {
name string name string
args args args args
wantErr bool err error
want interface{} want *TestBind
}{ }{
{ {
name: "error not nil", name: "error not nil",
args: args{ args: args{
req: &http.Request{Method: http.MethodPost}, req: &http.Request{Method: http.MethodPost},
target: &p1, target: &TestBind{},
}, },
wantErr: true, err: errors.New("missing form body"),
want: nil, want: nil,
}, },
{ {
name: "error is nil", name: "error is nil",
@ -110,10 +103,10 @@ func TestBindForm(t *testing.T) {
Header: http.Header{"Content-Type": {"application/x-www-form-urlencoded; param=value"}}, Header: http.Header{"Content-Type": {"application/x-www-form-urlencoded; param=value"}},
Body: io.NopCloser(strings.NewReader("name=kratos&url=https://go-kratos.dev/")), Body: io.NopCloser(strings.NewReader("name=kratos&url=https://go-kratos.dev/")),
}, },
target: &p1, target: &TestBind{},
}, },
wantErr: false, err: nil,
want: &TestBind{"kratos", "https://go-kratos.dev/"}, want: &TestBind{"kratos", "https://go-kratos.dev/"},
}, },
{ {
name: "error BadRequest", name: "error BadRequest",
@ -125,18 +118,17 @@ func TestBindForm(t *testing.T) {
}, },
target: &TestBind2{}, target: &TestBind2{},
}, },
wantErr: true, err: kratoserror.BadRequest("CODEC", "Field Namespace:age ERROR:Invalid Integer Value 'a' Type 'int' Namespace 'age'"),
want: nil, want: nil,
}, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
err := BindForm(tt.args.req, tt.args.target) err := BindForm(tt.args.req, tt.args.target)
if (err != nil) != tt.wantErr { if !reflect.DeepEqual(err, tt.err) {
t.Errorf("BindForm() error = %v, wantErr %v", err, tt.wantErr) t.Fatalf("BindForm() error = %v, err %v", err, tt.err)
} }
t.Log(err) if err == nil && !reflect.DeepEqual(tt.args.target, tt.want) {
if !tt.wantErr && !reflect.DeepEqual(tt.args.target, tt.want) {
t.Errorf("BindForm() target = %v, want %v", tt.args.target, tt.want) t.Errorf("BindForm() target = %v, want %v", tt.args.target, tt.want)
} }
}) })

@ -20,13 +20,12 @@ func EncodeURL(pathTemplate string, msg interface{}, needQuery bool) string {
pathParams := make(map[string]struct{}) pathParams := make(map[string]struct{})
path := reg.ReplaceAllStringFunc(pathTemplate, func(in string) string { path := reg.ReplaceAllStringFunc(pathTemplate, func(in string) string {
// it's unreachable because the reg means that must have more than one char in {} // it's unreachable because the reg means that must have more than one char in {}
//if len(in) < 4 { //nolint:gomnd // ** explain the 4 number here :-) ** // if len(in) < 4 { //nolint:gomnd // ** explain the 4 number here :-) **
// return in // return in
//} // }
key := in[1 : len(in)-1] key := in[1 : len(in)-1]
value := queryParams.Get(key)
pathParams[key] = struct{}{} pathParams[key] = struct{}{}
return value return queryParams.Get(key)
}) })
if !needQuery { if !needQuery {
if v, ok := msg.(proto.Message); ok { if v, ok := msg.(proto.Message); ok {

@ -227,7 +227,7 @@ func TestDefaultResponseDecoder(t *testing.T) {
resp1 := &http.Response{ resp1 := &http.Response{
Header: make(http.Header), Header: make(http.Header),
StatusCode: 200, StatusCode: 200,
Body: io.NopCloser(bytes.NewBufferString("{\"a\":\"1\", \"b\": 2}")), Body: io.NopCloser(bytes.NewBufferString(`{"a":"1", "b": 2}`)),
} }
v1 := &struct { v1 := &struct {
A string `json:"a"` A string `json:"a"`
@ -279,7 +279,7 @@ func TestDefaultErrorDecoder(t *testing.T) {
resp2 := &http.Response{ resp2 := &http.Response{
Header: make(http.Header), Header: make(http.Header),
StatusCode: 500, StatusCode: 500,
Body: io.NopCloser(bytes.NewBufferString("{\"code\":54321, \"message\": \"hi\", \"reason\": \"FOO\"}")), Body: io.NopCloser(bytes.NewBufferString(`{"code":54321, "message": "hi", "reason": "FOO"}`)),
} }
err := DefaultErrorDecoder(context.TODO(), resp2) err := DefaultErrorDecoder(context.TODO(), resp2)
if err == nil { if err == nil {

@ -7,12 +7,12 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"github.com/gorilla/mux"
"github.com/go-kratos/kratos/v2/encoding" "github.com/go-kratos/kratos/v2/encoding"
"github.com/go-kratos/kratos/v2/errors" "github.com/go-kratos/kratos/v2/errors"
"github.com/go-kratos/kratos/v2/internal/httputil" "github.com/go-kratos/kratos/v2/internal/httputil"
"github.com/go-kratos/kratos/v2/transport/http/binding" "github.com/go-kratos/kratos/v2/transport/http/binding"
"github.com/gorilla/mux"
) )
// SupportPackageIsVersion1 These constants should not be referenced from any other code. // SupportPackageIsVersion1 These constants should not be referenced from any other code.

@ -7,13 +7,13 @@ import (
"strings" "strings"
"time" "time"
"github.com/google/uuid"
"github.com/go-kratos/aegis/subset"
"github.com/go-kratos/kratos/v2/internal/endpoint" "github.com/go-kratos/kratos/v2/internal/endpoint"
"github.com/go-kratos/kratos/v2/log" "github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/registry" "github.com/go-kratos/kratos/v2/registry"
"github.com/go-kratos/kratos/v2/selector" "github.com/go-kratos/kratos/v2/selector"
"github.com/go-kratos/aegis/subset"
"github.com/google/uuid"
) )
// Target is resolver target // Target is resolver target
@ -135,7 +135,7 @@ func (r *resolver) update(services []*registry.ServiceInstance) bool {
if r.subsetSize != 0 { if r.subsetSize != 0 {
filtered = subset.Subset(r.selecterKey, filtered, r.subsetSize) filtered = subset.Subset(r.selecterKey, filtered, r.subsetSize)
} }
nodes := make([]selector.Node, 0) nodes := make([]selector.Node, 0, len(filtered))
for _, ins := range filtered { for _, ins := range filtered {
ept, _ := endpoint.ParseEndpoint(ins.Endpoints, endpoint.Scheme("http", !r.insecure)) ept, _ := endpoint.ParseEndpoint(ins.Endpoints, endpoint.Scheme("http", !r.insecure))
nodes = append(nodes, selector.NewNode("http", ept, ins)) nodes = append(nodes, selector.NewNode("http", ept, ins))

@ -9,15 +9,14 @@ import (
"net/url" "net/url"
"time" "time"
"github.com/go-kratos/kratos/v2/internal/endpoint" "github.com/gorilla/mux"
"github.com/go-kratos/kratos/v2/internal/matcher"
"github.com/go-kratos/kratos/v2/internal/endpoint"
"github.com/go-kratos/kratos/v2/internal/host" "github.com/go-kratos/kratos/v2/internal/host"
"github.com/go-kratos/kratos/v2/internal/matcher"
"github.com/go-kratos/kratos/v2/log" "github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/middleware" "github.com/go-kratos/kratos/v2/middleware"
"github.com/go-kratos/kratos/v2/transport" "github.com/go-kratos/kratos/v2/transport"
"github.com/gorilla/mux"
) )
var ( var (

Loading…
Cancel
Save