Skip to content

Commit

Permalink
WIP for #56.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlekSi committed Apr 28, 2019
1 parent c8eea00 commit 3aec770
Show file tree
Hide file tree
Showing 4 changed files with 257 additions and 4 deletions.
10 changes: 10 additions & 0 deletions internal/test/models/extra.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ type Extra struct {
Uint8sT Uint8s `reform:"uint8st"`
}

//reform:extra2
type Extra2 struct {
ID []byte `reform:"id,pk"` // https://github.com/go-reform/reform/issues/56
}

//reform:extra3
type Extra3 struct {
ID [16]byte `reform:"id,pk"` // https://github.com/go-reform/reform/issues/56
}

//reform:not_exported
type notExported struct {
ID string `reform:"id,pk"`
Expand Down
220 changes: 220 additions & 0 deletions internal/test/models/extra_reform.go

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

7 changes: 5 additions & 2 deletions parse/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import (
"strings"
)

var magicReformComment = regexp.MustCompile(`reform:([0-9A-Za-z_\.]+)`)
var (
magicReformComment = regexp.MustCompile(`reform:([0-9A-Za-z_\.]+)`)
sbUint8 = regexp.MustCompile(`^\[\d*\]uint8$`)
)

func fileGoType(x ast.Expr) string {
switch t := x.(type) {
Expand Down Expand Up @@ -80,7 +83,7 @@ func parseStructTypeSpec(ts *ast.TypeSpec, str *ast.StructType) (*StructInfo, er
if strings.HasPrefix(typ, "*") {
return nil, fmt.Errorf(`reform: %s has pointer field %s with with "pk" label in "reform:" tag, it is not allowed`, res.Type, name.Name)
}
if strings.HasPrefix(typ, "[") {
if strings.HasPrefix(typ, "[") && !sbUint8.MatchString(typ) {
return nil, fmt.Errorf(`reform: %s has slice field %s with with "pk" label in "reform:" tag, it is not allowed`, res.Type, name.Name)
}
if res.PKFieldIndex >= 0 {
Expand Down
24 changes: 22 additions & 2 deletions parse/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,24 @@ var (
PKFieldIndex: 0,
}

extra2 = StructInfo{
Type: "Extra2",
SQLName: "extra2",
Fields: []FieldInfo{
{Name: "ID", Type: "[]uint8", Column: "id"},
},
PKFieldIndex: 0,
}

extra3 = StructInfo{
Type: "Extra3",
SQLName: "extra3",
Fields: []FieldInfo{
{Name: "ID", Type: "[16]uint8", Column: "id"},
},
PKFieldIndex: 0,
}

notExported = StructInfo{
Type: "notExported",
SQLName: "not_exported",
Expand All @@ -114,9 +132,11 @@ func TestFileGood(t *testing.T) {
func TestFileExtra(t *testing.T) {
s, err := File(filepath.FromSlash("../internal/test/models/extra.go"))
assert.NoError(t, err)
require.Len(t, s, 2)
require.Len(t, s, 4)
assert.Equal(t, extra, s[0])
assert.Equal(t, notExported, s[1])
assert.Equal(t, extra2, s[1])
assert.Equal(t, extra3, s[2])
assert.Equal(t, notExported, s[3])
}

func TestFileBogus(t *testing.T) {
Expand Down

0 comments on commit 3aec770

Please sign in to comment.