Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions encoding/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
)

func init() {
encoding.RegisterDecoder(json.Unmarshal, "application/json")
encoding.RegisterEncoder(json.Marshal, "application/json")
mediaType := "application/json"

encoding.RegisterDecoder(json.Unmarshal, mediaType)
encoding.RegisterEncoder(json.Marshal, mediaType)
}
7 changes: 5 additions & 2 deletions encoding/msgpack/msgpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import (
)

func init() {
encoding.RegisterDecoder(msgpack.Unmarshal, "application/x-msgpack")
encoding.RegisterEncoder(msgpack.Marshal, "application/x-msgpack")
mediaType := "application/msgpack"
aliases := []string{"application/x-msgpack", "application/vnd.msgpack"}

encoding.RegisterDecoder(msgpack.Unmarshal, mediaType, aliases...)
encoding.RegisterEncoder(msgpack.Marshal, mediaType, aliases...)
}
11 changes: 3 additions & 8 deletions encoding/text/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ package text

import (
"bytes"
stdencoding "encoding"
"encoding"
"reflect"
"strconv"
"sync"

"github.com/abemedia/go-don"
"github.com/abemedia/go-don/encoding"
"github.com/abemedia/go-don/internal/byteconv"
"github.com/valyala/fasthttp"
)
Expand Down Expand Up @@ -103,7 +102,7 @@ func newUnmarshaler(typ reflect.Type) (func([]byte, reflect.Value) error, error)
if isPtr && v.IsNil() {
v.Set(reflect.New(typ))
}
return v.Interface().(stdencoding.TextUnmarshaler).UnmarshalText(b) //nolint:forcetypeassert
return v.Interface().(encoding.TextUnmarshaler).UnmarshalText(b) //nolint:forcetypeassert
}, nil
}

Expand All @@ -127,9 +126,5 @@ func newUnmarshaler(typ reflect.Type) (func([]byte, reflect.Value) error, error)
//nolint:gochecknoglobals
var (
unmarshalers sync.Map
unmarshalerType = reflect.TypeOf((*stdencoding.TextUnmarshaler)(nil)).Elem()
unmarshalerType = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()
)

func init() {
encoding.RegisterDecoder(decode, "text/plain")
}
9 changes: 2 additions & 7 deletions encoding/text/encode.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package text

import (
stdencoding "encoding"
"encoding"
"fmt"
"strconv"

"github.com/abemedia/go-don"
"github.com/abemedia/go-don/encoding"
"github.com/abemedia/go-don/internal/byteconv"
"github.com/valyala/fasthttp"
)
Expand Down Expand Up @@ -50,7 +49,7 @@ func encode(ctx *fasthttp.RequestCtx, v any) error {
b = strconv.AppendFloat(ctx.Response.Body(), v, 'f', -1, 64)
case bool:
b = strconv.AppendBool(ctx.Response.Body(), v)
case stdencoding.TextMarshaler:
case encoding.TextMarshaler:
b, err = v.MarshalText()
case error:
b = byteconv.Atob(v.Error())
Expand All @@ -67,7 +66,3 @@ func encode(ctx *fasthttp.RequestCtx, v any) error {

return err
}

func init() {
encoding.RegisterEncoder(encode, "text/plain")
}
12 changes: 12 additions & 0 deletions encoding/text/text.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package text

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

func init() {
mediaType := "text/plain"

encoding.RegisterDecoder(decode, mediaType)
encoding.RegisterEncoder(encode, mediaType)
}
7 changes: 5 additions & 2 deletions encoding/xml/xml.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ func encodeXML(ctx *fasthttp.RequestCtx, v any) error {
}

func init() {
encoding.RegisterDecoder(decodeXML, "application/xml", "text/xml")
encoding.RegisterEncoder(encodeXML, "application/xml", "text/xml")
mediaType := "application/xml"
alias := "text/xml"

encoding.RegisterDecoder(decodeXML, mediaType, alias)
encoding.RegisterEncoder(encodeXML, mediaType, alias)
}
7 changes: 5 additions & 2 deletions encoding/yaml/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ func encodeYAML(ctx *fasthttp.RequestCtx, v any) error {
}

func init() {
encoding.RegisterEncoder(encodeYAML, "application/x-yaml", "text/x-yaml")
encoding.RegisterDecoder(decodeYAML, "application/x-yaml", "text/x-yaml")
mediaType := "application/yaml"
aliases := []string{"text/yaml", "application/x-yaml", "text/x-yaml", "text/vnd.yaml"}

encoding.RegisterDecoder(decodeYAML, mediaType, aliases...)
encoding.RegisterEncoder(encodeYAML, mediaType, aliases...)
}
2 changes: 1 addition & 1 deletion encoding/yaml/yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type item struct {
}

var opt = test.EncodingOptions[item]{
Mime: "application/x-yaml",
Mime: "application/yaml",
Raw: "foo: bar\n",
Parsed: item{Foo: "bar"},
}
Expand Down