tools/cmd
Rob Pike 2c5c896732 go.tools/cmd/stringer: new tool
This tool creates String methods from constant definitions.
It's a time-saver designed to be used from go generate.
The methods generated are efficient, more so than one
is likely to create by hand.

Given

        package date

        type Day int

        const (
                Monday Day = iota
                Tuesday
                Wednesday
                Thursday
                Friday
                Saturday
                Sunday
        )

the command

        stringer -type Day

will create the file day_string.go containing

        package date

        import "fmt"

        var (
                _Day_indexes = []uint8{6, 13, 22, 30, 36, 44, 50}
                _Day_names   = "MondayTuesdayWednesdayThursdayFridaySaturdaySunday"
        )

        func (i Day) String() string {
                if i < 0 || i >= Day(len(_Day_indexes)) {
                        return fmt.Sprintf("Day(%d)", i)
                }
                hi := _Day_indexes[i]
                lo := uint8(0)
                if i > 0 {
                        lo = _Day_indexes[i-1]
                }
                return _Day_names[lo:hi]
        }

There are several strategies for the created method chosen according to
the structure of the sequence of constants.

Handles integer types only, both signed and unsigned. That's probably
all that is needed.

Tests to follow, but the test structure will be large so sending this out
separately. The code has been heavily hand-tested but there are
some bugs. Don't depend on this until the tests are installed.

LGTM=gri
R=gri, josharian
CC=golang-codereviews
https://golang.org/cl/136180043
2014-09-03 17:27:48 -07:00
..
benchcmp benchcmp: Fix invocation instructions. 2014-07-22 17:50:39 +10:00
cover cmd/cover: add start lines numbers to each function in -func mode 2014-08-21 16:28:12 -07:00
eg cmd/eg: add -beforeedit flag, which specifies a command to run before editing a file. 2014-08-26 15:52:40 -04:00
godex go.tools/go/gccgoimporter: keep track of package and import priority 2014-06-17 10:56:47 -07:00
godoc go.tools/cmd/godoc: register redirect handler for /dl/ 2014-06-18 15:44:09 +10:00
goimports go.tools/cmd/goimports: update doc.go to new emacs instructions 2014-07-01 10:24:22 -07:00
gotype go.tools/cmd/gotype: fix call site (fix build) 2014-06-17 11:06:19 -07:00
html2article html2article: Ignore empty anchor elements and obtain the original url from Google Docs 2014-08-19 17:55:46 -07:00
oracle go.tools: rename packages. 2014-01-16 09:33:58 -05:00
present go.tools/cmd/present: track page views for every slide. 2014-08-05 16:02:39 -07:00
ssadump go.tools/cmd/ssadump: print packages and functions to stdout, not stderr. 2014-08-01 14:44:37 -04:00
stringer go.tools/cmd/stringer: new tool 2014-09-03 17:27:48 -07:00
vet cmd/vet: simplify file-reading code per gri 2014-09-03 16:49:14 -07:00