Compare commits
1 Commits
6120ab7dea
...
36698933d7
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
36698933d7 |
26
extension.go
26
extension.go
@@ -1,7 +1,15 @@
|
||||
// Code generated by gitoa.ru/go-4devs/mime and sourse ../mime.yaml
|
||||
package mime
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"encoding"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
var (
|
||||
_ encoding.TextUnmarshaler = (*Mime)(nil)
|
||||
_ encoding.TextMarshaler = (*Mime)(nil)
|
||||
)
|
||||
|
||||
type Ext int
|
||||
|
||||
@@ -17,8 +25,21 @@ func (v Ext) Is(types ...Ext) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (v Ext) MarshalText() ([]byte, error) {
|
||||
return []byte(v.String()), nil
|
||||
}
|
||||
|
||||
func (s *Ext) UnmarshalText(text []byte) error {
|
||||
ext := ExtFromString(string(text))
|
||||
*s = ext
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v Ext) String() string {
|
||||
switch v {
|
||||
case ExtUnrecognized:
|
||||
return "unrecognized"
|
||||
case ExtPdf:
|
||||
return "pdf"
|
||||
case ExtEz:
|
||||
@@ -6940,6 +6961,7 @@ func (v Ext) MimeTypes() MimeTypes {
|
||||
}
|
||||
|
||||
const (
|
||||
ExtUnrecognized Ext = 0
|
||||
ExtPdf Ext = 1
|
||||
ExtEz Ext = 2
|
||||
ExtAnx Ext = 3
|
||||
@@ -12127,5 +12149,5 @@ func ExtFromString(name string) Ext {
|
||||
return ExtIce
|
||||
}
|
||||
|
||||
return 0
|
||||
return ExtUnrecognized
|
||||
}
|
||||
|
||||
41
extension_test.go
Normal file
41
extension_test.go
Normal 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)
|
||||
}
|
||||
}
|
||||
8
go.mod
8
go.mod
@@ -1,3 +1,11 @@
|
||||
module gitoa.ru/go-4devs/mime
|
||||
|
||||
go 1.19
|
||||
|
||||
require github.com/stretchr/testify v1.8.0
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
15
go.sum
15
go.sum
@@ -0,0 +1,15 @@
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
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=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
||||
27
mime.go
27
mime.go
@@ -1,7 +1,16 @@
|
||||
// Code generated by gitoa.ru/go-4devs/mime and sourse ../mime.yaml
|
||||
package mime
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"encoding"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
_ encoding.TextUnmarshaler = (*Mime)(nil)
|
||||
_ encoding.TextMarshaler = (*Mime)(nil)
|
||||
)
|
||||
|
||||
type Mime int
|
||||
|
||||
@@ -17,8 +26,21 @@ func (v Mime) Is(types ...Mime) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (v Mime) MarshalText() ([]byte, error) {
|
||||
return []byte(v.String()), nil
|
||||
}
|
||||
|
||||
func (s *Mime) UnmarshalText(text []byte) error {
|
||||
mime := MimeFromString(strings.ToLower(string(text)))
|
||||
*s = mime
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v Mime) String() string {
|
||||
switch v {
|
||||
case Unrecognized:
|
||||
return "unrecognized"
|
||||
case ApplicationAcrobat:
|
||||
return "application/acrobat"
|
||||
case ApplicationAndrewDashInset:
|
||||
@@ -6688,6 +6710,7 @@ func (v Mime) ExtTypes() ExtTypes {
|
||||
}
|
||||
|
||||
const (
|
||||
Unrecognized Mime = 0
|
||||
ApplicationAcrobat Mime = 1
|
||||
ApplicationAndrewDashInset Mime = 2
|
||||
ApplicationAnnodex Mime = 3
|
||||
@@ -11686,5 +11709,5 @@ func MimeFromString(name string) Mime {
|
||||
return ZzDashApplicationZzDashWinassocDashXls
|
||||
}
|
||||
|
||||
return 0
|
||||
return Unrecognized
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package mime_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"gitoa.ru/go-4devs/mime"
|
||||
@@ -34,3 +35,38 @@ func ExampleMime_ExtTypes() {
|
||||
// [html htm shtml]
|
||||
// [js mjs jsm]
|
||||
}
|
||||
|
||||
func ExampleMime_jsonUnmarshal() {
|
||||
type Expect struct {
|
||||
Data string `json:"data"`
|
||||
Mime mime.Mime `json:"mime"`
|
||||
}
|
||||
|
||||
var res Expect
|
||||
|
||||
_ = json.Unmarshal([]byte(`{"data":"text","mime":"text/html"}`), &res)
|
||||
|
||||
fmt.Printf("%[1]v: %[1]T(%#[1]v)", res.Mime)
|
||||
|
||||
// Output:
|
||||
// text/html: mime.Mime(1406)
|
||||
}
|
||||
|
||||
func ExampleMime_jsonMarshal() {
|
||||
type Expect struct {
|
||||
Data string `json:"data"`
|
||||
Mime mime.Mime `json:"mime"`
|
||||
}
|
||||
|
||||
res := Expect{
|
||||
Data: "some text",
|
||||
Mime: mime.TextMarkdown,
|
||||
}
|
||||
|
||||
result, _ := json.Marshal(res)
|
||||
|
||||
fmt.Printf("%s", result)
|
||||
|
||||
// Output:
|
||||
// {"data":"some text","mime":"text/markdown"}
|
||||
}
|
||||
|
||||
45
mime_test.go
Normal file
45
mime_test.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package mime_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"gitoa.ru/go-4devs/mime"
|
||||
)
|
||||
|
||||
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
|
||||
}{
|
||||
{
|
||||
data: "text/html",
|
||||
expext: mime.TextHTML,
|
||||
},
|
||||
{
|
||||
data: "text/HTML",
|
||||
expext: mime.TextHTML,
|
||||
},
|
||||
{
|
||||
data: "any text",
|
||||
expext: mime.Unrecognized,
|
||||
},
|
||||
}
|
||||
|
||||
for _, cs := range cases {
|
||||
var in mime.Mime
|
||||
err := in.UnmarshalText([]byte(cs.data))
|
||||
require.Equal(t, cs.expext, in)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,15 @@
|
||||
// Code generated by gitoa.ru/go-4devs/mime and sourse {{.Source}}
|
||||
package {{.ExtPackage}}
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"encoding"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
var (
|
||||
_ encoding.TextUnmarshaler = (*Mime)(nil)
|
||||
_ encoding.TextMarshaler = (*Mime)(nil)
|
||||
)
|
||||
|
||||
type Ext int
|
||||
|
||||
@@ -17,8 +25,21 @@ func (v Ext) Is(types ...Ext) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (v Ext) MarshalText() ([]byte, error) {
|
||||
return []byte(v.String()), nil
|
||||
}
|
||||
|
||||
func (s *Ext) UnmarshalText(text []byte) error {
|
||||
ext := ExtFromString(string(text))
|
||||
*s = ext
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v Ext) String() string {
|
||||
switch v {
|
||||
case {{ name .ExtPrefix "unrecognized" }}:
|
||||
return "unrecognized"
|
||||
{{- range $value := .Extensions }}
|
||||
case {{ name $.ExtPrefix $value.Name }}:
|
||||
return "{{- $value.Name -}}"
|
||||
@@ -40,6 +61,7 @@ func (v Ext) MimeTypes() MimeTypes{
|
||||
}
|
||||
|
||||
const (
|
||||
{{ name .ExtPrefix "unrecognized" }} Ext = 0
|
||||
{{- range $value := .Extensions }}
|
||||
{{ name $.ExtPrefix $value.Name }} Ext = {{ $value.ID }}
|
||||
{{- end}}
|
||||
@@ -53,6 +75,6 @@ func ExtFromString(name string) Ext {
|
||||
{{- end}}
|
||||
}
|
||||
|
||||
return 0
|
||||
return {{ name .ExtPrefix "unrecognized" }}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,16 @@
|
||||
// Code generated by gitoa.ru/go-4devs/mime and sourse {{.Source}}
|
||||
package {{.MimePackage}}
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"encoding"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
_ encoding.TextUnmarshaler = (*Mime)(nil)
|
||||
_ encoding.TextMarshaler = (*Mime)(nil)
|
||||
)
|
||||
|
||||
type Mime int
|
||||
|
||||
@@ -17,8 +26,21 @@ func (v Mime) Is(types ...Mime) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (v Mime) MarshalText() ([]byte, error) {
|
||||
return []byte(v.String()), nil
|
||||
}
|
||||
|
||||
func (s *Mime) UnmarshalText(text []byte) error {
|
||||
mime := MimeFromString(strings.ToLower(string(text)))
|
||||
*s = mime
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v Mime) String() string {
|
||||
switch v {
|
||||
case {{ name .MimePrefix "unrecognized" }}:
|
||||
return "unrecognized"
|
||||
{{- range $value := .Mimes }}
|
||||
case {{ name $.MimePrefix $value.Name }}:
|
||||
return "{{- $value.Name -}}"
|
||||
@@ -40,6 +62,7 @@ func (v Mime) ExtTypes() ExtTypes{
|
||||
}
|
||||
|
||||
const (
|
||||
{{ name .MimePrefix "unrecognized" }} Mime = 0
|
||||
{{- range $value := .Mimes }}
|
||||
{{ name $.MimePrefix $value.Name }} Mime = {{ $value.ID }}
|
||||
{{- end}}
|
||||
@@ -53,5 +76,5 @@ func MimeFromString(name string) Mime {
|
||||
{{- end}}
|
||||
}
|
||||
|
||||
return 0
|
||||
return {{ name .MimePrefix "unrecognized" }}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user