From 40b2404b3da771def8eb6b7af3dc2ec3194856e8 Mon Sep 17 00:00:00 2001 From: Kirill Motkov Date: Tue, 30 Apr 2019 11:51:02 +0300 Subject: [PATCH] Some code improvements * Simplify some functions. * strings.ToLower comparison changed to strings.EqualFold. --- auth/jwt/transport.go | 2 +- .../add_service-remote/add_service-remote.go | 329 +++++++++--------- metrics/generic/generic_test.go | 4 +- 3 files changed, 167 insertions(+), 168 deletions(-) diff --git a/auth/jwt/transport.go b/auth/jwt/transport.go index 57b3aaeef..4b7082a3f 100644 --- a/auth/jwt/transport.go +++ b/auth/jwt/transport.go @@ -77,7 +77,7 @@ func ContextToGRPC() grpc.ClientRequestFunc { func extractTokenFromAuthHeader(val string) (token string, ok bool) { authHeaderParts := strings.Split(val, " ") - if len(authHeaderParts) != 2 || strings.ToLower(authHeaderParts[0]) != bearer { + if len(authHeaderParts) != 2 || !strings.EqualFold(authHeaderParts[0], bearer) { return "", false } diff --git a/examples/addsvc/thrift/gen-go/addsvc/add_service-remote/add_service-remote.go b/examples/addsvc/thrift/gen-go/addsvc/add_service-remote/add_service-remote.go index 9e6800e72..ded0914af 100755 --- a/examples/addsvc/thrift/gen-go/addsvc/add_service-remote/add_service-remote.go +++ b/examples/addsvc/thrift/gen-go/addsvc/add_service-remote/add_service-remote.go @@ -4,183 +4,182 @@ package main import ( - "context" - "flag" - "fmt" - "math" - "net" - "net/url" - "os" - "strconv" - "strings" - "github.com/apache/thrift/lib/go/thrift" - "github.com/go-kit/kit/examples/addsvc/thrift/gen-go/addsvc" + "context" + "flag" + "fmt" + "github.com/apache/thrift/lib/go/thrift" + "github.com/go-kit/kit/examples/addsvc/thrift/gen-go/addsvc" + "math" + "net" + "net/url" + "os" + "strconv" + "strings" ) - func Usage() { - fmt.Fprintln(os.Stderr, "Usage of ", os.Args[0], " [-h host:port] [-u url] [-f[ramed]] function [arg1 [arg2...]]:") - flag.PrintDefaults() - fmt.Fprintln(os.Stderr, "\nFunctions:") - fmt.Fprintln(os.Stderr, " SumReply Sum(i64 a, i64 b)") - fmt.Fprintln(os.Stderr, " ConcatReply Concat(string a, string b)") - fmt.Fprintln(os.Stderr) - os.Exit(0) + fmt.Fprintln(os.Stderr, "Usage of ", os.Args[0], " [-h host:port] [-u url] [-f[ramed]] function [arg1 [arg2...]]:") + flag.PrintDefaults() + fmt.Fprintln(os.Stderr, "\nFunctions:") + fmt.Fprintln(os.Stderr, " SumReply Sum(i64 a, i64 b)") + fmt.Fprintln(os.Stderr, " ConcatReply Concat(string a, string b)") + fmt.Fprintln(os.Stderr) + os.Exit(0) } type httpHeaders map[string]string func (h httpHeaders) String() string { - var m map[string]string = h - return fmt.Sprintf("%s", m) + var m map[string]string = h + return fmt.Sprintf("%s", m) } func (h httpHeaders) Set(value string) error { - parts := strings.Split(value, ": ") - if len(parts) != 2 { - return fmt.Errorf("header should be of format 'Key: Value'") - } - h[parts[0]] = parts[1] - return nil + parts := strings.Split(value, ": ") + if len(parts) != 2 { + return fmt.Errorf("header should be of format 'Key: Value'") + } + h[parts[0]] = parts[1] + return nil } func main() { - flag.Usage = Usage - var host string - var port int - var protocol string - var urlString string - var framed bool - var useHttp bool - headers := make(httpHeaders) - var parsedUrl *url.URL - var trans thrift.TTransport - _ = strconv.Atoi - _ = math.Abs - flag.Usage = Usage - flag.StringVar(&host, "h", "localhost", "Specify host and port") - flag.IntVar(&port, "p", 9090, "Specify port") - flag.StringVar(&protocol, "P", "binary", "Specify the protocol (binary, compact, simplejson, json)") - flag.StringVar(&urlString, "u", "", "Specify the url") - flag.BoolVar(&framed, "framed", false, "Use framed transport") - flag.BoolVar(&useHttp, "http", false, "Use http") - flag.Var(headers, "H", "Headers to set on the http(s) request (e.g. -H \"Key: Value\")") - flag.Parse() - - if len(urlString) > 0 { - var err error - parsedUrl, err = url.Parse(urlString) - if err != nil { - fmt.Fprintln(os.Stderr, "Error parsing URL: ", err) - flag.Usage() - } - host = parsedUrl.Host - useHttp = len(parsedUrl.Scheme) <= 0 || parsedUrl.Scheme == "http" || parsedUrl.Scheme == "https" - } else if useHttp { - _, err := url.Parse(fmt.Sprint("http://", host, ":", port)) - if err != nil { - fmt.Fprintln(os.Stderr, "Error parsing URL: ", err) - flag.Usage() - } - } - - cmd := flag.Arg(0) - var err error - if useHttp { - trans, err = thrift.NewTHttpClient(parsedUrl.String()) - if len(headers) > 0 { - httptrans := trans.(*thrift.THttpClient) - for key, value := range headers { - httptrans.SetHeader(key, value) - } - } - } else { - portStr := fmt.Sprint(port) - if strings.Contains(host, ":") { - host, portStr, err = net.SplitHostPort(host) - if err != nil { - fmt.Fprintln(os.Stderr, "error with host:", err) - os.Exit(1) - } - } - trans, err = thrift.NewTSocket(net.JoinHostPort(host, portStr)) - if err != nil { - fmt.Fprintln(os.Stderr, "error resolving address:", err) - os.Exit(1) - } - if framed { - trans = thrift.NewTFramedTransport(trans) - } - } - if err != nil { - fmt.Fprintln(os.Stderr, "Error creating transport", err) - os.Exit(1) - } - defer trans.Close() - var protocolFactory thrift.TProtocolFactory - switch protocol { - case "compact": - protocolFactory = thrift.NewTCompactProtocolFactory() - break - case "simplejson": - protocolFactory = thrift.NewTSimpleJSONProtocolFactory() - break - case "json": - protocolFactory = thrift.NewTJSONProtocolFactory() - break - case "binary", "": - protocolFactory = thrift.NewTBinaryProtocolFactoryDefault() - break - default: - fmt.Fprintln(os.Stderr, "Invalid protocol specified: ", protocol) - Usage() - os.Exit(1) - } - iprot := protocolFactory.GetProtocol(trans) - oprot := protocolFactory.GetProtocol(trans) - client := addsvc.NewAddServiceClient(thrift.NewTStandardClient(iprot, oprot)) - if err := trans.Open(); err != nil { - fmt.Fprintln(os.Stderr, "Error opening socket to ", host, ":", port, " ", err) - os.Exit(1) - } - - switch cmd { - case "Sum": - if flag.NArg() - 1 != 2 { - fmt.Fprintln(os.Stderr, "Sum requires 2 args") - flag.Usage() - } - argvalue0, err6 := (strconv.ParseInt(flag.Arg(1), 10, 64)) - if err6 != nil { - Usage() - return - } - value0 := argvalue0 - argvalue1, err7 := (strconv.ParseInt(flag.Arg(2), 10, 64)) - if err7 != nil { - Usage() - return - } - value1 := argvalue1 - fmt.Print(client.Sum(context.Background(), value0, value1)) - fmt.Print("\n") - break - case "Concat": - if flag.NArg() - 1 != 2 { - fmt.Fprintln(os.Stderr, "Concat requires 2 args") - flag.Usage() - } - argvalue0 := flag.Arg(1) - value0 := argvalue0 - argvalue1 := flag.Arg(2) - value1 := argvalue1 - fmt.Print(client.Concat(context.Background(), value0, value1)) - fmt.Print("\n") - break - case "": - Usage() - break - default: - fmt.Fprintln(os.Stderr, "Invalid function ", cmd) - } + flag.Usage = Usage + var host string + var port int + var protocol string + var urlString string + var framed bool + var useHttp bool + headers := make(httpHeaders) + var parsedUrl *url.URL + var trans thrift.TTransport + _ = strconv.Atoi + _ = math.Abs + flag.Usage = Usage + flag.StringVar(&host, "h", "localhost", "Specify host and port") + flag.IntVar(&port, "p", 9090, "Specify port") + flag.StringVar(&protocol, "P", "binary", "Specify the protocol (binary, compact, simplejson, json)") + flag.StringVar(&urlString, "u", "", "Specify the url") + flag.BoolVar(&framed, "framed", false, "Use framed transport") + flag.BoolVar(&useHttp, "http", false, "Use http") + flag.Var(headers, "H", "Headers to set on the http(s) request (e.g. -H \"Key: Value\")") + flag.Parse() + + if len(urlString) > 0 { + var err error + parsedUrl, err = url.Parse(urlString) + if err != nil { + fmt.Fprintln(os.Stderr, "Error parsing URL: ", err) + flag.Usage() + } + host = parsedUrl.Host + useHttp = parsedUrl.Scheme == "" || parsedUrl.Scheme == "http" || parsedUrl.Scheme == "https" + } else if useHttp { + _, err := url.Parse(fmt.Sprint("http://", host, ":", port)) + if err != nil { + fmt.Fprintln(os.Stderr, "Error parsing URL: ", err) + flag.Usage() + } + } + + cmd := flag.Arg(0) + var err error + if useHttp { + trans, err = thrift.NewTHttpClient(parsedUrl.String()) + if len(headers) > 0 { + httptrans := trans.(*thrift.THttpClient) + for key, value := range headers { + httptrans.SetHeader(key, value) + } + } + } else { + portStr := fmt.Sprint(port) + if strings.Contains(host, ":") { + host, portStr, err = net.SplitHostPort(host) + if err != nil { + fmt.Fprintln(os.Stderr, "error with host:", err) + os.Exit(1) + } + } + trans, err = thrift.NewTSocket(net.JoinHostPort(host, portStr)) + if err != nil { + fmt.Fprintln(os.Stderr, "error resolving address:", err) + os.Exit(1) + } + if framed { + trans = thrift.NewTFramedTransport(trans) + } + } + if err != nil { + fmt.Fprintln(os.Stderr, "Error creating transport", err) + os.Exit(1) + } + defer trans.Close() + var protocolFactory thrift.TProtocolFactory + switch protocol { + case "compact": + protocolFactory = thrift.NewTCompactProtocolFactory() + break + case "simplejson": + protocolFactory = thrift.NewTSimpleJSONProtocolFactory() + break + case "json": + protocolFactory = thrift.NewTJSONProtocolFactory() + break + case "binary", "": + protocolFactory = thrift.NewTBinaryProtocolFactoryDefault() + break + default: + fmt.Fprintln(os.Stderr, "Invalid protocol specified: ", protocol) + Usage() + os.Exit(1) + } + iprot := protocolFactory.GetProtocol(trans) + oprot := protocolFactory.GetProtocol(trans) + client := addsvc.NewAddServiceClient(thrift.NewTStandardClient(iprot, oprot)) + if err := trans.Open(); err != nil { + fmt.Fprintln(os.Stderr, "Error opening socket to ", host, ":", port, " ", err) + os.Exit(1) + } + + switch cmd { + case "Sum": + if flag.NArg()-1 != 2 { + fmt.Fprintln(os.Stderr, "Sum requires 2 args") + flag.Usage() + } + argvalue0, err6 := (strconv.ParseInt(flag.Arg(1), 10, 64)) + if err6 != nil { + Usage() + return + } + value0 := argvalue0 + argvalue1, err7 := (strconv.ParseInt(flag.Arg(2), 10, 64)) + if err7 != nil { + Usage() + return + } + value1 := argvalue1 + fmt.Print(client.Sum(context.Background(), value0, value1)) + fmt.Print("\n") + break + case "Concat": + if flag.NArg()-1 != 2 { + fmt.Fprintln(os.Stderr, "Concat requires 2 args") + flag.Usage() + } + argvalue0 := flag.Arg(1) + value0 := argvalue0 + argvalue1 := flag.Arg(2) + value1 := argvalue1 + fmt.Print(client.Concat(context.Background(), value0, value1)) + fmt.Print("\n") + break + case "": + Usage() + break + default: + fmt.Fprintln(os.Stderr, "Invalid function ", cmd) + } } diff --git a/metrics/generic/generic_test.go b/metrics/generic/generic_test.go index 56e6a5caf..de30f39e3 100644 --- a/metrics/generic/generic_test.go +++ b/metrics/generic/generic_test.go @@ -20,7 +20,7 @@ func TestCounter(t *testing.T) { if want, have := name, counter.Name; want != have { t.Errorf("Name: want %q, have %q", want, have) } - value := func() float64 { return counter.Value() } + value := counter.Value if err := teststat.TestCounter(counter, value); err != nil { t.Fatal(err) } @@ -45,7 +45,7 @@ func TestGauge(t *testing.T) { if want, have := name, gauge.Name; want != have { t.Errorf("Name: want %q, have %q", want, have) } - value := func() float64 { return gauge.Value() } + value := gauge.Value if err := teststat.TestGauge(gauge, value); err != nil { t.Fatal(err) }