Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions slicewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ func (sw *SliceWriter) Write(w io.Writer, typ typewriter.Type) error {
return nil
}

tag.AddDefaultsIfNeeded(typ, templates[1:])

if includeSortImplementation(tag.Values) {
s := `// Sort implementation is a modification of http://golang.org/pkg/sort/#Sort
// Copyright 2009 The Go Authors. All rights reserved.
Expand Down Expand Up @@ -120,6 +122,13 @@ func (sw *SliceWriter) Write(w io.Writer, typ typewriter.Type) error {
}

func includeSortImplementation(values []typewriter.TagValue) bool {
// Don't include it if it's already included.
for _, v := range values {
if v.Name == "sortImplementation" {
return false
}
}

for _, v := range values {
if strings.HasPrefix(v.Name, "SortBy") {
return true
Expand All @@ -129,6 +138,13 @@ func includeSortImplementation(values []typewriter.TagValue) bool {
}

func includeSortInterface(values []typewriter.TagValue) bool {
// Don't include it if it's already included.
for _, v := range values {
if v.Name == "sortInterface" {
return false
}
}

reg := regexp.MustCompile(`^Sort(Desc)?$`)
for _, v := range values {
if reg.MatchString(v.Name) {
Expand Down
4 changes: 3 additions & 1 deletion sortimplementation.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,6 @@ func quickSort{{.SliceName}}(rcv {{.SliceName}}, less func({{.Type}}, {{.Type}})
insertionSort{{.SliceName}}(rcv, less, a, b)
}
}
`}
`,
TypeConstraint: typewriter.Constraint{Ordered: true},
}
4 changes: 3 additions & 1 deletion sortinterface.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ func (rcv {{.SliceName}}) Less(i, j int) bool {
func (rcv {{.SliceName}}) Swap(i, j int) {
rcv[i], rcv[j] = rcv[j], rcv[i]
}
`}
`,
TypeConstraint: typewriter.Constraint{Ordered: true},
}