From c14f9b0149554aa60843c2ffd7747e986994fce6 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Sat, 30 Jul 2022 15:37:05 +0400 Subject: [PATCH] ICaptcha interface renamed to Captcha. generateImage() method removed. Instead image is being generated returned by value in Image() method that formely was named a GetImage() method. Base captcha implementation renamed to BaseCaptcha. --- pkg/captcha/captcha.go | 34 +++++++++++----------------------- 1 file changed, 11 insertions(+), 23 deletions(-) diff --git a/pkg/captcha/captcha.go b/pkg/captcha/captcha.go index 4e16375..6b07607 100644 --- a/pkg/captcha/captcha.go +++ b/pkg/captcha/captcha.go @@ -16,13 +16,10 @@ var errorNotFound = errors.New("captcha not found") type Answer string -// ICaptcha interface that should be implemented by captcha. -type ICaptcha interface { - // generateImage is used to create a captcha image. - generateImage(style string) *image.Image - // GetImage() returns an image of captcha. Calls - // generateImage() method if image doesn't exist. - GetImage(style string) *image.Image +// Captcha interface that should be implemented by captcha. +type Captcha interface { + // Image generates and returns an image of captcha. + Image(style string) image.Image // GetAnswer returns a pregenerated answer. GetAnswer() Answer // Solve compares a stored answer with a passed one. @@ -34,7 +31,7 @@ type ICaptcha interface { Expiry() time.Time } -// Captcha is a base implementation of a CAPTCHA. +// BaseCaptcha is a base implementation of a CAPTCHA. // // All derivatives that embed this struct only need to // implement GetImage() and generateImage() methods. @@ -43,42 +40,33 @@ type ICaptcha interface { // You need to do that because in Go there is no inheritance, // and if you won't do that, calling a base GetImage // method will call an empty base generateImage() method. -type Captcha struct { - Image image.Image +type BaseCaptcha struct { Answer Answer Solved bool ExpireIn time.Time } -func (c *Captcha) generateImage(style string) *image.Image { +func (c *BaseCaptcha) Image(style string) *image.Image { return nil } -func (c *Captcha) GetImage(style string) *image.Image { - if c.Image == nil { - return c.generateImage(style) - } - - return &c.Image -} - -func (c *Captcha) Solve(answer Answer) bool { +func (c *BaseCaptcha) Solve(answer Answer) bool { c.Solved = c.Answer == answer return c.Solved } -func (c *Captcha) GetAnswer() Answer { +func (c *BaseCaptcha) GetAnswer() Answer { if c.Answer == "" { c.Answer = generateAnswer() } return c.Answer } -func (c *Captcha) IsSolved() bool { +func (c *BaseCaptcha) IsSolved() bool { return c.Solved } -func (c *Captcha) Expiry() time.Time { +func (c *BaseCaptcha) Expiry() time.Time { return c.ExpireIn }