Reviewed-on: #10 Co-authored-by: andrey <andrey@4devs.io> Co-committed-by: andrey <andrey@4devs.io>
This commit was merged in pull request #10.
This commit is contained in:
70
definition/generate/generator.go
Normal file
70
definition/generate/generator.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package generate
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"gitoa.ru/go-4devs/config/definition"
|
||||
)
|
||||
|
||||
type Generator struct {
|
||||
pkg string
|
||||
ViewOption
|
||||
Imp Imports
|
||||
errs []error
|
||||
defaultErrors []string
|
||||
}
|
||||
|
||||
func (g Generator) Pkg() string {
|
||||
return g.pkg
|
||||
}
|
||||
|
||||
func (g Generator) Imports() []Import {
|
||||
return g.Imp.Imports()
|
||||
}
|
||||
|
||||
func (g Generator) Handle(w io.Writer, data Handler, opt definition.Option) error {
|
||||
handle := get(opt.Kind())
|
||||
|
||||
return handle(w, data, opt)
|
||||
}
|
||||
|
||||
func (g Generator) StructName() string {
|
||||
return FuncName(g.Prefix + "_" + g.Struct + "_" + g.Suffix)
|
||||
}
|
||||
|
||||
func (g Generator) Options() ViewOption {
|
||||
return g.ViewOption
|
||||
}
|
||||
|
||||
func (g Generator) Keys() []string {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g Generator) DefaultErrors() []string {
|
||||
if len(g.defaultErrors) > 0 {
|
||||
return g.defaultErrors
|
||||
}
|
||||
|
||||
if len(g.ViewOption.Errors.Default) > 0 {
|
||||
g.Imp.Adds("errors")
|
||||
}
|
||||
|
||||
g.defaultErrors = make([]string, len(g.ViewOption.Errors.Default))
|
||||
for idx, name := range g.ViewOption.Errors.Default {
|
||||
short, err := g.AddType(name)
|
||||
if err != nil {
|
||||
g.errs = append(g.errs, fmt.Errorf("add default error[%d]:%w", idx, err))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
g.defaultErrors[idx] = short
|
||||
}
|
||||
|
||||
return g.defaultErrors
|
||||
}
|
||||
|
||||
func (g *Generator) AddType(pkg string) (string, error) {
|
||||
return g.Imp.AddType(pkg)
|
||||
}
|
||||
18
definition/generate/helpers.go
Normal file
18
definition/generate/helpers.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package generate
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/iancoleman/strcase"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNotFound = errors.New("not found")
|
||||
ErrAlreadyExist = errors.New("already exist")
|
||||
ErrWrongType = errors.New("wrong type")
|
||||
ErrWrongFormat = errors.New("wrong format")
|
||||
)
|
||||
|
||||
func FuncName(in string) string {
|
||||
return strcase.ToCamel(in)
|
||||
}
|
||||
89
definition/generate/imports.go
Normal file
89
definition/generate/imports.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package generate
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func NewImports() Imports {
|
||||
return Imports{
|
||||
data: make(map[string]string),
|
||||
}
|
||||
}
|
||||
|
||||
type Imports struct {
|
||||
data map[string]string
|
||||
}
|
||||
|
||||
func (i Imports) Imports() []Import {
|
||||
imports := make([]Import, 0, len(i.data))
|
||||
for name, alias := range i.data {
|
||||
imports = append(imports, Import{
|
||||
Package: name,
|
||||
Alias: alias,
|
||||
})
|
||||
}
|
||||
|
||||
return imports
|
||||
}
|
||||
|
||||
func (i *Imports) Short(fullType string) (string, error) {
|
||||
idx := strings.LastIndexByte(fullType, '.')
|
||||
if idx == -1 {
|
||||
return "", fmt.Errorf("%w: expect package.Type", ErrWrongFormat)
|
||||
}
|
||||
|
||||
if alias, ok := i.data[fullType[:idx]]; ok {
|
||||
return alias + fullType[idx:], nil
|
||||
}
|
||||
|
||||
return "", fmt.Errorf("%w alias for pkg %v", ErrNotFound, fullType[:idx])
|
||||
}
|
||||
|
||||
func (i *Imports) AddType(fullType string) (string, error) {
|
||||
idx := strings.LastIndexByte(fullType, '.')
|
||||
if idx == -1 {
|
||||
return "", fmt.Errorf("%w: expect pckage.Type", ErrWrongFormat)
|
||||
}
|
||||
|
||||
imp := i.Add(fullType[:idx])
|
||||
|
||||
return imp.Alias + fullType[idx:], nil
|
||||
}
|
||||
|
||||
func (i *Imports) Adds(pkgs ...string) {
|
||||
for _, pkg := range pkgs {
|
||||
i.Add(pkg)
|
||||
}
|
||||
}
|
||||
|
||||
func (i *Imports) Add(pkg string) Import {
|
||||
alias := pkg
|
||||
|
||||
if idx := strings.LastIndexByte(pkg, '/'); idx != -1 {
|
||||
alias = pkg[idx+1:]
|
||||
}
|
||||
|
||||
if al, ok := i.data[pkg]; ok {
|
||||
return Import{Package: pkg, Alias: al}
|
||||
}
|
||||
|
||||
for _, al := range i.data {
|
||||
if al == alias {
|
||||
alias += strconv.Itoa(len(i.data))
|
||||
}
|
||||
}
|
||||
|
||||
i.data[pkg] = alias
|
||||
|
||||
return Import{
|
||||
Alias: alias,
|
||||
Package: pkg,
|
||||
}
|
||||
}
|
||||
|
||||
type Import struct {
|
||||
Alias string
|
||||
Package string
|
||||
}
|
||||
39
definition/generate/run.go
Normal file
39
definition/generate/run.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package generate
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"gitoa.ru/go-4devs/config/definition"
|
||||
)
|
||||
|
||||
func Run(w io.Writer, pkgName string, defs definition.Definition, viewOpt ViewOption) error {
|
||||
gen := Generator{
|
||||
pkg: pkgName,
|
||||
ViewOption: viewOpt,
|
||||
Imp: NewImports(),
|
||||
}
|
||||
|
||||
gen.Imp.Adds("gitoa.ru/go-4devs/config", "fmt", "context")
|
||||
|
||||
var view bytes.Buffer
|
||||
|
||||
err := defs.View(func(o definition.Option) error {
|
||||
return gen.Handle(&view, &gen, o)
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("render options:%w", err)
|
||||
}
|
||||
|
||||
if err := tpl.Execute(w, gen); err != nil {
|
||||
return fmt.Errorf("render base:%w", err)
|
||||
}
|
||||
|
||||
_, cerr := io.Copy(w, &view)
|
||||
if cerr != nil {
|
||||
return fmt.Errorf("copy error:%w", cerr)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
43
definition/generate/template.go
Normal file
43
definition/generate/template.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package generate
|
||||
|
||||
import "text/template"
|
||||
|
||||
//nolint:gochecknoglobals
|
||||
var (
|
||||
tpl = template.Must(template.New("tpls").Parse(baseTemplate))
|
||||
baseTemplate = `// Code generated gitoa.ru/go-4devs/config DO NOT EDIT.
|
||||
package {{.Pkg}}
|
||||
|
||||
import (
|
||||
{{range .Imports}}
|
||||
{{- .Alias }}"{{ .Package }}"
|
||||
{{end}}
|
||||
)
|
||||
|
||||
func With{{.StructName}}Log(log func(context.Context, string, ...any)) func(*{{.StructName}}) {
|
||||
return func(ci *{{.StructName}}) {
|
||||
ci.log = log
|
||||
}
|
||||
}
|
||||
|
||||
func New{{.StructName}}(prov config.Provider, opts ...func(*{{.StructName}})) {{.StructName}} {
|
||||
i := {{.StructName}}{
|
||||
Provider: prov,
|
||||
log: func(_ context.Context, format string, args ...any) {
|
||||
fmt.Printf(format, args...)
|
||||
},
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(&i)
|
||||
}
|
||||
|
||||
return i
|
||||
}
|
||||
|
||||
type {{.StructName}} struct {
|
||||
config.Provider
|
||||
log func(context.Context, string, ...any)
|
||||
}
|
||||
`
|
||||
)
|
||||
63
definition/generate/view.go
Normal file
63
definition/generate/view.go
Normal file
@@ -0,0 +1,63 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user