Skip to content

Commit

Permalink
v4 ready for release
Browse files Browse the repository at this point in the history
  • Loading branch information
rickb777 committed Feb 13, 2025
1 parent 132d3af commit c499424
Show file tree
Hide file tree
Showing 85 changed files with 739 additions and 1,543 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea/shelf/
.idea/workspace.xml
/bin/
/temp/
12 changes: 12 additions & 0 deletions .idea/runConfigurations/enumeration_example_method.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/runConfigurations/enumeration_example_month.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ type SalesChannel int

const (
_ SalesChannel = iota
Online // json:"webshop" sql:"o" -- String() is "online"
Instore // json:"store" sql:"s" -- String() is "instore"
Telephone // json:"phone" sql:"t" -- String() is "telephone"
Online // json:"webshop" sql:"o"
Instore // json:"store" sql:"s"
Telephone // json:"phone" sql:"t"
)
```

Expand Down Expand Up @@ -166,8 +166,8 @@ Options are:
* `-s`
- generate a simple enumeration without parsing methods.

* `-nopoly`
- do not generate code that would make the enumeration polymorphic as.
* `-poly`
- generate code that makes the enumeration polymorphic via [github.com/rickb777/enumeration/v4/enum](https://pkg.go.dev/github.com/rickb777/enumeration/v4/enum) types.

* `-lc`
- convert to lower case the string representations of the enumeration values.
Expand Down
7 changes: 5 additions & 2 deletions build+test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ function v
}

v go mod download
#v go test .

v go build -o bin/enumeration -ldflags "-s -X main.version=$VERSION -X main.date=$DATE" .
#type enumeration

v go clean -testcache

Expand All @@ -38,4 +37,8 @@ v gofmt -l -w -s *.go

sleep 0.25s # wait for the files to be stable

mkdir -p temp/example # used in ./enumeration_test.go
cp example/*.go temp/example
rm -f temp/example/*_enum.go temp/example/*_test.go

v go test ./...
8 changes: 4 additions & 4 deletions enum/enum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import (
func TestIntEnums_Strings(t *testing.T) {
RegisterTestingT(t)

days := example.AllDayEnums.Strings()
methods := example.AllMethodEnums.Strings()

Ω(strings.Join(days, "|")).Should(Equal("Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday"))
Ω(strings.Join(methods, "|")).Should(Equal("HEAD|GET|PUT|POST|PATCH|DELETE"))

days = enum.Enums{example.Wednesday, example.Friday, example.Sunday}.Strings()
methods = enum.Enums{example.HEAD, example.PUT, example.PATCH}.Strings()

Ω(strings.Join(days, "|")).Should(Equal("Wednesday|Friday|Sunday"))
Ω(strings.Join(methods, "|")).Should(Equal("HEAD|PUT|PATCH"))
}

func TestIntEnums_Ordinals(t *testing.T) {
Expand Down
9 changes: 8 additions & 1 deletion enum/representation.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package enum

//go:generate enumeration -type Representation -ic
//go:generate enumeration -type Representation -ic -alias altReps

type Representation int

Expand All @@ -9,3 +9,10 @@ const (
Identifier // uses the main identifier of the corresponding constant
Number // the value of the enumerant as a decimal number
)

var altReps = map[string]Representation{
"x": None,
"id": Identifier,
"num": Number,
"n": Number,
}
11 changes: 9 additions & 2 deletions enum/representation_enum.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// generated code - do not edit
// github.com/rickb777/enumeration/v4 v3.4.0
// github.com/rickb777/enumeration/v4

package enum

Expand Down Expand Up @@ -118,6 +118,12 @@ func (v *Representation) parseFallback(in, s string) error {
return nil
}

var ok bool
*v, ok = altReps[s]
if ok {
return nil
}

return errors.New(in + ": unrecognised representation")
}

Expand All @@ -133,7 +139,8 @@ func (v *Representation) parseString(s string, concats string, indexes []uint16)
}
i0 = i1
}
return false
*v, ok = altReps[s]
return ok
}

// representationTransformInput may alter input strings before they are parsed.
Expand Down
9 changes: 5 additions & 4 deletions enumeration.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

var config model.Config
var inputGo, outputGo, outputJSON, marshalTextRep, marshalJSONRep, storeRep string
var force, lowercase, uppercase, showVersion bool
var force, nocase, lowercase, uppercase, showVersion bool

func defineFlags() {
flag.StringVar(&config.MainType, "type", "", "Name of the enumeration type (required).")
Expand All @@ -34,11 +34,12 @@ func defineFlags() {

flag.BoolVar(&config.Lenient, "lenient", false, "Allow parsing to yield invalid values.")
flag.BoolVar(&force, "f", false, "Force writing the output file even if up to date (not used when piping stdin or stdout).")
flag.BoolVar(&nocase, "nc", false, "Don't convert strings to upper or lowercase (this is the default)")
flag.BoolVar(&lowercase, "lc", false, "Convert strings to lowercase and ignore case when parsing")
flag.BoolVar(&uppercase, "uc", false, "Convert strings to uppercase and ignore case when parsing.")
flag.BoolVar(&config.IgnoreCase, "ic", false, "Ignore case when parsing but keep the mixed case when outputting.")
flag.BoolVar(&config.Unsnake, "unsnake", false, "Convert underscores in identifiers to spaces.")
flag.BoolVar(&config.Simple, "s", false, "Generate simple enumerations without serialising or parsing functions")
flag.BoolVar(&config.SimpleOnly, "s", false, "Generate simple enumerations without serialising or parsing functions")
flag.BoolVar(&config.Polymorphic, "poly", false, "Generate polymorphic representation code")
flag.BoolVar(&util.Verbose, "v", false, "Verbose progress messages.")
flag.BoolVar(&util.Dbg, "z", false, "Debug messages.")
Expand Down Expand Up @@ -115,7 +116,7 @@ func generate() {
util.Must(err)

units := m.BuildUnits()
model.WriteGo(units, m.SelectImports(), out)
model.WriteGo(units, m, out)
util.Info("Generated %s.\n", outputGo)
}

Expand Down Expand Up @@ -155,7 +156,7 @@ func doMain() {
outputJSON = strings.ToLower(config.MainType) + "_enum.json"
}

if config.Simple {
if config.SimpleOnly {
config.IgnoreCase = false
config.Lenient = false
}
Expand Down
20 changes: 10 additions & 10 deletions enumeration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ import (

func TestMainApp_Day(t *testing.T) {
g := gomega.NewWithT(t)
outputFile := "example/day_enum.go"
outputFile := "temp/example/day_enum.go"
err := os.Remove(outputFile)
if err != nil {
t.Logf("rm %s: %s", outputFile, err.Error())
// continue anyway
}

os.Args = []string{"", "-f", "-type", "Day", "-i", "example/day.go", "-o", outputFile}
os.Args = []string{"", "-f", "-type", "Day", "-i", "temp/example/day.go", "-o", outputFile}

main()

Expand All @@ -32,12 +32,12 @@ func TestMainApp_Day(t *testing.T) {
func TestMainApp_Channel(t *testing.T) {
g := gomega.NewWithT(t)

inputGo = "example/channel.go"
outputGo = "example/channel_enum.go"
inputGo = "temp/example/channel.go"
outputGo = "temp/example/channel_enum.go"

err := os.Remove(outputGo)
if err != nil {
t.Logf("rm %s: %s", "example/channel_enum.go", err.Error())
t.Logf("rm %s: %s", "temp/example/channel_enum.go", err.Error())
// continue anyway
}

Expand All @@ -61,8 +61,8 @@ func TestMainApp_Channel(t *testing.T) {
func TestMainApp_Country(t *testing.T) {
g := gomega.NewWithT(t)

inputGo = "example/country.go"
outputGo = "example/country_enum.go"
inputGo = "temp/example/country.go"
outputGo = "temp/example/country_enum.go"

err := os.Remove(outputGo)
if err != nil {
Expand Down Expand Up @@ -93,12 +93,12 @@ func TestMainApp_Country(t *testing.T) {
func TestMainApp_Method(t *testing.T) {
g := gomega.NewWithT(t)

inputGo = "example/method.go"
outputGo = "example/method_enum.go"
inputGo = "temp/example/method.go"
outputGo = "temp/example/method_enum.go"

err := os.Remove(outputGo)
if err != nil {
t.Logf("rm %s: %s", "example/method_enum.go", err.Error())
t.Logf("rm %s: %s", "temp/example/method_enum.go", err.Error())
// continue anyway
}

Expand Down
6 changes: 3 additions & 3 deletions example/base.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package example

//go:generate enumeration -v -f -type Base -lc
//go:generate enumeration -v -f -type Base -lc -poly

// Base: This example demonstrates using floating point values instead of integers.
// These are float32 but could be float64; the only ither restriction is that
// Base example demonstrates using floating point values instead of integers.
// These are float32 but could be float64; the only other restriction is that
// no two values can be the same number.
type Base float32

Expand Down
16 changes: 8 additions & 8 deletions example/base_enum.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// generated code - do not edit
// github.com/rickb777/enumeration/v4 v4.0.0-dirty
// github.com/rickb777/enumeration/v4 v4.0.0-1-g132d3af-dirty

package example

Expand Down Expand Up @@ -30,6 +30,13 @@ var (
baseEnumIndex = [...]uint16{0, 1, 2, 3, 4}
)

// String returns the literal string representation of a Base, which is
// the same as the const identifier but without prefix or suffix.
func (v Base) String() string {
o := v.Ordinal()
return v.toString(o, baseEnumStrings, baseEnumIndex[:])
}

// Ordinal returns the ordinal number of a Base. This is an integer counting
// from zero. It is *not* the same as the const number assigned to the value.
func (v Base) Ordinal() int {
Expand All @@ -46,13 +53,6 @@ func (v Base) Ordinal() int {
return -1
}

// String returns the literal string representation of a Base, which is
// the same as the const identifier but without prefix or suffix.
func (v Base) String() string {
o := v.Ordinal()
return v.toString(o, baseEnumStrings, baseEnumIndex[:])
}

func (v Base) toString(o int, concats string, indexes []uint16) string {
if o < 0 || o >= len(AllBases) {
return fmt.Sprintf("Base(%g)", v)
Expand Down
8 changes: 4 additions & 4 deletions example/channel.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package example

//go:generate enumeration -v -i channel.go -o channel_enum.go -lc -type SalesChannel -suffix Sales
//go:generate enumeration -v -i channel.go -o channel_enum.go -lc -type SalesChannel -suffix Sales -poly

// preamble const declarations are ignored
// preamble const declarations are ignored by the enumeration tool
const IgnoreThisItem, AndThis = 741, "quack"

// this is ignored too
const (
// One is ignored by the enumeration tool too
One = 1
)

// SalesChannel: The example demonstrates the removing of a suffix string from the identifiers
// SalesChannel example demonstrates the removing of a suffix string from the identifiers
// when their string equivalent is accessed.
//
// The `json` tags in comments control values used for JSON marshalling.
Expand Down
23 changes: 8 additions & 15 deletions example/channel_enum.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// generated code - do not edit
// github.com/rickb777/enumeration/v4 v4.0.0-dirty
// github.com/rickb777/enumeration/v4 v4.0.0-1-g132d3af-dirty

package example

Expand Down Expand Up @@ -35,6 +35,13 @@ var (
saleschannelSQLIndex = [...]uint16{0, 1, 2, 3}
)

// String returns the literal string representation of a SalesChannel, which is
// the same as the const identifier but without prefix or suffix.
func (v SalesChannel) String() string {
o := v.Ordinal()
return v.toString(o, saleschannelEnumStrings, saleschannelEnumIndex[:])
}

// Ordinal returns the ordinal number of a SalesChannel. This is an integer counting
// from zero. It is *not* the same as the const number assigned to the value.
func (v SalesChannel) Ordinal() int {
Expand All @@ -49,13 +56,6 @@ func (v SalesChannel) Ordinal() int {
return -1
}

// String returns the literal string representation of a SalesChannel, which is
// the same as the const identifier but without prefix or suffix.
func (v SalesChannel) String() string {
o := v.Ordinal()
return v.toString(o, saleschannelEnumStrings, saleschannelEnumIndex[:])
}

func (v SalesChannel) toString(o int, concats string, indexes []uint16) string {
if o < 0 || o >= len(AllSalesChannels) {
return fmt.Sprintf("SalesChannel(%d)", v)
Expand Down Expand Up @@ -237,13 +237,6 @@ func (v *SalesChannel) unmarshalJSON(in string) error {
return errors.New(in + ": unrecognised saleschannel")
}

// saleschannelMarshalNumber handles marshaling where a number is required or where
// the value is out of range.
// This function can be replaced with any bespoke function than matches signature.
var saleschannelMarshalNumber = func(v SalesChannel) string {
return strconv.FormatInt(int64(v), 10)
}

// Scan parses some value, which can be a number, a string or []byte.
// It implements sql.Scanner, https://golang.org/pkg/database/sql/#Scanner
func (v *SalesChannel) Scan(value interface{}) error {
Expand Down
2 changes: 1 addition & 1 deletion example/country.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package example

//go:generate enumeration -v -type Country -plural Countries -ic -unsnake -alias iso3166_3LetterCodes

// Country: This example shows use of the '-plural' option to set the name of plural
// Country example shows use of the '-plural' option to set the name of plural
// collections. Because of '-ic', the parser ignores case. Because of '-unsnake', the parser
// treats underscores and spaces alike.
//
Expand Down
Loading

0 comments on commit c499424

Please sign in to comment.