Repository: googleapis/gapic-generator-go
Pull request #1336 (2023-05-22 15:02:42)

refactor: remove usage of deprecated io/ioutil


Patch:
diff --git a/cmd/protoc-gen-go_cli/main.go b/cmd/protoc-gen-go_cli/main.go
index ae2229c..6b69001 100644
--- a/cmd/protoc-gen-go_cli/main.go
+++ b/cmd/protoc-gen-go_cli/main.go
@@ -15,7 +15,7 @@
 package main
 
 import (
-	"io/ioutil"
+	"io"
 	"log"
 	"os"
 
@@ -25,7 +25,7 @@ import (
 )
 
 func main() {
-	reqBytes, err := ioutil.ReadAll(os.Stdin)
+	reqBytes, err := io.ReadAll(os.Stdin)
 	if err != nil {
 		log.Fatal(err)
 	}
diff --git a/cmd/protoc-gen-go_gapic/main.go b/cmd/protoc-gen-go_gapic/main.go
index db561d5..37fcfc3 100644
--- a/cmd/protoc-gen-go_gapic/main.go
+++ b/cmd/protoc-gen-go_gapic/main.go
@@ -15,7 +15,7 @@
 package main
 
 import (
-	"io/ioutil"
+	"io"
 	"log"
 	"os"
 
@@ -25,7 +25,7 @@ import (
 )
 
 func main() {
-	reqBytes, err := ioutil.ReadAll(os.Stdin)
+	reqBytes, err := io.ReadAll(os.Stdin)
 	if err != nil {
 		log.Fatal(err)
 	}
diff --git a/internal/gengapic/generator.go b/internal/gengapic/generator.go
index aa06db7..1d15a70 100644
--- a/internal/gengapic/generator.go
+++ b/internal/gengapic/generator.go
@@ -16,7 +16,6 @@ package gengapic
 
 import (
 	"fmt"
-	"io/ioutil"
 	"os"
 	"strings"
 	"time"
@@ -110,7 +109,7 @@ func (g *generator) init(req *plugin.CodeGeneratorRequest) error {
 	files = append(files, wellKnownTypeFiles...)
 
 	if opts.serviceConfigPath != "" {
-		y, err := ioutil.ReadFile(opts.serviceConfigPath)
+		y, err := os.ReadFile(opts.serviceConfigPath)
 		if err != nil {
 			return errors.E(nil, "error reading service config: %v", err)
 		}
diff --git a/internal/gengapic/genrest.go b/internal/gengapic/genrest.go
index e34072f..c691cb4 100644
--- a/internal/gengapic/genrest.go
+++ b/internal/gengapic/genrest.go
@@ -842,7 +842,7 @@ func (g *generator) pagingRESTCall(servName string, m *descriptor.MethodDescript
 	p(`      return err`)
 	p("    }")
 	p("")
-	p("    buf, err := ioutil.ReadAll(httpRsp.Body)")
+	p("    buf, err := io.ReadAll(httpRsp.Body)")
 	p("    if err != nil {")
 	p(`      return err`)
 	p("    }")
@@ -865,7 +865,7 @@ func (g *generator) pagingRESTCall(servName string, m *descriptor.MethodDescript
 	p("}")
 
 	g.imports[pbinfo.ImportSpec{Path: "google.golang.org/api/iterator"}] = true
-	g.imports[pbinfo.ImportSpec{Path: "io/ioutil"}] = true
+	g.imports[pbinfo.ImportSpec{Path: "io"}] = true
 	g.imports[pbinfo.ImportSpec{Path: "google.golang.org/protobuf/proto"}] = true
 	g.imports[pbinfo.ImportSpec{Path: "google.golang.org/protobuf/encoding/protojson"}] = true
 	g.imports[pbinfo.ImportSpec{Path: "google.golang.org/api/googleapi"}] = true
@@ -964,7 +964,7 @@ func (g *generator) lroRESTCall(servName string, m *descriptor.MethodDescriptorP
 	p("    return err")
 	p("  }")
 	p("")
-	p("  buf, err := ioutil.ReadAll(httpRsp.Body)")
+	p("  buf, err := io.ReadAll(httpRsp.Body)")
 	p("  if err != nil {")
 	p("    return err")
 	p("  }")
@@ -989,7 +989,7 @@ func (g *generator) lroRESTCall(servName string, m *descriptor.MethodDescriptorP
 	p("")
 
 	g.imports[pbinfo.ImportSpec{Path: "fmt"}] = true
-	g.imports[pbinfo.ImportSpec{Path: "io/ioutil"}] = true
+	g.imports[pbinfo.ImportSpec{Path: "io"}] = true
 	g.imports[pbinfo.ImportSpec{Path: "cloud.google.com/go/longrunning"}] = true
 	g.imports[pbinfo.ImportSpec{Path: "google.golang.org/api/googleapi"}] = true
 	g.imports[pbinfo.ImportSpec{Path: "google.golang.org/grpc/metadata"}] = true
@@ -1172,7 +1172,7 @@ func (g *generator) unaryRESTCall(servName string, m *descriptor.MethodDescripto
 	p("    return err")
 	p("  }")
 	p("")
-	p("  buf, err := ioutil.ReadAll(httpRsp.Body)")
+	p("  buf, err := io.ReadAll(httpRsp.Body)")
 	p("  if err != nil {")
 	p("    return err")
 	p("  }")
@@ -1203,7 +1203,7 @@ func (g *generator) unaryRESTCall(servName string, m *descriptor.MethodDescripto
 	p(ret)
 	p("}")
 
-	g.imports[pbinfo.ImportSpec{Path: "io/ioutil"}] = true
+	g.imports[pbinfo.ImportSpec{Path: "io"}] = true
 	g.imports[pbinfo.ImportSpec{Path: "google.golang.org/api/googleapi"}] = true
 	g.imports[inSpec] = true
 	g.imports[outSpec] = true
diff --git a/internal/grpc_service_config/config.go b/internal/grpc_service_config/config.go
index 267d840..bfc15b5 100644
--- a/internal/grpc_service_config/config.go
+++ b/internal/grpc_service_config/config.go
@@ -16,7 +16,6 @@ package grpc_service_config
 
 import (
 	"io"
-	"io/ioutil"
 	"log"
 
 	"github.com/golang/protobuf/ptypes"
@@ -37,7 +36,7 @@ type Config struct {
 // mapped by the names to the specific config values. Use the accessors on the
 // resulting Config to retrieve values for a Service or Method.
 func New(in io.Reader) (Config, error) {
-	data, err := ioutil.ReadAll(in)
+	data, err := io.ReadAll(in)
 	if err != nil {
 		return Config{}, err
 	}
diff --git a/internal/txtdiff/diff.go b/internal/txtdiff/diff.go
index 5bcd015..4f1660c 100644
--- a/internal/txtdiff/diff.go
+++ b/internal/txtdiff/diff.go
@@ -17,7 +17,7 @@ package txtdiff
 
 import (
 	"flag"
-	"io/ioutil"
+	"os"
 	"strings"
 	"testing"
 )
@@ -30,13 +30,13 @@ func Diff(t *testing.T, name, got, goldenFile string) {
 	t.Helper()
 
 	if *updateGolden {
-		if err := ioutil.WriteFile(goldenFile, []byte(got), 0644); err != nil {
+		if err := os.WriteFile(goldenFile, []byte(got), 0644); err != nil {
 			t.Fatal(err)
 		}
 		return
 	}
 
-	wantBytes, err := ioutil.ReadFile(goldenFile)
+	wantBytes, err := os.ReadFile(goldenFile)
 	if err != nil {
 		t.Fatal(err)
 	}


Test patch:
diff --git a/internal/gengapic/genrest_test.go b/internal/gengapic/genrest_test.go
index 6b8b31c..3cb672f 100644
--- a/internal/gengapic/genrest_test.go
+++ b/internal/gengapic/genrest_test.go
@@ -743,9 +743,9 @@ func TestGenRestMethod(t *testing.T) {
 			options: &options{diregapic: true},
 			imports: map[pbinfo.ImportSpec]bool{
 				{Path: "google.golang.org/protobuf/encoding/protojson"}: true,
-				{Path: "io/ioutil"}: true,
-				{Path: "net/url"}:   true,
-				{Path: "fmt"}:       true,
+				{Path: "io"}:      true,
+				{Path: "net/url"}: true,
+				{Path: "fmt"}:     true,
 				{Path: "google.golang.org/api/googleapi"}:                        true,
 				{Name: "foopb", Path: "google.golang.org/genproto/cloud/foo/v1"}: true,
 			},
@@ -769,7 +769,7 @@ func TestGenRestMethod(t *testing.T) {
 				{Path: "bytes"}: true,
 				{Path: "fmt"}:   true,
 				{Path: "google.golang.org/protobuf/encoding/protojson"}: true,
-				{Path: "io/ioutil"}:                       true,
+				{Path: "io"}: true,
 				{Path: "google.golang.org/api/googleapi"}: true,
 				{Path: "net/url"}:                         true,
 				{Path: "regexp"}:                          true,
@@ -786,10 +786,10 @@ func TestGenRestMethod(t *testing.T) {
 				{Path: "net/url"}: true,
 				{Path: "google.golang.org/protobuf/encoding/protojson"}: true,
 				{Path: "fmt"}: true,
-				{Path: "google.golang.org/api/googleapi"}:                        true,
-				{Path: "google.golang.org/api/iterator"}:                         true,
-				{Path: "google.golang.org/protobuf/proto"}:                       true,
-				{Path: "io/ioutil"}:                                              true,
+				{Path: "google.golang.org/api/googleapi"}:  true,
+				{Path: "google.golang.org/api/iterator"}:   true,
+				{Path: "google.golang.org/protobuf/proto"}: true,
+				{Path: "io"}: true,
 				{Name: "foopb", Path: "google.golang.org/genproto/cloud/foo/v1"}: true,
 			},
 		},
@@ -828,13 +828,13 @@ func TestGenRestMethod(t *testing.T) {
 				{Path: "bytes"}: true,
 				{Path: "cloud.google.com/go/longrunning"}: true,
 				{Path: "fmt"}: true,
-				{Path: "google.golang.org/api/googleapi"}:                        true,
-				{Path: "google.golang.org/grpc/metadata"}:                        true,
-				{Path: "google.golang.org/protobuf/encoding/protojson"}:          true,
-				{Path: "io/ioutil"}:                                              true,
-				{Path: "net/url"}:                                                true,
-				{Path: "time"}:                                                   true,
-				{Name: "foopb", Path: "google.golang.org/genproto/cloud/foo/v1"}: true,
+				{Path: "google.golang.org/api/googleapi"}:               true,
+				{Path: "google.golang.org/grpc/metadata"}:               true,
+				{Path: "google.golang.org/protobuf/encoding/protojson"}: true,
+				{Path: "io"}:      true,
+				{Path: "net/url"}: true,
+				{Path: "time"}:    true,
+				{Name: "foopb", Path: "google.golang.org/genproto/cloud/foo/v1"}:                       true,
 				{Name: "longrunningpb", Path: "cloud.google.com/go/longrunning/autogen/longrunningpb"}: true,
 			},
 		},
@@ -846,7 +846,7 @@ func TestGenRestMethod(t *testing.T) {
 				{Path: "bytes"}: true,
 				{Path: "fmt"}:   true,
 				{Path: "google.golang.org/protobuf/encoding/protojson"}: true,
-				{Path: "io/ioutil"}:                       true,
+				{Path: "io"}: true,
 				{Path: "google.golang.org/api/googleapi"}: true,
 				{Path: "net/url"}:                         true,
 				{Path: "regexp"}:                          true,
@@ -863,9 +863,9 @@ func TestGenRestMethod(t *testing.T) {
 				{Path: "bytes"}: true,
 				{Path: "fmt"}:   true,
 				{Path: "google.golang.org/protobuf/encoding/protojson"}: true,
-				{Path: "io/ioutil"}:                       true,
-				{Path: "google.golang.org/api/googleapi"}: true,
-				{Path: "net/url"}:                         true,
+				{Path: "io"}: true,
+				{Path: "google.golang.org/api/googleapi"}:                        true,
+				{Path: "net/url"}:                                                true,
 				{Name: "foopb", Path: "google.golang.org/genproto/cloud/foo/v1"}: true,
 			},
 		},
diff --git a/internal/gengapic/testdata/rest_CustomOp.want b/internal/gengapic/testdata/rest_CustomOp.want
index cb4ad95..06454d0 100644
--- a/internal/gengapic/testdata/rest_CustomOp.want
+++ b/internal/gengapic/testdata/rest_CustomOp.want
@@ -39,7 +39,7 @@ func (c *fooRESTClient) CustomOp(ctx context.Context, req *foopb.Foo, opts ...ga
 			return err
 		}
 
-		buf, err := ioutil.ReadAll(httpRsp.Body)
+		buf, err := io.ReadAll(httpRsp.Body)
 		if err != nil {
 			return err
 		}
diff --git a/internal/gengapic/testdata/rest_HttpBodyRPC.want b/internal/gengapic/testdata/rest_HttpBodyRPC.want
index 18ce2e3..636e56e 100644
--- a/internal/gengapic/testdata/rest_HttpBodyRPC.want
+++ b/internal/gengapic/testdata/rest_HttpBodyRPC.want
@@ -47,7 +47,7 @@ func (c *fooRESTClient) HttpBodyRPC(ctx context.Context, req *foopb.Foo, opts ..
 			return err
 		}
 
-		buf, err := ioutil.ReadAll(httpRsp.Body)
+		buf, err := io.ReadAll(httpRsp.Body)
 		if err != nil {
 			return err
 		}
diff --git a/internal/gengapic/testdata/rest_LongrunningRPC.want b/internal/gengapic/testdata/rest_LongrunningRPC.want
index 871c66d..21ac04e 100644
--- a/internal/gengapic/testdata/rest_LongrunningRPC.want
+++ b/internal/gengapic/testdata/rest_LongrunningRPC.want
@@ -36,7 +36,7 @@ func (c *fooRESTClient) LongrunningRPC(ctx context.Context, req *foopb.Foo, opts
 			return err
 		}
 
-		buf, err := ioutil.ReadAll(httpRsp.Body)
+		buf, err := io.ReadAll(httpRsp.Body)
 		if err != nil {
 			return err
 		}
diff --git a/internal/gengapic/testdata/rest_PagingRPC.want b/internal/gengapic/testdata/rest_PagingRPC.want
index c5747bf..f09f92a 100644
--- a/internal/gengapic/testdata/rest_PagingRPC.want
+++ b/internal/gengapic/testdata/rest_PagingRPC.want
@@ -50,7 +50,7 @@ func (c *fooRESTClient) PagingRPC(ctx context.Context, req *foopb.PagedFooReques
 				return err
 			}
 
-			buf, err := ioutil.ReadAll(httpRsp.Body)
+			buf, err := io.ReadAll(httpRsp.Body)
 			if err != nil {
 				return err
 			}
diff --git a/internal/gengapic/testdata/rest_UnaryRPC.want b/internal/gengapic/testdata/rest_UnaryRPC.want
index 95aad1a..1d5f0f5 100644
--- a/internal/gengapic/testdata/rest_UnaryRPC.want
+++ b/internal/gengapic/testdata/rest_UnaryRPC.want
@@ -53,7 +53,7 @@ func (c *fooRESTClient) UnaryRPC(ctx context.Context, req *foopb.Foo, opts ...ga
 			return err
 		}
 
-		buf, err := ioutil.ReadAll(httpRsp.Body)
+		buf, err := io.ReadAll(httpRsp.Body)
 		if err != nil {
 			return err
 		}
diff --git a/internal/gengapic/testdata/rest_UpdateRPC.want b/internal/gengapic/testdata/rest_UpdateRPC.want
index 2236933..096a94e 100644
--- a/internal/gengapic/testdata/rest_UpdateRPC.want
+++ b/internal/gengapic/testdata/rest_UpdateRPC.want
@@ -55,7 +55,7 @@ func (c *fooRESTClient) UpdateRPC(ctx context.Context, req *foopb.UpdateRequest,
 			return err
 		}
 
-		buf, err := ioutil.ReadAll(httpRsp.Body)
+		buf, err := io.ReadAll(httpRsp.Body)
 		if err != nil {
 			return err
 		}
diff --git a/showcase/compliance_test.go b/showcase/compliance_test.go
index 137b631..74102ad 100644
--- a/showcase/compliance_test.go
+++ b/showcase/compliance_test.go
@@ -17,7 +17,6 @@ package showcase
 import (
 	"context"
 	"fmt"
-	"io/ioutil"
 	"os"
 	"os/exec"
 	"path"
@@ -110,7 +109,7 @@ func getComplianceSuite() (*genprotopb.ComplianceSuite, error) {
 		return nil, err
 	}
 
-	complianceSuiteJSON, err := ioutil.ReadFile(filePath)
+	complianceSuiteJSON, err := os.ReadFile(filePath)
 	if err != nil {
 		return nil, fmt.Errorf("could not open suite file %q", filePath)
 	}