From 741af81a7beec9e44a6bb2d66623596a8ce55624 Mon Sep 17 00:00:00 2001 From: Sasha Melentyev Date: Sun, 13 Nov 2022 19:54:12 +0300 Subject: [PATCH] Cleanup --- .../internal/integration/integration_test.go | 119 +++++++++--------- internal/codegenerator/parse_req.go | 9 +- internal/descriptor/grpc_api_configuration.go | 6 +- internal/descriptor/openapi_configuration.go | 13 +- internal/httprule/fuzz.go | 4 +- .../internal/genopenapi/helpers.go | 2 +- .../internal/genopenapi/helpers_go111_old.go | 3 +- .../internal/genopenapi/template.go | 3 +- runtime/convert.go | 46 ++++--- runtime/handler_test.go | 7 +- runtime/marshal_json_test.go | 5 +- runtime/marshal_jsonpb_test.go | 14 +-- runtime/marshal_proto.go | 6 +- runtime/query.go | 29 +++-- runtime/query_test.go | 36 +++--- utilities/readerfactory.go | 3 +- 16 files changed, 146 insertions(+), 159 deletions(-) diff --git a/examples/internal/integration/integration_test.go b/examples/internal/integration/integration_test.go index 480deff3ab4..e650e0b896f 100644 --- a/examples/internal/integration/integration_test.go +++ b/examples/internal/integration/integration_test.go @@ -8,7 +8,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "net/http" "net/url" "reflect" @@ -68,9 +67,9 @@ func TestEchoUnauthorized(t *testing.T) { return } defer resp.Body.Close() - buf, err := ioutil.ReadAll(resp.Body) + buf, err := io.ReadAll(resp.Body) if err != nil { - t.Errorf("ioutil.ReadAll(resp.Body) failed with %v; want success", err) + t.Errorf("io.ReadAll(resp.Body) failed with %v; want success", err) return } msg := new(statuspb.Status) @@ -125,9 +124,9 @@ func TestEchoPatch(t *testing.T) { return } defer resp.Body.Close() - buf, err := ioutil.ReadAll(resp.Body) + buf, err := io.ReadAll(resp.Body) if err != nil { - t.Errorf("ioutil.ReadAll(resp.Body) failed with %v; want success", err) + t.Errorf("io.ReadAll(resp.Body) failed with %v; want success", err) return } @@ -224,9 +223,9 @@ func testEcho(t *testing.T, port int, apiPrefix string, contentType string) { return } defer resp.Body.Close() - buf, err := ioutil.ReadAll(resp.Body) + buf, err := io.ReadAll(resp.Body) if err != nil { - t.Errorf("ioutil.ReadAll(resp.Body) failed with %v; want success", err) + t.Errorf("io.ReadAll(resp.Body) failed with %v; want success", err) return } @@ -257,9 +256,9 @@ func testEchoOneof(t *testing.T, port int, apiPrefix string, contentType string) return } defer resp.Body.Close() - buf, err := ioutil.ReadAll(resp.Body) + buf, err := io.ReadAll(resp.Body) if err != nil { - t.Errorf("ioutil.ReadAll(resp.Body) failed with %v; want success", err) + t.Errorf("io.ReadAll(resp.Body) failed with %v; want success", err) return } @@ -290,9 +289,9 @@ func testEchoOneof1(t *testing.T, port int, apiPrefix string, contentType string return } defer resp.Body.Close() - buf, err := ioutil.ReadAll(resp.Body) + buf, err := io.ReadAll(resp.Body) if err != nil { - t.Errorf("ioutil.ReadAll(resp.Body) failed with %v; want success", err) + t.Errorf("io.ReadAll(resp.Body) failed with %v; want success", err) return } @@ -323,9 +322,9 @@ func testEchoOneof2(t *testing.T, port int, apiPrefix string, contentType string return } defer resp.Body.Close() - buf, err := ioutil.ReadAll(resp.Body) + buf, err := io.ReadAll(resp.Body) if err != nil { - t.Errorf("ioutil.ReadAll(resp.Body) failed with %v; want success", err) + t.Errorf("io.ReadAll(resp.Body) failed with %v; want success", err) return } @@ -372,9 +371,9 @@ func testEchoBody(t *testing.T, port int, apiPrefix string, useTrailers bool) { return } defer resp.Body.Close() - buf, err := ioutil.ReadAll(resp.Body) + buf, err := io.ReadAll(resp.Body) if err != nil { - t.Errorf("ioutil.ReadAll(resp.Body) failed with %v; want success", err) + t.Errorf("io.ReadAll(resp.Body) failed with %v; want success", err) return } @@ -470,9 +469,9 @@ func testABECreate(t *testing.T, port int) { return } defer resp.Body.Close() - buf, err := ioutil.ReadAll(resp.Body) + buf, err := io.ReadAll(resp.Body) if err != nil { - t.Errorf("ioutil.ReadAll(resp.Body) failed with %v; want success", err) + t.Errorf("io.ReadAll(resp.Body) failed with %v; want success", err) return } @@ -580,9 +579,9 @@ func testABECreateBody(t *testing.T, port int) { return } defer resp.Body.Close() - buf, err := ioutil.ReadAll(resp.Body) + buf, err := io.ReadAll(resp.Body) if err != nil { - t.Errorf("ioutil.ReadAll(resp.Body) failed with %v; want success", err) + t.Errorf("io.ReadAll(resp.Body) failed with %v; want success", err) return } @@ -709,9 +708,9 @@ func testABEBulkCreate(t *testing.T, port int, useTrailers bool) { } defer resp.Body.Close() - buf, err := ioutil.ReadAll(resp.Body) + buf, err := io.ReadAll(resp.Body) if err != nil { - t.Errorf("ioutil.ReadAll(resp.Body) failed with %v; want success", err) + t.Errorf("io.ReadAll(resp.Body) failed with %v; want success", err) return } @@ -792,9 +791,9 @@ func testABEBulkCreateWithError(t *testing.T, port int) { return } defer resp.Body.Close() - buf, err := ioutil.ReadAll(resp.Body) + buf, err := io.ReadAll(resp.Body) if err != nil { - t.Errorf("ioutil.ReadAll(resp.Body) failed with %v; want success", err) + t.Errorf("io.ReadAll(resp.Body) failed with %v; want success", err) return } @@ -819,9 +818,9 @@ func testABELookup(t *testing.T, port int) { return } defer cresp.Body.Close() - buf, err := ioutil.ReadAll(cresp.Body) + buf, err := io.ReadAll(cresp.Body) if err != nil { - t.Errorf("ioutil.ReadAll(cresp.Body) failed with %v; want success", err) + t.Errorf("io.ReadAll(cresp.Body) failed with %v; want success", err) return } if got, want := cresp.StatusCode, http.StatusOK; got != want { @@ -844,9 +843,9 @@ func testABELookup(t *testing.T, port int) { } defer resp.Body.Close() - buf, err = ioutil.ReadAll(resp.Body) + buf, err = io.ReadAll(resp.Body) if err != nil { - t.Errorf("ioutil.ReadAll(resp.Body) failed with %v; want success", err) + t.Errorf("io.ReadAll(resp.Body) failed with %v; want success", err) return } @@ -893,7 +892,7 @@ func TestABEPatch(t *testing.T) { t.Fatalf("failed to issue PATCH request: %v", err) } if got, want := patchResp.StatusCode, http.StatusOK; got != want { - if body, err := ioutil.ReadAll(patchResp.Body); err != nil { + if body, err := io.ReadAll(patchResp.Body); err != nil { t.Errorf("patchResp body couldn't be read: %v", err) } else { t.Errorf("patchResp.StatusCode= %d; want %d resp: %v", got, want, string(body)) @@ -1026,7 +1025,7 @@ func TestABEPatchBody(t *testing.T) { t.Fatalf("failed to issue PATCH request: %v", err) } if got, want := patchResp.StatusCode, http.StatusOK; got != want { - if body, err := ioutil.ReadAll(patchResp.Body); err != nil { + if body, err := io.ReadAll(patchResp.Body); err != nil { t.Errorf("patchResp body couldn't be read: %v", err) } else { t.Errorf("patchResp.StatusCode= %d; want %d resp: %v", got, want, string(body)) @@ -1061,7 +1060,7 @@ func postABE(t *testing.T, port int, abe *examplepb.ABitOfEverything) (uuid stri t.Fatalf("http.Post(%q) failed with %v; want success", apiURL, err) return } - body, err := ioutil.ReadAll(postResp.Body) + body, err := io.ReadAll(postResp.Body) if err != nil { t.Fatalf("postResp body couldn't be read: %v", err) } @@ -1090,7 +1089,7 @@ func getABE(t *testing.T, port int, uuid string) *examplepb.ABitOfEverything { t.Fatalf("getResp.StatusCode= %d, want %d. resp: %v", got, want, getResp) } var getRestatuspbody examplepb.ABitOfEverything - body, err := ioutil.ReadAll(getResp.Body) + body, err := io.ReadAll(getResp.Body) if err != nil { t.Fatalf("getResp body couldn't be read: %v", err) } @@ -1123,9 +1122,9 @@ func testABELookupNotFound(t *testing.T, port int, useTrailers bool) { } defer resp.Body.Close() - buf, err := ioutil.ReadAll(resp.Body) + buf, err := io.ReadAll(resp.Body) if err != nil { - t.Errorf("ioutil.ReadAll(resp.Body) failed with %v; want success", err) + t.Errorf("io.ReadAll(resp.Body) failed with %v; want success", err) return } @@ -1409,9 +1408,9 @@ func testAdditionalBindings(t *testing.T, port int) { } defer resp.Body.Close() - buf, err := ioutil.ReadAll(resp.Body) + buf, err := io.ReadAll(resp.Body) if err != nil { - t.Errorf("ioutil.ReadAll(resp.Body) failed with %v; want success; i=%d", err, i) + t.Errorf("io.ReadAll(resp.Body) failed with %v; want success; i=%d", err, i) return } if got, want := resp.StatusCode, http.StatusOK; got != want { @@ -1528,9 +1527,9 @@ func testABERepeated(t *testing.T, port int) { return } defer resp.Body.Close() - buf, err := ioutil.ReadAll(resp.Body) + buf, err := io.ReadAll(resp.Body) if err != nil { - t.Errorf("ioutil.ReadAll(resp.Body) failed with %v; want success", err) + t.Errorf("io.ReadAll(resp.Body) failed with %v; want success", err) return } @@ -1632,9 +1631,9 @@ func TestUnknownPath(t *testing.T) { return } defer resp.Body.Close() - buf, err := ioutil.ReadAll(resp.Body) + buf, err := io.ReadAll(resp.Body) if err != nil { - t.Errorf("ioutil.ReadAll(resp.Body) failed with %v; want success", err) + t.Errorf("io.ReadAll(resp.Body) failed with %v; want success", err) return } @@ -1657,9 +1656,9 @@ func TestNotImplemented(t *testing.T) { return } defer resp.Body.Close() - buf, err := ioutil.ReadAll(resp.Body) + buf, err := io.ReadAll(resp.Body) if err != nil { - t.Errorf("ioutil.ReadAll(resp.Body) failed with %v; want success", err) + t.Errorf("io.ReadAll(resp.Body) failed with %v; want success", err) return } if got, want := resp.StatusCode, http.StatusNotImplemented; got != want { @@ -1681,9 +1680,9 @@ func TestInvalidArgument(t *testing.T) { return } defer resp.Body.Close() - buf, err := ioutil.ReadAll(resp.Body) + buf, err := io.ReadAll(resp.Body) if err != nil { - t.Errorf("ioutil.ReadAll(resp.Body) failed with %v; want success", err) + t.Errorf("io.ReadAll(resp.Body) failed with %v; want success", err) return } @@ -1726,9 +1725,9 @@ func testResponseBody(t *testing.T, port int) { } defer resp.Body.Close() - buf, err := ioutil.ReadAll(resp.Body) + buf, err := io.ReadAll(resp.Body) if err != nil { - t.Fatalf("ioutil.ReadAll(resp.Body) failed with %v; want success", err) + t.Fatalf("io.ReadAll(resp.Body) failed with %v; want success", err) } if got, want := resp.StatusCode, tt.wantStatus; got != want { @@ -1806,9 +1805,9 @@ func testResponseBodies(t *testing.T, port int) { return } defer resp.Body.Close() - buf, err := ioutil.ReadAll(resp.Body) + buf, err := io.ReadAll(resp.Body) if err != nil { - t.Errorf("ioutil.ReadAll(resp.Body) failed with %v; want success", err) + t.Errorf("io.ReadAll(resp.Body) failed with %v; want success", err) return } @@ -1863,9 +1862,9 @@ func testResponseStrings(t *testing.T, port int) { return } defer resp.Body.Close() - buf, err := ioutil.ReadAll(resp.Body) + buf, err := io.ReadAll(resp.Body) if err != nil { - t.Errorf("ioutil.ReadAll(resp.Body) failed with %v; want success", err) + t.Errorf("io.ReadAll(resp.Body) failed with %v; want success", err) return } @@ -1894,9 +1893,9 @@ func testResponseStrings(t *testing.T, port int) { return } defer resp.Body.Close() - buf, err := ioutil.ReadAll(resp.Body) + buf, err := io.ReadAll(resp.Body) if err != nil { - t.Errorf("ioutil.ReadAll(resp.Body) failed with %v; want success", err) + t.Errorf("io.ReadAll(resp.Body) failed with %v; want success", err) return } @@ -1925,9 +1924,9 @@ func testResponseStrings(t *testing.T, port int) { return } defer resp.Body.Close() - buf, err := ioutil.ReadAll(resp.Body) + buf, err := io.ReadAll(resp.Body) if err != nil { - t.Errorf("ioutil.ReadAll(resp.Body) failed with %v; want success", err) + t.Errorf("io.ReadAll(resp.Body) failed with %v; want success", err) return } @@ -2062,9 +2061,9 @@ func testRequestQueryParams(t *testing.T, port int) { } defer resp.Body.Close() - buf, err := ioutil.ReadAll(resp.Body) + buf, err := io.ReadAll(resp.Body) if err != nil { - t.Errorf("ioutil.ReadAll(resp.Body) failed with %v; want success", err) + t.Errorf("io.ReadAll(resp.Body) failed with %v; want success", err) return } @@ -2293,7 +2292,7 @@ func testNonStandardNames(t *testing.T, port int, method string, jsonBody string t.Fatalf("failed to issue PATCH request: %v", err) } - body, err := ioutil.ReadAll(patchResp.Body) + body, err := io.ReadAll(patchResp.Body) if err != nil { t.Errorf("patchResp body couldn't be read: %v", err) } @@ -2324,9 +2323,9 @@ func testABEExists(t *testing.T, port int) { return } defer cresp.Body.Close() - buf, err := ioutil.ReadAll(cresp.Body) + buf, err := io.ReadAll(cresp.Body) if err != nil { - t.Errorf("ioutil.ReadAll(cresp.Body) failed with %v; want success", err) + t.Errorf("io.ReadAll(cresp.Body) failed with %v; want success", err) return } if got, want := cresp.StatusCode, http.StatusOK; got != want { @@ -2415,9 +2414,9 @@ func testABETrace(t *testing.T, port int) { t.Fatal(err) } defer resp.Body.Close() - buf, err := ioutil.ReadAll(resp.Body) + buf, err := io.ReadAll(resp.Body) if err != nil { - t.Errorf("ioutil.ReadAll(cresp.Body) failed with %v; want success", err) + t.Errorf("io.ReadAll(cresp.Body) failed with %v; want success", err) return } if got, want := resp.StatusCode, http.StatusOK; got != want { diff --git a/internal/codegenerator/parse_req.go b/internal/codegenerator/parse_req.go index ca92d8e9985..65749a65a87 100644 --- a/internal/codegenerator/parse_req.go +++ b/internal/codegenerator/parse_req.go @@ -3,7 +3,6 @@ package codegenerator import ( "fmt" "io" - "io/ioutil" "google.golang.org/protobuf/proto" "google.golang.org/protobuf/types/pluginpb" @@ -11,13 +10,13 @@ import ( // ParseRequest parses a code generator request from a proto Message. func ParseRequest(r io.Reader) (*pluginpb.CodeGeneratorRequest, error) { - input, err := ioutil.ReadAll(r) + input, err := io.ReadAll(r) if err != nil { - return nil, fmt.Errorf("failed to read code generator request: %v", err) + return nil, fmt.Errorf("failed to read code generator request: %w", err) } req := new(pluginpb.CodeGeneratorRequest) - if err = proto.Unmarshal(input, req); err != nil { - return nil, fmt.Errorf("failed to unmarshal code generator request: %v", err) + if err := proto.Unmarshal(input, req); err != nil { + return nil, fmt.Errorf("failed to unmarshal code generator request: %w", err) } return req, nil } diff --git a/internal/descriptor/grpc_api_configuration.go b/internal/descriptor/grpc_api_configuration.go index a3ae3b83ad1..ad6cfa1de07 100644 --- a/internal/descriptor/grpc_api_configuration.go +++ b/internal/descriptor/grpc_api_configuration.go @@ -3,7 +3,7 @@ package descriptor import ( "encoding/json" "fmt" - "io/ioutil" + "os" "strings" "github.com/grpc-ecosystem/grpc-gateway/v2/internal/descriptor/apiconfig" @@ -64,9 +64,9 @@ func registerHTTPRulesFromGrpcAPIService(registry *Registry, service *apiconfig. // Note that for the purposes of the gateway generator we only consider a subset of all // available features google supports in their service descriptions. func (r *Registry) LoadGrpcAPIServiceFromYAML(yamlFile string) error { - yamlFileContents, err := ioutil.ReadFile(yamlFile) + yamlFileContents, err := os.ReadFile(yamlFile) if err != nil { - return fmt.Errorf("failed to read gRPC API Configuration description from '%v': %v", yamlFile, err) + return fmt.Errorf("failed to read gRPC API Configuration description from %q: %w", yamlFile, err) } service, err := loadGrpcAPIServiceFromYAML(yamlFileContents, yamlFile) diff --git a/internal/descriptor/openapi_configuration.go b/internal/descriptor/openapi_configuration.go index 3487c06d64b..c0a58a671e8 100644 --- a/internal/descriptor/openapi_configuration.go +++ b/internal/descriptor/openapi_configuration.go @@ -3,7 +3,7 @@ package descriptor import ( "encoding/json" "fmt" - "io/ioutil" + "os" "github.com/grpc-ecosystem/grpc-gateway/v2/internal/descriptor/openapiconfig" "google.golang.org/protobuf/encoding/protojson" @@ -12,9 +12,8 @@ import ( func loadOpenAPIConfigFromYAML(yamlFileContents []byte, yamlSourceLogName string) (*openapiconfig.OpenAPIConfig, error) { var yamlContents interface{} - err := yaml.Unmarshal(yamlFileContents, &yamlContents) - if err != nil { - return nil, fmt.Errorf("failed to parse gRPC API Configuration from YAML in '%v': %v", yamlSourceLogName, err) + if err := yaml.Unmarshal(yamlFileContents, &yamlContents); err != nil { + return nil, fmt.Errorf("failed to parse gRPC API Configuration from YAML in %q: %w", yamlSourceLogName, err) } jsonContents, err := json.Marshal(yamlContents) @@ -29,7 +28,7 @@ func loadOpenAPIConfigFromYAML(yamlFileContents []byte, yamlSourceLogName string openapiConfiguration := openapiconfig.OpenAPIConfig{} if err := unmarshaler.Unmarshal(jsonContents, &openapiConfiguration); err != nil { - return nil, fmt.Errorf("failed to parse OpenAPI Configuration from YAML in '%v': %v", yamlSourceLogName, err) + return nil, fmt.Errorf("failed to parse OpenAPI Configuration from YAML in '%v': %w", yamlSourceLogName, err) } return &openapiConfiguration, nil @@ -42,7 +41,7 @@ func registerOpenAPIOptions(registry *Registry, openAPIConfig *openapiconfig.Ope } if err := registry.RegisterOpenAPIOptions(openAPIConfig.OpenapiOptions); err != nil { - return fmt.Errorf("failed to register option in %s: %s", yamlSourceLogName, err) + return fmt.Errorf("failed to register option in %s: %w", yamlSourceLogName, err) } return nil } @@ -51,7 +50,7 @@ func registerOpenAPIOptions(registry *Registry, openAPIConfig *openapiconfig.Ope // and registers the OpenAPI options the given registry. // This must be done after loading the proto file. func (r *Registry) LoadOpenAPIConfigFromYAML(yamlFile string) error { - yamlFileContents, err := ioutil.ReadFile(yamlFile) + yamlFileContents, err := os.ReadFile(yamlFile) if err != nil { return fmt.Errorf("failed to read OpenAPI Configuration description from '%v': %v", yamlFile, err) } diff --git a/internal/httprule/fuzz.go b/internal/httprule/fuzz.go index 138f7c12f0e..c056bd3058a 100644 --- a/internal/httprule/fuzz.go +++ b/internal/httprule/fuzz.go @@ -1,10 +1,10 @@ +//go:build gofuzz // +build gofuzz package httprule func Fuzz(data []byte) int { - _, err := Parse(string(data)) - if err != nil { + if _, err := Parse(string(data)); err != nil { return 0 } return 0 diff --git a/protoc-gen-openapiv2/internal/genopenapi/helpers.go b/protoc-gen-openapiv2/internal/genopenapi/helpers.go index 0885be81162..37b328c5575 100644 --- a/protoc-gen-openapiv2/internal/genopenapi/helpers.go +++ b/protoc-gen-openapiv2/internal/genopenapi/helpers.go @@ -14,7 +14,7 @@ func fieldName(k string) string { return strings.ReplaceAll(cases.Title(language.AmericanEnglish).String(k), "-", "_") } -//this method will filter the same fields and return the unique one +// this method will filter the same fields and return the unique one func getUniqueFields(schemaFieldsRequired []string, fieldsRequired []string) []string { var unique []string var index *int diff --git a/protoc-gen-openapiv2/internal/genopenapi/helpers_go111_old.go b/protoc-gen-openapiv2/internal/genopenapi/helpers_go111_old.go index b8db1196508..d2b504a9ab8 100644 --- a/protoc-gen-openapiv2/internal/genopenapi/helpers_go111_old.go +++ b/protoc-gen-openapiv2/internal/genopenapi/helpers_go111_old.go @@ -1,4 +1,5 @@ -//+build !go1.12 +//go:build !go1.12 +// +build !go1.12 package genopenapi diff --git a/protoc-gen-openapiv2/internal/genopenapi/template.go b/protoc-gen-openapiv2/internal/genopenapi/template.go index 7bbd8d643a8..f23b01cf208 100644 --- a/protoc-gen-openapiv2/internal/genopenapi/template.go +++ b/protoc-gen-openapiv2/internal/genopenapi/template.go @@ -4,7 +4,6 @@ import ( "bytes" "encoding/json" "fmt" - "io/ioutil" "math" "net/textproto" "os" @@ -2234,7 +2233,7 @@ func goTemplateComments(comment string, data interface{}, reg *descriptor.Regist tpl, err := template.New("").Funcs(template.FuncMap{ // Allows importing documentation from a file "import": func(name string) string { - file, err := ioutil.ReadFile(name) + file, err := os.ReadFile(name) if err != nil { return err.Error() } diff --git a/runtime/convert.go b/runtime/convert.go index cfa540787fd..d7b15fcfb3f 100644 --- a/runtime/convert.go +++ b/runtime/convert.go @@ -37,7 +37,7 @@ func BoolSlice(val, sep string) ([]bool, error) { for i, v := range s { value, err := Bool(v) if err != nil { - return values, err + return nil, err } values[i] = value } @@ -57,7 +57,7 @@ func Float64Slice(val, sep string) ([]float64, error) { for i, v := range s { value, err := Float64(v) if err != nil { - return values, err + return nil, err } values[i] = value } @@ -81,7 +81,7 @@ func Float32Slice(val, sep string) ([]float32, error) { for i, v := range s { value, err := Float32(v) if err != nil { - return values, err + return nil, err } values[i] = value } @@ -101,7 +101,7 @@ func Int64Slice(val, sep string) ([]int64, error) { for i, v := range s { value, err := Int64(v) if err != nil { - return values, err + return nil, err } values[i] = value } @@ -125,7 +125,7 @@ func Int32Slice(val, sep string) ([]int32, error) { for i, v := range s { value, err := Int32(v) if err != nil { - return values, err + return nil, err } values[i] = value } @@ -145,7 +145,7 @@ func Uint64Slice(val, sep string) ([]uint64, error) { for i, v := range s { value, err := Uint64(v) if err != nil { - return values, err + return nil, err } values[i] = value } @@ -169,7 +169,7 @@ func Uint32Slice(val, sep string) ([]uint32, error) { for i, v := range s { value, err := Uint32(v) if err != nil { - return values, err + return nil, err } values[i] = value } @@ -197,7 +197,7 @@ func BytesSlice(val, sep string) ([][]byte, error) { for i, v := range s { value, err := Bytes(v) if err != nil { - return values, err + return nil, err } values[i] = value } @@ -209,8 +209,7 @@ func Timestamp(val string) (*timestamppb.Timestamp, error) { var r timestamppb.Timestamp val = strconv.Quote(strings.Trim(val, `"`)) unmarshaler := &protojson.UnmarshalOptions{} - err := unmarshaler.Unmarshal([]byte(val), &r) - if err != nil { + if err := unmarshaler.Unmarshal([]byte(val), &r); err != nil { return nil, err } return &r, nil @@ -221,8 +220,7 @@ func Duration(val string) (*durationpb.Duration, error) { var r durationpb.Duration val = strconv.Quote(strings.Trim(val, `"`)) unmarshaler := &protojson.UnmarshalOptions{} - err := unmarshaler.Unmarshal([]byte(val), &r) - if err != nil { + if err := unmarshaler.Unmarshal([]byte(val), &r); err != nil { return nil, err } return &r, nil @@ -257,66 +255,64 @@ func EnumSlice(val, sep string, enumValMap map[string]int32) ([]int32, error) { for i, v := range s { value, err := Enum(v, enumValMap) if err != nil { - return values, err + return nil, err } values[i] = value } return values, nil } -/* - Support for google.protobuf.wrappers on top of primitive types -*/ +// Support for google.protobuf.wrappers on top of primitive types // StringValue well-known type support as wrapper around string type func StringValue(val string) (*wrapperspb.StringValue, error) { - return &wrapperspb.StringValue{Value: val}, nil + return wrapperspb.String(val), nil } // FloatValue well-known type support as wrapper around float32 type func FloatValue(val string) (*wrapperspb.FloatValue, error) { parsedVal, err := Float32(val) - return &wrapperspb.FloatValue{Value: parsedVal}, err + return wrapperspb.Float(parsedVal), err } // DoubleValue well-known type support as wrapper around float64 type func DoubleValue(val string) (*wrapperspb.DoubleValue, error) { parsedVal, err := Float64(val) - return &wrapperspb.DoubleValue{Value: parsedVal}, err + return wrapperspb.Double(parsedVal), err } // BoolValue well-known type support as wrapper around bool type func BoolValue(val string) (*wrapperspb.BoolValue, error) { parsedVal, err := Bool(val) - return &wrapperspb.BoolValue{Value: parsedVal}, err + return wrapperspb.Bool(parsedVal), err } // Int32Value well-known type support as wrapper around int32 type func Int32Value(val string) (*wrapperspb.Int32Value, error) { parsedVal, err := Int32(val) - return &wrapperspb.Int32Value{Value: parsedVal}, err + return wrapperspb.Int32(parsedVal), err } // UInt32Value well-known type support as wrapper around uint32 type func UInt32Value(val string) (*wrapperspb.UInt32Value, error) { parsedVal, err := Uint32(val) - return &wrapperspb.UInt32Value{Value: parsedVal}, err + return wrapperspb.UInt32(parsedVal), err } // Int64Value well-known type support as wrapper around int64 type func Int64Value(val string) (*wrapperspb.Int64Value, error) { parsedVal, err := Int64(val) - return &wrapperspb.Int64Value{Value: parsedVal}, err + return wrapperspb.Int64(parsedVal), err } // UInt64Value well-known type support as wrapper around uint64 type func UInt64Value(val string) (*wrapperspb.UInt64Value, error) { parsedVal, err := Uint64(val) - return &wrapperspb.UInt64Value{Value: parsedVal}, err + return wrapperspb.UInt64(parsedVal), err } // BytesValue well-known type support as wrapper around bytes[] type func BytesValue(val string) (*wrapperspb.BytesValue, error) { parsedVal, err := Bytes(val) - return &wrapperspb.BytesValue{Value: parsedVal}, err + return wrapperspb.Bytes(parsedVal), err } diff --git a/runtime/handler_test.go b/runtime/handler_test.go index 3755cd31366..90629721916 100644 --- a/runtime/handler_test.go +++ b/runtime/handler_test.go @@ -3,7 +3,6 @@ package runtime_test import ( "context" "io" - "io/ioutil" "net/http" "net/http/httptest" "testing" @@ -104,7 +103,7 @@ func TestForwardResponseStream(t *testing.T) { if h := w.Header.Get("Transfer-Encoding"); h != "chunked" { t.Errorf("ForwardResponseStream missing header chunked") } - body, err := ioutil.ReadAll(w.Body) + body, err := io.ReadAll(w.Body) if err != nil { t.Errorf("Failed to read response body with %v", err) } @@ -238,7 +237,7 @@ func TestForwardResponseStreamCustomMarshaler(t *testing.T) { if h := w.Header.Get("Transfer-Encoding"); h != "chunked" { t.Errorf("ForwardResponseStream missing header chunked") } - body, err := ioutil.ReadAll(w.Body) + body, err := io.ReadAll(w.Body) if err != nil { t.Errorf("Failed to read response body with %v", err) } @@ -302,7 +301,7 @@ func TestForwardResponseMessage(t *testing.T) { if h := w.Header.Get("Content-Type"); h != tt.contentType { t.Errorf("Content-Type %v want %v", h, tt.contentType) } - body, err := ioutil.ReadAll(w.Body) + body, err := io.ReadAll(w.Body) if err != nil { t.Errorf("Failed to read response body with %v", err) } diff --git a/runtime/marshal_json_test.go b/runtime/marshal_json_test.go index 5037aea9348..8fe08aeb9bf 100644 --- a/runtime/marshal_json_test.go +++ b/runtime/marshal_json_test.go @@ -100,9 +100,8 @@ func TestJSONBuiltinUnmarshalField(t *testing.T) { func alloc(t reflect.Type) reflect.Value { if t == nil { return reflect.ValueOf(new(interface{})) - } else { - return reflect.New(t) } + return reflect.New(t) } func TestJSONBuiltinUnmarshalFieldKnownErrors(t *testing.T) { @@ -250,7 +249,7 @@ var ( json: `"2016-05-10T10:19:13.123Z"`, }, { - data: &wrapperspb.Int32Value{Value: 123}, + data: wrapperspb.Int32(123), json: "123", }, } diff --git a/runtime/marshal_jsonpb_test.go b/runtime/marshal_jsonpb_test.go index d82290c1416..43029736e37 100644 --- a/runtime/marshal_jsonpb_test.go +++ b/runtime/marshal_jsonpb_test.go @@ -637,31 +637,31 @@ var ( }, { - data: &wrapperspb.BoolValue{Value: true}, + data: wrapperspb.Bool(true), json: "true", }, { - data: &wrapperspb.DoubleValue{Value: 123.456}, + data: wrapperspb.Double(123.456), json: "123.456", }, { - data: &wrapperspb.FloatValue{Value: 123.456}, + data: wrapperspb.Float(123.456), json: "123.456", }, { - data: &wrapperspb.Int32Value{Value: -123}, + data: wrapperspb.Int32(-123), json: "-123", }, { - data: &wrapperspb.Int64Value{Value: -123}, + data: wrapperspb.Int64(-123), json: `"-123"`, }, { - data: &wrapperspb.UInt32Value{Value: 123}, + data: wrapperspb.UInt32(123), json: "123", }, { - data: &wrapperspb.UInt64Value{Value: 123}, + data: wrapperspb.UInt64(123), json: `"123"`, }, // TODO(yugui) Add other well-known types once jsonpb supports them diff --git a/runtime/marshal_proto.go b/runtime/marshal_proto.go index 007f8f1a2c7..521da0f5dca 100644 --- a/runtime/marshal_proto.go +++ b/runtime/marshal_proto.go @@ -1,10 +1,8 @@ package runtime import ( - "io" - "errors" - "io/ioutil" + "io" "google.golang.org/protobuf/proto" ) @@ -38,7 +36,7 @@ func (*ProtoMarshaller) Unmarshal(data []byte, value interface{}) error { // NewDecoder returns a Decoder which reads proto stream from "reader". func (marshaller *ProtoMarshaller) NewDecoder(reader io.Reader) Decoder { return DecoderFunc(func(value interface{}) error { - buffer, err := ioutil.ReadAll(reader) + buffer, err := io.ReadAll(reader) if err != nil { return err } diff --git a/runtime/query.go b/runtime/query.go index 65d0da47166..56b796e6f4d 100644 --- a/runtime/query.go +++ b/runtime/query.go @@ -180,10 +180,10 @@ func parseField(fieldDescriptor protoreflect.FieldDescriptor, value string) (pro return protoreflect.ValueOfBool(v), nil case protoreflect.EnumKind: enum, err := protoregistry.GlobalTypes.FindEnumByName(fieldDescriptor.Enum().FullName()) - switch { - case errors.Is(err, protoregistry.NotFound): - return protoreflect.Value{}, fmt.Errorf("enum %q is not registered", fieldDescriptor.Enum().FullName()) - case err != nil: + if err != nil { + if errors.Is(err, protoregistry.NotFound) { + return protoreflect.Value{}, fmt.Errorf("enum %q is not registered", fieldDescriptor.Enum().FullName()) + } return protoreflect.Value{}, fmt.Errorf("failed to look up enum: %w", err) } // Look for enum by name @@ -194,8 +194,7 @@ func parseField(fieldDescriptor protoreflect.FieldDescriptor, value string) (pro return protoreflect.Value{}, fmt.Errorf("%q is not a valid value", value) } // Look for enum by number - v = enum.Descriptor().Values().ByNumber(protoreflect.EnumNumber(i)) - if v == nil { + if v = enum.Descriptor().Values().ByNumber(protoreflect.EnumNumber(i)); v == nil { return protoreflect.Value{}, fmt.Errorf("%q is not a valid value", value) } } @@ -271,51 +270,51 @@ func parseMessage(msgDescriptor protoreflect.MessageDescriptor, value string) (p if err != nil { return protoreflect.Value{}, err } - msg = &wrapperspb.DoubleValue{Value: v} + msg = wrapperspb.Double(v) case "google.protobuf.FloatValue": v, err := strconv.ParseFloat(value, 32) if err != nil { return protoreflect.Value{}, err } - msg = &wrapperspb.FloatValue{Value: float32(v)} + msg = wrapperspb.Float(float32(v)) case "google.protobuf.Int64Value": v, err := strconv.ParseInt(value, 10, 64) if err != nil { return protoreflect.Value{}, err } - msg = &wrapperspb.Int64Value{Value: v} + msg = wrapperspb.Int64(v) case "google.protobuf.Int32Value": v, err := strconv.ParseInt(value, 10, 32) if err != nil { return protoreflect.Value{}, err } - msg = &wrapperspb.Int32Value{Value: int32(v)} + msg = wrapperspb.Int32(int32(v)) case "google.protobuf.UInt64Value": v, err := strconv.ParseUint(value, 10, 64) if err != nil { return protoreflect.Value{}, err } - msg = &wrapperspb.UInt64Value{Value: v} + msg = wrapperspb.UInt64(v) case "google.protobuf.UInt32Value": v, err := strconv.ParseUint(value, 10, 32) if err != nil { return protoreflect.Value{}, err } - msg = &wrapperspb.UInt32Value{Value: uint32(v)} + msg = wrapperspb.UInt32(uint32(v)) case "google.protobuf.BoolValue": v, err := strconv.ParseBool(value) if err != nil { return protoreflect.Value{}, err } - msg = &wrapperspb.BoolValue{Value: v} + msg = wrapperspb.Bool(v) case "google.protobuf.StringValue": - msg = &wrapperspb.StringValue{Value: value} + msg = wrapperspb.String(value) case "google.protobuf.BytesValue": v, err := Bytes(value) if err != nil { return protoreflect.Value{}, err } - msg = &wrapperspb.BytesValue{Value: v} + msg = wrapperspb.Bytes(v) case "google.protobuf.FieldMask": fm := &field_mask.FieldMask{} fm.Paths = append(fm.Paths, strings.Split(value, ",")...) diff --git a/runtime/query_test.go b/runtime/query_test.go index bc885aaf28e..0680264be05 100644 --- a/runtime/query_test.go +++ b/runtime/query_test.go @@ -190,15 +190,15 @@ func TestPopulateParameters(t *testing.T) { TimestampValue: timePb, DurationValue: durationPb, FieldmaskValue: fieldmaskPb, - WrapperFloatValue: &wrapperspb.FloatValue{Value: 1.5}, - WrapperDoubleValue: &wrapperspb.DoubleValue{Value: 2.5}, - WrapperInt64Value: &wrapperspb.Int64Value{Value: -1}, - WrapperInt32Value: &wrapperspb.Int32Value{Value: -2}, - WrapperUInt64Value: &wrapperspb.UInt64Value{Value: 3}, - WrapperUInt32Value: &wrapperspb.UInt32Value{Value: 4}, - WrapperBoolValue: &wrapperspb.BoolValue{Value: true}, - WrapperStringValue: &wrapperspb.StringValue{Value: "str"}, - WrapperBytesValue: &wrapperspb.BytesValue{Value: []byte("abc123!?$*&()'-=@~")}, + WrapperFloatValue: wrapperspb.Float(1.5), + WrapperDoubleValue: wrapperspb.Double(2.5), + WrapperInt64Value: wrapperspb.Int64(-1), + WrapperInt32Value: wrapperspb.Int32(-2), + WrapperUInt64Value: wrapperspb.UInt64(3), + WrapperUInt32Value: wrapperspb.UInt32(4), + WrapperBoolValue: wrapperspb.Bool(true), + WrapperStringValue: wrapperspb.String("str"), + WrapperBytesValue: wrapperspb.Bytes([]byte("abc123!?$*&()'-=@~")), MapValue: map[string]string{ "key": "value", "second": "bar", @@ -269,15 +269,15 @@ func TestPopulateParameters(t *testing.T) { TimestampValue: timePb, DurationValue: durationPb, FieldmaskValue: fieldmaskPb, - WrapperFloatValue: &wrapperspb.FloatValue{Value: 1.5}, - WrapperDoubleValue: &wrapperspb.DoubleValue{Value: 2.5}, - WrapperInt64Value: &wrapperspb.Int64Value{Value: -1}, - WrapperInt32Value: &wrapperspb.Int32Value{Value: -2}, - WrapperUInt64Value: &wrapperspb.UInt64Value{Value: 3}, - WrapperUInt32Value: &wrapperspb.UInt32Value{Value: 4}, - WrapperBoolValue: &wrapperspb.BoolValue{Value: true}, - WrapperStringValue: &wrapperspb.StringValue{Value: "str"}, - WrapperBytesValue: &wrapperspb.BytesValue{Value: []byte("bytes")}, + WrapperFloatValue: wrapperspb.Float(1.5), + WrapperDoubleValue: wrapperspb.Double(2.5), + WrapperInt64Value: wrapperspb.Int64(-1), + WrapperInt32Value: wrapperspb.Int32(-2), + WrapperUInt64Value: wrapperspb.UInt64(3), + WrapperUInt32Value: wrapperspb.UInt32(4), + WrapperBoolValue: wrapperspb.Bool(true), + WrapperStringValue: wrapperspb.String("str"), + WrapperBytesValue: wrapperspb.Bytes([]byte("bytes")), StructValueValue: structValueValues[1], StructValue: structValues[1], }, diff --git a/utilities/readerfactory.go b/utilities/readerfactory.go index 6dd3854665f..01d26edae3c 100644 --- a/utilities/readerfactory.go +++ b/utilities/readerfactory.go @@ -3,13 +3,12 @@ package utilities import ( "bytes" "io" - "io/ioutil" ) // IOReaderFactory takes in an io.Reader and returns a function that will allow you to create a new reader that begins // at the start of the stream func IOReaderFactory(r io.Reader) (func() io.Reader, error) { - b, err := ioutil.ReadAll(r) + b, err := io.ReadAll(r) if err != nil { return nil, err }