Skip to content

Commit

Permalink
refactor: use github.com/pelletier/go-toml
Browse files Browse the repository at this point in the history
  • Loading branch information
aofei committed Sep 15, 2020
1 parent 4f8e52d commit ff43997
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion air.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ import (
"sync"
"time"

"github.com/BurntSushi/toml"
"github.com/mitchellh/mapstructure"
"github.com/pelletier/go-toml"
"golang.org/x/crypto/acme"
"golang.org/x/crypto/acme/autocert"
"golang.org/x/net/http2"
Expand Down
4 changes: 2 additions & 2 deletions binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"reflect"
"strings"

"github.com/BurntSushi/toml"
"github.com/pelletier/go-toml"
"github.com/vmihailenco/msgpack/v5"
"google.golang.org/protobuf/proto"
"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -59,7 +59,7 @@ func (b *binder) bind(v interface{}, r *Request) error {
case "application/msgpack":
err = msgpack.NewDecoder(r.Body).Decode(v)
case "application/toml":
_, err = toml.DecodeReader(r.Body, v)
err = toml.NewDecoder(r.Body).Decode(v)
case "application/yaml":
err = yaml.NewDecoder(r.Body).Decode(v)
case "application/x-www-form-urlencoded", "multipart/form-data":
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ module github.com/aofei/air
go 1.13

require (
github.com/BurntSushi/toml v0.3.1
github.com/VictoriaMetrics/fastcache v1.5.7
github.com/aofei/mimesniffer v1.1.5
github.com/cespare/xxhash/v2 v2.1.1
github.com/fsnotify/fsnotify v1.4.9
github.com/golang/protobuf v1.4.2 // indirect
github.com/gorilla/websocket v1.4.2
github.com/mitchellh/mapstructure v1.3.3
github.com/pelletier/go-toml v1.8.1
github.com/stretchr/testify v1.6.1
github.com/tdewolff/minify/v2 v2.9.3
github.com/vmihailenco/msgpack/v5 v5.0.0-beta.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/matryer/try v0.0.0-20161228173917-9ac251b645a2/go.mod h1:0KeJpeMD6o+O4hW7qJOT7vyQPKrWmj26uf5wMc/IiIs=
github.com/mitchellh/mapstructure v1.3.3 h1:SzB1nHZ2Xi+17FP0zVQBHIZqvwRN9408fJO8h+eeNA8=
github.com/mitchellh/mapstructure v1.3.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/pelletier/go-toml v1.8.1 h1:1Nf83orprkJyknT6h7zbuEGUEjcyVlCxSUGTENmNCRM=
github.com/pelletier/go-toml v1.8.1/go.mod h1:T2/BmBdy8dvIRq1a/8aqjN41wvWlN4lrapLU/GW4pbc=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
Expand Down
8 changes: 6 additions & 2 deletions i18n.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"strings"
"sync"

"github.com/BurntSushi/toml"
"github.com/fsnotify/fsnotify"
"github.com/pelletier/go-toml"
"golang.org/x/text/language"
)

Expand Down Expand Up @@ -93,7 +93,11 @@ func (i *i18n) load() {

n := filepath.Join(lr, fi.Name())
l := map[string]string{}
if _, i.loadError = toml.DecodeFile(n, &l); i.loadError != nil {

var tt *toml.Tree
if tt, i.loadError = toml.LoadFile(n); i.loadError != nil {
return
} else if i.loadError = tt.Unmarshal(&l); i.loadError != nil {
return
} else if i.loadError = i.watcher.Add(n); i.loadError != nil {
return
Expand Down
14 changes: 7 additions & 7 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ import (
"sync"
"time"

"github.com/BurntSushi/toml"
"github.com/aofei/mimesniffer"
"github.com/cespare/xxhash/v2"
"github.com/gorilla/websocket"
"github.com/pelletier/go-toml"
"github.com/vmihailenco/msgpack/v5"
"golang.org/x/net/http/httpguts"
"golang.org/x/net/http2"
Expand Down Expand Up @@ -380,27 +380,27 @@ func (r *Response) WriteMsgpack(v interface{}) error {
// WriteTOML writes an "application/toml" content encoded from the v to the
// client.
func (r *Response) WriteTOML(v interface{}) error {
buf := bytes.Buffer{}
if err := toml.NewEncoder(&buf).Encode(v); err != nil {
b, err := toml.Marshal(v)
if err != nil {
return err
}

r.Header.Set("Content-Type", "application/toml; charset=utf-8")

return r.Write(bytes.NewReader(buf.Bytes()))
return r.Write(bytes.NewReader(b))
}

// WriteYAML writes an "application/yaml" content encoded from the v to the
// client.
func (r *Response) WriteYAML(v interface{}) error {
buf := bytes.Buffer{}
if err := yaml.NewEncoder(&buf).Encode(v); err != nil {
b, err := yaml.Marshal(v)
if err != nil {
return err
}

r.Header.Set("Content-Type", "application/yaml; charset=utf-8")

return r.Write(bytes.NewReader(buf.Bytes()))
return r.Write(bytes.NewReader(b))
}

// WriteFile writes a file content targeted by the filename to the client.
Expand Down

0 comments on commit ff43997

Please sign in to comment.