Skip to content

Commit

Permalink
improved error messages from source code parser
Browse files Browse the repository at this point in the history
  • Loading branch information
rickb777 committed Jun 16, 2024
1 parent 5c5dfe3 commit 5390e6d
Show file tree
Hide file tree
Showing 46 changed files with 168 additions and 107 deletions.
2 changes: 1 addition & 1 deletion 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/v3 v3.2.1
// github.com/rickb777/enumeration/v3 v3.3.0

package example

Expand Down
2 changes: 1 addition & 1 deletion 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/v3 v3.2.1
// github.com/rickb777/enumeration/v3 v3.3.0

package example

Expand Down
2 changes: 1 addition & 1 deletion example/country_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/v3 v3.2.1
// github.com/rickb777/enumeration/v3 v3.3.0

package example

Expand Down
2 changes: 1 addition & 1 deletion example/day_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/v3 v3.2.1
// github.com/rickb777/enumeration/v3 v3.3.0

package example

Expand Down
2 changes: 1 addition & 1 deletion example/greekalphabet_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/v3 v3.2.1
// github.com/rickb777/enumeration/v3 v3.3.0

package example

Expand Down
2 changes: 1 addition & 1 deletion example/method_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/v3 v3.2.1
// github.com/rickb777/enumeration/v3 v3.3.0

package example

Expand Down
2 changes: 1 addition & 1 deletion example/month_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/v3 v3.2.1
// github.com/rickb777/enumeration/v3 v3.3.0

package example

Expand Down
2 changes: 1 addition & 1 deletion example/pet_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/v3 v3.2.1
// github.com/rickb777/enumeration/v3 v3.3.0

package example

Expand Down
20 changes: 3 additions & 17 deletions internal/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ type Imports struct {
type Model struct {
Config
LcType, BaseType string
BaseKind types.BasicKind
Version string
Values Values
Case transform.Case
Expand Down Expand Up @@ -261,26 +262,11 @@ func (m Model) FnMap() template.FuncMap {
}

func (m Model) IsFloat() bool {
return m.BaseKind() == types.Float64
}

func (m Model) BaseKind() types.BasicKind {
var kind types.BasicKind
switch m.BaseType {
case "int", "uint",
"int8", "uint8",
"int16", "uint16",
"int32", "uint32",
"int64", "uint64":
kind = types.Int
case "float32", "float64":
kind = types.Float64
}
return kind
return m.BaseKind == types.Float64
}

func (m Model) Placeholder() string {
switch m.BaseKind() {
switch m.BaseKind {
case types.Int:
return "%d"
case types.Float64:
Expand Down
2 changes: 1 addition & 1 deletion internal/model/write_core.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var All<<.MainType>>Enums = <<.AllItemsSlice>>{
`

func (m Model) AllItemsSlice() string {
switch m.BaseKind() {
switch m.BaseKind {
case types.Int:
return "enum.IntEnums"
case types.Float64:
Expand Down
21 changes: 12 additions & 9 deletions internal/parse/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package parse
import (
"fmt"
"go/token"
"go/types"
"io"
"regexp"
"strings"
Expand Down Expand Up @@ -55,20 +56,22 @@ func Convert(in io.Reader, input string, xCase transform.Case, config model.Conf

s := newFileScanner(input, src)

var foundMainType = false
var numFound = 0
var constItems []constItem
var baseType string
var baseKind types.BasicKind

for s.Scan() != token.EOF {
switch s.Tok {
case token.TYPE:
baseType, err = parseType(s, MainType)
baseType, baseKind, err = parseType(s, MainType, numFound)
if err != nil {
return m, err
return m, fmt.Errorf("%s: %w", s.Position(), err)
}
if baseType != "" {
foundMainType = true
if baseKind != types.Invalid {
numFound++
m.BaseType = baseType
m.BaseKind = baseKind
}

case token.CONST:
Expand All @@ -83,15 +86,15 @@ func Convert(in io.Reader, input string, xCase transform.Case, config model.Conf
debugValues(m.Values)

if s.gs.ErrorCount > 0 {
return model.Model{}, fmt.Errorf("Syntax error in %s\n%s", input, strings.Join(s.errs, "\n"))
return model.Model{}, fmt.Errorf("%s: syntax error\n%s", input, strings.Join(s.errs, "\n"))
}

if !foundMainType {
return model.Model{}, fmt.Errorf("Failed to find %s in %s", config.MainType, input)
if numFound == 0 {
return model.Model{}, fmt.Errorf("%s: failed to find type %s", input, config.MainType)
}

if len(m.Values) == 0 {
return model.Model{}, fmt.Errorf("Failed to find any values for %s in %s", config.MainType, input)
return model.Model{}, fmt.Errorf("%s: failed to find any values for %s", input, config.MainType)
}

if e2 := m.CheckBadPrefixSuffix(); e2 != nil {
Expand Down
116 changes: 88 additions & 28 deletions internal/parse/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package parse
import (
"bytes"
"flag"
"go/types"
"os"
"testing"

Expand Down Expand Up @@ -45,6 +46,7 @@ func TestConvertBlock1(t *testing.T) {
},
LcType: "sweet",
BaseType: "int",
BaseKind: types.Int,
Version: util.Version,
Values: model.ValuesOf("Mars", "Bounty", "Snickers", "Kitkat"),
Case: transform.Upper,
Expand All @@ -54,7 +56,9 @@ func TestConvertBlock1(t *testing.T) {
}

const enumBlock2 = `
/* inline comments are allowed */
/* inline comments are allowed, also var declarations are ignored */
var x = 100 *
100
type Sweet int // <-- buried here
const (
_ Sweet = iota
Expand Down Expand Up @@ -91,6 +95,7 @@ func TestConvertBlock2(t *testing.T) {
},
LcType: "sweet",
BaseType: "int",
BaseKind: types.Int,
Version: util.Version,
Values: model.ValuesOf("Mars", "Bounty", "Snickers", "Kitkat"),
Extra: make(map[string]interface{}),
Expand Down Expand Up @@ -129,6 +134,7 @@ func TestConvertBlock3(t *testing.T) {
},
LcType: "sweet",
BaseType: "int",
BaseKind: types.Int,
Version: util.Version,
Values: model.ValuesOf("Mars", "Bounty", "Snickers", "Kitkat"),
Case: transform.Upper,
Expand Down Expand Up @@ -206,6 +212,7 @@ func TestConvertBlock4(t *testing.T) {
},
LcType: "sweet",
BaseType: "int",
BaseKind: types.Int,
Version: util.Version,
Values: values,
AliasTable: "sweetAliases",
Expand All @@ -217,7 +224,9 @@ func TestConvertBlock4(t *testing.T) {
//-------------------------------------------------------------------------------------------------

const enumBlockMultiple = `
type Sweet int
type (
Sweet int
)
const (
Mars, Bounty, Snickers, Kitkat Sweet = 1, 2, 3, 4
)
Expand All @@ -244,6 +253,7 @@ func TestConvertBlockMultiple(t *testing.T) {
},
LcType: "sweet",
BaseType: "int",
BaseKind: types.Int,
Version: util.Version,
Values: model.ValuesOf("Mars", "Bounty", "Snickers", "Kitkat"),
Case: transform.Upper,
Expand Down Expand Up @@ -283,6 +293,7 @@ func TestConvertSeparate1(t *testing.T) {
},
LcType: "sweet",
BaseType: "int",
BaseKind: types.Int,
Version: util.Version,
Values: model.ValuesOf("Mars", "Bounty", "Snickers", "Kitkat"),
Case: transform.Upper,
Expand Down Expand Up @@ -328,6 +339,7 @@ func TestConvertSeparate2(t *testing.T) {
},
LcType: "sweet",
BaseType: "int",
BaseKind: types.Int,
Version: util.Version,
Values: expected,
Case: transform.Upper,
Expand All @@ -337,7 +349,7 @@ func TestConvertSeparate2(t *testing.T) {
}

const enumSeparateMultiple = `
type Sweet int
type Sweet float64
const Mars, Bounty, Snickers, Kitkat Sweet = 1, 2, 3, 4
`

Expand All @@ -359,7 +371,8 @@ func TestConvertSeparateMultiple(t *testing.T) {
Pkg: "confectionary",
},
LcType: "sweet",
BaseType: "int",
BaseType: "float64",
BaseKind: types.Float64,
Version: util.Version,
Values: model.ValuesOf("Mars", "Bounty", "Snickers", "Kitkat"),
Case: transform.Upper,
Expand All @@ -370,32 +383,79 @@ func TestConvertSeparateMultiple(t *testing.T) {

//-------------------------------------------------------------------------------------------------

const enumError1 = `
type IgnoreMe int
// type Sweet is missing
const (
Mars Sweet = iota
Bounty
Snickers
Kitkat
)
const (
Jam IgnoreMe = iota
Toast
Butter
)
`
var enumErrors = map[string]string{
// type Sweet is missing
`type Sweet uint
type Sweet int
const (
Mars Sweet = iota
)
`: "filename.go:2:7: found multiple type Sweet declarations",

// type Sweet is missing
`type IgnoreMe int
const (
Mars Sweet = iota
)
const (
Jam IgnoreMe = iota
)
`: "filename.go: failed to find type Sweet",

// type Sweet is not numeric - simple
`type Sweet string
const (
Mars Sweet = iota
)
const (
Jam IgnoreMe = iota
)
`: "filename.go:1:12: enumeration type Sweet must be an integer or float type",

// type Sweet is not numeric - block
`type (
Sweet string
)
const (
Mars Sweet = iota
)
`: "filename.go:2:9: enumeration type Sweet must be an integer or float type",

// type Sweet is a type alias
`type Sweet = Alias
const (
Mars Sweet = iota
)
`: "filename.go:1:12: type Sweet is a type alias (not supported)",

// type Sweet is a type alias - block
`type (
Sweet = Alias
)
const (
Mars Sweet = iota
)
`: "filename.go:2:9: type Sweet is a type alias (not supported)",

// type Sweet is a type alias - block
`type (
Sweet
)
`: "filename.go:2:8: syntax error in type Sweet declaration",
}

func TestConvertError1(t *testing.T) {
func TestConvertErrors(t *testing.T) {
RegisterTestingT(t)
s := bytes.NewBufferString(enumError1)
_, err := Convert(s, "filename.go", transform.Stet,
model.Config{
MainType: "Sweet",
Plural: "Sweets",
Pkg: "confectionary",
})
Ω(err.Error()).Should(Equal("Failed to find Sweet in filename.go"))
for src, msg := range enumErrors {
s := bytes.NewBufferString(src)
_, err := Convert(s, "filename.go", transform.Stet,
model.Config{
MainType: "Sweet",
Plural: "Sweets",
Pkg: "confectionary",
})
Ω(err.Error()).Should(Equal(msg), msg)
}
}

func TestMain(m *testing.M) {
Expand Down
Loading

0 comments on commit 5390e6d

Please sign in to comment.