Browse Source

update ext/mime types

pull/1/head
andrey1s 2 years ago
parent
commit
d09969eb02
  1. 17050
      extension.go
  2. 13351
      mime.go
  3. 1666
      mime.json
  4. 3394
      mime.yaml
  5. 2
      scripts/command/mime.go
  6. 1
      scripts/go.mod
  7. 3
      scripts/go.sum
  8. 59
      scripts/mime/generate.go
  9. 53
      scripts/mime/tpl/extension.text.tmpl
  10. 52
      scripts/mime/tpl/mime.text.tmpl
  11. 11
      scripts/mime/varmane.go

17050
extension.go

File diff suppressed because it is too large

13351
mime.go

File diff suppressed because it is too large

1666
mime.json

File diff suppressed because it is too large

3394
mime.yaml

File diff suppressed because it is too large

2
scripts/command/mime.go

@ -27,7 +27,7 @@ func Mime() *console.Command {
Name: "mime",
Description: "generate mime from file",
Configure: func(ctx context.Context, cfg *input.Definition) error {
cfg.SetArgument(ArgFile, "file", argument.Required)
cfg.SetArgument(ArgFile, "yaml file", argument.Required)
cfg.SetOptions(
option.String(OptExtTpl, "extension template", option.Default("mime/tpl/extension.text.tmpl")),
option.String(OptExtPackage, "extension package", option.Default("mime")),

1
scripts/go.mod

@ -6,4 +6,5 @@ require (
github.com/achiku/varfmt v0.0.0-20160708124000-f820e1efecee
gitoa.ru/go-4devs/closer v0.1.1
gitoa.ru/go-4devs/console v0.1.2
gopkg.in/yaml.v3 v3.0.1
)

3
scripts/go.sum

@ -3,8 +3,10 @@ github.com/achiku/varfmt v0.0.0-20160708124000-f820e1efecee/go.mod h1:RKS7P4TSY/
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/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
@ -18,6 +20,7 @@ gitoa.ru/go-4devs/closer v0.1.1/go.mod h1:S+QAdgSt4CVLH3v3YZK1Mukl7SVn2Z0CYj0oJQ
gitoa.ru/go-4devs/console v0.1.2 h1:SsQWLSClXFwWFseH6CGKQfmCtG84aHOiaFHG3oZlJ8s=
gitoa.ru/go-4devs/console v0.1.2/go.mod h1:ddqmjQ0yr9v+oa5E3Bu3X/SUcws/ENR5f5cz1g5fHbk=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/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=

59
scripts/mime/generate.go

@ -1,31 +1,74 @@
package mime
import (
"encoding/json"
"fmt"
"go/format"
"io/ioutil"
"os"
"path"
"text/template"
"gopkg.in/yaml.v3"
)
type Config struct {
Source string
Mimes map[string][]string
Mimes []Data `yaml:"mime"`
MimePrefix string
MimeTpl string
MimeResult string
MimePackage string
Extensions map[string][]string
Extensions []Data `yaml:"ext"`
ExtPrefix string
ExtTpl string
ExtResult string
ExtPackage string
}
type Data struct {
Name string `yaml:"name"`
ID int `yaml:"id"`
Value []string `yaml:"value,omitempty"`
}
func (c *Config) ValidateAndFillExt() error {
mimes := make(map[string]int)
mimeIDs := make(map[int]string)
extIndex := make(map[string]int)
for idx, ext := range c.Extensions {
if _, ok := extIndex[ext.Name]; ok {
return fmt.Errorf("extension %v duplicate: with id %v", ext.Name, c.Extensions[extIndex[ext.Name]].ID)
}
extIndex[ext.Name] = idx
}
for _, mime := range c.Mimes {
if _, ok := mimes[mime.Name]; ok {
return fmt.Errorf("mime %v duplicate: with id %v", mime.Name, mimes[mime.Name])
}
if _, ok := mimeIDs[mime.ID]; ok {
return fmt.Errorf("ID %v duplicate: with name %v", mime.ID, mimeIDs[mime.ID])
}
for _, ext := range mime.Value {
idx, ok := extIndex[ext]
if !ok {
return fmt.Errorf("not fount ext by %v", mime.Name)
}
c.Extensions[idx].Value = append(c.Extensions[idx].Value, mime.Name)
}
}
return nil
}
func WithExtTpl(name string) Option {
return func(c *Config) {
c.ExtTpl = name
@ -78,11 +121,9 @@ func Generate(fileName string, opts ...Option) error {
ExtPrefix: "Ext",
ExtTpl: "mime/tpl/extension.text.tmpl",
ExtResult: "extension.go",
Extensions: make(map[string][]string),
ExtPackage: "mime",
MimeTpl: "mime/tpl/mime.text.tmpl",
Mimes: make(map[string][]string),
MimePrefix: "",
MimeResult: "mime.go",
MimePackage: "mime",
@ -97,11 +138,13 @@ func Generate(fileName string, opts ...Option) error {
return fmt.Errorf("read file:%w", err)
}
if err := json.Unmarshal(data, &cfg.Mimes); err != nil {
if err := yaml.Unmarshal(data, &cfg); err != nil {
return fmt.Errorf("unmarshal:%w", err)
}
cfg.Extensions = extensions(cfg.Mimes)
if err := cfg.ValidateAndFillExt(); err != nil {
return fmt.Errorf("config:%w", err)
}
template, err := template.New("mimes").Funcs(funcMap()).ParseFiles(cfg.ExtTpl, cfg.MimeTpl)
if err != nil {
@ -169,7 +212,7 @@ func extensions(mimes map[string][]string) map[string][]string {
out[ext] = append(out[ext], mime)
continue
}
out[ext] = []string{ext}
out[ext] = []string{mime}
}
}

53
scripts/mime/tpl/extension.text.tmpl

@ -1,19 +1,58 @@
// Code generated by gitoa.ru/go-4devs/mime and sourse {{.Source}}
package {{.ExtPackage}}
import "fmt"
type Ext int
type ExtTypes []Ext
func (v Ext) Is(types ...Ext) bool {
for _, ext := range types {
if ext == v {
return true
}
}
return false
}
func (v Ext) String() string {
switch v {
{{- range $value := .Extensions }}
case {{ name $.ExtPrefix $value.Name }}:
return "{{- $value.Name -}}"
{{- end}}
}
return fmt.Sprintf("Ext(%d)",v)
}
func (v Ext) MimeTypes() MimeTypes{
switch v {
{{- range $value := .Extensions }}
case {{ name $.ExtPrefix $value.Name }}:
return MimeTypes{ {{- value $.MimePrefix $value.Value -}} }
{{- end}}
}
return nil
}
const (
{{- range $key, $value := .Extensions }}
{{ name $.ExtPrefix $key }} = "{{ $key }}"
{{- range $value := .Extensions }}
{{ name $.ExtPrefix $value.Name }} Ext = {{ $value.ID }}
{{- end}}
)
func Mime(name string) []string {
func ExtFromString(name string) Ext {
switch name {
{{- range $key, $value := .Extensions }}
case {{ name $.ExtPrefix $key }}:
return {{ value $value }}
{{- range $value := .Extensions }}
case "{{- $value.Name -}}":
return {{ name $.ExtPrefix $value.Name }}
{{- end}}
}
return nil
return 0
}

52
scripts/mime/tpl/mime.text.tmpl

@ -1,19 +1,57 @@
// Code generated by gitoa.ru/go-4devs/mime and sourse {{.Source}}
package {{.MimePackage}}
import "fmt"
type Mime int
type MimeTypes []Mime
func (v Mime) Is(types ...Mime) bool {
for _, mime := range types {
if mime == v {
return true
}
}
return false
}
func (v Mime) String() string {
switch v {
{{- range $value := .Mimes }}
case {{ name $.MimePrefix $value.Name }}:
return "{{- $value.Name -}}"
{{- end}}
}
return fmt.Sprintf("Mime(%d)",v)
}
func (v Mime) ExtTypes() ExtTypes{
switch v {
{{- range $value := .Mimes }}
case {{ name $.MimePrefix $value.Name }}:
return []Ext{ {{- value $.ExtPrefix $value.Value -}} }
{{- end}}
}
return nil
}
const (
{{- range $key, $value := .Mimes }}
{{ name $.MimePrefix $key }} = "{{ $key }}"
{{- range $value := .Mimes }}
{{ name $.MimePrefix $value.Name }} Mime = {{ $value.ID }}
{{- end}}
)
func Extension(name string) []string {
func MimeFromString(name string) Mime {
switch name {
{{- range $key, $value := .Mimes }}
case {{ name $.MimePrefix $key }}:
return {{ value $value }}
{{- range $value := .Mimes }}
case "{{- $value.Name -}}":
return {{ name $.MimePrefix $value.Name }}
{{- end}}
}
return nil
return 0
}

11
scripts/mime/varmane.go

@ -1,7 +1,7 @@
package mime
import (
"fmt"
"bytes"
"strings"
"unicode"
@ -27,6 +27,11 @@ func VarName(prefix, name string) string {
return varfmt.PublicVarName(prefix + "_" + name)
}
func Value(val []string) string {
return fmt.Sprintf("%#v", val)
func Value(prefix string, val []string) string {
var out bytes.Buffer
for _, name := range val {
out.WriteString(VarName(prefix, name) + ",")
}
return out.String()
}

Loading…
Cancel
Save