Преглед изворни кода

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.
Andrey Nering пре 9 година
родитељ
комит
81ed5c4bee
2 измењених фајлова са 25 додато и 18 уклоњено
  1. 21 14
      models/git_diff.go
  2. 4 4
      templates/repo/diff_box.tmpl

+ 21 - 14
models/git_diff.go

@@ -28,31 +28,34 @@ import (
 	"github.com/sergi/go-diff/diffmatchpatch"
 )
 
-// Diff line types.
+type DiffLineType uint8
+
 const (
-	DIFF_LINE_PLAIN = iota + 1
-	DIFF_LINE_ADD
-	DIFF_LINE_DEL
-	DIFF_LINE_SECTION
+	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 = iota + 1
-	DIFF_FILE_CHANGE
-	DIFF_FILE_DEL
-	DIFF_FILE_RENAME
+	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

+ 4 - 4
templates/repo/diff_box.tmpl

@@ -26,7 +26,7 @@
 						{{end}}
 					</div>
 					<!-- todo finish all file status, now modify, add, delete and rename -->
-					<span class="status {{DiffTypeToStr .Type}} poping up" data-content="{{DiffTypeToStr .Type}}" data-variation="inverted tiny" data-position="right center">&nbsp;</span>
+					<span class="status {{DiffTypeToStr .GetType}} poping up" data-content="{{DiffTypeToStr .GetType}}" data-variation="inverted tiny" data-position="right center">&nbsp;</span>
 					<a class="file" href="#diff-{{.Index}}">{{.Name}}</a>
 				</li>
 			{{end}}
@@ -71,7 +71,7 @@
 									{{if $.IsSplitStyle}}
 										{{range $j, $section := .Sections}}
 											{{range $k, $line := .Lines}}
-												<tr class="{{DiffLineTypeToStr .Type}}-code nl-{{$k}} ol-{{$k}}">
+												<tr class="{{DiffLineTypeToStr .GetType}}-code nl-{{$k}} ol-{{$k}}">
 													<td class="lines-num lines-num-old">
 														<span rel="{{if $line.LeftIdx}}diff-{{Sha1 $file.Name}}L{{$line.LeftIdx}}{{end}}">{{if $line.LeftIdx}}{{$line.LeftIdx}}{{end}}</span>
 													</td>
@@ -90,8 +90,8 @@
 									{{else}}
 										{{range $j, $section := .Sections}}
 											{{range $k, $line := .Lines}}
-												<tr class="{{DiffLineTypeToStr .Type}}-code nl-{{$k}} ol-{{$k}}">
-													{{if eq .Type 4}}
+												<tr class="{{DiffLineTypeToStr .GetType}}-code nl-{{$k}} ol-{{$k}}">
+													{{if eq .GetType 4}}
 													<td colspan="2" class="lines-num">
 														{{if gt $j 0}}<span class="fold octicon octicon-fold"></span>{{end}}
 													</td>