处理字段
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -19,8 +20,8 @@ type Image struct {
|
||||
CollectCount int `json:"collect_count" db:"collect_count"`
|
||||
ArticleID int `json:"article_id" db:"article_id"`
|
||||
UserID int `json:"user_id" db:"user_id"`
|
||||
User User `json:"user" db:"user"`
|
||||
Article Article `json:"article" db:"article"`
|
||||
User User `json:"user" db:"-"`
|
||||
Article Article `json:"article" db:"-"`
|
||||
CreateTime time.Time `json:"create_time" db:"create_time"`
|
||||
UpdateTime time.Time `json:"update_time" db:"update_time"`
|
||||
}
|
||||
@@ -41,3 +42,40 @@ type Article struct {
|
||||
CreateTime time.Time `json:"create_time" db:"create_time"`
|
||||
UpdateTime time.Time `json:"update_time" db:"update_time"`
|
||||
}
|
||||
|
||||
// 利用反射获取每个结构体所有字段
|
||||
func getFields(s interface{}) (fields []string) {
|
||||
t := reflect.TypeOf(s)
|
||||
for i := 0; i < t.NumField(); i++ {
|
||||
fields = append(fields, t.Field(i).Name)
|
||||
}
|
||||
return fields
|
||||
}
|
||||
|
||||
// 检查字段是否存在
|
||||
func checkFields(fields []string, fields_list []string) bool {
|
||||
for _, field := range fields {
|
||||
if !contains(fields_list, field) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// 判断字符串是否存在于字符串数组中
|
||||
func contains(s []string, e string) bool {
|
||||
for _, v := range s {
|
||||
if v == e {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func QueryUserList(fields []string) (user_list []User) {
|
||||
if !checkFields(fields, getFields(User{})) {
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
Reference in New Issue
Block a user