本文整理匯總了Golang中util.Struct2Map函數的典型用法代碼示例。如果您正苦於以下問題:Golang Struct2Map函數的具體用法?Golang Struct2Map怎麽用?Golang Struct2Map使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Struct2Map函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: FindHotTopics
// 獲得回複最多的10條帖子(TODO:避免一直顯示相同的)
func FindHotTopics() []map[string]interface{} {
topicExList, err := model.NewTopicEx().Order("reply DESC").Limit("0,10").FindAll()
if err != nil {
logger.Errorln("topic service FindHotReplies error:", err)
return nil
}
tidMap := make(map[int]int, len(topicExList))
topicExMap := make(map[int]*model.TopicEx, len(topicExList))
for _, topicEx := range topicExList {
tidMap[topicEx.Tid] = topicEx.Tid
topicExMap[topicEx.Tid] = topicEx
}
tids := util.MapIntKeys(tidMap)
topics := FindTopicsByTids(tids)
if topics == nil {
return nil
}
uidMap := make(map[int]int, len(topics))
for _, topic := range topics {
uidMap[topic.Uid] = topic.Uid
}
userMap := getUserInfos(uidMap)
result := make([]map[string]interface{}, len(topics))
for i, topic := range topics {
oneTopic := make(map[string]interface{})
util.Struct2Map(oneTopic, topic)
util.Struct2Map(oneTopic, topicExMap[topic.Tid])
oneTopic["user"] = userMap[topic.Uid]
result[i] = oneTopic
}
return result
}
示例2: FindResource
// 獲得資源詳細信息
func FindResource(id string) (resourceMap map[string]interface{}, comments []map[string]interface{}) {
condition := "id=" + id
resource := model.NewResource()
err := resource.Where(condition).Find()
if err != nil {
logger.Errorln("resource service FindResource error:", err)
return
}
resourceMap = make(map[string]interface{})
util.Struct2Map(resourceMap, resource)
resourceMap["catname"] = GetCategoryName(resource.Catid)
// 鏈接的host
if resource.Form == model.LinkForm {
urlObj, err := url.Parse(resource.Url)
if err == nil {
resourceMap["host"] = urlObj.Host
}
} else {
resourceMap["url"] = "/resources/" + strconv.Itoa(resource.Id)
}
resourceEx := model.NewResourceEx()
err = resourceEx.Where(condition).Find()
if err != nil {
logger.Errorln("resource service FindResource Error:", err)
return
}
util.Struct2Map(resourceMap, resourceEx)
// 評論信息
comments, ownerUser, _ := FindObjComments(id, strconv.Itoa(model.TYPE_RESOURCE), resource.Uid, 0)
resourceMap["user"] = ownerUser
return
}
示例3: FindTopicByTid
// 獲得帖子詳細信息(包括詳細回複)
// 為了避免轉換,tid傳string類型
func FindTopicByTid(tid string) (topicMap map[string]interface{}, replies []map[string]interface{}, err error) {
condition := "tid=" + tid
// 帖子信息
topic := model.NewTopic()
err = topic.Where(condition).Find()
if err != nil {
logger.Errorln("topic service FindTopicByTid Error:", err)
return
}
// 帖子不存在
if topic.Tid == 0 {
return
}
topicMap = make(map[string]interface{})
util.Struct2Map(topicMap, topic)
topicEx := model.NewTopicEx()
err = topicEx.Where(condition).Find()
if err != nil {
logger.Errorln("topic service FindTopicByTid Error:", err)
return
}
if topicEx.Tid == 0 {
return
}
util.Struct2Map(topicMap, topicEx)
// 節點名字
topicMap["node"] = model.GetNodeName(topic.Nid)
// 回複信息(評論)
replyList, err := model.NewComment().Where("objid=" + tid + " and objtype=" + strconv.Itoa(model.TYPE_TOPIC)).FindAll()
if err != nil {
logger.Errorln("topic service FindTopicByTid Error:", err)
return
}
replyNum := len(replyList)
uids := make(map[int]int, replyNum+1)
uids[topic.Uid] = topic.Uid
for _, reply := range replyList {
uids[reply.Uid] = reply.Uid
}
// 獲得用戶信息
userMap := getUserInfos(uids)
topicMap["user"] = userMap[topic.Uid]
// 有人回複
if topic.Lastreplyuid != 0 {
topicMap["lastreplyusername"] = userMap[topicMap["lastreplyuid"].(int)].Username
}
replies = make([]map[string]interface{}, 0, replyNum)
for _, reply := range replyList {
tmpMap := make(map[string]interface{})
util.Struct2Map(tmpMap, reply)
tmpMap["user"] = userMap[reply.Uid]
replies = append(replies, tmpMap)
}
return
}
示例4: FindTopicByTid
// 獲得主題詳細信息(包括詳細回複)
// 為了避免轉換,tid傳string類型
func FindTopicByTid(tid string) (topicMap map[string]interface{}, replies []map[string]interface{}, err error) {
condition := "tid=" + tid
// 主題信息
topic := model.NewTopic()
err = topic.Where(condition).Find()
if err != nil {
logger.Errorln("topic service FindTopicByTid Error:", err)
return
}
// 主題不存在
if topic.Tid == 0 {
err = errors.New("The topic of tid is not exists")
return
}
topicMap = make(map[string]interface{})
util.Struct2Map(topicMap, topic)
// 解析內容中的 @
topicMap["content"] = decodeTopicContent(topic)
topicEx := model.NewTopicEx()
err = topicEx.Where(condition).Find()
if err != nil {
logger.Errorln("topic service FindTopicByTid Error:", err)
return
}
if topicEx.Tid == 0 {
return
}
util.Struct2Map(topicMap, topicEx)
// 節點名字
topicMap["node"] = GetNodeName(topic.Nid)
// 回複信息(評論)
replies, owerUser, lastReplyUser := FindObjComments(tid, strconv.Itoa(model.TYPE_TOPIC), topic.Uid, topic.Lastreplyuid)
topicMap["user"] = owerUser
// 有人回複
if topic.Lastreplyuid != 0 {
topicMap["lastreplyusername"] = lastReplyUser.Username
}
if topic.EditorUid != 0 {
topicMap["editor_username"] = FindUsernameByUid(topic.EditorUid)
}
return
}
示例5: FindToMsgsByUid
// 獲得發給某人的短消息(收件箱)
func FindToMsgsByUid(uid string) []map[string]interface{} {
messages, err := model.NewMessage().Where("to=" + uid + " AND tdel=" + model.TdelNotDel).Order("ctime DESC").FindAll()
if err != nil {
logger.Errorln("message service FindToMsgsByUid Error:", err)
return nil
}
uids := make(map[int]int)
ids := make([]int, 0, len(messages))
for _, message := range messages {
uids[message.From] = message.From
if message.Hasread == model.NotRead {
ids = append(ids, message.Id)
}
}
// 標記已讀
go MarkHasRead(ids, false, util.MustInt(uid))
userMap := getUserInfos(uids)
result := make([]map[string]interface{}, len(messages))
for i, message := range messages {
tmpMap := make(map[string]interface{})
util.Struct2Map(tmpMap, message)
tmpMap["user"] = userMap[message.From]
// 為了跟係統消息一致
tmpMap["title"] = "發來了一條消息:"
result[i] = tmpMap
}
return result
}
示例6: FindWiki
// 某個wiki頁麵詳細信息
func FindWiki(uri string) map[string]interface{} {
wiki := model.NewWiki()
if err := wiki.Where("uri=" + uri).Find(); err != nil {
logger.Errorln("wiki service FindWiki error:", err)
return nil
}
uids := make(map[int]int)
uids[wiki.Uid] = wiki.Uid
if wiki.Cuid != "" {
cuids := strings.Split(wiki.Cuid, ",")
for _, cuid := range cuids {
tmpUid := util.MustInt(cuid)
uids[tmpUid] = tmpUid
}
}
userMap := getUserInfos(uids)
result := make(map[string]interface{})
util.Struct2Map(result, wiki)
result["user"] = userMap[wiki.Uid]
if wiki.Cuid != "" {
cuids := strings.Split(wiki.Cuid, ",")
cusers := make([]*model.User, len(cuids))
for i, cuid := range cuids {
cusers[i] = userMap[util.MustInt(cuid)]
}
result["cuser"] = cusers
}
return result
}
示例7: FindTopicByTid
// 獲得帖子詳細信息(包括詳細回複)
// 為了避免轉換,tid傳string類型
func FindTopicByTid(tid string) (topicMap map[string]interface{}, replies []map[string]interface{}, err error) {
condition := "tid=" + tid
// 帖子信息
topic := model.NewTopic()
err = topic.Where(condition).Find()
if err != nil {
logger.Errorln("topic service FindTopicByTid Error:", err)
return
}
// 帖子不存在
if topic.Tid == 0 {
return
}
topicMap = make(map[string]interface{})
util.Struct2Map(topicMap, topic)
// 節點名字
topicMap["node"] = model.GetNodeName(topic.Nid)
// 回複信息(評論)
replies, owerUser, lastReplyUser := FindObjComments(tid, strconv.Itoa(model.TYPE_TOPIC), topic.Uid, topic.Lastreplyuid)
topicMap["user"] = owerUser
// 有人回複
if topic.Lastreplyuid != 0 {
topicMap["lastreplyusername"] = lastReplyUser.Username
}
return
}
示例8: FindObjComments
// 獲得某個對象的所有評論
// owner: 被評論對象屬主
// TODO:分頁暫不做
func FindObjComments(objid, objtype string, owner, lastCommentUid int /*, page, pageNum int*/) (comments []map[string]interface{}, ownerUser, lastReplyUser *model.User) {
commentList, err := model.NewComment().Where("objid=" + objid + " and objtype=" + objtype).FindAll()
if err != nil {
logger.Errorln("comment service FindObjComments Error:", err)
return
}
commentNum := len(commentList)
uids := make(map[int]int, commentNum+1)
uids[owner] = owner
// 避免某些情況下最後回複人沒在回複列表中
uids[lastCommentUid] = lastCommentUid
for _, comment := range commentList {
uids[comment.Uid] = comment.Uid
}
// 獲得用戶信息
userMap := getUserInfos(uids)
ownerUser = userMap[owner]
if lastCommentUid != 0 {
lastReplyUser = userMap[lastCommentUid]
}
comments = make([]map[string]interface{}, 0, commentNum)
for _, comment := range commentList {
tmpMap := make(map[string]interface{})
util.Struct2Map(tmpMap, comment)
tmpMap["content"] = decodeCmtContent(comment)
tmpMap["user"] = userMap[comment.Uid]
comments = append(comments, tmpMap)
}
return
}
示例9: FindObjComments
// 獲得某個對象的所有評論
// owner: 被評論對象屬主
// TODO:分頁暫不做
func FindObjComments(objid, objtype string, owner, lastCommentUid int /*, page, pageNum int*/) (comments []map[string]interface{}, ownerUser, lastReplyUser *model.User) {
commentList, err := model.NewComment().Where("objid=" + objid + " and objtype=" + objtype).FindAll()
if err != nil {
logger.Errorln("comment service FindObjComments Error:", err)
return
}
uids := util.Models2Intslice(commentList, "Uid")
// 避免某些情況下最後回複人沒在回複列表中
uids = append(uids, owner, lastCommentUid)
// 獲得用戶信息
userMap := GetUserInfos(uids)
ownerUser = userMap[owner]
if lastCommentUid != 0 {
lastReplyUser = userMap[lastCommentUid]
}
comments = make([]map[string]interface{}, 0, len(commentList))
for _, comment := range commentList {
tmpMap := make(map[string]interface{})
util.Struct2Map(tmpMap, comment)
tmpMap["content"] = template.HTML(decodeCmtContent(comment))
tmpMap["user"] = userMap[comment.Uid]
comments = append(comments, tmpMap)
}
return
}
示例10: FindResourcesByCatid
// 獲得某個分類的資源列表
func FindResourcesByCatid(catid string) []map[string]interface{} {
resourceList, err := model.NewResource().Where("catid=" + catid).Order("mtime DESC").FindAll()
if err != nil {
logger.Errorln("resource service FindResourcesByCatid error:", err)
return nil
}
count := len(resourceList)
ids := make([]int, count)
uids := make(map[int]int)
for i, resource := range resourceList {
ids[i] = resource.Id
uids[resource.Uid] = resource.Uid
}
// 獲取擴展信息(計數)
resourceExList, err := model.NewResourceEx().Where("id in(" + util.Join(ids, ",") + ")").FindAll()
if err != nil {
logger.Errorln("resource service FindResourcesByCatid Error:", err)
return nil
}
resourceExMap := make(map[int]*model.ResourceEx, len(resourceExList))
for _, resourceEx := range resourceExList {
resourceExMap[resourceEx.Id] = resourceEx
}
userMap := getUserInfos(uids)
resources := make([]map[string]interface{}, count)
for i, resource := range resourceList {
tmpMap := make(map[string]interface{})
util.Struct2Map(tmpMap, resource)
util.Struct2Map(tmpMap, resourceExMap[resource.Id])
tmpMap["user"] = userMap[resource.Uid]
// 鏈接的host
if resource.Form == model.LinkForm {
urlObj, err := url.Parse(resource.Url)
if err == nil {
tmpMap["host"] = urlObj.Host
}
} else {
tmpMap["url"] = "/resources/" + strconv.Itoa(resource.Id)
}
resources[i] = tmpMap
}
return resources
}
示例11: FindTopicsByWhere
func FindTopicsByWhere(where, order, limit string) (topics []map[string]interface{}, total int) {
topicObj := model.NewTopic()
if where != "" {
topicObj.Where(where)
}
if order != "" {
topicObj.Order(order)
}
if limit != "" {
topicObj.Limit(limit)
}
topicList, err := topicObj.FindAll()
if err != nil {
logger.Errorln("topic service topicObj.FindAll Error:", err)
return
}
// 獲得總帖子數
total, err = topicObj.Count()
if err != nil {
logger.Errorln("topic service topicObj.Count Error:", err)
return
}
count := len(topicList)
tids := make([]int, count)
uids := make(map[int]int)
nids := make([]int, count)
for i, topic := range topicList {
tids[i] = topic.Tid
uids[topic.Uid] = topic.Uid
if topic.Lastreplyuid != 0 {
uids[topic.Lastreplyuid] = topic.Lastreplyuid
}
nids[i] = topic.Nid
}
userMap := getUserInfos(uids)
// 獲取節點信息
nodes := model.GetNodesName(nids)
topics = make([]map[string]interface{}, count)
for i, topic := range topicList {
tmpMap := make(map[string]interface{})
util.Struct2Map(tmpMap, topic)
tmpMap["user"] = userMap[topic.Uid]
// 有人回複
if tmpMap["lastreplyuid"].(int) != 0 {
tmpMap["lastreplyusername"] = userMap[tmpMap["lastreplyuid"].(int)].Username
}
tmpMap["node"] = nodes[tmpMap["nid"].(int)]
topics[i] = tmpMap
}
return
}
示例12: FindFromMsgsByUid
// 獲取某人發送的消息
func FindFromMsgsByUid(uid string) []map[string]interface{} {
messages, err := model.NewMessage().Where("from=" + uid + " AND fdel=" + model.FdelNotDel).Order("ctime DESC").FindAll()
if err != nil {
logger.Errorln("message service FindFromMsgsByUid Error:", err)
return nil
}
uids := make(map[int]int)
for _, message := range messages {
uids[message.To] = message.To
}
userMap := getUserInfos(uids)
result := make([]map[string]interface{}, len(messages))
for i, message := range messages {
tmpMap := make(map[string]interface{})
util.Struct2Map(tmpMap, message)
tmpMap["user"] = userMap[message.To]
result[i] = tmpMap
}
return result
}
示例13: FindRecentResources
// 獲得最新資源
func FindRecentResources() []map[string]interface{} {
resourceList, err := model.NewResource().Limit("0,10").Order("mtime DESC").FindAll()
if err != nil {
logger.Errorln("resource service FindRecentResources error:", err)
return nil
}
uids := util.Models2Intslice(resourceList, "Uid")
userMap := GetUserInfos(uids)
count := len(resourceList)
resources := make([]map[string]interface{}, count)
for i, resource := range resourceList {
tmpMap := make(map[string]interface{})
util.Struct2Map(tmpMap, resource)
tmpMap["user"] = userMap[resource.Uid]
resources[i] = tmpMap
}
return resources
}
示例14: FindHotTopics
// 獲得回複最多的10條帖子(TODO:避免一直顯示相同的)
func FindHotTopics() []map[string]interface{} {
topics, err := model.NewTopic().Order("reply DESC").Limit("0,10").FindAll()
if err != nil {
logger.Errorln("topic service FindHotReplies error:", err)
return nil
}
uidMap := make(map[int]int, len(topics))
for _, topic := range topics {
uidMap[topic.Uid] = topic.Uid
}
userMap := getUserInfos(uidMap)
result := make([]map[string]interface{}, len(topics))
for i, topic := range topics {
oneTopic := make(map[string]interface{})
util.Struct2Map(oneTopic, topic)
oneTopic["user"] = userMap[topic.Uid]
result[i] = oneTopic
}
return result
}
示例15: FindResourcesByCatid
// 獲得某個分類的資源列表
// page 當前第幾頁
func FindResourcesByCatid(catid string, page int) (resources []map[string]interface{}, total int) {
var offset = 0
if page > 1 {
offset = (page - 1) * PAGE_NUM
}
resourceObj := model.NewResource()
limit := strconv.Itoa(offset) + "," + strconv.Itoa(PAGE_NUM)
resourceList, err := resourceObj.Where("catid=?", catid).Order("mtime DESC").Limit(limit).FindAll()
if err != nil {
logger.Errorln("resource service FindResourcesByCatid error:", err)
return
}
// 獲得該類別總資源數
total, err = resourceObj.Count()
if err != nil {
logger.Errorln("resource service resourceObj.Count Error:", err)
return
}
count := len(resourceList)
ids := make([]int, count)
uids := make([]int, count)
for i, resource := range resourceList {
ids[i] = resource.Id
uids[i] = resource.Uid
}
// 獲取擴展信息(計數)
resourceExList, err := model.NewResourceEx().Where("id in(" + util.Join(ids, ",") + ")").FindAll()
if err != nil {
logger.Errorln("resource service FindResourcesByCatid Error:", err)
return
}
resourceExMap := make(map[int]*model.ResourceEx, len(resourceExList))
for _, resourceEx := range resourceExList {
resourceExMap[resourceEx.Id] = resourceEx
}
userMap := GetUserInfos(uids)
resources = make([]map[string]interface{}, count)
for i, resource := range resourceList {
tmpMap := make(map[string]interface{})
util.Struct2Map(tmpMap, resource)
util.Struct2Map(tmpMap, resourceExMap[resource.Id])
tmpMap["user"] = userMap[resource.Uid]
// 鏈接的host
if resource.Form == model.LinkForm {
urlObj, err := url.Parse(resource.Url)
if err == nil {
tmpMap["host"] = urlObj.Host
}
} else {
tmpMap["url"] = "/resources/" + strconv.Itoa(resource.Id)
}
resources[i] = tmpMap
}
return
}