Compare commits
1 Commits
v0.0.1
...
6120ab7dea
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6120ab7dea |
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
@@ -63,11 +63,10 @@ func ExampleMime_jsonMarshal() {
|
||||
Mime: mime.TextMarkdown,
|
||||
}
|
||||
|
||||
result, err := json.Marshal(res)
|
||||
result, _ := json.Marshal(res)
|
||||
|
||||
fmt.Printf("%s\n%v", result, err)
|
||||
fmt.Printf("%s", result)
|
||||
|
||||
// Output:
|
||||
// {"data":"some text","mime":"text/markdown"}
|
||||
// <nil>
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user