From a2bcff84d85d84c86aa8cf97a20c450583cd40ed Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Fri, 19 Aug 2022 02:21:57 +0400 Subject: [PATCH] Fields for BaseCaptcha are made private. NewBaseCaptcha() constructor added. --- pkg/captcha/captcha.go | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/pkg/captcha/captcha.go b/pkg/captcha/captcha.go index 6e3b746..d1fcd20 100644 --- a/pkg/captcha/captcha.go +++ b/pkg/captcha/captcha.go @@ -38,9 +38,15 @@ type Captcha interface { // All derivatives that embed this struct only need to // implement Image() method. type BaseCaptcha struct { - Answer Answer - Solved bool - ExpireIn time.Time + answer Answer + solved bool + expireIn time.Time +} + +func NewBaseCaptcha(expiry time.Duration) *BaseCaptcha { + return &BaseCaptcha{ + expireIn: ExpiryDate(expiry), + } } 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 { - if c.Answer == "" { - c.Answer = NewIntAnswer() + if c.answer == "" { + c.answer = NewIntAnswer() } - return c.Answer + return c.answer } func (c *BaseCaptcha) Solve(answer Answer) bool { - c.Solved = c.Answer == answer - return c.Solved + c.solved = c.answer == answer + return c.solved } func (c *BaseCaptcha) IsSolved() bool { - return c.Solved + return c.solved } func (c *BaseCaptcha) Expiry() time.Time { - return c.ExpireIn + return c.expireIn } func ExpiryDate(expiry time.Duration) time.Time {