From 81ed5c4bee112beacd8e0f34b336b4f3b6c6a6c1 Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Wed, 6 Jan 2016 18:00:40 -0200 Subject: [PATCH] Declaring specific types for enums constants. This makes the code more strict since you can't assign or compare values of different types without proper cast. --- models/git_diff.go | 39 +++++++++++++++++++++--------------- templates/repo/diff_box.tmpl | 8 ++++---- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/models/git_diff.go b/models/git_diff.go index 9656813bf8..2b9bda2531 100644 --- a/models/git_diff.go +++ b/models/git_diff.go @@ -28,31 +28,34 @@ import ( "github.com/sergi/go-diff/diffmatchpatch" ) -// Diff line types. -const ( - DIFF_LINE_PLAIN = iota + 1 - DIFF_LINE_ADD - DIFF_LINE_DEL - DIFF_LINE_SECTION -) +type DiffLineType uint8 const ( - DIFF_FILE_ADD = iota + 1 - DIFF_FILE_CHANGE - DIFF_FILE_DEL - DIFF_FILE_RENAME + DIFF_LINE_PLAIN DiffLineType = iota + 1 + DIFF_LINE_ADD DiffLineType = iota + 1 + DIFF_LINE_DEL DiffLineType = iota + 1 + DIFF_LINE_SECTION DiffLineType = iota + 1 +) + +type DiffFileType uint8 + +const ( + DIFF_FILE_ADD DiffFileType = iota + 1 + DIFF_FILE_CHANGE DiffFileType = iota + 1 + DIFF_FILE_DEL DiffFileType = iota + 1 + DIFF_FILE_RENAME DiffFileType = iota + 1 ) type DiffLine struct { LeftIdx int RightIdx int - Type int + Type DiffLineType Content string ParsedContent template.HTML } -func (d DiffLine) GetType() int { - return d.Type +func (d *DiffLine) GetType() int { + return int(d.Type) } type DiffSection struct { @@ -60,7 +63,7 @@ type DiffSection struct { Lines []*DiffLine } -func diffToHtml(diffRecord []diffmatchpatch.Diff, lineType int) template.HTML { +func diffToHtml(diffRecord []diffmatchpatch.Diff, lineType DiffLineType) template.HTML { result := "" for _, s := range diffRecord { if s.Type == diffmatchpatch.DiffInsert && lineType == DIFF_LINE_ADD { @@ -146,7 +149,7 @@ type DiffFile struct { OldName string Index int Addition, Deletion int - Type int + Type DiffFileType IsCreated bool IsDeleted bool IsBin bool @@ -154,6 +157,10 @@ type DiffFile struct { Sections []*DiffSection } +func (diffFile *DiffFile) GetType() int { + return int(diffFile.Type) +} + type Diff struct { TotalAddition, TotalDeletion int Files []*DiffFile diff --git a/templates/repo/diff_box.tmpl b/templates/repo/diff_box.tmpl index f7851822f0..a3245d1c2b 100644 --- a/templates/repo/diff_box.tmpl +++ b/templates/repo/diff_box.tmpl @@ -26,7 +26,7 @@ {{end}} -   +   {{.Name}} {{end}} @@ -71,7 +71,7 @@ {{if $.IsSplitStyle}} {{range $j, $section := .Sections}} {{range $k, $line := .Lines}} - + {{if $line.LeftIdx}}{{$line.LeftIdx}}{{end}} @@ -90,8 +90,8 @@ {{else}} {{range $j, $section := .Sections}} {{range $k, $line := .Lines}} - - {{if eq .Type 4}} + + {{if eq .GetType 4}} {{if gt $j 0}}{{end}}