0

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.

  1. https://github.com/frederikhors/authbossQuicktemplate_1, with factory pattern and initializators

  2. 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

  1. 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}
    }
    
  2. 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
    }
    
Fred Hors
  • 3,258
  • 3
  • 25
  • 71
  • Is there a problem you need solved, or you're just looking for general code review? – Jonathan Hall Aug 04 '19 at 14:49
  • Code review, better ways of doing things, ideas, pointers or not... Something like that. SO is for this kind of stuff too, or am I wrong? – Fred Hors Aug 04 '19 at 15:07
  • On SO, we expect questions to be more about a specific problem. For more open-ended code review, try [Code Review.SE](https://codereview.stackexchange.com/). – Jonathan Hall Aug 04 '19 at 15:11
  • Opened, thanks: https://codereview.stackexchange.com/questions/225525/precompiled-templates-with-initializators-factory-pattern-or-single-interface But I hope you can answer this too please. Thanks. – Fred Hors Aug 04 '19 at 15:27

0 Answers0