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
1 changed files with 11 additions and 23 deletions

View File

@ -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
}