diff --git a/template/builder.go b/template/builder.go index 21d35d3497c7167e066ef1dedecb9cb3751a6d42..1674432f7a8ea0cf96c1b9f78295e4f4bf6a9104 100644 --- a/template/builder.go +++ b/template/builder.go @@ -13,13 +13,12 @@ import ( "git.perx.ru/perxis/perxis-go/pkg/spaces" ) -type Executor[T any] interface { - Parse(text string) (T, error) +type Template interface { Execute(w io.Writer, data any) error } -type Builder[T Executor[T]] struct { - template func(string) T +type Builder struct { + parseFunc func(text string) (Template, error) ctx context.Context cnt *content.Content @@ -34,48 +33,48 @@ type Builder[T Executor[T]] struct { collection *collections.Collection } -func NewBuilder(cnt *content.Content, space, env, col string) Builder[*templtext.Template] { - b := Builder[*templtext.Template]{ +func NewBuilder(cnt *content.Content, space, env, col string) Builder { + b := Builder{ ctx: context.Background(), cnt: cnt, SpaceID: space, EnvID: env, CollID: col, } - b.template = func(name string) *templtext.Template { - return templtext.New(name).Funcs(b.getFuncs()) + b.parseFunc = func(text string) (Template, error) { + return templtext.New("main_text").Funcs(b.getFuncs()).Parse(text) } return b } -func NewHTMLBuilder(cnt *content.Content, space, env, col string) Builder[*templhtml.Template] { - b := Builder[*templhtml.Template]{ +func NewHTMLBuilder(cnt *content.Content, space, env, col string) Builder { + b := Builder{ ctx: context.Background(), cnt: cnt, SpaceID: space, EnvID: env, CollID: col, } - b.template = func(name string) *templhtml.Template { - return templhtml.New(name).Funcs(b.getFuncs()) + b.parseFunc = func(text string) (Template, error) { + return templhtml.New("main_html").Funcs(b.getFuncs()).Parse(text) } return b } -func (b *Builder[T]) getFuncs() map[string]any { +func (b *Builder) getFuncs() map[string]any { return map[string]any{ "lookup": getLookup(b), "system": getSystem(b), } } -func (b *Builder[T]) WithData(data map[string]interface{}) *Builder[T] { +func (b *Builder) WithData(data map[string]interface{}) *Builder { bld := *b bld.data = data return &bld } -func (b *Builder[T]) WithKV(kv ...any) *Builder[T] { +func (b *Builder) WithKV(kv ...any) *Builder { bld := *b if bld.data == nil { bld.data = make(map[string]interface{}, 10) @@ -90,31 +89,30 @@ func (b *Builder[T]) WithKV(kv ...any) *Builder[T] { return &bld } -func (b *Builder[T]) GetData() map[string]interface{} { +func (b *Builder) GetData() map[string]interface{} { return b.data } -func (b *Builder[T]) WithSpace(space, env string) *Builder[T] { +func (b *Builder) WithSpace(space, env string) *Builder { bld := *b bld.SpaceID = space bld.EnvID = env return &bld } -func (b *Builder[T]) WithContext(ctx context.Context) *Builder[T] { +func (b *Builder) WithContext(ctx context.Context) *Builder { bld := *b bld.ctx = ctx return &bld } -func (b *Builder[T]) Context() context.Context { +func (b *Builder) Context() context.Context { return b.ctx } -func (b *Builder[T]) Execute(str string, data ...any) (string, error) { - t := b.template("main") +func (b *Builder) Execute(str string, data ...any) (string, error) { buf := new(bytes.Buffer) - t, err := t.Parse(str) + t, err := b.parseFunc(str) if err != nil { return "", err } @@ -124,15 +122,14 @@ func (b *Builder[T]) Execute(str string, data ...any) (string, error) { return buf.String(), nil } -func (b *Builder[T]) ExecuteList(str []string, data ...any) ([]string, error) { - t := b.template("main") +func (b *Builder) ExecuteList(str []string, data ...any) ([]string, error) { result := make([]string, len(str)) buffer := new(bytes.Buffer) for i, tmpl := range str { if tmpl == "" { continue } - t, err := t.Parse(tmpl) + t, err := b.parseFunc(tmpl) if err != nil { return []string{}, err } @@ -145,7 +142,7 @@ func (b *Builder[T]) ExecuteList(str []string, data ...any) ([]string, error) { return result, nil } -func (b *Builder[T]) ExecuteMap(str map[string]interface{}, data ...any) (map[string]interface{}, error) { +func (b *Builder) ExecuteMap(str map[string]interface{}, data ...any) (map[string]interface{}, error) { result := make(map[string]interface{}, len(str)) for k, v := range str { switch t := v.(type) { @@ -173,7 +170,7 @@ func (b *Builder[T]) ExecuteMap(str map[string]interface{}, data ...any) (map[st return result, nil } -func (b *Builder[T]) getData(data ...any) any { +func (b *Builder) getData(data ...any) any { if len(data) == 0 { return b.data } diff --git a/template/system.go b/template/system.go index d6e66616e7dd0e2fae251dbc58b8ff0e83998a7a..c7dda43f08c852f590cd7cfa16ec00709423c7a3 100644 --- a/template/system.go +++ b/template/system.go @@ -6,23 +6,23 @@ import ( "git.perx.ru/perxis/perxis-go/pkg/spaces" ) -type System[T Executor[T]] struct { - builder *Builder[T] +type System struct { + builder *Builder } -func (s *System[T]) SpaceID() string { +func (s *System) SpaceID() string { return s.builder.SpaceID } -func (s *System[T]) EnvID() string { +func (s *System) EnvID() string { return s.builder.EnvID } -func (s *System[T]) CollectionID() string { +func (s *System) CollectionID() string { return s.builder.CollID } -func (s *System[T]) Space() (*spaces.Space, error) { +func (s *System) Space() (*spaces.Space, error) { if s.builder.space != nil { return s.builder.space, nil } @@ -32,7 +32,7 @@ func (s *System[T]) Space() (*spaces.Space, error) { return s.builder.space, err } -func (s *System[T]) Environment() (*environments.Environment, error) { +func (s *System) Environment() (*environments.Environment, error) { if s.builder.environment != nil { return s.builder.environment, nil } @@ -42,7 +42,7 @@ func (s *System[T]) Environment() (*environments.Environment, error) { return s.builder.environment, err } -func (s *System[T]) Collection() (*collections.Collection, error) { +func (s *System) Collection() (*collections.Collection, error) { if s.builder.collection != nil { return s.builder.collection, nil }