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.
This commit is contained in:
Andrey Nering 2016-01-06 18:00:40 -02:00
parent 73474c043b
commit 81ed5c4bee
2 changed files with 27 additions and 20 deletions

View File

@ -28,31 +28,34 @@ import (
"github.com/sergi/go-diff/diffmatchpatch" "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
)
const ( const (
DIFF_FILE_ADD = iota + 1 DIFF_LINE_PLAIN DiffLineType = iota + 1
DIFF_FILE_CHANGE DIFF_LINE_ADD DiffLineType = iota + 1
DIFF_FILE_DEL DIFF_LINE_DEL DiffLineType = iota + 1
DIFF_FILE_RENAME 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 { 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 { func (d *DiffLine) GetType() int {
return d.Type 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

View File

@ -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}}"> <tr class="{{DiffLineTypeToStr .GetType}}-code nl-{{$k}} ol-{{$k}}">
{{if eq .Type 4}} {{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>