add Marshal/Unmarshal text
Some checks failed
continuous-integration/drone/push Build is failing
continuous-integration/drone/pr Build is failing

This commit is contained in:
andrey1s
2022-09-30 11:01:29 +03:00
parent 8ff25d212d
commit 36698933d7
9 changed files with 243 additions and 8 deletions

View File

@@ -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" }}
}

View File

@@ -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" }}
}