Compare commits

1 Commits

Author SHA1 Message Date
99c54bfc0c add Marshal/Unmarshal text (#2)
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
Co-authored-by: andrey1s <andrey@4devs.pro>
Reviewed-on: #2
Co-authored-by: andrey <andrey@4devs.io>
Co-committed-by: andrey <andrey@4devs.io>
2022-09-30 11:20:06 +03:00
5 changed files with 51 additions and 7 deletions

View File

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

41
extension_test.go Normal file
View File

@@ -0,0 +1,41 @@
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 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) fmt.Printf("%[1]v: %[1]T(%#[1]v)", res.Mime)
@@ -63,10 +63,11 @@ func ExampleMime_jsonMarshal() {
Mime: mime.TextMarkdown, Mime: mime.TextMarkdown,
} }
result, _ := json.Marshal(res) result, err := json.Marshal(res)
fmt.Printf("%s", result) fmt.Printf("%s\n%v", result, err)
// Output: // Output:
// {"data":"some text","mime":"text/markdown"} // {"data":"some text","mime":"text/markdown"}
// <nil>
} }

View File

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

View File

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