mirror of
				https://github.com/go-kratos/kratos.git
				synced 2025-10-30 23:47:59 +02:00 
			
		
		
		
	fix lint check (#746)
This commit is contained in:
		
							
								
								
									
										5
									
								
								.github/workflows/go.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										5
									
								
								.github/workflows/go.yml
									
									
									
									
										vendored
									
									
								
							| @@ -24,6 +24,11 @@ jobs: | ||||
|     - name: Test | ||||
|       run: go test -v ./... | ||||
|  | ||||
|     - name: Lint | ||||
|       run: | | ||||
|         go get golang.org/x/lint/golint | ||||
|         golint ./... | ||||
|  | ||||
|     - name: Kratos | ||||
|       run: | | ||||
|         cd cmd/kratos | ||||
|   | ||||
| @@ -24,12 +24,14 @@ func NewRepo(url string) *Repo { | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // Path returns the repository cache path. | ||||
| func (r *Repo) Path() string { | ||||
| 	start := strings.LastIndex(r.url, "/") | ||||
| 	end := strings.LastIndex(r.url, ".git") | ||||
| 	return path.Join(r.home, r.url[start+1:end]) | ||||
| } | ||||
|  | ||||
| // Pull fetchs the repository from remote url. | ||||
| func (r *Repo) Pull(ctx context.Context, url string) error { | ||||
| 	repo, err := git.PlainOpen(r.Path()) | ||||
| 	if err != nil { | ||||
| @@ -48,6 +50,7 @@ func (r *Repo) Pull(ctx context.Context, url string) error { | ||||
| 	return err | ||||
| } | ||||
|  | ||||
| // Clone clones the repository to cache path. | ||||
| func (r *Repo) Clone(ctx context.Context) error { | ||||
| 	if _, err := os.Stat(r.Path()); !os.IsNotExist(err) { | ||||
| 		return r.Pull(ctx, r.url) | ||||
| @@ -59,6 +62,7 @@ func (r *Repo) Clone(ctx context.Context) error { | ||||
| 	return err | ||||
| } | ||||
|  | ||||
| // CopyTo copies the repository to project path. | ||||
| func (r *Repo) CopyTo(ctx context.Context, to string, modPath string, ignores []string) error { | ||||
| 	if err := r.Clone(ctx); err != nil { | ||||
| 		return err | ||||
|   | ||||
| @@ -8,6 +8,8 @@ import ( | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/go-kratos/kratos/v2/encoding" | ||||
| 	// init json encoder | ||||
| 	_ "github.com/go-kratos/kratos/v2/encoding/json" | ||||
| 	"github.com/go-kratos/kratos/v2/log" | ||||
| ) | ||||
|  | ||||
|   | ||||
| @@ -4,7 +4,6 @@ import ( | ||||
| 	"fmt" | ||||
| 	"strings" | ||||
|  | ||||
| 	_ "github.com/go-kratos/kratos/v2/encoding/json" | ||||
| 	"github.com/imdario/mergo" | ||||
| ) | ||||
|  | ||||
|   | ||||
| @@ -22,8 +22,6 @@ var ( | ||||
| 	UnmarshalOptions = protojson.UnmarshalOptions{ | ||||
| 		DiscardUnknown: true, | ||||
| 	} | ||||
|  | ||||
| 	typeProtoMessage = reflect.TypeOf((*proto.Message)(nil)).Elem() | ||||
| ) | ||||
|  | ||||
| func init() { | ||||
|   | ||||
| @@ -15,6 +15,8 @@ func Cancelled(reason, format string, a ...interface{}) error { | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // IsCancelled determines if err is an error which indicates a cancelled error. | ||||
| // It supports wrapped errors. | ||||
| func IsCancelled(err error) bool { | ||||
| 	if se := new(StatusError); errors.As(err, &se) { | ||||
| 		return se.Code == 1 | ||||
| @@ -32,6 +34,8 @@ func Unknown(reason, format string, a ...interface{}) error { | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // IsUnknown determines if err is an error which indicates a unknown error. | ||||
| // It supports wrapped errors. | ||||
| func IsUnknown(err error) bool { | ||||
| 	if se := new(StatusError); errors.As(err, &se) { | ||||
| 		return se.Code == 2 | ||||
| @@ -49,6 +53,8 @@ func InvalidArgument(reason, format string, a ...interface{}) error { | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // IsInvalidArgument determines if err is an error which indicates an invalid argument error. | ||||
| // It supports wrapped errors. | ||||
| func IsInvalidArgument(err error) bool { | ||||
| 	if se := new(StatusError); errors.As(err, &se) { | ||||
| 		return se.Code == 3 | ||||
| @@ -66,6 +72,8 @@ func DeadlineExceeded(reason, format string, a ...interface{}) error { | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // IsDeadlineExceeded determines if err is an error which indicates a deadline exceeded error. | ||||
| // It supports wrapped errors. | ||||
| func IsDeadlineExceeded(err error) bool { | ||||
| 	if se := new(StatusError); errors.As(err, &se) { | ||||
| 		return se.Code == 4 | ||||
| @@ -83,6 +91,8 @@ func NotFound(reason, format string, a ...interface{}) error { | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // IsNotFound determines if err is an error which indicates a not found error. | ||||
| // It supports wrapped errors. | ||||
| func IsNotFound(err error) bool { | ||||
| 	if se := new(StatusError); errors.As(err, &se) { | ||||
| 		return se.Code == 5 | ||||
| @@ -100,6 +110,8 @@ func AlreadyExists(reason, format string, a ...interface{}) error { | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // IsAlreadyExists determines if err is an error which indicates a already exsits error. | ||||
| // It supports wrapped errors. | ||||
| func IsAlreadyExists(err error) bool { | ||||
| 	if se := new(StatusError); errors.As(err, &se) { | ||||
| 		return se.Code == 6 | ||||
| @@ -117,6 +129,8 @@ func PermissionDenied(reason, format string, a ...interface{}) error { | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // IsPermissionDenied determines if err is an error which indicates a permission denied error. | ||||
| // It supports wrapped errors. | ||||
| func IsPermissionDenied(err error) bool { | ||||
| 	if se := new(StatusError); errors.As(err, &se) { | ||||
| 		return se.Code == 7 | ||||
| @@ -135,6 +149,8 @@ func ResourceExhausted(reason, format string, a ...interface{}) error { | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // IsResourceExhausted determines if err is an error which indicates a resource exhausted error. | ||||
| // It supports wrapped errors. | ||||
| func IsResourceExhausted(err error) bool { | ||||
| 	if se := new(StatusError); errors.As(err, &se) { | ||||
| 		return se.Code == 8 | ||||
| @@ -153,6 +169,8 @@ func FailedPrecondition(reason, format string, a ...interface{}) error { | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // IsFailedPrecondition determines if err is an error which indicates a failed precondition error. | ||||
| // It supports wrapped errors. | ||||
| func IsFailedPrecondition(err error) bool { | ||||
| 	if se := new(StatusError); errors.As(err, &se) { | ||||
| 		return se.Code == 9 | ||||
| @@ -171,6 +189,8 @@ func Aborted(reason, format string, a ...interface{}) error { | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // IsAborted determines if err is an error which indicates an aborted error. | ||||
| // It supports wrapped errors. | ||||
| func IsAborted(err error) bool { | ||||
| 	if se := new(StatusError); errors.As(err, &se) { | ||||
| 		return se.Code == 10 | ||||
| @@ -189,6 +209,8 @@ func OutOfRange(reason, format string, a ...interface{}) error { | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // IsOutOfRange determines if err is an error which indicates a out of range error. | ||||
| // It supports wrapped errors. | ||||
| func IsOutOfRange(err error) bool { | ||||
| 	if se := new(StatusError); errors.As(err, &se) { | ||||
| 		return se.Code == 11 | ||||
| @@ -206,6 +228,8 @@ func Unimplemented(reason, format string, a ...interface{}) error { | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // IsUnimplemented determines if err is an error which indicates a unimplemented error. | ||||
| // It supports wrapped errors. | ||||
| func IsUnimplemented(err error) bool { | ||||
| 	if se := new(StatusError); errors.As(err, &se) { | ||||
| 		return se.Code == 12 | ||||
| @@ -226,6 +250,8 @@ func Internal(reason, format string, a ...interface{}) error { | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // IsInternal determines if err is an error which indicates an internal server error. | ||||
| // It supports wrapped errors. | ||||
| func IsInternal(err error) bool { | ||||
| 	if se := new(StatusError); errors.As(err, &se) { | ||||
| 		return se.Code == 13 | ||||
| @@ -243,6 +269,8 @@ func Unavailable(reason, format string, a ...interface{}) error { | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // IsUnavailable determines if err is an error which indicates a unavailable error. | ||||
| // It supports wrapped errors. | ||||
| func IsUnavailable(err error) bool { | ||||
| 	if se := new(StatusError); errors.As(err, &se) { | ||||
| 		return se.Code == 14 | ||||
| @@ -260,6 +288,8 @@ func DataLoss(reason, format string, a ...interface{}) error { | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // IsDataLoss determines if err is an error which indicates a data loss error. | ||||
| // It supports wrapped errors. | ||||
| func IsDataLoss(err error) bool { | ||||
| 	if se := new(StatusError); errors.As(err, &se) { | ||||
| 		return se.Code == 15 | ||||
| @@ -277,6 +307,8 @@ func Unauthorized(reason, format string, a ...interface{}) error { | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // IsUnauthorized determines if err is an error which indicates a unauthorized error. | ||||
| // It supports wrapped errors. | ||||
| func IsUnauthorized(err error) bool { | ||||
| 	if se := new(StatusError); errors.As(err, &se) { | ||||
| 		return se.Code == 16 | ||||
|   | ||||
| @@ -1,7 +1,6 @@ | ||||
| package host | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"net" | ||||
| 	"strconv" | ||||
| ) | ||||
| @@ -46,7 +45,7 @@ func Extract(hostport string, lis net.Listener) (string, error) { | ||||
| 	} | ||||
| 	ifaces, err := net.Interfaces() | ||||
| 	if err != nil { | ||||
| 		return "", fmt.Errorf("Failed to get net interfaces: %v", err) | ||||
| 		return "", err | ||||
| 	} | ||||
| 	for _, iface := range ifaces { | ||||
| 		addrs, err := iface.Addrs() | ||||
|   | ||||
| @@ -4,8 +4,6 @@ import ( | ||||
| 	"fmt" | ||||
| ) | ||||
|  | ||||
| var nop Logger = new(nopLogger) | ||||
|  | ||||
| // Helper is a logger helper. | ||||
| type Helper struct { | ||||
| 	debug Logger | ||||
|   | ||||
| @@ -1,5 +0,0 @@ | ||||
| package log | ||||
|  | ||||
| type nopLogger struct{} | ||||
|  | ||||
| func (l *nopLogger) Print(kvpair ...interface{}) {} | ||||
| @@ -5,6 +5,8 @@ import ( | ||||
|  | ||||
| 	"github.com/go-kratos/kratos/v2/errors" | ||||
| 	"github.com/go-kratos/kratos/v2/middleware" | ||||
|  | ||||
| 	//lint:ignore SA1019 grpc | ||||
| 	"github.com/golang/protobuf/proto" | ||||
| 	"github.com/golang/protobuf/ptypes" | ||||
| 	"google.golang.org/genproto/googleapis/rpc/errdetails" | ||||
| @@ -71,7 +73,7 @@ func errorEncode(err error) error { | ||||
| 	se, ok := errors.FromError(err) | ||||
| 	if !ok { | ||||
| 		se = &errors.StatusError{ | ||||
| 			Code: 2, | ||||
| 			Code:    2, | ||||
| 			Message: err.Error(), | ||||
| 		} | ||||
| 	} | ||||
|   | ||||
| @@ -10,8 +10,8 @@ type Registrar interface { | ||||
| 	Deregister(ctx context.Context, service *ServiceInstance) error | ||||
| } | ||||
|  | ||||
| // Discoverer is service discovery. | ||||
| type Discoverer interface { | ||||
| // Instancer is service instancer. | ||||
| type Instancer interface { | ||||
| 	// Service return the service instances in memory according to the service name. | ||||
| 	Fetch(ctx context.Context, serviceName string) ([]*ServiceInstance, error) | ||||
| 	// Watch creates a watcher according to the service name. | ||||
|   | ||||
| @@ -39,9 +39,9 @@ func WithMiddleware(m middleware.Middleware) ClientOption { | ||||
| } | ||||
|  | ||||
| // WithRegistry with client registry. | ||||
| func WithRegistry(d registry.Discoverer) ClientOption { | ||||
| func WithRegistry(in registry.Instancer) ClientOption { | ||||
| 	return func(o *clientOptions) { | ||||
| 		o.discoverer = d | ||||
| 		o.instancer = in | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @@ -57,7 +57,7 @@ type clientOptions struct { | ||||
| 	endpoint   string | ||||
| 	timeout    time.Duration | ||||
| 	middleware middleware.Middleware | ||||
| 	discoverer registry.Discoverer | ||||
| 	instancer  registry.Instancer | ||||
| 	grpcOpts   []grpc.DialOption | ||||
| } | ||||
|  | ||||
| @@ -83,11 +83,10 @@ func dial(ctx context.Context, insecure bool, opts ...ClientOption) (*grpc.Clien | ||||
| 		o(&options) | ||||
| 	} | ||||
| 	var grpcOpts = []grpc.DialOption{ | ||||
| 		grpc.WithTimeout(options.timeout), | ||||
| 		grpc.WithUnaryInterceptor(UnaryClientInterceptor(options.middleware)), | ||||
| 		grpc.WithUnaryInterceptor(unaryClientInterceptor(options.middleware, options.timeout)), | ||||
| 	} | ||||
| 	if options.discoverer != nil { | ||||
| 		grpcOpts = append(grpcOpts, grpc.WithResolvers(discovery.NewBuilder(options.discoverer))) | ||||
| 	if options.instancer != nil { | ||||
| 		grpcOpts = append(grpcOpts, grpc.WithResolvers(discovery.NewBuilder(options.instancer))) | ||||
| 	} | ||||
| 	if insecure { | ||||
| 		grpcOpts = append(grpcOpts, grpc.WithInsecure()) | ||||
| @@ -98,11 +97,15 @@ func dial(ctx context.Context, insecure bool, opts ...ClientOption) (*grpc.Clien | ||||
| 	return grpc.DialContext(ctx, options.endpoint, grpcOpts...) | ||||
| } | ||||
|  | ||||
| // UnaryClientInterceptor retruns a unary client interceptor. | ||||
| func UnaryClientInterceptor(m middleware.Middleware) grpc.UnaryClientInterceptor { | ||||
| func unaryClientInterceptor(m middleware.Middleware, timeout time.Duration) grpc.UnaryClientInterceptor { | ||||
| 	return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { | ||||
| 		ctx = transport.NewContext(ctx, transport.Transport{Kind: transport.KindGRPC}) | ||||
| 		ctx = NewClientContext(ctx, ClientInfo{FullMethod: method}) | ||||
| 		if timeout > 0 { | ||||
| 			var cancel context.CancelFunc | ||||
| 			ctx, cancel = context.WithTimeout(ctx, timeout) | ||||
| 			defer cancel() | ||||
| 		} | ||||
| 		h := func(ctx context.Context, req interface{}) (interface{}, error) { | ||||
| 			return reply, invoker(ctx, method, req, reply, cc, opts...) | ||||
| 		} | ||||
|   | ||||
| @@ -33,11 +33,11 @@ type clientKey struct{} | ||||
|  | ||||
| // NewClientContext returns a new Context that carries value. | ||||
| func NewClientContext(ctx context.Context, info ClientInfo) context.Context { | ||||
| 	return context.WithValue(ctx, serverKey{}, info) | ||||
| 	return context.WithValue(ctx, clientKey{}, info) | ||||
| } | ||||
|  | ||||
| // FromClientContext returns the Transport value stored in ctx, if any. | ||||
| func FromClientContext(ctx context.Context) (info ClientInfo, ok bool) { | ||||
| 	info, ok = ctx.Value(serverKey{}).(ClientInfo) | ||||
| 	info, ok = ctx.Value(clientKey{}).(ClientInfo) | ||||
| 	return | ||||
| } | ||||
|   | ||||
| @@ -21,15 +21,15 @@ func WithLogger(logger log.Logger) Option { | ||||
| } | ||||
|  | ||||
| type builder struct { | ||||
| 	discoverer registry.Discoverer | ||||
| 	logger     log.Logger | ||||
| 	instancer registry.Instancer | ||||
| 	logger    log.Logger | ||||
| } | ||||
|  | ||||
| // NewBuilder creates a builder which is used to factory registry resolvers. | ||||
| func NewBuilder(r registry.Discoverer, opts ...Option) resolver.Builder { | ||||
| func NewBuilder(in registry.Instancer, opts ...Option) resolver.Builder { | ||||
| 	b := &builder{ | ||||
| 		discoverer: r, | ||||
| 		logger:     log.DefaultLogger, | ||||
| 		instancer: in, | ||||
| 		logger:    log.DefaultLogger, | ||||
| 	} | ||||
| 	for _, o := range opts { | ||||
| 		o(b) | ||||
| @@ -38,7 +38,7 @@ func NewBuilder(r registry.Discoverer, opts ...Option) resolver.Builder { | ||||
| } | ||||
|  | ||||
| func (d *builder) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) (resolver.Resolver, error) { | ||||
| 	w, err := d.discoverer.Watch(context.Background(), target.Endpoint) | ||||
| 	w, err := d.instancer.Watch(context.Background(), target.Endpoint) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
|   | ||||
| @@ -94,8 +94,7 @@ func NewServer(opts ...ServerOption) *Server { | ||||
| 	} | ||||
| 	var grpcOpts = []grpc.ServerOption{ | ||||
| 		grpc.ChainUnaryInterceptor( | ||||
| 			UnaryServerInterceptor(srv.middleware), | ||||
| 			UnaryTimeoutInterceptor(srv.timeout), | ||||
| 			unaryServerInterceptor(srv.middleware, srv.timeout), | ||||
| 		), | ||||
| 	} | ||||
| 	if len(srv.grpcOpts) > 0 { | ||||
| @@ -134,20 +133,15 @@ func (s *Server) Stop() error { | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| // UnaryTimeoutInterceptor returns a unary timeout interceptor. | ||||
| func UnaryTimeoutInterceptor(timeout time.Duration) grpc.UnaryServerInterceptor { | ||||
| 	return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { | ||||
| 		ctx, cancel := context.WithTimeout(ctx, timeout) | ||||
| 		defer cancel() | ||||
| 		return handler(ctx, req) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // UnaryServerInterceptor returns a unary server interceptor. | ||||
| func UnaryServerInterceptor(m middleware.Middleware) grpc.UnaryServerInterceptor { | ||||
| func unaryServerInterceptor(m middleware.Middleware, timeout time.Duration) grpc.UnaryServerInterceptor { | ||||
| 	return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { | ||||
| 		ctx = transport.NewContext(ctx, transport.Transport{Kind: transport.KindGRPC}) | ||||
| 		ctx = NewServerContext(ctx, ServerInfo{Server: info.Server, FullMethod: info.FullMethod}) | ||||
| 		if timeout > 0 { | ||||
| 			var cancel context.CancelFunc | ||||
| 			ctx, cancel = context.WithTimeout(ctx, timeout) | ||||
| 			defer cancel() | ||||
| 		} | ||||
| 		h := func(ctx context.Context, req interface{}) (interface{}, error) { | ||||
| 			return handler(ctx, req) | ||||
| 		} | ||||
|   | ||||
		Reference in New Issue
	
	Block a user