From 69ffe616475e0e95c207d55d8fd3a7943ee5da38 Mon Sep 17 00:00:00 2001 From: Issac Trotts Date: Tue, 1 Mar 2016 13:10:49 -0800 Subject: [PATCH 1/3] Add support for default methods Default methods are included if "*" is in the tag or if there is no tag. --- slicewriter.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/slicewriter.go b/slicewriter.go index 9b10028..ac3243a 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) + 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) { From 3999aae9dda62db712bc372ff5f29ddf49d6c9e4 Mon Sep 17 00:00:00 2001 From: Issac Trotts Date: Tue, 1 Mar 2016 16:29:27 -0800 Subject: [PATCH 2/3] Add missing Ordered type constraint --- sortimplementation.go | 4 +++- sortinterface.go | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) 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}, +} From 72fb7206c611b1fdb2fe7fd10b10f4b5fe19315d Mon Sep 17 00:00:00 2001 From: Issac Trotts Date: Tue, 1 Mar 2016 17:19:06 -0800 Subject: [PATCH 3/3] Bugfix: Omit slice tmpl when generating defaults --- slicewriter.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/slicewriter.go b/slicewriter.go index ac3243a..95ad63a 100644 --- a/slicewriter.go +++ b/slicewriter.go @@ -41,7 +41,7 @@ func (sw *SliceWriter) Write(w io.Writer, typ typewriter.Type) error { return nil } - tag.AddDefaultsIfNeeded(typ, templates) + tag.AddDefaultsIfNeeded(typ, templates[1:]) if includeSortImplementation(tag.Values) { s := `// Sort implementation is a modification of http://golang.org/pkg/sort/#Sort