Introducing CodeComments as type for map[string]map[int64][]*Comment

Other minor code improvements

Signed-off-by: Jonas Franz <info@jonasfranz.software>
This commit is contained in:
Jonas Franz 2018-07-24 13:03:46 +02:00
parent cb29fdb201
commit 021f0288c0
No known key found for this signature in database
GPG Key ID: 506AEEBE80BEDECD
3 changed files with 18 additions and 19 deletions

View File

@ -193,6 +193,7 @@ func (c *Comment) HTMLURL() string {
}
if c.Review == nil {
if err := c.LoadReview(); err != nil {
log.Warn("LoadReview(%d): %v", c.ReviewID, err)
return fmt.Sprintf("%s/files#%s", c.Issue.HTMLURL(), c.HashTag())
}
}
@ -1061,12 +1062,15 @@ func DeleteComment(doer *User, comment *Comment) error {
return nil
}
func fetchCodeComments(e Engine, issue *Issue, currentUser *User) (map[string]map[int64][]*Comment, error) {
// CodeComments represents comments on code by using this structure: FILENAME -> LINE (+ == proposed; - == previous) -> COMMENTS
type CodeComments map[string]map[int64][]*Comment
func fetchCodeComments(e Engine, issue *Issue, currentUser *User) (CodeComments, error) {
return fetchCodeCommentsByReview(e, issue, currentUser, nil)
}
func fetchCodeCommentsByReview(e Engine, issue *Issue, currentUser *User, review *Review) (map[string]map[int64][]*Comment, error) {
pathToLineToComment := make(map[string]map[int64][]*Comment)
func fetchCodeCommentsByReview(e Engine, issue *Issue, currentUser *User, review *Review) (CodeComments, error) {
pathToLineToComment := make(CodeComments)
if review == nil {
review = &Review{ID: 0}
}
@ -1126,6 +1130,6 @@ func fetchCodeCommentsByReview(e Engine, issue *Issue, currentUser *User, review
}
// FetchCodeComments will return a 2d-map: ["Path"]["Line"] = Comments at line
func FetchCodeComments(issue *Issue, currentUser *User) (map[string]map[int64][]*Comment, error) {
func FetchCodeComments(issue *Issue, currentUser *User) (CodeComments, error) {
return fetchCodeComments(x, issue, currentUser)
}

View File

@ -60,7 +60,7 @@ type Review struct {
UpdatedUnix util.TimeStamp `xorm:"INDEX updated"`
// CodeComments are the initial code comments of the review
CodeComments map[string]map[int64][]*Comment `xorm:"-"`
CodeComments CodeComments `xorm:"-"`
}
func (r *Review) loadCodeComments(e Engine) (err error) {

View File

@ -15,7 +15,6 @@ import (
"mime"
"net/url"
"path/filepath"
"reflect"
"runtime"
"strings"
"time"
@ -190,7 +189,6 @@ func NewFuncMap() []template.FuncMap {
"DefaultTheme": func() string {
return setting.UI.DefaultTheme
},
"mul": func(first int, second int64) int64 { return second * int64(first) },
"dict": func(values ...interface{}) (map[string]interface{}, error) {
if len(values) == 0 {
return nil, errors.New("invalid dict call")
@ -199,24 +197,21 @@ func NewFuncMap() []template.FuncMap {
dict := make(map[string]interface{})
for i := 0; i < len(values); i++ {
key, isset := values[i].(string)
if !isset {
if reflect.TypeOf(values[i]).Kind() == reflect.Map {
m := values[i].(map[string]interface{})
for i, v := range m {
dict[i] = v
}
} else {
return nil, errors.New("dict values must be maps")
}
} else {
switch key := values[i].(type) {
case string:
i++
if i == len(values) {
return nil, errors.New("specify the key for non array values")
}
dict[key] = values[i]
case map[string]interface{}:
m := values[i].(map[string]interface{})
for i, v := range m {
dict[i] = v
}
default:
return nil, errors.New("dict values must be maps")
}
}
return dict, nil
},