github.com/vnforks/kid/v5@v5.22.1-0.20200408055009-b89d99c65676/model/class_count.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package model 5 6 import ( 7 "crypto/md5" 8 "encoding/json" 9 "fmt" 10 "io" 11 "sort" 12 "strconv" 13 ) 14 15 type ClassCounts struct { 16 Counts map[string]int64 `json:"counts"` 17 UpdateTimes map[string]int64 `json:"update_times"` 18 } 19 20 func (o *ClassCounts) Etag() string { 21 22 ids := []string{} 23 for id := range o.Counts { 24 ids = append(ids, id) 25 } 26 sort.Strings(ids) 27 28 str := "" 29 for _, id := range ids { 30 str += id + strconv.FormatInt(o.Counts[id], 10) 31 } 32 33 md5Counts := fmt.Sprintf("%x", md5.Sum([]byte(str))) 34 35 var update int64 = 0 36 for _, u := range o.UpdateTimes { 37 if u > update { 38 update = u 39 } 40 } 41 42 return Etag(md5Counts, update) 43 } 44 45 func (o *ClassCounts) ToJson() string { 46 b, _ := json.Marshal(o) 47 return string(b) 48 } 49 50 func ClassCountsFromJson(data io.Reader) *ClassCounts { 51 var o *ClassCounts 52 json.NewDecoder(data).Decode(&o) 53 return o 54 }