我只发布我的代码:
/*
* Role will ALWAYS reserve the session key "role".
*/
package goserver
const (
ROLE_KEY string = "role"
)
type Role string
//if index is higher or equal than role, will pass
type RolesHierarchy []Role
func (r Role) String() string {
return string(r)
}
func NewRole(session ServerSession) Role {
return session.GetValue(ROLE_KEY).(Role)
}
func (this Role) IsRole(role Role, hierarchy RolesHierarchy) bool {
if role == this {
return true
}
if len(hierarchy) == 0 {
return false
}
var thisI int = 0
var roleI int = 0
//Duped roles in hierarchy are verified in verifyConfig during parse
for i, r := range hierarchy {
if this == r {
thisI = i
}
if role == r {
roleI = i
}
}
//TODO I can probably condense what follows into one if
if thisI == 0 && roleI == 0 {
return false
}
return thisI >= roleI
}
func (this *Role) AssumeRole(session ServerSession, role Role) {
session.SetValue(ROLE_KEY, role)
*this = role
}
需要注意的是,ServerSession也是一个接口,调用"ServerSessioner"对我来说是不可靠的.
我正在考虑用IsRole()和AssumeRole()创建一个接口,但是"Roler"似乎非常不可靠.我开始意识到,除了标准的"er"后缀之外,我并不真正了解或曾经遇到过接口的命名约定.我似乎还记得VS C++的惯例是在所有内容前面加上一个"i".这是"惯用语"吗?
有什么建议吗?