PROBLEM
I'm trying to use authboss (https://github.com/volatiletech/authboss) with quicktemplate (https://github.com/valyala/quicktemplate).
The authboss rendering system is defined by one interface: Renderer.
I have created two different versions of the solution and would like to know which one is best or if there is a third one better.
https://github.com/frederikhors/authbossQuicktemplate_1, with factory pattern and initializators
https://github.com/frederikhors/authbossQuicktemplate_2, with interface and a
SetData(data authboss.HTMLData) (page PageImpl)
method
QUESTIONS
I don't know what to improve for performances.
Is it possible to do the same thing differently and less in terms of hardware resources?
Do you think I can improve using pointers somewhere?
RELEVANT CODE
Solution with Factory pattern and initializators:
type HTML struct { templates map[string]func(authboss.HTMLData) templates.PageImpl } func (h *HTML) Load(names ...string) error { for _, n := range names { switch n { case "login": h.templates[n] = InitializeLoginPage case "recover": h.templates[n] = InitializeRecoverPage } } return nil } func (h *HTML) Render(ctx context.Context, page string, data authboss.HTMLData) (output []byte, contentType string, err error) { buf := &bytes.Buffer{} tpl, ok := h.templates[page] if !ok { return nil, "", errors.Errorf("template for page %s not found", page) } templates.WritePage(buf, tpl(data)) return buf.Bytes(), "text/html", nil } func InitializeLoginPage(data authboss.HTMLData) templates.PageImpl { return &templates.LoginPage{Data: data} } func InitializeRecoverPage(data authboss.HTMLData) templates.PageImpl { return &templates.RecoverPage{Data: data} }
Solution with interface with a method
type HTML struct { templates map[string]templates.AuthPageImpl } func (h *HTML) Load(names ...string) error { for _, n := range names { switch n { case "login": h.templates[n] = &templates.LoginPage{} case "recover": h.templates[n] = &templates.RecoverPage{} } } return nil } func (h *HTML) Render(ctx context.Context, page string, data authboss.HTMLData) (output []byte, contentType string, err error) { buf := &bytes.Buffer{} tpl, ok := h.templates[page] if !ok { return nil, "", errors.Errorf("template for page %s not found", page) } template := tpl.SetData(data) templates.WritePage(buf, template) return buf.Bytes(), "text/html", nil } type AuthPageImpl interface { SetData(data authboss.HTMLData) PageImpl } type LoginPage struct { Data authboss.HTMLData } type RecoverPage struct { Data authboss.HTMLData } func (p *LoginPage) SetData(data authboss.HTMLData) (page PageImpl) { p.Data = data return p } func (p *RecoverPage) SetData(data authboss.HTMLData) PageImpl { p.Data = data return p }