Skip to content

Commit

Permalink
feat(encoding): support toml (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
abemedia authored May 10, 2023
1 parent d88115c commit e95b4ae
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,13 @@ MIME: `application/x-msgpack`
Parses MessagePack requests & encodes responses as MessagePack. Use the `msgpack` tag in your
request & response structs.

#### TOML

MIME: `application/toml`

Parses TOML requests & encodes responses as TOML. Use the `toml` tag in your request & response
structs.

### Adding custom encoding

Adding your own is easy. See [encoding/json/json.go](./encoding/json/json.go).
Expand Down
13 changes: 13 additions & 0 deletions encoding/toml/toml.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package toml

import (
"github.com/abemedia/go-don/encoding"
"github.com/pelletier/go-toml"
)

func init() {
mediaType := "application/toml"

encoding.RegisterDecoder(toml.Unmarshal, mediaType)
encoding.RegisterEncoder(toml.Marshal, mediaType)
}
25 changes: 25 additions & 0 deletions encoding/toml/toml_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package toml_test

import (
"testing"

"github.com/abemedia/go-don/internal/test"
)

type item struct {
Foo string `toml:"foo"`
}

var opt = test.EncodingOptions[item]{
Mime: "application/toml",
Raw: `foo = "bar"` + "\n",
Parsed: item{Foo: "bar"},
}

func TestTOML(t *testing.T) {
test.Encoding(t, opt)
}

func BenchmarkTOML(b *testing.B) {
test.BenchmarkEncoding(b, opt)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/abemedia/httprouter v0.0.0-20230505023925-232e0e5a4b1b
github.com/goccy/go-json v0.10.2
github.com/google/go-cmp v0.5.9
github.com/pelletier/go-toml v1.9.5
github.com/valyala/fasthttp v1.47.0
github.com/vmihailenco/msgpack/v5 v5.3.5
gopkg.in/yaml.v3 v3.0.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ github.com/klauspost/compress v1.15.0/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47e
github.com/klauspost/compress v1.16.3/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/klauspost/compress v1.16.5 h1:IFV2oUNUzZaz+XyusxpLzpzS8Pt5rh0Z16For/djlyI=
github.com/klauspost/compress v1.16.5/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
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/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down

0 comments on commit e95b4ae

Please sign in to comment.