update option
This commit is contained in:
@@ -9,6 +9,8 @@ linters:
|
||||
# deprecated
|
||||
- wsl
|
||||
settings:
|
||||
funcorder:
|
||||
constructor: false
|
||||
dupl:
|
||||
threshold: 100
|
||||
funlen:
|
||||
|
||||
@@ -5,7 +5,9 @@ import (
|
||||
)
|
||||
|
||||
func New() Definition {
|
||||
return Definition{}
|
||||
return Definition{
|
||||
options: nil,
|
||||
}
|
||||
}
|
||||
|
||||
type Definition struct {
|
||||
|
||||
@@ -8,50 +8,51 @@ import (
|
||||
)
|
||||
|
||||
type Generator struct {
|
||||
pkg string
|
||||
ViewOption
|
||||
|
||||
pkg string
|
||||
Imp Imports
|
||||
errs []error
|
||||
defaultErrors []string
|
||||
}
|
||||
|
||||
func (g Generator) Pkg() string {
|
||||
func (g *Generator) Pkg() string {
|
||||
return g.pkg
|
||||
}
|
||||
|
||||
func (g Generator) Imports() []Import {
|
||||
func (g *Generator) Imports() []Import {
|
||||
return g.Imp.Imports()
|
||||
}
|
||||
|
||||
func (g Generator) Handle(w io.Writer, data Handler, opt definition.Option) error {
|
||||
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 {
|
||||
func (g *Generator) StructName() string {
|
||||
return FuncName(g.Prefix + "_" + g.Struct + "_" + g.Suffix)
|
||||
}
|
||||
|
||||
func (g Generator) Options() ViewOption {
|
||||
func (g *Generator) Options() ViewOption {
|
||||
return g.ViewOption
|
||||
}
|
||||
|
||||
func (g Generator) Keys() []string {
|
||||
func (g *Generator) Keys() []string {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g Generator) DefaultErrors() []string {
|
||||
func (g *Generator) DefaultErrors() []string {
|
||||
if len(g.defaultErrors) > 0 {
|
||||
return g.defaultErrors
|
||||
}
|
||||
|
||||
if len(g.ViewOption.Errors.Default) > 0 {
|
||||
if len(g.Errors.Default) > 0 {
|
||||
g.Imp.Adds("errors")
|
||||
}
|
||||
|
||||
g.defaultErrors = make([]string, len(g.ViewOption.Errors.Default))
|
||||
for idx, name := range g.ViewOption.Errors.Default {
|
||||
g.defaultErrors = make([]string, len(g.Errors.Default))
|
||||
for idx, name := range g.Errors.Default {
|
||||
short, err := g.AddType(name)
|
||||
if err != nil {
|
||||
g.errs = append(g.errs, fmt.Errorf("add default error[%d]:%w", idx, err))
|
||||
|
||||
@@ -16,7 +16,7 @@ type Imports struct {
|
||||
data map[string]string
|
||||
}
|
||||
|
||||
func (i Imports) Imports() []Import {
|
||||
func (i *Imports) Imports() []Import {
|
||||
imports := make([]Import, 0, len(i.data))
|
||||
for name, alias := range i.data {
|
||||
imports = append(imports, Import{
|
||||
|
||||
@@ -10,9 +10,11 @@ import (
|
||||
|
||||
func Run(w io.Writer, pkgName string, defs definition.Definition, viewOpt ViewOption) error {
|
||||
gen := Generator{
|
||||
pkg: pkgName,
|
||||
ViewOption: viewOpt,
|
||||
Imp: NewImports(),
|
||||
errs: nil,
|
||||
defaultErrors: nil,
|
||||
pkg: pkgName,
|
||||
ViewOption: viewOpt,
|
||||
Imp: NewImports(),
|
||||
}
|
||||
|
||||
gen.Imp.Adds("gitoa.ru/go-4devs/config", "fmt", "context")
|
||||
|
||||
@@ -25,7 +25,7 @@ func Add(kind string, h Handle) error {
|
||||
func get(kind string) Handle {
|
||||
handler, ok := handlers.Load(kind)
|
||||
if !ok {
|
||||
return func(w io.Writer, h Handler, o definition.Option) error {
|
||||
return func(_ io.Writer, _ Handler, _ definition.Option) error {
|
||||
return fmt.Errorf("handler by %v:%w", kind, ErrNotFound)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
module gitoa.ru/go-4devs/config/definition
|
||||
|
||||
go 1.21
|
||||
|
||||
require github.com/iancoleman/strcase v0.3.0
|
||||
@@ -1,2 +0,0 @@
|
||||
github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI=
|
||||
github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
|
||||
@@ -6,8 +6,6 @@ import (
|
||||
|
||||
const Kind = "group"
|
||||
|
||||
var _ definition.Option = Group{}
|
||||
|
||||
func New(name, desc string, opts ...definition.Option) Group {
|
||||
return Group{
|
||||
Name: name,
|
||||
|
||||
@@ -47,6 +47,7 @@ func handle(w io.Writer, data generate.Handler, option definition.Option) error
|
||||
|
||||
type ChildData struct {
|
||||
generate.Handler
|
||||
|
||||
structName string
|
||||
keys []string
|
||||
}
|
||||
@@ -61,8 +62,9 @@ func (c ChildData) Keys() []string {
|
||||
|
||||
type View struct {
|
||||
Group
|
||||
ParentName string
|
||||
generate.ViewOption
|
||||
|
||||
ParentName string
|
||||
}
|
||||
|
||||
func (v View) FuncName() string {
|
||||
|
||||
30
definition/option/errors.go
Normal file
30
definition/option/errors.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package option
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Error struct {
|
||||
Key []string
|
||||
Err error
|
||||
}
|
||||
|
||||
func (o Error) Error() string {
|
||||
return fmt.Sprintf("%s: %s", o.Key, o.Err)
|
||||
}
|
||||
|
||||
func (o Error) Is(err error) bool {
|
||||
return errors.Is(err, o.Err)
|
||||
}
|
||||
|
||||
func (o Error) Unwrap() error {
|
||||
return o.Err
|
||||
}
|
||||
|
||||
func Err(err error, key ...string) Error {
|
||||
return Error{
|
||||
Key: key,
|
||||
Err: err,
|
||||
}
|
||||
}
|
||||
101
definition/option/option.go
Executable file → Normal file
101
definition/option/option.go
Executable file → Normal file
@@ -1,100 +1,63 @@
|
||||
package option
|
||||
|
||||
import (
|
||||
"gitoa.ru/go-4devs/config/definition"
|
||||
"time"
|
||||
|
||||
"gitoa.ru/go-4devs/config/param"
|
||||
)
|
||||
|
||||
var _ definition.Option = Option{}
|
||||
|
||||
const (
|
||||
Kind = "option"
|
||||
)
|
||||
|
||||
const (
|
||||
TypeString = "string"
|
||||
TypeInt = "int"
|
||||
TypeInt64 = "int64"
|
||||
TypeUint = "uint"
|
||||
TypeUint64 = "uint64"
|
||||
TypeFloat64 = "float64"
|
||||
TypeBool = "bool"
|
||||
TypeTime = "time.Time"
|
||||
TypeDuration = "time.Duration"
|
||||
)
|
||||
|
||||
func Default(v any) func(*Option) {
|
||||
return func(o *Option) {
|
||||
o.Default = v
|
||||
}
|
||||
}
|
||||
|
||||
func New(name, desc string, vtype any, opts ...func(*Option)) Option {
|
||||
option := Option{
|
||||
Name: name,
|
||||
Description: desc,
|
||||
Type: vtype,
|
||||
func New(name, desc string, vtype any, opts ...param.Option) Option {
|
||||
opts = append(opts, Description(desc), WithType(vtype))
|
||||
res := Option{
|
||||
name: name,
|
||||
Param: param.New(opts...),
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(&option)
|
||||
}
|
||||
|
||||
return option
|
||||
return res
|
||||
}
|
||||
|
||||
type Option struct {
|
||||
Name string
|
||||
Description string
|
||||
Type any
|
||||
Default any
|
||||
Params definition.Params
|
||||
param.Param
|
||||
|
||||
name string
|
||||
}
|
||||
|
||||
func (o Option) WithParams(params ...definition.Param) Option {
|
||||
return Option{
|
||||
Name: o.Name,
|
||||
Description: o.Description,
|
||||
Type: o.Type,
|
||||
Params: append(params, o.Params...),
|
||||
}
|
||||
func (o Option) Name() string {
|
||||
return o.name
|
||||
}
|
||||
|
||||
func (o Option) Kind() string {
|
||||
return Kind
|
||||
func String(name, description string, opts ...param.Option) Option {
|
||||
return New(name, description, "", opts...)
|
||||
}
|
||||
|
||||
func Time(name, desc string, opts ...func(*Option)) Option {
|
||||
return New(name, desc, TypeTime, opts...)
|
||||
func Bool(name, description string, opts ...param.Option) Option {
|
||||
return New(name, description, false, opts...)
|
||||
}
|
||||
|
||||
func Duration(name, desc string, opts ...func(*Option)) Option {
|
||||
return New(name, desc, TypeDuration, opts...)
|
||||
func Duration(name, description string, opts ...param.Option) Option {
|
||||
return New(name, description, time.Duration(0), opts...)
|
||||
}
|
||||
|
||||
func String(name, desc string, opts ...func(*Option)) Option {
|
||||
return New(name, desc, TypeString, opts...)
|
||||
func Float64(name, description string, opts ...param.Option) Option {
|
||||
return New(name, description, float64(0), opts...)
|
||||
}
|
||||
|
||||
func Int(name, desc string, opts ...func(*Option)) Option {
|
||||
return New(name, desc, TypeInt, opts...)
|
||||
func Int(name, description string, opts ...param.Option) Option {
|
||||
return New(name, description, int(0), opts...)
|
||||
}
|
||||
|
||||
func Int64(name, desc string, opts ...func(*Option)) Option {
|
||||
return New(name, desc, TypeInt64, opts...)
|
||||
func Int64(name, description string, opts ...param.Option) Option {
|
||||
return New(name, description, int64(0), opts...)
|
||||
}
|
||||
|
||||
func Uint(name, desc string, opts ...func(*Option)) Option {
|
||||
return New(name, desc, TypeUint, opts...)
|
||||
func Time(name, description string, opts ...param.Option) Option {
|
||||
return New(name, description, time.Time{}, opts...)
|
||||
}
|
||||
|
||||
func Uint64(name, desc string, opts ...func(*Option)) Option {
|
||||
return New(name, desc, TypeUint64, opts...)
|
||||
func Uint(name, description string, opts ...param.Option) Option {
|
||||
return New(name, description, uint(0), opts...)
|
||||
}
|
||||
|
||||
func Float64(name, desc string, opts ...func(*Option)) Option {
|
||||
return New(name, desc, TypeFloat64, opts...)
|
||||
}
|
||||
|
||||
func Bool(name, desc string, opts ...func(*Option)) Option {
|
||||
return New(name, desc, TypeBool, opts...)
|
||||
func Uint64(name, descriontion string, opts ...param.Option) Option {
|
||||
return New(name, descriontion, uint64(0), opts...)
|
||||
}
|
||||
|
||||
130
definition/option/params.go
Normal file
130
definition/option/params.go
Normal file
@@ -0,0 +1,130 @@
|
||||
package option
|
||||
|
||||
import (
|
||||
"gitoa.ru/go-4devs/config/param"
|
||||
)
|
||||
|
||||
type key int
|
||||
|
||||
const (
|
||||
paramHidden key = iota + 1
|
||||
paramDefault
|
||||
paramDesc
|
||||
paramRequired
|
||||
paramSlice
|
||||
paramBool
|
||||
paramType
|
||||
paramPos
|
||||
paramShort
|
||||
)
|
||||
|
||||
func Short(in rune) param.Option {
|
||||
return func(v param.Param) param.Param {
|
||||
return param.With(v, paramShort, string(in))
|
||||
}
|
||||
}
|
||||
|
||||
func ParamShort(fn param.Param) (string, bool) {
|
||||
data, ok := param.String(paramShort, fn)
|
||||
|
||||
return data, ok
|
||||
}
|
||||
|
||||
func HasShort(short string, fn param.Param) bool {
|
||||
data, ok := param.String(paramShort, fn)
|
||||
|
||||
return ok && data == short
|
||||
}
|
||||
|
||||
func WithType(in any) param.Option {
|
||||
return func(v param.Param) param.Param {
|
||||
out := param.With(v, paramType, in)
|
||||
if _, ok := in.(bool); ok {
|
||||
return param.With(out, paramBool, ok)
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
}
|
||||
|
||||
func Position(pos uint64) param.Option {
|
||||
return func(p param.Param) param.Param {
|
||||
return param.With(p, paramPos, pos)
|
||||
}
|
||||
}
|
||||
|
||||
func Hidden(v param.Param) param.Param {
|
||||
return param.With(v, paramHidden, true)
|
||||
}
|
||||
|
||||
func Required(v param.Param) param.Param {
|
||||
return param.With(v, paramRequired, true)
|
||||
}
|
||||
|
||||
func Slice(v param.Param) param.Param {
|
||||
return param.With(v, paramSlice, true)
|
||||
}
|
||||
|
||||
func Default(in any) param.Option {
|
||||
return func(v param.Param) param.Param {
|
||||
return param.With(v, paramDefault, in)
|
||||
}
|
||||
}
|
||||
|
||||
func Description(in string) param.Option {
|
||||
return func(v param.Param) param.Param {
|
||||
return param.With(v, paramDesc, in)
|
||||
}
|
||||
}
|
||||
|
||||
func HasDefaut(fn param.Param) bool {
|
||||
_, ok := fn.Value(paramDefault)
|
||||
|
||||
return ok
|
||||
}
|
||||
|
||||
func DataPosition(fn param.Param) (uint64, bool) {
|
||||
return param.Uint64(paramPos, fn)
|
||||
}
|
||||
|
||||
func DataDefaut(fn param.Param) (any, bool) {
|
||||
data, ok := fn.Value(paramDefault)
|
||||
|
||||
return data, ok
|
||||
}
|
||||
|
||||
func IsSlice(fn param.Param) bool {
|
||||
data, ok := param.Bool(paramSlice, fn)
|
||||
|
||||
return ok && data
|
||||
}
|
||||
|
||||
func IsBool(fn param.Param) bool {
|
||||
data, ok := param.Bool(paramBool, fn)
|
||||
|
||||
return ok && data
|
||||
}
|
||||
|
||||
func IsHidden(fn param.Param) bool {
|
||||
data, ok := param.Bool(paramHidden, fn)
|
||||
|
||||
return ok && data
|
||||
}
|
||||
|
||||
func IsRequired(fn param.Param) bool {
|
||||
data, ok := param.Bool(paramRequired, fn)
|
||||
|
||||
return ok && data
|
||||
}
|
||||
|
||||
func DataType(fn param.Param) any {
|
||||
param, _ := fn.Value(paramType)
|
||||
|
||||
return param
|
||||
}
|
||||
|
||||
func DataDescription(fn param.Param) string {
|
||||
data, _ := param.String(paramDesc, fn)
|
||||
|
||||
return data
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
// read{{.FuncName}} {{.Description}}.
|
||||
func (i {{.StructName}}) read{{.FuncName}}(ctx context.Context) (v {{.Type}},e error) {
|
||||
val, err := i.Value(ctx, {{ .ParentKeys }}"{{ .Name }}")
|
||||
if err != nil {
|
||||
{{if .HasDefault}}
|
||||
{{$default := .Default}}
|
||||
{{range .DefaultErrors}}
|
||||
if errors.Is(err,{{.}}){
|
||||
return {{$default}}
|
||||
}
|
||||
{{end}}
|
||||
{{end}}
|
||||
return v, fmt.Errorf("read {{.Keys}}:%w",err)
|
||||
}
|
||||
|
||||
{{.Parse "val" "v" .Keys }}
|
||||
}
|
||||
|
||||
// Read{{.FuncName}} {{.Description}}.
|
||||
func (i {{.StructName}}) Read{{.FuncName}}(ctx context.Context) ({{.Type}}, error) {
|
||||
return i.read{{.FuncName}}(ctx)
|
||||
}
|
||||
|
||||
// {{.FuncName}} {{.Description}}.
|
||||
func (i {{.StructName}}) {{.FuncName}}({{if .Context}} ctx context.Context {{end}}) {{.Type}} {
|
||||
{{if not .Context}} ctx := context.Background() {{end}}
|
||||
val, err := i.read{{.FuncName}}(ctx)
|
||||
if err != nil {
|
||||
i.log(ctx, "get {{.Keys}}: %v", err)
|
||||
}
|
||||
|
||||
return val
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
{{block "Parse" .}}
|
||||
return {{.ValName}}.Parse{{ .FuncType}}()
|
||||
{{end}}
|
||||
@@ -1,8 +0,0 @@
|
||||
{{block "UnmarshalJSON" . }}
|
||||
pval, perr := {{.ValName}}.ParseString()
|
||||
if perr != nil {
|
||||
return {{.Value}}, fmt.Errorf("read {{.Keys}}:%w", perr)
|
||||
}
|
||||
|
||||
return {{.Value}}, {{.Value}}.UnmarshalJSON([]byte(pval))
|
||||
{{end}}
|
||||
@@ -1,8 +0,0 @@
|
||||
{{block "UnmarshalText" . }}
|
||||
pval, perr := {{.ValName}}.ParseString()
|
||||
if perr != nil {
|
||||
return {{.Value}}, fmt.Errorf("read {{.Keys}}:%w", perr)
|
||||
}
|
||||
|
||||
return {{.Value}}, {{.Value}}.UnmarshalText([]byte(pval))
|
||||
{{end}}
|
||||
@@ -1,232 +0,0 @@
|
||||
package option
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"embed"
|
||||
"encoding"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"reflect"
|
||||
"strings"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
"gitoa.ru/go-4devs/config/definition"
|
||||
"gitoa.ru/go-4devs/config/definition/generate"
|
||||
)
|
||||
|
||||
//go:embed tpl/*
|
||||
var tpls embed.FS
|
||||
|
||||
var tpl = template.Must(template.New("tpls").ParseFS(tpls, "tpl/*.tmpl"))
|
||||
|
||||
//nolint:gochecknoinits
|
||||
func init() {
|
||||
generate.MustAdd(Kind, Handle(tpl.Lookup("option.tmpl")))
|
||||
}
|
||||
|
||||
func Handle(tpl *template.Template) generate.Handle {
|
||||
return func(w io.Writer, h generate.Handler, o definition.Option) error {
|
||||
opt, _ := o.(Option)
|
||||
if err := tpl.Execute(w, View{Option: opt, Handler: h}); err != nil {
|
||||
return fmt.Errorf("option tpl:%w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
type View struct {
|
||||
Option
|
||||
generate.Handler
|
||||
}
|
||||
|
||||
func (v View) Context() bool {
|
||||
return v.Options().Context
|
||||
}
|
||||
|
||||
func (v View) FuncName() string {
|
||||
if funcName, ok := v.Option.Params.Get(ViewParamFunctName); ok {
|
||||
name, _ := funcName.(string)
|
||||
|
||||
return name
|
||||
}
|
||||
|
||||
return generate.FuncName(v.Name)
|
||||
}
|
||||
|
||||
func (v View) Description() string {
|
||||
if desc, ok := v.Option.Params.Get(ViewParamDescription); ok {
|
||||
description, _ := desc.(string)
|
||||
|
||||
return description
|
||||
}
|
||||
|
||||
return v.Option.Description
|
||||
}
|
||||
|
||||
func (v View) Default() string {
|
||||
switch data := v.Option.Default.(type) {
|
||||
case time.Time:
|
||||
return fmt.Sprintf("time.Parse(%q,time.RFC3339Nano)", data.Format(time.RFC3339Nano))
|
||||
case time.Duration:
|
||||
return fmt.Sprintf("time.ParseDuration(%q)", data)
|
||||
default:
|
||||
return fmt.Sprintf("%#v, nil", data)
|
||||
}
|
||||
}
|
||||
|
||||
func (v View) HasDefault() bool {
|
||||
return v.Option.Default != nil
|
||||
}
|
||||
|
||||
func (v View) ParentKeys() string {
|
||||
if len(v.Handler.Keys()) > 0 {
|
||||
return `"` + strings.Join(v.Handler.Keys(), `","`) + `",`
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
func (v View) Type() string {
|
||||
slice := ""
|
||||
|
||||
if vtype, ok := v.Option.Type.(string); ok {
|
||||
if strings.Contains(vtype, ".") {
|
||||
if name, err := v.AddType(vtype); err == nil {
|
||||
return slice + name
|
||||
}
|
||||
}
|
||||
|
||||
return vtype
|
||||
}
|
||||
|
||||
rtype := reflect.TypeOf(v.Option.Type)
|
||||
|
||||
if rtype.PkgPath() == "" {
|
||||
return rtype.String()
|
||||
}
|
||||
|
||||
if rtype.Kind() == reflect.Slice {
|
||||
slice = "[]"
|
||||
}
|
||||
|
||||
short, err := v.AddType(rtype.PkgPath() + "." + rtype.Name())
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
}
|
||||
|
||||
return slice + short
|
||||
}
|
||||
|
||||
func (v View) FuncType() string {
|
||||
return generate.FuncName(v.Type())
|
||||
}
|
||||
|
||||
func (v View) Parse(valName string, value string, keys []string) string {
|
||||
h := parser(v.Option.Type)
|
||||
|
||||
data, err := h(ParseData{
|
||||
Value: value,
|
||||
ValName: valName,
|
||||
Keys: keys,
|
||||
View: v,
|
||||
})
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
var parses = map[string]func(data ParseData) (string, error){
|
||||
typesIntreface[0].Name(): func(data ParseData) (string, error) {
|
||||
var b bytes.Buffer
|
||||
|
||||
err := tpl.ExecuteTemplate(&b, "unmarshal_text.tmpl", data)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("execute unmarshal text:%w", err)
|
||||
}
|
||||
|
||||
return b.String(), nil
|
||||
},
|
||||
typesIntreface[1].Name(): func(data ParseData) (string, error) {
|
||||
var b bytes.Buffer
|
||||
|
||||
err := tpl.ExecuteTemplate(&b, "unmarshal_json.tmpl", data)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("execute unmarshal json:%w", err)
|
||||
}
|
||||
|
||||
return b.String(), nil
|
||||
},
|
||||
TypeInt: internal,
|
||||
TypeInt64: internal,
|
||||
TypeBool: internal,
|
||||
TypeString: internal,
|
||||
TypeFloat64: internal,
|
||||
TypeUint: internal,
|
||||
TypeUint64: internal,
|
||||
"time.Duration": func(data ParseData) (string, error) {
|
||||
return fmt.Sprintf("return %s.ParseDuration()", data.ValName), nil
|
||||
},
|
||||
"time.Time": func(data ParseData) (string, error) {
|
||||
return fmt.Sprintf("return %s.ParseTime()", data.ValName), nil
|
||||
},
|
||||
"any": func(data ParseData) (string, error) {
|
||||
return fmt.Sprintf("return %[2]s, %[1]s.Unmarshal(&%[2]s)", data.ValName, data.Value), nil
|
||||
},
|
||||
}
|
||||
|
||||
func internal(data ParseData) (string, error) {
|
||||
var b bytes.Buffer
|
||||
|
||||
err := tpl.ExecuteTemplate(&b, "parse.tmpl", data)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("execute parse.tmpl:%w", err)
|
||||
}
|
||||
|
||||
return b.String(), nil
|
||||
}
|
||||
|
||||
var typesIntreface = [...]reflect.Type{
|
||||
reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem(),
|
||||
reflect.TypeOf((*json.Unmarshaler)(nil)).Elem(),
|
||||
}
|
||||
|
||||
func parser(data any) func(ParseData) (string, error) {
|
||||
vtype := reflect.TypeOf(data)
|
||||
name := vtype.Name()
|
||||
|
||||
if v, ok := data.(string); ok {
|
||||
name = v
|
||||
}
|
||||
|
||||
if vtype.Kind() == reflect.Slice {
|
||||
return parses["any"]
|
||||
}
|
||||
|
||||
if h, ok := parses[name]; ok {
|
||||
return h
|
||||
}
|
||||
|
||||
for _, extypes := range typesIntreface {
|
||||
if vtype.Implements(extypes) {
|
||||
return parses[extypes.Name()]
|
||||
}
|
||||
|
||||
if vtype.Kind() != reflect.Ptr && reflect.PointerTo(vtype).Implements(extypes) {
|
||||
return parses[extypes.Name()]
|
||||
}
|
||||
}
|
||||
|
||||
return parses["any"]
|
||||
}
|
||||
|
||||
type ParseData struct {
|
||||
Value string
|
||||
ValName string
|
||||
Keys []string
|
||||
View
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
package option
|
||||
|
||||
const (
|
||||
ViewParamFunctName = "view.funcName"
|
||||
ViewParamDescription = "view.description"
|
||||
)
|
||||
@@ -1,79 +0,0 @@
|
||||
package proto
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"gitoa.ru/go-4devs/config/definition"
|
||||
"gitoa.ru/go-4devs/config/definition/generate"
|
||||
"gitoa.ru/go-4devs/config/definition/option"
|
||||
)
|
||||
|
||||
//nolint:gochecknoinits
|
||||
func init() {
|
||||
generate.MustAdd(Kind, handle)
|
||||
}
|
||||
|
||||
func handle(w io.Writer, data generate.Handler, opt definition.Option) error {
|
||||
proto, ok := opt.(Proto)
|
||||
if !ok {
|
||||
return fmt.Errorf("%w:%T", generate.ErrWrongType, opt)
|
||||
}
|
||||
|
||||
if viewOpt, ok := proto.Option.(option.Option); ok {
|
||||
viewOpt = viewOpt.WithParams(
|
||||
definition.Param{
|
||||
Name: option.ViewParamFunctName,
|
||||
Value: generate.FuncName(proto.Name) + generate.FuncName(viewOpt.Name),
|
||||
},
|
||||
definition.Param{
|
||||
Name: option.ViewParamDescription,
|
||||
Value: proto.Description + " " + viewOpt.Description,
|
||||
},
|
||||
)
|
||||
|
||||
return option.Handle(tpl)(w, data, viewOpt)
|
||||
}
|
||||
|
||||
return fmt.Errorf("%w:%T", generate.ErrWrongType, opt)
|
||||
}
|
||||
|
||||
var (
|
||||
tpl = template.Must(template.New("tpls").Funcs(template.FuncMap{"join": strings.Join}).Parse(templateOption))
|
||||
templateOption = `// read{{.FuncName}} {{.Description}}.
|
||||
func (i {{.StructName}}) read{{.FuncName}}(ctx context.Context, key string) (v {{.Type}},e error) {
|
||||
val, err := i.Value(ctx, {{ .ParentKeys }} key, "{{.Name}}")
|
||||
if err != nil {
|
||||
{{if .HasDefault}}
|
||||
{{$default := .Default}}
|
||||
{{range .DefaultErrors}}
|
||||
if errors.Is(err,{{.}}){
|
||||
return {{$default}}
|
||||
}
|
||||
{{end}}
|
||||
{{end}}
|
||||
return v, fmt.Errorf("read {{.Keys}}:%w",err)
|
||||
}
|
||||
|
||||
{{.Parse "val" "v" .Keys }}
|
||||
}
|
||||
|
||||
// Read{{.FuncName}} {{.Description}}.
|
||||
func (i {{.StructName}}) Read{{.FuncName}}(ctx context.Context, key string) ({{.Type}}, error) {
|
||||
return i.read{{.FuncName}}(ctx, key)
|
||||
}
|
||||
|
||||
// {{.FuncName}} {{.Description}}.
|
||||
func (i {{.StructName}}) {{.FuncName}}({{if .Context}} ctx context.Context, {{end}} key string) {{.Type}} {
|
||||
{{if not .Context}} ctx := context.Background() {{end}}
|
||||
val, err := i.read{{.FuncName}}(ctx, key)
|
||||
if err != nil {
|
||||
i.log(ctx, "get {{.Keys}}: %v", err)
|
||||
}
|
||||
|
||||
return val
|
||||
}
|
||||
`
|
||||
)
|
||||
2
go.mod
2
go.mod
@@ -1,3 +1,5 @@
|
||||
module gitoa.ru/go-4devs/config
|
||||
|
||||
go 1.23
|
||||
|
||||
require github.com/iancoleman/strcase v0.3.0
|
||||
|
||||
2
go.sum
2
go.sum
@@ -0,0 +1,2 @@
|
||||
github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI=
|
||||
github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package param
|
||||
|
||||
import "gitoa.ru/go-4devs/config"
|
||||
|
||||
func String(key any, fn Param) (string, bool) {
|
||||
val, ok := fn.Value(key)
|
||||
if !ok {
|
||||
@@ -21,3 +23,25 @@ func Bool(key any, fn Param) (bool, bool) {
|
||||
|
||||
return data, ok
|
||||
}
|
||||
|
||||
func Value(key any, fn Param) (config.Value, bool) {
|
||||
data, ok := fn.Value(key)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
res, ok := data.(config.Value)
|
||||
|
||||
return res, ok
|
||||
}
|
||||
|
||||
func Uint64(key any, fn Param) (uint64, bool) {
|
||||
data, ok := fn.Value(key)
|
||||
if !ok {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
res, ok := data.(uint64)
|
||||
|
||||
return res, ok
|
||||
}
|
||||
|
||||
@@ -6,8 +6,10 @@ const (
|
||||
paramTimeFormat key = iota + 1
|
||||
)
|
||||
|
||||
func WithTimeFormat(parent Param, format string) Param {
|
||||
return With(parent, paramTimeFormat, format)
|
||||
func WithTimeFormat(format string) Option {
|
||||
return func(p Param) Param {
|
||||
return With(p, paramTimeFormat, format)
|
||||
}
|
||||
}
|
||||
|
||||
func TimeFormat(fn Param) (string, bool) {
|
||||
|
||||
@@ -8,6 +8,8 @@ var (
|
||||
emptyParam = empty{}
|
||||
)
|
||||
|
||||
type Option func(p Param) Param
|
||||
|
||||
type Param interface {
|
||||
Value(key any) (any, bool)
|
||||
}
|
||||
@@ -26,8 +28,16 @@ func With(parent Param, key, val any) Param {
|
||||
}
|
||||
}
|
||||
|
||||
func New() Param {
|
||||
return emptyParam
|
||||
func New(opts ...Option) Param {
|
||||
var parms Param
|
||||
|
||||
parms = emptyParam
|
||||
|
||||
for _, opt := range opts {
|
||||
parms = opt(parms)
|
||||
}
|
||||
|
||||
return parms
|
||||
}
|
||||
|
||||
type empty struct{}
|
||||
|
||||
Reference in New Issue
Block a user