forked from mirror/enumer
Compare commits
27 Commits
dan/testin
...
master
Author | SHA1 | Date |
---|---|---|
re | e3c2a6fbb2 | |
Dan Markham | ff555f73ed | |
moritamori | ed2895e2a7 | |
Dan Markham | 71d3edd22d | |
Eduardo Solis | 49441931ac | |
Eduardo Solis | f8c85a7a99 | |
Dan Markham | 6e1910cc4f | |
Eduardo Solis | e79db5ce74 | |
Dan Markham | 0f231a24f5 | |
urso | 98641f4fd2 | |
Dan Markham | aa45c0d477 | |
Scott Leggett | df780b2a0e | |
Dan Markham | caff60ea13 | |
Dan Markham | 881cbb24bf | |
Dan Markham | b43356ef4a | |
Dan Markham | 3bf002e2f3 | |
Dan Markham | 28a1efd624 | |
Nir Haas | af60bb2988 | |
mieubrisse | a1605fba51 | |
Dan Markham | 585d6b237e | |
Menno | 67b1febc1d | |
Menno | 074b95bab8 | |
Menno | f80c486834 | |
Dan Markham | 328a338ddb | |
Dan Markham | 367f14bcb1 | |
Dan Markham | 9d0b7d4c67 | |
Dan Markham | b85f109b7f |
|
@ -4,9 +4,8 @@ matrix:
|
|||
allow_failures:
|
||||
- go: master
|
||||
include:
|
||||
- go: 1.14.x
|
||||
- go: 1.15.x
|
||||
- go: master
|
||||
- go: 1.17.x
|
||||
- go: 1.18.x
|
||||
env:
|
||||
global:
|
||||
- GO111MODULE=on
|
||||
|
@ -21,9 +20,10 @@ deploy:
|
|||
file:
|
||||
- cli/build/enumer.linux-amd64.tar.gz
|
||||
- cli/build/enumer.darwin-amd64.tar.gz
|
||||
- cli/build/enumer.darwin-arm64.tar.gz
|
||||
- cli/build/enumer.windows-amd64.exe.tar.gz
|
||||
- cli/build/sha256sum.txt
|
||||
on:
|
||||
go: 1.15.x
|
||||
go: 1.17.x
|
||||
repo: dmarkham/enumer
|
||||
tags: true
|
||||
|
|
1
Makefile
1
Makefile
|
@ -7,6 +7,7 @@ build-cli: clean
|
|||
-mkdir -p ./cli/build
|
||||
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o ./cli/build/enumer.linux-amd64 .
|
||||
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -a -o ./cli/build/enumer.darwin-amd64 .
|
||||
CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -a -o ./cli/build/enumer.darwin-arm64 .
|
||||
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -a -o ./cli/build/enumer.windows-amd64.exe .
|
||||
cd ./cli/build && find . -name 'enumer*' | xargs -I{} tar czf {}.tar.gz {}
|
||||
cd ./cli/build && shasum -a 256 * > sha256sum.txt
|
||||
|
|
16
README.md
16
README.md
|
@ -1,10 +1,10 @@
|
|||
# Enumer [![GoDoc](https://godoc.org/github.com/dmarkham/enumer?status.svg)](https://godoc.org/github.com/dmarkham/enumer) [![Go Report Card](https://goreportcard.com/badge/github.com/dmarkham/enumer)](https://goreportcard.com/report/github.com/dmarkham/enumer) [![GitHub Release](https://img.shields.io/github/release/dmarkham/enumer.svg)](https://github.com/dmarkham/enumer/releases)[![Build Status](https://travis-ci.org/dmarkham/enumer.svg?branch=master)](https://travis-ci.org/dmarkham/enumer)
|
||||
# Enumer [![GoDoc](https://godoc.org/git.internal/re/enumer?status.svg)](https://godoc.org/git.internal/re/enumer) [![Go Report Card](https://goreportcard.com/badge/git.internal/re/enumer)](https://goreportcard.com/report/git.internal/re/enumer) [![GitHub Release](https://img.shields.io/github/release/dmarkham/enumer.svg)](https://git.internal/re/enumer/releases)[![Build Status](https://travis-ci.com/dmarkham/enumer.svg?branch=master)](https://travis-ci.com/dmarkham/enumer)
|
||||
|
||||
|
||||
Enumer is a tool to generate Go code that adds useful methods to Go enums (constants with a specific type).
|
||||
It started as a fork of [Rob Pike’s Stringer tool](https://godoc.org/golang.org/x/tools/cmd/stringer)
|
||||
maintained by [Álvaro López Espinosa](https://github.com/alvaroloes/enumer).
|
||||
This was again forked here as (https://github.com/dmarkham/enumer) picking up where Álvaro left off.
|
||||
This was again forked here as (https://git.internal/re/enumer) picking up where Álvaro left off.
|
||||
|
||||
|
||||
```
|
||||
|
@ -14,7 +14,7 @@ Usage of ./enumer:
|
|||
Enumer [flags] -type T [directory]
|
||||
Enumer [flags] -type T files... # Must be a single package
|
||||
For more information, see:
|
||||
http://godoc.org/github.com/dmarkham/enumer
|
||||
http://godoc.org/git.internal/re/enumer
|
||||
Flags:
|
||||
-addprefix string
|
||||
transform each item name by adding a prefix. Default: ""
|
||||
|
@ -38,6 +38,8 @@ Flags:
|
|||
transform each item name by removing a prefix. Default: ""
|
||||
-type string
|
||||
comma-separated list of type names; must be set
|
||||
-values
|
||||
if true, alternative string values method will be generated. Default: false
|
||||
-yaml
|
||||
if true, yaml marshaling methods will be generated. Default: false
|
||||
```
|
||||
|
@ -193,6 +195,12 @@ name := MyTypeValue.String() // name => "my_type_value"
|
|||
|
||||
## How to use
|
||||
|
||||
For a module-aware repo with `enumer` in the `go.mod` file, generation can be called by adding the following to a `.go` source file:
|
||||
|
||||
```golang
|
||||
//go:generate go run git.internal/re/enumer -type=YOURTYPE
|
||||
```
|
||||
|
||||
There are four boolean flags: `json`, `text`, `yaml` and `sql`. You can use any combination of them (i.e. `enumer -type=Pill -json -text`),
|
||||
|
||||
For enum string representation transformation the `transform` and `trimprefix` flags
|
||||
|
@ -205,6 +213,8 @@ it is transformed). If a name doesn't have the prefix it will be passed unchange
|
|||
|
||||
If a prefix is provided via the `addprefix` flag, it will be added to the start of each name (after trimming and after transforming).
|
||||
|
||||
The boolean flag `values` will additionally create an alternative string values method `Values() []string` to fullfill the `EnumValues` interface of [ent](https://entgo.io/docs/schema-fields/#enum-fields).
|
||||
|
||||
## Inspiring projects
|
||||
|
||||
- [Álvaro López Espinosa](https://github.com/alvaroloes/enumer)
|
||||
|
|
16
enumer.go
16
enumer.go
|
@ -10,8 +10,8 @@ func %[1]sString(s string) (%[1]s, error) {
|
|||
if val, ok := _%[1]sNameToValueMap[s]; ok {
|
||||
return val, nil
|
||||
}
|
||||
s = strings.ToLower(s)
|
||||
if val, ok := _%[1]sNameToValueMap[s]; ok {
|
||||
|
||||
if val, ok := _%[1]sNameToValueMap[strings.ToLower(s)]; ok {
|
||||
return val, nil
|
||||
}
|
||||
return 0, fmt.Errorf("%%s does not belong to %[1]s values", s)
|
||||
|
@ -58,6 +58,18 @@ func (i %[1]s) IsA%[1]s() bool {
|
|||
}
|
||||
`
|
||||
|
||||
// Arguments to format are:
|
||||
// [1]: type name
|
||||
const altStringValuesMethod = `func (%[1]s) Values() []string {
|
||||
return %[1]sStrings()
|
||||
}
|
||||
`
|
||||
|
||||
func (g *Generator) buildAltStringValuesMethod(typeName string) {
|
||||
g.Printf("\n")
|
||||
g.Printf(altStringValuesMethod, typeName)
|
||||
}
|
||||
|
||||
func (g *Generator) buildBasicExtras(runs [][]Value, typeName string, runsThreshold int) {
|
||||
// At this moment, either "g.declareIndexAndNameVars()" or "g.declareNameVars()" has been called
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
## Steps
|
||||
|
||||
1. Go get enumer `go get -u github.com/dmarkham/enumer`
|
||||
1. Go get enumer `go get -u git.internal/re/enumer`
|
||||
2. `go generate` This should create `pill_enumer.go`
|
||||
3. `go run *.go` to see it in action
|
||||
4. `go mod tidy` to remove the deps for `enumer` once your happy.
|
||||
|
|
|
@ -2,7 +2,7 @@ package main
|
|||
|
||||
import "fmt"
|
||||
|
||||
//go:generate go run github.com/dmarkham/enumer -type=Pill -json
|
||||
//go:generate go run git.internal/re/enumer -type=Pill -json
|
||||
type Pill int
|
||||
|
||||
const (
|
||||
|
|
11
go.mod
11
go.mod
|
@ -1,8 +1,13 @@
|
|||
module github.com/dmarkham/enumer
|
||||
module git.internal/re/enumer
|
||||
|
||||
require (
|
||||
github.com/pascaldekloe/name v1.0.0
|
||||
golang.org/x/tools v0.0.0-20200221224223-e1da425f72fd
|
||||
golang.org/x/tools v0.1.12
|
||||
)
|
||||
|
||||
go 1.14
|
||||
require (
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
|
||||
)
|
||||
|
||||
go 1.17
|
||||
|
|
30
go.sum
30
go.sum
|
@ -1,16 +1,28 @@
|
|||
github.com/pascaldekloe/name v1.0.0 h1:n7LKFgHixETzxpRv2R77YgPUFo85QHGZKrdaYm7eY5U=
|
||||
github.com/pascaldekloe/name v1.0.0/go.mod h1:Z//MfYJnH4jVpQ9wkclwu2I2MkHmXTlT9wR5UZScttM=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee h1:WG0RUwxtNT4qqaXX3DPA8zHFNm/D9xaBpxzHt1WcA/E=
|
||||
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s=
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/tools v0.0.0-20200221224223-e1da425f72fd h1:hHkvGJK23seRCflePJnVa9IMv8fsuavSCWKd11kDQFs=
|
||||
golang.org/x/tools v0.0.0-20200221224223-e1da425f72fd/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
|
||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898 h1:/atklqdjdhuosWIl6AIbOeHJjicWYPqR9bpxqxYG2pA=
|
||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU=
|
||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
|
|
|
@ -315,45 +315,45 @@ const (
|
|||
|
||||
func TestGolden(t *testing.T) {
|
||||
for _, test := range golden {
|
||||
runGoldenTest(t, test, false, false, false, false, false, false, "", "")
|
||||
runGoldenTest(t, test, false, false, false, false, false, false, true, "", "")
|
||||
}
|
||||
for _, test := range goldenJSON {
|
||||
runGoldenTest(t, test, true, false, false, false, false, false, "", "")
|
||||
runGoldenTest(t, test, true, false, false, false, false, false, false, "", "")
|
||||
}
|
||||
for _, test := range goldenText {
|
||||
runGoldenTest(t, test, false, false, false, true, false, false, "", "")
|
||||
runGoldenTest(t, test, false, false, false, true, false, false, false, "", "")
|
||||
}
|
||||
for _, test := range goldenYAML {
|
||||
runGoldenTest(t, test, false, true, false, false, false, false, "", "")
|
||||
runGoldenTest(t, test, false, true, false, false, false, false, false, "", "")
|
||||
}
|
||||
for _, test := range goldenSQL {
|
||||
runGoldenTest(t, test, false, false, true, false, false, false, "", "")
|
||||
runGoldenTest(t, test, false, false, true, false, false, false, false, "", "")
|
||||
}
|
||||
for _, test := range goldenJSONAndSQL {
|
||||
runGoldenTest(t, test, true, false, true, false, false, false, "", "")
|
||||
runGoldenTest(t, test, true, false, true, false, false, false, false, "", "")
|
||||
}
|
||||
for _, test := range goldenGQLGen {
|
||||
runGoldenTest(t, test, false, false, false, false, false, true, "", "")
|
||||
runGoldenTest(t, test, false, false, false, false, false, true, false, "", "")
|
||||
}
|
||||
for _, test := range goldenTrimPrefix {
|
||||
runGoldenTest(t, test, false, false, false, false, false, false, "Day", "")
|
||||
runGoldenTest(t, test, false, false, false, false, false, false, false, "Day", "")
|
||||
}
|
||||
for _, test := range goldenTrimPrefixMultiple {
|
||||
runGoldenTest(t, test, false, false, false, false, false, false, "Day,Night", "")
|
||||
runGoldenTest(t, test, false, false, false, false, false, false, false, "Day,Night", "")
|
||||
}
|
||||
for _, test := range goldenWithPrefix {
|
||||
runGoldenTest(t, test, false, false, false, false, false, false, "", "Day")
|
||||
runGoldenTest(t, test, false, false, false, false, false, false, false, "", "Day")
|
||||
}
|
||||
for _, test := range goldenTrimAndAddPrefix {
|
||||
runGoldenTest(t, test, false, false, false, false, false, false, "Day", "Night")
|
||||
runGoldenTest(t, test, false, false, false, false, false, false, false, "Day", "Night")
|
||||
}
|
||||
for _, test := range goldenLinecomment {
|
||||
runGoldenTest(t, test, false, false, false, false, true, false, "", "")
|
||||
runGoldenTest(t, test, false, false, false, false, true, false, false, "", "")
|
||||
}
|
||||
}
|
||||
|
||||
func runGoldenTest(t *testing.T, test Golden,
|
||||
generateJSON, generateYAML, generateSQL, generateText, linecomment, generateGQLGen bool,
|
||||
generateJSON, generateYAML, generateSQL, generateText, linecomment, generateGQLGen, generateValuesMethod bool,
|
||||
trimPrefix string, prefix string) {
|
||||
|
||||
var g Generator
|
||||
|
@ -382,7 +382,7 @@ func runGoldenTest(t *testing.T, test Golden,
|
|||
if len(tokens) != 3 {
|
||||
t.Fatalf("%s: need type declaration on first line", test.name)
|
||||
}
|
||||
g.generate(tokens[1], generateJSON, generateYAML, generateSQL, generateText, generateGQLGen, "noop", trimPrefix, prefix, linecomment)
|
||||
g.generate(tokens[1], generateJSON, generateYAML, generateSQL, generateText, generateGQLGen, "noop", trimPrefix, prefix, linecomment, generateValuesMethod)
|
||||
got := string(g.format())
|
||||
if got != loadGolden(test.name) {
|
||||
// Use this to help build a golden text when changes are needed
|
||||
|
|
15
stringer.go
15
stringer.go
|
@ -5,7 +5,7 @@
|
|||
// Enumer is a tool to generate Go code that adds useful methods to Go enums (constants with a specific type).
|
||||
// It started as a fork of Rob Pike’s Stringer tool
|
||||
//
|
||||
// Please visit http://github.com/dmarkham/enumer for a comprehensive documentation
|
||||
// Please visit http://git.internal/re/enumer for a comprehensive documentation
|
||||
package main
|
||||
|
||||
import (
|
||||
|
@ -50,6 +50,7 @@ var (
|
|||
yaml = flag.Bool("yaml", false, "if true, yaml marshaling methods will be generated. Default: false")
|
||||
text = flag.Bool("text", false, "if true, text marshaling methods will be generated. Default: false")
|
||||
gqlgen = flag.Bool("gqlgen", false, "if true, GraphQL marshaling methods for gqlgen will be generated. Default: false")
|
||||
altValuesFunc = flag.Bool("values", false, "if true, alternative string values method will be generated. Default: false")
|
||||
output = flag.String("output", "", "output file name; default srcdir/<type>_string.go")
|
||||
transformMethod = flag.String("transform", "noop", "enum item name transformation method. Default: noop")
|
||||
trimPrefix = flag.String("trimprefix", "", "transform each item name by removing a prefix. Default: \"\"")
|
||||
|
@ -70,7 +71,7 @@ func Usage() {
|
|||
_, _ = fmt.Fprintf(os.Stderr, "\tEnumer [flags] -type T [directory]\n")
|
||||
_, _ = fmt.Fprintf(os.Stderr, "\tEnumer [flags] -type T files... # Must be a single package\n")
|
||||
_, _ = fmt.Fprintf(os.Stderr, "For more information, see:\n")
|
||||
_, _ = fmt.Fprintf(os.Stderr, "\thttp://godoc.org/github.com/dmarkham/enumer\n")
|
||||
_, _ = fmt.Fprintf(os.Stderr, "\thttp://godoc.org/git.internal/re/enumer\n")
|
||||
_, _ = fmt.Fprintf(os.Stderr, "Flags:\n")
|
||||
flag.PrintDefaults()
|
||||
}
|
||||
|
@ -134,7 +135,7 @@ func main() {
|
|||
|
||||
// Run generate for each type.
|
||||
for _, typeName := range typs {
|
||||
g.generate(typeName, *json, *yaml, *sql, *text, *gqlgen, *transformMethod, *trimPrefix, *addPrefix, *linecomment)
|
||||
g.generate(typeName, *json, *yaml, *sql, *text, *gqlgen, *transformMethod, *trimPrefix, *addPrefix, *linecomment, *altValuesFunc)
|
||||
}
|
||||
|
||||
// Format the output.
|
||||
|
@ -414,7 +415,7 @@ func (g *Generator) prefixValueNames(values []Value, prefix string) {
|
|||
// generate produces the String method for the named type.
|
||||
func (g *Generator) generate(typeName string,
|
||||
includeJSON, includeYAML, includeSQL, includeText, includeGQLGen bool,
|
||||
transformMethod string, trimPrefix string, addPrefix string, lineComment bool) {
|
||||
transformMethod string, trimPrefix string, addPrefix string, lineComment bool, includeValuesMethod bool) {
|
||||
values := make([]Value, 0, 100)
|
||||
for _, file := range g.pkg.files {
|
||||
file.lineComment = lineComment
|
||||
|
@ -461,6 +462,10 @@ func (g *Generator) generate(typeName string,
|
|||
default:
|
||||
g.buildMap(runs, typeName)
|
||||
}
|
||||
if includeValuesMethod {
|
||||
g.buildAltStringValuesMethod(typeName)
|
||||
}
|
||||
|
||||
g.buildNoOpOrderChangeDetect(runs, typeName)
|
||||
|
||||
g.buildBasicExtras(runs, typeName, runsThreshold)
|
||||
|
@ -785,8 +790,6 @@ const stringOneRun = `func (i %[1]s) String() string {
|
|||
// [2]: lowest defined value for type, as a string
|
||||
// [3]: size of index element (8 for uint8 etc.)
|
||||
// [4]: less than zero check (for signed types)
|
||||
/*
|
||||
*/
|
||||
const stringOneRunWithOffset = `func (i %[1]s) String() string {
|
||||
i -= %[2]s
|
||||
if %[4]si >= %[1]s(len(_%[1]sIndex)-1) {
|
||||
|
|
|
@ -12,6 +12,10 @@ func (i Day) String() string {
|
|||
return _DayName[_DayIndex[i]:_DayIndex[i+1]]
|
||||
}
|
||||
|
||||
func (Day) Values() []string {
|
||||
return DayStrings()
|
||||
}
|
||||
|
||||
// An "invalid array index" compiler error signifies that the constant values have changed.
|
||||
// Re-run the stringer command to generate them again.
|
||||
func _DayNoOp() {
|
||||
|
@ -60,8 +64,8 @@ func DayString(s string) (Day, error) {
|
|||
if val, ok := _DayNameToValueMap[s]; ok {
|
||||
return val, nil
|
||||
}
|
||||
s = strings.ToLower(s)
|
||||
if val, ok := _DayNameToValueMap[s]; ok {
|
||||
|
||||
if val, ok := _DayNameToValueMap[strings.ToLower(s)]; ok {
|
||||
return val, nil
|
||||
}
|
||||
return 0, fmt.Errorf("%s does not belong to Day values", s)
|
||||
|
|
|
@ -60,8 +60,8 @@ func DayString(s string) (Day, error) {
|
|||
if val, ok := _DayNameToValueMap[s]; ok {
|
||||
return val, nil
|
||||
}
|
||||
s = strings.ToLower(s)
|
||||
if val, ok := _DayNameToValueMap[s]; ok {
|
||||
|
||||
if val, ok := _DayNameToValueMap[strings.ToLower(s)]; ok {
|
||||
return val, nil
|
||||
}
|
||||
return 0, fmt.Errorf("%s does not belong to Day values", s)
|
||||
|
|
|
@ -60,8 +60,8 @@ func DayString(s string) (Day, error) {
|
|||
if val, ok := _DayNameToValueMap[s]; ok {
|
||||
return val, nil
|
||||
}
|
||||
s = strings.ToLower(s)
|
||||
if val, ok := _DayNameToValueMap[s]; ok {
|
||||
|
||||
if val, ok := _DayNameToValueMap[strings.ToLower(s)]; ok {
|
||||
return val, nil
|
||||
}
|
||||
return 0, fmt.Errorf("%s does not belong to Day values", s)
|
||||
|
|
|
@ -60,8 +60,8 @@ func DayString(s string) (Day, error) {
|
|||
if val, ok := _DayNameToValueMap[s]; ok {
|
||||
return val, nil
|
||||
}
|
||||
s = strings.ToLower(s)
|
||||
if val, ok := _DayNameToValueMap[s]; ok {
|
||||
|
||||
if val, ok := _DayNameToValueMap[strings.ToLower(s)]; ok {
|
||||
return val, nil
|
||||
}
|
||||
return 0, fmt.Errorf("%s does not belong to Day values", s)
|
||||
|
|
|
@ -29,6 +29,10 @@ func (i Gap) String() string {
|
|||
}
|
||||
}
|
||||
|
||||
func (Gap) Values() []string {
|
||||
return GapStrings()
|
||||
}
|
||||
|
||||
// An "invalid array index" compiler error signifies that the constant values have changed.
|
||||
// Re-run the stringer command to generate them again.
|
||||
func _GapNoOp() {
|
||||
|
@ -81,8 +85,8 @@ func GapString(s string) (Gap, error) {
|
|||
if val, ok := _GapNameToValueMap[s]; ok {
|
||||
return val, nil
|
||||
}
|
||||
s = strings.ToLower(s)
|
||||
if val, ok := _GapNameToValueMap[s]; ok {
|
||||
|
||||
if val, ok := _GapNameToValueMap[strings.ToLower(s)]; ok {
|
||||
return val, nil
|
||||
}
|
||||
return 0, fmt.Errorf("%s does not belong to Gap values", s)
|
||||
|
|
|
@ -13,6 +13,10 @@ func (i Num) String() string {
|
|||
return _NumName[_NumIndex[i]:_NumIndex[i+1]]
|
||||
}
|
||||
|
||||
func (Num) Values() []string {
|
||||
return NumStrings()
|
||||
}
|
||||
|
||||
// An "invalid array index" compiler error signifies that the constant values have changed.
|
||||
// Re-run the stringer command to generate them again.
|
||||
func _NumNoOp() {
|
||||
|
@ -53,8 +57,8 @@ func NumString(s string) (Num, error) {
|
|||
if val, ok := _NumNameToValueMap[s]; ok {
|
||||
return val, nil
|
||||
}
|
||||
s = strings.ToLower(s)
|
||||
if val, ok := _NumNameToValueMap[s]; ok {
|
||||
|
||||
if val, ok := _NumNameToValueMap[strings.ToLower(s)]; ok {
|
||||
return val, nil
|
||||
}
|
||||
return 0, fmt.Errorf("%s does not belong to Num values", s)
|
||||
|
|
|
@ -13,6 +13,10 @@ func (i Number) String() string {
|
|||
return _NumberName[_NumberIndex[i]:_NumberIndex[i+1]]
|
||||
}
|
||||
|
||||
func (Number) Values() []string {
|
||||
return NumberStrings()
|
||||
}
|
||||
|
||||
// An "invalid array index" compiler error signifies that the constant values have changed.
|
||||
// Re-run the stringer command to generate them again.
|
||||
func _NumberNoOp() {
|
||||
|
@ -45,8 +49,8 @@ func NumberString(s string) (Number, error) {
|
|||
if val, ok := _NumberNameToValueMap[s]; ok {
|
||||
return val, nil
|
||||
}
|
||||
s = strings.ToLower(s)
|
||||
if val, ok := _NumberNameToValueMap[s]; ok {
|
||||
|
||||
if val, ok := _NumberNameToValueMap[strings.ToLower(s)]; ok {
|
||||
return val, nil
|
||||
}
|
||||
return 0, fmt.Errorf("%s does not belong to Number values", s)
|
||||
|
|
|
@ -25,6 +25,10 @@ func (i Prime) String() string {
|
|||
return fmt.Sprintf("Prime(%d)", i)
|
||||
}
|
||||
|
||||
func (Prime) Values() []string {
|
||||
return PrimeStrings()
|
||||
}
|
||||
|
||||
// An "invalid array index" compiler error signifies that the constant values have changed.
|
||||
// Re-run the stringer command to generate them again.
|
||||
func _PrimeNoOp() {
|
||||
|
@ -97,8 +101,8 @@ func PrimeString(s string) (Prime, error) {
|
|||
if val, ok := _PrimeNameToValueMap[s]; ok {
|
||||
return val, nil
|
||||
}
|
||||
s = strings.ToLower(s)
|
||||
if val, ok := _PrimeNameToValueMap[s]; ok {
|
||||
|
||||
if val, ok := _PrimeNameToValueMap[strings.ToLower(s)]; ok {
|
||||
return val, nil
|
||||
}
|
||||
return 0, fmt.Errorf("%s does not belong to Prime values", s)
|
||||
|
|
|
@ -97,8 +97,8 @@ func PrimeString(s string) (Prime, error) {
|
|||
if val, ok := _PrimeNameToValueMap[s]; ok {
|
||||
return val, nil
|
||||
}
|
||||
s = strings.ToLower(s)
|
||||
if val, ok := _PrimeNameToValueMap[s]; ok {
|
||||
|
||||
if val, ok := _PrimeNameToValueMap[strings.ToLower(s)]; ok {
|
||||
return val, nil
|
||||
}
|
||||
return 0, fmt.Errorf("%s does not belong to Prime values", s)
|
||||
|
|
|
@ -97,8 +97,8 @@ func PrimeString(s string) (Prime, error) {
|
|||
if val, ok := _PrimeNameToValueMap[s]; ok {
|
||||
return val, nil
|
||||
}
|
||||
s = strings.ToLower(s)
|
||||
if val, ok := _PrimeNameToValueMap[s]; ok {
|
||||
|
||||
if val, ok := _PrimeNameToValueMap[strings.ToLower(s)]; ok {
|
||||
return val, nil
|
||||
}
|
||||
return 0, fmt.Errorf("%s does not belong to Prime values", s)
|
||||
|
|
|
@ -97,8 +97,8 @@ func PrimeString(s string) (Prime, error) {
|
|||
if val, ok := _PrimeNameToValueMap[s]; ok {
|
||||
return val, nil
|
||||
}
|
||||
s = strings.ToLower(s)
|
||||
if val, ok := _PrimeNameToValueMap[s]; ok {
|
||||
|
||||
if val, ok := _PrimeNameToValueMap[strings.ToLower(s)]; ok {
|
||||
return val, nil
|
||||
}
|
||||
return 0, fmt.Errorf("%s does not belong to Prime values", s)
|
||||
|
|
|
@ -97,8 +97,8 @@ func PrimeString(s string) (Prime, error) {
|
|||
if val, ok := _PrimeNameToValueMap[s]; ok {
|
||||
return val, nil
|
||||
}
|
||||
s = strings.ToLower(s)
|
||||
if val, ok := _PrimeNameToValueMap[s]; ok {
|
||||
|
||||
if val, ok := _PrimeNameToValueMap[strings.ToLower(s)]; ok {
|
||||
return val, nil
|
||||
}
|
||||
return 0, fmt.Errorf("%s does not belong to Prime values", s)
|
||||
|
|
|
@ -97,8 +97,8 @@ func PrimeString(s string) (Prime, error) {
|
|||
if val, ok := _PrimeNameToValueMap[s]; ok {
|
||||
return val, nil
|
||||
}
|
||||
s = strings.ToLower(s)
|
||||
if val, ok := _PrimeNameToValueMap[s]; ok {
|
||||
|
||||
if val, ok := _PrimeNameToValueMap[strings.ToLower(s)]; ok {
|
||||
return val, nil
|
||||
}
|
||||
return 0, fmt.Errorf("%s does not belong to Prime values", s)
|
||||
|
|
|
@ -97,8 +97,8 @@ func PrimeString(s string) (Prime, error) {
|
|||
if val, ok := _PrimeNameToValueMap[s]; ok {
|
||||
return val, nil
|
||||
}
|
||||
s = strings.ToLower(s)
|
||||
if val, ok := _PrimeNameToValueMap[s]; ok {
|
||||
|
||||
if val, ok := _PrimeNameToValueMap[strings.ToLower(s)]; ok {
|
||||
return val, nil
|
||||
}
|
||||
return 0, fmt.Errorf("%s does not belong to Prime values", s)
|
||||
|
|
|
@ -60,8 +60,8 @@ func DayString(s string) (Day, error) {
|
|||
if val, ok := _DayNameToValueMap[s]; ok {
|
||||
return val, nil
|
||||
}
|
||||
s = strings.ToLower(s)
|
||||
if val, ok := _DayNameToValueMap[s]; ok {
|
||||
|
||||
if val, ok := _DayNameToValueMap[strings.ToLower(s)]; ok {
|
||||
return val, nil
|
||||
}
|
||||
return 0, fmt.Errorf("%s does not belong to Day values", s)
|
||||
|
|
|
@ -60,8 +60,8 @@ func DayString(s string) (Day, error) {
|
|||
if val, ok := _DayNameToValueMap[s]; ok {
|
||||
return val, nil
|
||||
}
|
||||
s = strings.ToLower(s)
|
||||
if val, ok := _DayNameToValueMap[s]; ok {
|
||||
|
||||
if val, ok := _DayNameToValueMap[strings.ToLower(s)]; ok {
|
||||
return val, nil
|
||||
}
|
||||
return 0, fmt.Errorf("%s does not belong to Day values", s)
|
||||
|
|
|
@ -23,6 +23,10 @@ func (i Unum) String() string {
|
|||
}
|
||||
}
|
||||
|
||||
func (Unum) Values() []string {
|
||||
return UnumStrings()
|
||||
}
|
||||
|
||||
// An "invalid array index" compiler error signifies that the constant values have changed.
|
||||
// Re-run the stringer command to generate them again.
|
||||
func _UnumNoOp() {
|
||||
|
@ -63,8 +67,8 @@ func UnumString(s string) (Unum, error) {
|
|||
if val, ok := _UnumNameToValueMap[s]; ok {
|
||||
return val, nil
|
||||
}
|
||||
s = strings.ToLower(s)
|
||||
if val, ok := _UnumNameToValueMap[s]; ok {
|
||||
|
||||
if val, ok := _UnumNameToValueMap[strings.ToLower(s)]; ok {
|
||||
return val, nil
|
||||
}
|
||||
return 0, fmt.Errorf("%s does not belong to Unum values", s)
|
||||
|
|
Loading…
Reference in New Issue