Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix function comments based on best practices from Effective Go #55

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions modules/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func CanRegistered(userName string, email string) (bool, bool, error) {
return e1, e2, nil
}

// check if exist user by username or email
// HasUser checks if exist user by username or email
func HasUser(user *models.User, username string) bool {
var err error
qs := orm.NewOrm()
Expand All @@ -76,7 +76,7 @@ func HasUser(user *models.User, username string) bool {
return false
}

// register create user
// RegisterUser: register create user
func RegisterUser(user *models.User, username, email, password string) error {
// use random salt encode password
salt := models.GetUserSalt()
Expand All @@ -97,14 +97,14 @@ func RegisterUser(user *models.User, username, email, password string) error {
return user.Insert()
}

// set a new password to user
// SaveNewPassword: set a new password to user
func SaveNewPassword(user *models.User, password string) error {
salt := models.GetUserSalt()
user.Password = fmt.Sprintf("%s$%s", salt, utils.EncodePassword(password, salt))
return user.Update("Password", "Rands", "Updated")
}

// get login redirect url from cookie
// GetLoginRedirect gets login redirect url from cookie
func GetLoginRedirect(ctx *context.Context) string {
loginRedirect := strings.TrimSpace(ctx.GetCookie("login_to"))
if utils.IsMatchHost(loginRedirect) == false {
Expand All @@ -115,7 +115,7 @@ func GetLoginRedirect(ctx *context.Context) string {
return loginRedirect
}

// login user
// LoginUser: login user
func LoginUser(user *models.User, ctx *context.Context, remember bool) {
// werid way of beego session regenerate id...
ctx.Input.CruSession.SessionRelease(ctx.ResponseWriter)
Expand Down Expand Up @@ -167,7 +167,7 @@ func LoginUserFromRememberCookie(user *models.User, ctx *context.Context) (succe
return true
}

// logout user
// LogoutUser: logout user
func LogoutUser(ctx *context.Context) {
DeleteRememberCookie(ctx)
ctx.Input.CruSession.Delete("auth_user_id")
Expand All @@ -182,7 +182,7 @@ func GetUserIdFromSession(sess session.Store) int {
return 0
}

// get user if key exist in session
// GetUserFromSession gets user if key exist in session
func GetUserFromSession(user *models.User, sess session.Store) bool {
id := GetUserIdFromSession(sess)
if id > 0 {
Expand All @@ -196,7 +196,7 @@ func GetUserFromSession(user *models.User, sess session.Store) bool {
return false
}

// verify username/email and password
// VerifyUser: verify username/email and password
func VerifyUser(user *models.User, username, password string) (success bool) {
// search user by username or email
if HasUser(user, username) == false {
Expand All @@ -217,7 +217,7 @@ func VerifyUser(user *models.User, username, password string) (success bool) {
return
}

// compare raw password and encoded password
// VerifyPassword compares raw password and encoded password
func VerifyPassword(rawPwd, encodedPwd string) bool {

// for discuz accounts
Expand Down Expand Up @@ -255,7 +255,7 @@ func getVerifyUser(user *models.User, code string) bool {
return false
}

// verify active code when active account
// VerifyUserActiveCode: verify active code when active account
func VerifyUserActiveCode(user *models.User, code string) bool {
minutes := setting.ActiveCodeLives

Expand All @@ -270,7 +270,7 @@ func VerifyUserActiveCode(user *models.User, code string) bool {
return false
}

// create a time limit code for user active
// CreateUserActiveCode: create a time limit code for user active
func CreateUserActiveCode(user *models.User, startInf interface{}) string {
minutes := setting.ActiveCodeLives
data := utils.ToStr(user.Id) + user.Email + user.UserName + user.Password + user.Rands
Expand All @@ -281,7 +281,7 @@ func CreateUserActiveCode(user *models.User, startInf interface{}) string {
return code
}

// verify code when reset password
// VerifyUserResetPwdCode: verify code when reset password
func VerifyUserResetPwdCode(user *models.User, code string) bool {
minutes := setting.ResetPwdCodeLives

Expand All @@ -296,7 +296,7 @@ func VerifyUserResetPwdCode(user *models.User, code string) bool {
return false
}

// create a time limit code for user reset password
// CreateUserResetPwdCode: create a time limit code for user reset password
func CreateUserResetPwdCode(user *models.User, startInf interface{}) string {
minutes := setting.ResetPwdCodeLives
data := utils.ToStr(user.Id) + user.Email + user.UserName + user.Password + user.Rands + user.Updated.String()
Expand Down
6 changes: 3 additions & 3 deletions modules/auth/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"github.com/beego/wetalk/modules/utils"
)

// Send user register mail with active code
// SendRegisterMail: Send user register mail with active code
func SendRegisterMail(locale i18n.Locale, user *models.User) {
code := CreateUserActiveCode(user, nil)

Expand All @@ -41,7 +41,7 @@ func SendRegisterMail(locale i18n.Locale, user *models.User) {
mailer.SendAsync(msg)
}

// Send user reset password mail with verify code
// SendResetPwdMail: Send user reset password mail with verify code
func SendResetPwdMail(locale i18n.Locale, user *models.User) {
code := CreateUserResetPwdCode(user, nil)

Expand All @@ -58,7 +58,7 @@ func SendResetPwdMail(locale i18n.Locale, user *models.User) {
mailer.SendAsync(msg)
}

// Send email verify active email.
// SendActiveMail: Send email verify active email.
func SendActiveMail(locale i18n.Locale, user *models.User) {
code := CreateUserActiveCode(user, nil)

Expand Down
2 changes: 1 addition & 1 deletion modules/mailer/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/beego/wetalk/setting"
)

// Create New mail message use MailFrom and MailUser
// NewMailMessage: Create New mail message use MailFrom and MailUser
func NewMailMessage(To []string, subject, body string) Message {
msg := NewHtmlMessage(To, setting.MailFrom, subject, body)
msg.User = setting.MailUser
Expand Down
8 changes: 4 additions & 4 deletions modules/mailer/mailer.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type Message struct {
Info string
}

// create mail content
// Content: create mail content
func (m Message) Content() string {
// set mail type
contentType := "text/plain; charset=UTF-8"
Expand All @@ -49,7 +49,7 @@ func (m Message) Content() string {
return content
}

// Direct Send mail message
// Send: Direct Send mail message
func Send(msg Message) (int, error) {
host := strings.Split(setting.MailHost, ":")

Expand Down Expand Up @@ -91,7 +91,7 @@ func Send(msg Message) (int, error) {
}
}

// Async Send mail message
// SendAsync: Async Send mail message
func SendAsync(msg Message) {
// TODO may be need pools limit concurrent nums
go func() {
Expand All @@ -107,7 +107,7 @@ func SendAsync(msg Message) {
}()
}

// Create html mail message
// NewHtmlMessage: Create html mail message
func NewHtmlMessage(To []string, From, Subject, Body string) Message {
return Message{
To: To,
Expand Down
2 changes: 1 addition & 1 deletion modules/models/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func init() {
orm.RegisterModel(new(Setting), new(User), new(Follow))
}

// return a user salt token
// GetUserSalt returns a user salt token
func GetUserSalt() string {
return utils.GetRandomString(10)
}
12 changes: 6 additions & 6 deletions modules/utils/forms.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ func (*fakeLocale) Tr(text string, args ...interface{}) string {

var fakeLocaler FormLocaler = new(fakeLocale)

// register a custom label/input creater
// RegisterFieldCreater: register a custom label/input creater
func RegisterFieldCreater(name string, field FieldCreater) {
customCreaters[name] = field
}

// register a custom label/input creater
// RegisterFieldFilter: register a custom label/input creater
func RegisterFieldFilter(name string, field FieldFilter) {
customFilters[name] = field
}
Expand Down Expand Up @@ -114,7 +114,7 @@ func (this *FormSets) SetError(fieldName, errMsg string) {
}
}

// create formSets for generate label/field html code
// NewFormSets: create formSets for generate label/field html code
func NewFormSets(form interface{}, errs map[string]*validation.Error, locale FormLocaler) *FormSets {
fSets := new(FormSets)
fSets.errs = errs
Expand Down Expand Up @@ -476,7 +476,7 @@ func initExtraField() {
})
}

// parse request.Form values to form
// ParseForm: parse request.Form values to form
func ParseForm(form interface{}, values url.Values) {
val := reflect.ValueOf(form)
elm := reflect.Indirect(val)
Expand Down Expand Up @@ -558,7 +558,7 @@ func panicAssertStructPtr(val reflect.Value) {
panic(fmt.Errorf("%s must be a struct pointer", val.Type().Name()))
}

// set values from one struct to other struct
// SetFormValues: set values from one struct to other struct
// both need ptr struct
func SetFormValues(from interface{}, to interface{}, skips ...string) {
val := reflect.ValueOf(from)
Expand Down Expand Up @@ -632,7 +632,7 @@ outFor:
}
}

// compare field values between two struct pointer
// FormChanges compares field values between two struct pointer
// return changed field names
func FormChanges(base interface{}, modified interface{}, skips ...string) (fields []string) {
val := reflect.ValueOf(base)
Expand Down
14 changes: 7 additions & 7 deletions modules/utils/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func NumberDecode(token string, alphabet []byte) string {
return x.String()
}

// Random generate string
// GetRandomString: Random generate string
func GetRandomString(n int) string {
const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
var bytes = make([]byte, n)
Expand Down Expand Up @@ -112,7 +112,7 @@ func PBKDF2(password, salt []byte, iter, keyLen int, h func() hash.Hash) []byte
return dk[:keyLen]
}

// verify time limit code
// VerifyTimeLimitCode: verify time limit code
func VerifyTimeLimitCode(data string, minutes int, code string) bool {
if len(code) <= 18 {
return false
Expand Down Expand Up @@ -140,7 +140,7 @@ func VerifyTimeLimitCode(data string, minutes int, code string) bool {

const TimeLimitCodeLength = 12 + 6 + 40

// create a time limit code
// CreateTimeLimitCode: create a time limit code
// code format: 12 length date time string + 6 minutes string + 40 sha1 encoded string
func CreateTimeLimitCode(data string, minutes int, startInf interface{}) string {
format := "YmdHi"
Expand Down Expand Up @@ -171,14 +171,14 @@ func CreateTimeLimitCode(data string, minutes int, startInf interface{}) string
return code
}

// Encode string to md5 hex value
// EncodeMd5: Encode string to md5 hex value
func EncodeMd5(str string) string {
m := md5.New()
m.Write([]byte(str))
return hex.EncodeToString(m.Sum(nil))
}

// use pbkdf2 encode password
// EncodePassword: use pbkdf2 encode password
func EncodePassword(rawPwd string, salt string) string {
pwd := PBKDF2([]byte(rawPwd), []byte(salt), 10000, 50, sha256.New)
return hex.EncodeToString(pwd)
Expand Down Expand Up @@ -308,7 +308,7 @@ func (f StrTo) String() string {
return ""
}

// convert any type to string
// ToStr: convert any type to string
func ToStr(value interface{}, args ...int) (s string) {
switch v := value.(type) {
case bool:
Expand Down Expand Up @@ -347,7 +347,7 @@ func ToStr(value interface{}, args ...int) (s string) {
return s
}

// convert any numeric value to int64
// ToInt64: convert any numeric value to int64
func ToInt64(value interface{}) (d int64, err error) {
val := reflect.ValueOf(value)
switch value.(type) {
Expand Down
14 changes: 7 additions & 7 deletions routers/admin/admin_article.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (this *ArticleAdminRouter) ObjectQs() orm.QuerySeter {
return models.Articles().RelatedSel()
}

// view for list model data
// List: view for list model data
func (this *ArticleAdminRouter) List() {
var articles []models.Article
qs := models.Articles().RelatedSel()
Expand All @@ -48,13 +48,13 @@ func (this *ArticleAdminRouter) List() {
}
}

// view for create object
// Create: view for create object
func (this *ArticleAdminRouter) Create() {
form := article.ArticleAdminForm{Create: true}
this.SetFormSets(&form)
}

// view for new object save
// Save: view for new object save
func (this *ArticleAdminRouter) Save() {
form := article.ArticleAdminForm{Create: true}
if !this.ValidFormSets(&form) {
Expand All @@ -72,14 +72,14 @@ func (this *ArticleAdminRouter) Save() {
}
}

// view for edit object
// Edit: view for edit object
func (this *ArticleAdminRouter) Edit() {
form := article.ArticleAdminForm{}
form.SetFromArticle(&this.object)
this.SetFormSets(&form)
}

// view for update object
// Update: view for update object
func (this *ArticleAdminRouter) Update() {
form := article.ArticleAdminForm{}
if this.ValidFormSets(&form) == false {
Expand All @@ -106,11 +106,11 @@ func (this *ArticleAdminRouter) Update() {
}
}

// view for confirm delete object
// Confirm: view for confirm delete object
func (this *ArticleAdminRouter) Confirm() {
}

// view for delete object
// Delete: view for delete object
func (this *ArticleAdminRouter) Delete() {
if this.FormOnceNotMatch() {
return
Expand Down
4 changes: 2 additions & 2 deletions routers/admin/admin_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (this *ModelAdminRouter) ModelPrepare() {
}
}

// query objects and set to template
// SetObjects: query objects and set to template
func (this *ModelAdminRouter) SetObjects(qs orm.QuerySeter, objects interface{}) error {
cnt, err := qs.Count()
if err != nil {
Expand All @@ -115,7 +115,7 @@ func (this *ModelAdminRouter) SetObjects(qs orm.QuerySeter, objects interface{})
return nil
}

// query object and set to template
// QueryObject: query object and set to template
func (this *ModelAdminRouter) QueryObject() bool {
id, _ := utils.StrTo(this.GetString(":id")).Int()
if id <= 0 {
Expand Down
Loading