Skip to content

Commit

Permalink
Merge pull request #11 from go-gorm/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
tr1v3r authored Aug 3, 2021
2 parents 513adc6 + 7ebf3d8 commit 12fbf41
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 12 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
# gen
# GORM/GEN

## Overview

* Code generator
* Developer Friendly

## License

Released under the [MIT License](https://github.com/go-gorm/gen/blob/master/License)
11 changes: 8 additions & 3 deletions internal/check/checkstruct.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,23 @@ func (b *BaseStruct) getTableName(st interface{}) {
b.TableName = stmt.Table
}

// check if struct is exportable and if member's type is regular
// check if struct is exportable and if struct in main package and if member's type is regular
func (b *BaseStruct) check() (err error) {
if b.StructInfo.InMainPkg() {
err = fmt.Errorf("can't generated data object for struct in main package,ignored:%s", b.StructName)
log.Println(err)
return
}
if !isCapitalize(b.StructName) {
err = fmt.Errorf("ignoring non exportable struct name:%s", b.NewStructName)
err = fmt.Errorf("can't generated data object for non-exportable struct,ignore:%s", b.NewStructName)
log.Println(err)
return
}
for index, m := range b.Members {
if !allowType(m.Type) {
b.Members[index].Type = "field"
}
b.Members[index].NewType = getTypeName(m.Type)
b.Members[index].NewType = getNewTypeName(m.Type)
}
return nil
}
2 changes: 1 addition & 1 deletion internal/check/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func CheckStructs(db *gorm.DB, structs ...interface{}) (bases []*BaseStruct, err

for _, st := range structs {
structType := reflect.TypeOf(st)
name := getTypeName(structType.String())
name := getStructName(structType.String())
base := &BaseStruct{
S: GetSimpleName(name),
StructName: name,
Expand Down
15 changes: 9 additions & 6 deletions internal/check/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,13 @@ func GetSimpleName(s string) string {
return string(strings.ToLower(DelPointerSym(s))[0])
}

func getTypeName(t string) string {
var res string
for _, s := range strings.Split(t, ".") {
res = s
}
return strings.Title(res)
func getNewTypeName(t string) string {
list := strings.Split(t, ".")
return strings.Title(list[len(list)-1])
}

// not need capitalize
func getStructName(t string) string {
list := strings.Split(t, ".")
return list[len(list)-1]
}
2 changes: 1 addition & 1 deletion internal/parser/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func GetInterfacePath(v interface{}) (paths []*InterfacePath, err error) {
}

if strings.Split(arg.String(), ".")[0] == "main" {
_, file, _, ok := runtime.Caller(2)
_, file, _, ok := runtime.Caller(3)
if ok {
path.Files = append(path.Files, file)
}
Expand Down
4 changes: 4 additions & 0 deletions internal/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ func (p *Param) IsNull() bool {
return p.Package == "" && p.Type == "" && p.Name == ""
}

func (p *Param) InMainPkg() bool {
return p.Package == "main"
}

func (p *Param) IsTime() bool {
return p.Package == "time" && p.Type == "Time"
}
Expand Down

0 comments on commit 12fbf41

Please sign in to comment.