From cfb3ca96c4316b5798d4babcea889161ad2266b1 Mon Sep 17 00:00:00 2001 From: Martin Tournoij Date: Thu, 8 Jun 2023 13:24:59 +0200 Subject: [PATCH] Run Go 1.16 in CI The only reason I retained backwards-compatibility is because Debian 11 is still on Go 1.15, but Debian 12 should be released this week with Go 1.19 so we can fairly safely drop it. --- .github/workflows/test.yml | 2 +- bench_test.go | 6 +----- decode.go | 15 +++++++++++++-- decode_go116.go | 19 ------------------- decode_go116_test.go | 29 ----------------------------- decode_test.go | 23 +++++++++++++++++++++-- error_test.go | 3 --- internal/toml-test/json.go | 3 --- internal/toml-test/runner.go | 3 --- internal/toml-test/toml.go | 3 --- internal/toml-test/version.go | 3 --- toml_test.go | 3 --- 12 files changed, 36 insertions(+), 76 deletions(-) delete mode 100644 decode_go116.go delete mode 100644 decode_go116_test.go diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ac0a3512..9b95081d 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -5,7 +5,7 @@ "test": { "strategy": { "matrix": { - "go-version": ["1.13.x", "1.20.x"], + "go-version": ["1.16.x", "1.20.x"], "os": ["ubuntu-latest", "macos-latest", "windows-latest"] } }, diff --git a/bench_test.go b/bench_test.go index 198ce5cc..76942593 100644 --- a/bench_test.go +++ b/bench_test.go @@ -1,12 +1,8 @@ -//go:build go1.16 -// +build go1.16 - package toml_test import ( "bytes" "io/fs" - "io/ioutil" "os" "path/filepath" "sort" @@ -132,7 +128,7 @@ func BenchmarkEncode(b *testing.B) { } func BenchmarkExample(b *testing.B) { - d, err := ioutil.ReadFile("_example/example.toml") + d, err := os.ReadFile("_example/example.toml") if err != nil { b.Fatal(err) } diff --git a/decode.go b/decode.go index 4d38f3bf..455a1424 100644 --- a/decode.go +++ b/decode.go @@ -6,7 +6,7 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" + "io/fs" "math" "os" "reflect" @@ -46,6 +46,17 @@ func DecodeFile(path string, v interface{}) (MetaData, error) { return NewDecoder(fp).Decode(v) } +// DecodeFS reads the contents of a file from [fs.FS] and decodes it with +// [Decode]. +func DecodeFS(fsys fs.FS, path string, v interface{}) (MetaData, error) { + fp, err := fsys.Open(path) + if err != nil { + return MetaData{}, err + } + defer fp.Close() + return NewDecoder(fp).Decode(v) +} + // Primitive is a TOML value that hasn't been decoded into a Go value. // // This type can be used for any value, which will cause decoding to be delayed. @@ -148,7 +159,7 @@ func (dec *Decoder) Decode(v interface{}) (MetaData, error) { // TODO: parser should read from io.Reader? Or at the very least, make it // read from []byte rather than string - data, err := ioutil.ReadAll(dec.r) + data, err := io.ReadAll(dec.r) if err != nil { return MetaData{}, err } diff --git a/decode_go116.go b/decode_go116.go deleted file mode 100644 index 086d0b68..00000000 --- a/decode_go116.go +++ /dev/null @@ -1,19 +0,0 @@ -//go:build go1.16 -// +build go1.16 - -package toml - -import ( - "io/fs" -) - -// DecodeFS reads the contents of a file from [fs.FS] and decodes it with -// [Decode]. -func DecodeFS(fsys fs.FS, path string, v interface{}) (MetaData, error) { - fp, err := fsys.Open(path) - if err != nil { - return MetaData{}, err - } - defer fp.Close() - return NewDecoder(fp).Decode(v) -} diff --git a/decode_go116_test.go b/decode_go116_test.go deleted file mode 100644 index 83722f2b..00000000 --- a/decode_go116_test.go +++ /dev/null @@ -1,29 +0,0 @@ -//go:build go1.16 -// +build go1.16 - -package toml - -import ( - "fmt" - "testing" - "testing/fstest" -) - -func TestDecodeFS(t *testing.T) { - fsys := fstest.MapFS{ - "test.toml": &fstest.MapFile{ - Data: []byte("a = 42"), - }, - } - - var i struct{ A int } - meta, err := DecodeFS(fsys, "test.toml", &i) - if err != nil { - t.Fatal(err) - } - have := fmt.Sprintf("%v %v %v", i, meta.Keys(), meta.Type("a")) - want := "{42} [a] Integer" - if have != want { - t.Errorf("\nhave: %s\nwant: %s", have, want) - } -} diff --git a/decode_test.go b/decode_test.go index 6f08d3ae..e9041080 100644 --- a/decode_test.go +++ b/decode_test.go @@ -5,7 +5,6 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" "math" "os" "reflect" @@ -13,6 +12,7 @@ import ( "strings" "sync" "testing" + "testing/fstest" "time" "github.com/BurntSushi/toml/internal" @@ -38,7 +38,7 @@ func TestDecodeReader(t *testing.T) { } func TestDecodeFile(t *testing.T) { - tmp, err := ioutil.TempFile("", "toml-") + tmp, err := os.CreateTemp("", "toml-") if err != nil { t.Fatal(err) } @@ -63,6 +63,25 @@ func TestDecodeFile(t *testing.T) { } } +func TestDecodeFS(t *testing.T) { + fsys := fstest.MapFS{ + "test.toml": &fstest.MapFile{ + Data: []byte("a = 42"), + }, + } + + var i struct{ A int } + meta, err := DecodeFS(fsys, "test.toml", &i) + if err != nil { + t.Fatal(err) + } + have := fmt.Sprintf("%v %v %v", i, meta.Keys(), meta.Type("a")) + want := "{42} [a] Integer" + if have != want { + t.Errorf("\nhave: %s\nwant: %s", have, want) + } +} + func TestDecodeBOM(t *testing.T) { for _, tt := range [][]byte{ []byte("\xff\xfea = \"b\""), diff --git a/error_test.go b/error_test.go index cd95263c..907f34ba 100644 --- a/error_test.go +++ b/error_test.go @@ -1,6 +1,3 @@ -//go:build go1.16 -// +build go1.16 - package toml_test import ( diff --git a/internal/toml-test/json.go b/internal/toml-test/json.go index 4394f419..a5fa2830 100644 --- a/internal/toml-test/json.go +++ b/internal/toml-test/json.go @@ -1,6 +1,3 @@ -//go:build go1.16 -// +build go1.16 - package tomltest import ( diff --git a/internal/toml-test/runner.go b/internal/toml-test/runner.go index 2d7e9315..6bec6f2b 100644 --- a/internal/toml-test/runner.go +++ b/internal/toml-test/runner.go @@ -1,8 +1,5 @@ //go:generate ./gen-multi.py -//go:build go1.16 -// +build go1.16 - package tomltest import ( diff --git a/internal/toml-test/toml.go b/internal/toml-test/toml.go index 934e6314..346f0a96 100644 --- a/internal/toml-test/toml.go +++ b/internal/toml-test/toml.go @@ -1,6 +1,3 @@ -//go:build go1.16 -// +build go1.16 - package tomltest import ( diff --git a/internal/toml-test/version.go b/internal/toml-test/version.go index 54454e2c..87c5fda6 100644 --- a/internal/toml-test/version.go +++ b/internal/toml-test/version.go @@ -1,6 +1,3 @@ -//go:build go1.16 -// +build go1.16 - package tomltest type versionSpec struct { diff --git a/toml_test.go b/toml_test.go index fe2db9f6..ad1043f1 100644 --- a/toml_test.go +++ b/toml_test.go @@ -1,6 +1,3 @@ -//go:build go1.16 -// +build go1.16 - package toml_test import (