Преглед на файлове

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"
 	"github.com/sergi/go-diff/diffmatchpatch"
 )
 )
 
 
-// Diff line types.
+type DiffLineType uint8
+
 const (
 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 (
 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 {
 type DiffLine struct {
 	LeftIdx  int
 	LeftIdx  int
 	RightIdx int
 	RightIdx int
-	Type     int
+	Type     DiffLineType
 	Content  string
 	Content  string
 	ParsedContent template.HTML
 	ParsedContent template.HTML
 }
 }
 
 
-func (d DiffLine) GetType() int {
-	return d.Type
+func (d *DiffLine) GetType() int {
+	return int(d.Type)
 }
 }
 
 
 type DiffSection struct {
 type DiffSection struct {
@@ -60,7 +63,7 @@ type DiffSection struct {
 	Lines []*DiffLine
 	Lines []*DiffLine
 }
 }
 
 
-func diffToHtml(diffRecord []diffmatchpatch.Diff, lineType int) template.HTML {
+func diffToHtml(diffRecord []diffmatchpatch.Diff, lineType DiffLineType) template.HTML {
 	result := ""
 	result := ""
 	for _, s := range diffRecord {
 	for _, s := range diffRecord {
 		if s.Type == diffmatchpatch.DiffInsert && lineType == DIFF_LINE_ADD {
 		if s.Type == diffmatchpatch.DiffInsert && lineType == DIFF_LINE_ADD {
@@ -146,7 +149,7 @@ type DiffFile struct {
 	OldName            string
 	OldName            string
 	Index              int
 	Index              int
 	Addition, Deletion int
 	Addition, Deletion int
-	Type               int
+	Type               DiffFileType
 	IsCreated          bool
 	IsCreated          bool
 	IsDeleted          bool
 	IsDeleted          bool
 	IsBin              bool
 	IsBin              bool
@@ -154,6 +157,10 @@ type DiffFile struct {
 	Sections           []*DiffSection
 	Sections           []*DiffSection
 }
 }
 
 
+func (diffFile *DiffFile) GetType() int {
+	return int(diffFile.Type)
+}
+
 type Diff struct {
 type Diff struct {
 	TotalAddition, TotalDeletion int
 	TotalAddition, TotalDeletion int
 	Files                        []*DiffFile
 	Files                        []*DiffFile

+ 4 - 4
templates/repo/diff_box.tmpl

@@ -26,7 +26,7 @@
 						{{end}}
 						{{end}}
 					</div>
 					</div>
 					<!-- todo finish all file status, now modify, add, delete and rename -->
 					<!-- 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>
 					<a class="file" href="#diff-{{.Index}}">{{.Name}}</a>
 				</li>
 				</li>
 			{{end}}
 			{{end}}
@@ -71,7 +71,7 @@
 									{{if $.IsSplitStyle}}
 									{{if $.IsSplitStyle}}
 										{{range $j, $section := .Sections}}
 										{{range $j, $section := .Sections}}
 											{{range $k, $line := .Lines}}
 											{{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">
 													<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>
 														<span rel="{{if $line.LeftIdx}}diff-{{Sha1 $file.Name}}L{{$line.LeftIdx}}{{end}}">{{if $line.LeftIdx}}{{$line.LeftIdx}}{{end}}</span>
 													</td>
 													</td>
@@ -90,8 +90,8 @@
 									{{else}}
 									{{else}}
 										{{range $j, $section := .Sections}}
 										{{range $j, $section := .Sections}}
 											{{range $k, $line := .Lines}}
 											{{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">
 													<td colspan="2" class="lines-num">
 														{{if gt $j 0}}<span class="fold octicon octicon-fold"></span>{{end}}
 														{{if gt $j 0}}<span class="fold octicon octicon-fold"></span>{{end}}
 													</td>
 													</td>