Skip to content

Commit

Permalink
Replace deprecated ioutil pkg with os & io (#2254)
Browse files Browse the repository at this point in the history
As of Go 1.16, the same functionality is now provided by package io or
package os, and those implementations should be preferred in new code.

So replacing all usage of ioutil pkg with io & os.
  • Loading branch information
abhinavnair authored Jun 25, 2022
1 parent a8f112e commit 53ada82
Show file tree
Hide file tree
Showing 24 changed files with 70 additions and 77 deletions.
2 changes: 1 addition & 1 deletion _examples/embedding/subdir/resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package subdir

import (
context "context"
"context"

"github.com/99designs/gqlgen/_examples/embedding/subdir/gendir"
)
Expand Down
15 changes: 7 additions & 8 deletions _examples/fileupload/fileupload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package fileupload
import (
"context"
"io"
"io/ioutil"
"net/http/httptest"
"os"
"testing"
Expand All @@ -23,12 +22,12 @@ func TestFileUpload(t *testing.T) {
defer srv.Close()
gql := gqlclient.New(srv.Config.Handler, gqlclient.Path("/graphql"))

aTxtFile, _ := ioutil.TempFile(os.TempDir(), "a.txt")
aTxtFile, _ := os.CreateTemp(os.TempDir(), "a.txt")
defer os.Remove(aTxtFile.Name())
aTxtFile.WriteString(`test`)

a1TxtFile, _ := ioutil.TempFile(os.TempDir(), "a.txt")
b1TxtFile, _ := ioutil.TempFile(os.TempDir(), "b.txt")
a1TxtFile, _ := os.CreateTemp(os.TempDir(), "a.txt")
b1TxtFile, _ := os.CreateTemp(os.TempDir(), "b.txt")
defer os.Remove(a1TxtFile.Name())
defer os.Remove(b1TxtFile.Name())
a1TxtFile.WriteString(`test1`)
Expand All @@ -38,7 +37,7 @@ func TestFileUpload(t *testing.T) {
resolver.MutationResolver.SingleUpload = func(ctx context.Context, file graphql.Upload) (*model.File, error) {
require.NotNil(t, file)
require.NotNil(t, file.File)
content, err := ioutil.ReadAll(file.File)
content, err := io.ReadAll(file.File)
require.Nil(t, err)
require.Equal(t, string(content), "test")

Expand Down Expand Up @@ -75,7 +74,7 @@ func TestFileUpload(t *testing.T) {
require.Equal(t, req.ID, 1)
require.NotNil(t, req.File)
require.NotNil(t, req.File.File)
content, err := ioutil.ReadAll(req.File.File)
content, err := io.ReadAll(req.File.File)
require.Nil(t, err)
require.Equal(t, string(content), "test")

Expand Down Expand Up @@ -114,7 +113,7 @@ func TestFileUpload(t *testing.T) {
var resp []*model.File
for i := range files {
require.NotNil(t, files[i].File)
content, err := ioutil.ReadAll(files[i].File)
content, err := io.ReadAll(files[i].File)
require.Nil(t, err)
contents = append(contents, string(content))
resp = append(resp, &model.File{
Expand Down Expand Up @@ -164,7 +163,7 @@ func TestFileUpload(t *testing.T) {
for i := range req {
require.NotNil(t, req[i].File)
require.NotNil(t, req[i].File.File)
content, err := ioutil.ReadAll(req[i].File.File)
content, err := io.ReadAll(req[i].File.File)
require.Nil(t, err)
ids = append(ids, req[i].ID)
contents = append(contents, string(content))
Expand Down
10 changes: 5 additions & 5 deletions _examples/fileupload/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"context"
"errors"
"io/ioutil"
"io"
"log"
"net/http"

Expand Down Expand Up @@ -41,7 +41,7 @@ func getResolver() *fileupload.Stub {
resolver := &fileupload.Stub{}

resolver.MutationResolver.SingleUpload = func(ctx context.Context, file graphql.Upload) (*model.File, error) {
content, err := ioutil.ReadAll(file.File)
content, err := io.ReadAll(file.File)
if err != nil {
return nil, err
}
Expand All @@ -52,7 +52,7 @@ func getResolver() *fileupload.Stub {
}, nil
}
resolver.MutationResolver.SingleUploadWithPayload = func(ctx context.Context, req model.UploadFile) (*model.File, error) {
content, err := ioutil.ReadAll(req.File.File)
content, err := io.ReadAll(req.File.File)
if err != nil {
return nil, err
}
Expand All @@ -68,7 +68,7 @@ func getResolver() *fileupload.Stub {
}
var resp []*model.File
for i := range files {
content, err := ioutil.ReadAll(files[i].File)
content, err := io.ReadAll(files[i].File)
if err != nil {
return []*model.File{}, err
}
Expand All @@ -86,7 +86,7 @@ func getResolver() *fileupload.Stub {
}
var resp []*model.File
for i := range req {
content, err := ioutil.ReadAll(req[i].File.File)
content, err := io.ReadAll(req[i].File.File)
if err != nil {
return []*model.File{}, err
}
Expand Down
4 changes: 2 additions & 2 deletions _examples/scalars/resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
package scalars

import (
context "context"
"context"
"fmt"
time "time"
"time"

"github.com/99designs/gqlgen/_examples/scalars/external"
"github.com/99designs/gqlgen/_examples/scalars/model"
Expand Down
4 changes: 2 additions & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"regexp"
Expand Down Expand Up @@ -130,7 +130,7 @@ func (p *Client) newRequest(query string, options ...Option) (*http.Request, err
if err != nil {
return nil, fmt.Errorf("encode: %w", err)
}
bd.HTTP.Body = ioutil.NopCloser(bytes.NewBuffer(requestBody))
bd.HTTP.Body = io.NopCloser(bytes.NewBuffer(requestBody))
default:
panic("unsupported encoding " + bd.HTTP.Header.Get("Content-Type"))
}
Expand Down
10 changes: 5 additions & 5 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package client_test
import (
"bytes"
"encoding/json"
"io/ioutil"
"io"
"mime/multipart"
"net/http"
"net/textproto"
Expand All @@ -15,7 +15,7 @@ import (

func TestClient(t *testing.T) {
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
b, err := io.ReadAll(r.Body)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -44,7 +44,7 @@ func TestClient(t *testing.T) {

func TestClientMultipartFormData(t *testing.T) {
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
bodyBytes, err := ioutil.ReadAll(r.Body)
bodyBytes, err := io.ReadAll(r.Body)
require.NoError(t, err)
require.Contains(t, string(bodyBytes), `Content-Disposition: form-data; name="operations"`)
require.Contains(t, string(bodyBytes), `{"query":"mutation ($input: Input!) {}","variables":{"file":{}}`)
Expand Down Expand Up @@ -74,7 +74,7 @@ func TestClientMultipartFormData(t *testing.T) {
ff.Write([]byte("Hello World"))
bodyWriter.Close()

bd.HTTP.Body = ioutil.NopCloser(bodyBuf)
bd.HTTP.Body = io.NopCloser(bodyBuf)
bd.HTTP.Header.Set("Content-Type", bodyWriter.FormDataContentType())
},
)
Expand Down Expand Up @@ -145,7 +145,7 @@ func TestAddCookie(t *testing.T) {

func TestAddExtensions(t *testing.T) {
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
b, err := io.ReadAll(r.Body)
if err != nil {
panic(err)
}
Expand Down
4 changes: 2 additions & 2 deletions client/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package client
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http/httptest"
"strings"

Expand Down Expand Up @@ -56,7 +56,7 @@ func (p *Client) WebsocketWithPayload(query string, initPayload map[string]inter
return errorSubscription(fmt.Errorf("request: %w", err))
}

requestBody, err := ioutil.ReadAll(r.Body)
requestBody, err := io.ReadAll(r.Body)
if err != nil {
return errorSubscription(fmt.Errorf("parse body: %w", err))
}
Expand Down
8 changes: 4 additions & 4 deletions client/withfilesoption.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"mime/multipart"
"net/http"
"net/textproto"
Expand Down Expand Up @@ -50,7 +50,7 @@ func WithFiles() Option {
bodyBuf := &bytes.Buffer{}
bodyWriter := multipart.NewWriter(bodyBuf)

//-b7955bd2e1d17b67ac157b9e9ddb6238888caefc6f3541920a1debad284d
// -b7955bd2e1d17b67ac157b9e9ddb6238888caefc6f3541920a1debad284d
// Content-Disposition: form-data; name="operations"
//
// {"query":"mutation ($input: Input!) {}","variables":{"input":{"file":{}}}
Expand Down Expand Up @@ -108,14 +108,14 @@ func WithFiles() Option {
for i, fileData := range filesGroup {
h := make(textproto.MIMEHeader)
h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="%d"; filename="%s"`, i, fileData[0].file.Name()))
b, _ := ioutil.ReadFile(fileData[0].file.Name())
b, _ := os.ReadFile(fileData[0].file.Name())
h.Set("Content-Type", http.DetectContentType(b))
ff, _ := bodyWriter.CreatePart(h)
ff.Write(b)
}
bodyWriter.Close()

bd.HTTP.Body = ioutil.NopCloser(bodyBuf)
bd.HTTP.Body = io.NopCloser(bodyBuf)
bd.HTTP.Header.Set("Content-Type", bodyWriter.FormDataContentType())
}
}
Expand Down
15 changes: 7 additions & 8 deletions client/withfilesoption_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package client_test

import (
"io"
"io/ioutil"
"mime"
"mime/multipart"
"net/http"
Expand All @@ -16,9 +15,9 @@ import (
)

func TestWithFiles(t *testing.T) {
tempFile1, _ := ioutil.TempFile(os.TempDir(), "tempFile1")
tempFile2, _ := ioutil.TempFile(os.TempDir(), "tempFile2")
tempFile3, _ := ioutil.TempFile(os.TempDir(), "tempFile3")
tempFile1, _ := os.CreateTemp(os.TempDir(), "tempFile1")
tempFile2, _ := os.CreateTemp(os.TempDir(), "tempFile2")
tempFile3, _ := os.CreateTemp(os.TempDir(), "tempFile3")
defer os.Remove(tempFile1.Name())
defer os.Remove(tempFile2.Name())
defer os.Remove(tempFile3.Name())
Expand All @@ -40,7 +39,7 @@ func TestWithFiles(t *testing.T) {
}
require.NoError(t, err)

slurp, err := ioutil.ReadAll(p)
slurp, err := io.ReadAll(p)
require.NoError(t, err)

contentDisposition := p.Header.Get("Content-Disposition")
Expand Down Expand Up @@ -82,7 +81,7 @@ func TestWithFiles(t *testing.T) {
}
require.NoError(t, err)

slurp, err := ioutil.ReadAll(p)
slurp, err := io.ReadAll(p)
require.NoError(t, err)

contentDisposition := p.Header.Get("Content-Disposition")
Expand Down Expand Up @@ -135,7 +134,7 @@ func TestWithFiles(t *testing.T) {
}
require.NoError(t, err)

slurp, err := ioutil.ReadAll(p)
slurp, err := io.ReadAll(p)
require.NoError(t, err)

contentDisposition := p.Header.Get("Content-Disposition")
Expand Down Expand Up @@ -194,7 +193,7 @@ func TestWithFiles(t *testing.T) {
}
require.NoError(t, err)

slurp, err := ioutil.ReadAll(p)
slurp, err := io.ReadAll(p)
require.NoError(t, err)

contentDisposition := p.Header.Get("Content-Disposition")
Expand Down
7 changes: 3 additions & 4 deletions codegen/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package config

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -61,7 +60,7 @@ func LoadDefaultConfig() (*Config, error) {
filename = filepath.ToSlash(filename)
var err error
var schemaRaw []byte
schemaRaw, err = ioutil.ReadFile(filename)
schemaRaw, err = os.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("unable to open schema: %w", err)
}
Expand Down Expand Up @@ -98,7 +97,7 @@ var path2regex = strings.NewReplacer(
func LoadConfig(filename string) (*Config, error) {
config := DefaultConfig()

b, err := ioutil.ReadFile(filename)
b, err := os.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("unable to read config: %w", err)
}
Expand Down Expand Up @@ -177,7 +176,7 @@ func CompleteConfig(config *Config) error {
filename = filepath.ToSlash(filename)
var err error
var schemaRaw []byte
schemaRaw, err = ioutil.ReadFile(filename)
schemaRaw, err = os.ReadFile(filename)
if err != nil {
return fmt.Errorf("unable to open schema: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions codegen/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package codegen
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
Expand Down Expand Up @@ -131,7 +131,7 @@ func generateRootFile(data *Data) error {
_, thisFile, _, _ := runtime.Caller(0)
rootDir := filepath.Dir(thisFile)
templatePath := filepath.Join(rootDir, "root_.gotpl")
templateBytes, err := ioutil.ReadFile(templatePath)
templateBytes, err := os.ReadFile(templatePath)
if err != nil {
return err
}
Expand Down
7 changes: 3 additions & 4 deletions codegen/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"go/types"
"io/ioutil"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -95,7 +94,7 @@ func Render(cfg Options) error {
if strings.HasSuffix(info.Name(), "_.gotpl") {
return nil
}
b, err := ioutil.ReadFile(path)
b, err := os.ReadFile(path)
if err != nil {
return err
}
Expand Down Expand Up @@ -576,7 +575,7 @@ func resolveName(name string, skip int) string {
func render(filename string, tpldata interface{}) (*bytes.Buffer, error) {
t := template.New("").Funcs(Funcs())

b, err := ioutil.ReadFile(filename)
b, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
Expand All @@ -602,7 +601,7 @@ func write(filename string, b []byte, packages *code.Packages) error {
formatted = b
}

err = ioutil.WriteFile(filename, formatted, 0o644)
err = os.WriteFile(filename, formatted, 0o644)
if err != nil {
return fmt.Errorf("failed to write %s: %w", filename, err)
}
Expand Down
Loading

0 comments on commit 53ada82

Please sign in to comment.