You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
1.1 KiB
64 lines
1.1 KiB
10 months ago
|
package generate
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"io"
|
||
|
"sync"
|
||
|
|
||
|
"gitoa.ru/go-4devs/config/definition"
|
||
|
)
|
||
|
|
||
|
//nolint:gochecknoglobals
|
||
|
var handlers = sync.Map{}
|
||
|
|
||
|
func Add(kind string, h Handle) error {
|
||
|
_, ok := handlers.Load(kind)
|
||
|
if ok {
|
||
|
return fmt.Errorf("kind %v: %w", kind, ErrAlreadyExist)
|
||
|
}
|
||
|
|
||
|
handlers.Store(kind, h)
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
//nolint:forcetypeassert
|
||
|
func get(kind string) Handle {
|
||
|
handler, ok := handlers.Load(kind)
|
||
|
if !ok {
|
||
|
return func(w io.Writer, h Handler, o definition.Option) error {
|
||
|
return fmt.Errorf("handler by %v:%w", kind, ErrNotFound)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return handler.(Handle)
|
||
|
}
|
||
|
|
||
|
func MustAdd(kind string, h Handle) {
|
||
|
if err := Add(kind, h); err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
type Handle func(io.Writer, Handler, definition.Option) error
|
||
|
|
||
|
type Handler interface {
|
||
|
StructName() string
|
||
|
Handle(w io.Writer, handler Handler, opt definition.Option) error
|
||
|
Options() ViewOption
|
||
|
Keys() []string
|
||
|
AddType(fullName string) (string, error)
|
||
|
DefaultErrors() []string
|
||
|
}
|
||
|
|
||
|
type ViewOption struct {
|
||
|
Prefix, Suffix string
|
||
|
Context bool
|
||
|
Struct string
|
||
|
Errors ViewErrors
|
||
|
}
|
||
|
|
||
|
type ViewErrors struct {
|
||
|
Default []string
|
||
|
}
|