move toMap function to be a method on the quota structs

This commit is contained in:
woodsaj
2015-09-15 17:18:26 +08:00
parent b7de847236
commit 86ed85aa6e
3 changed files with 38 additions and 26 deletions

View File

@@ -1,5 +1,9 @@
package setting
import (
"reflect"
)
type OrgQuota struct {
User int64 `target:"org_user"`
DataSource int64 `target:"data_source"`
@@ -20,6 +24,38 @@ type GlobalQuota struct {
Session int64 `target:"-"`
}
func (q *OrgQuota) ToMap() map[string]int64 {
return quotaToMap(*q)
}
func (q *UserQuota) ToMap() map[string]int64 {
return quotaToMap(*q)
}
func (q *GlobalQuota) ToMap() map[string]int64 {
return quotaToMap(*q)
}
func quotaToMap(q interface{}) map[string]int64 {
qMap := make(map[string]int64)
typ := reflect.TypeOf(q)
val := reflect.ValueOf(q)
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
name := field.Tag.Get("target")
if name == "" {
name = field.Name
}
if name == "-" {
continue
}
value := val.Field(i)
qMap[name] = value.Int()
}
return qMap
}
type QuotaSettings struct {
Enabled bool
Org *OrgQuota