add Marshal/Unmarshal text
This commit is contained in:
@@ -1,7 +1,16 @@
|
||||
// Code generated by gitoa.ru/go-4devs/mime and sourse {{.Source}}
|
||||
package {{.ExtPackage}}
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"encoding"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
_ encoding.TextUnmarshaler = (*Mime)(nil)
|
||||
_ encoding.TextMarshaler = (*Mime)(nil)
|
||||
)
|
||||
|
||||
type Ext int
|
||||
|
||||
@@ -17,8 +26,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(strings.ToLower(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 +62,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 +76,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