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.

This commit is contained in:
Alexander Andreev 2022-07-30 15:37:05 +04:00
parent f5ea776e87
commit c14f9b0149
Signed by: Arav
GPG Key ID: 0388CC8FAA51063F

View File

@ -16,13 +16,10 @@ var errorNotFound = errors.New("captcha not found")
type Answer string type Answer string
// ICaptcha interface that should be implemented by captcha. // Captcha interface that should be implemented by captcha.
type ICaptcha interface { type Captcha interface {
// generateImage is used to create a captcha image. // Image generates and returns an image of captcha.
generateImage(style string) *image.Image Image(style string) image.Image
// GetImage() returns an image of captcha. Calls
// generateImage() method if image doesn't exist.
GetImage(style string) *image.Image
// GetAnswer returns a pregenerated answer. // GetAnswer returns a pregenerated answer.
GetAnswer() Answer GetAnswer() Answer
// Solve compares a stored answer with a passed one. // Solve compares a stored answer with a passed one.
@ -34,7 +31,7 @@ type ICaptcha interface {
Expiry() time.Time 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 // All derivatives that embed this struct only need to
// implement GetImage() and generateImage() methods. // implement GetImage() and generateImage() methods.
@ -43,42 +40,33 @@ type ICaptcha interface {
// You need to do that because in Go there is no inheritance, // You need to do that because in Go there is no inheritance,
// and if you won't do that, calling a base GetImage // and if you won't do that, calling a base GetImage
// method will call an empty base generateImage() method. // method will call an empty base generateImage() method.
type Captcha struct { type BaseCaptcha struct {
Image image.Image
Answer Answer Answer Answer
Solved bool Solved bool
ExpireIn time.Time ExpireIn time.Time
} }
func (c *Captcha) generateImage(style string) *image.Image { func (c *BaseCaptcha) Image(style string) *image.Image {
return nil return nil
} }
func (c *Captcha) GetImage(style string) *image.Image { func (c *BaseCaptcha) Solve(answer Answer) bool {
if c.Image == nil {
return c.generateImage(style)
}
return &c.Image
}
func (c *Captcha) Solve(answer Answer) bool {
c.Solved = c.Answer == answer c.Solved = c.Answer == answer
return c.Solved return c.Solved
} }
func (c *Captcha) GetAnswer() Answer { func (c *BaseCaptcha) GetAnswer() Answer {
if c.Answer == "" { if c.Answer == "" {
c.Answer = generateAnswer() c.Answer = generateAnswer()
} }
return c.Answer return c.Answer
} }
func (c *Captcha) IsSolved() bool { func (c *BaseCaptcha) IsSolved() bool {
return c.Solved return c.Solved
} }
func (c *Captcha) Expiry() time.Time { func (c *BaseCaptcha) Expiry() time.Time {
return c.ExpireIn return c.ExpireIn
} }