Compare commits

..

1 Commits

Author SHA1 Message Date
andrey1s
6120ab7dea add Marshal/Unmarshal text
Some checks failed
continuous-integration/drone/push Build is failing
continuous-integration/drone/pr Build is failing
2022-09-30 11:01:51 +03:00
5 changed files with 5 additions and 48 deletions

View File

@@ -4,6 +4,7 @@ package mime
import (
"encoding"
"fmt"
"strings"
)
var (
@@ -30,7 +31,7 @@ func (v Ext) MarshalText() ([]byte, error) {
}
func (s *Ext) UnmarshalText(text []byte) error {
ext := ExtFromString(string(text))
ext := ExtFromString(strings.ToLower(string(text)))
*s = ext
return nil

View File

@@ -1,41 +0,0 @@
package mime_test
import (
"testing"
"github.com/stretchr/testify/require"
"gitoa.ru/go-4devs/mime"
)
func TestExtUnmarshalText(t *testing.T) {
t.Parallel()
cases := []struct {
data string
expext mime.Ext
}{
{
data: "txt",
expext: mime.ExtTxt,
},
{
data: "TXT",
expext: mime.ExtUnrecognized,
},
{
data: "C",
expext: mime.Ext_C,
},
{
data: "c",
expext: mime.ExtC,
},
}
for _, cs := range cases {
var in mime.Ext
err := in.UnmarshalText([]byte(cs.data))
require.Equal(t, cs.expext, in)
require.NoError(t, err)
}
}

View File

@@ -44,7 +44,7 @@ func ExampleMime_jsonUnmarshal() {
var res Expect
_ = json.Unmarshal([]byte(`{"data":"text","mime":"text/html"}`), &res)
json.Unmarshal([]byte(`{"data":"text","mime":"text/html"}`), &res)
fmt.Printf("%[1]v: %[1]T(%#[1]v)", res.Mime)

View File

@@ -8,16 +8,12 @@ import (
)
func TestMimeMarshalText(t *testing.T) {
t.Parallel()
res, err := mime.ApplicationJavascript.MarshalText()
require.Equal(t, "application/javascript", string(res))
require.NoError(t, err)
}
func TestMimeUnmarshalText(t *testing.T) {
t.Parallel()
cases := []struct {
data string
expext mime.Mime

View File

@@ -4,6 +4,7 @@ package {{.ExtPackage}}
import (
"encoding"
"fmt"
"strings"
)
var (
@@ -30,7 +31,7 @@ func (v Ext) MarshalText() ([]byte, error) {
}
func (s *Ext) UnmarshalText(text []byte) error {
ext := ExtFromString(string(text))
ext := ExtFromString(strings.ToLower(string(text)))
*s = ext
return nil