diff --git a/slicewriter.go b/slicewriter.go index 9b10028..95ad63a 100644 --- a/slicewriter.go +++ b/slicewriter.go @@ -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. @@ -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 @@ -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) { diff --git a/sortimplementation.go b/sortimplementation.go index 1b2bf77..fffb15e 100644 --- a/sortimplementation.go +++ b/sortimplementation.go @@ -177,4 +177,6 @@ func quickSort{{.SliceName}}(rcv {{.SliceName}}, less func({{.Type}}, {{.Type}}) insertionSort{{.SliceName}}(rcv, less, a, b) } } -`} +`, + TypeConstraint: typewriter.Constraint{Ordered: true}, +} diff --git a/sortinterface.go b/sortinterface.go index a267df4..4f6f089 100644 --- a/sortinterface.go +++ b/sortinterface.go @@ -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}, +}