Fields for BaseCaptcha are made private. NewBaseCaptcha() constructor added.

This commit is contained in:
Alexander Andreev 2022-08-19 02:21:57 +04:00
parent 7737c360d0
commit a2bcff84d8
Signed by: Arav
GPG Key ID: 0388CC8FAA51063F

View File

@ -38,9 +38,15 @@ type Captcha interface {
// All derivatives that embed this struct only need to // All derivatives that embed this struct only need to
// implement Image() method. // implement Image() method.
type BaseCaptcha struct { type BaseCaptcha struct {
Answer Answer answer Answer
Solved bool solved bool
ExpireIn time.Time expireIn time.Time
}
func NewBaseCaptcha(expiry time.Duration) *BaseCaptcha {
return &BaseCaptcha{
expireIn: ExpiryDate(expiry),
}
} }
func (c *BaseCaptcha) Image(style string) *image.Image { func (c *BaseCaptcha) Image(style string) *image.Image {
@ -48,23 +54,23 @@ func (c *BaseCaptcha) Image(style string) *image.Image {
} }
func (c *BaseCaptcha) GetAnswer() Answer { func (c *BaseCaptcha) GetAnswer() Answer {
if c.Answer == "" { if c.answer == "" {
c.Answer = NewIntAnswer() c.answer = NewIntAnswer()
} }
return c.Answer return c.answer
} }
func (c *BaseCaptcha) Solve(answer Answer) bool { func (c *BaseCaptcha) Solve(answer Answer) bool {
c.Solved = c.Answer == answer c.solved = c.answer == answer
return c.Solved return c.solved
} }
func (c *BaseCaptcha) IsSolved() bool { func (c *BaseCaptcha) IsSolved() bool {
return c.Solved return c.solved
} }
func (c *BaseCaptcha) Expiry() time.Time { func (c *BaseCaptcha) Expiry() time.Time {
return c.ExpireIn return c.expireIn
} }
func ExpiryDate(expiry time.Duration) time.Time { func ExpiryDate(expiry time.Duration) time.Time {