Skip to content
This repository has been archived by the owner on Jan 8, 2025. It is now read-only.

Commit

Permalink
minor improvements
Browse files Browse the repository at this point in the history
- removed unnecessary dependencies
- simplify integration test
- removed Schemaer interface
- update to latest version of mira
  • Loading branch information
stevenferrer committed Mar 1, 2021
1 parent 3f60412 commit 16e7d71
Show file tree
Hide file tree
Showing 29 changed files with 460 additions and 1,285 deletions.
4 changes: 2 additions & 2 deletions column.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Column struct {
// name is the column name
name string
// typeInfo is inferred column type info
typeInfo *mira.Type
typeInfo *mira.TypeInfo
// StructField overrides the struct field
structField string
// Auto is an auto-filled column flag
Expand All @@ -26,7 +26,7 @@ type Column struct {
}

// TypeInfo returns the type info
func (c *Column) TypeInfo() *mira.Type {
func (c *Column) TypeInfo() *mira.TypeInfo {
return c.typeInfo
}

Expand Down
2 changes: 1 addition & 1 deletion column_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type ColumnBuilder struct {
func NewColumnBuilder(name string, v interface{}) *ColumnBuilder {
return &ColumnBuilder{&Column{
name: name,
typeInfo: mira.NewType(v),
typeInfo: mira.NewTypeInfo(v),
}}
}

Expand Down
30 changes: 0 additions & 30 deletions example/group.go

This file was deleted.

41 changes: 0 additions & 41 deletions example/map.go

This file was deleted.

32 changes: 0 additions & 32 deletions example/nopreds.go

This file was deleted.

43 changes: 16 additions & 27 deletions example/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,36 @@ package example
import (
"time"

"github.com/google/uuid"

"github.com/sf9v/nero"
)

// User is a basic example type
// User is a
type User struct {
ID int64
UUID uuid.UUID
Name string
Group string
Age int
IsRegistered bool
Tags [10]string
Empty struct{}
UpdatedAt *time.Time
CreatedAt *time.Time
ID int64
Name string
Department string
UpdatedAt *time.Time
CreatedAt *time.Time
}

// Schema implements nero.Schemaer
func (u *User) Schema() *nero.Schema {
return nero.NewSchemaBuilder(u).
func (u User) Schema() *nero.Schema {
return nero.NewSchemaBuilder(&u).
PkgName("user").Collection("users").
Identity(
nero.NewColumnBuilder("id", u.ID).
StructField("ID").Auto().Build(),
nero.NewColumnBuilder("id", u.ID).StructField("ID").
Auto().Build(),
).
Columns(
nero.NewColumnBuilder("uuid", u.UUID).
StructField("UUID").Build(),
nero.NewColumnBuilder("name", u.Name).Build(),
nero.NewColumnBuilder("group_res", u.Group).
StructField("Group").Build(),
nero.NewColumnBuilder("age", u.Age).Build(),
nero.NewColumnBuilder("is_registered", u.IsRegistered).Build(),
nero.NewColumnBuilder("tags", u.Tags).Build(),
nero.NewColumnBuilder("empty", u.Empty).Build(),
nero.NewColumnBuilder("department", u.Department).Build(),
nero.NewColumnBuilder("updated_at", u.UpdatedAt).
Optional().Comparable().Build(),
nero.NewColumnBuilder("created_at", u.CreatedAt).Auto().Build(),
nero.NewColumnBuilder("created_at", u.CreatedAt).
Auto().Build(),
).
Templates(
nero.NewPostgresTemplate().WithFilename("postgres.go"),
).
Templates(nero.NewPostgresTemplate().WithFilename("postgres.go")).
Build()
}
35 changes: 9 additions & 26 deletions gen/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,25 @@ import (
)

// Generate generates the repository code
func Generate(schemaer nero.Schemaer) ([]*File, error) {
schema := schemaer.Schema()

func Generate(schema *nero.Schema) ([]*File, error) {
files := []*File{}
buf, err := newMetaFile(schema)
if err != nil {
return nil, errors.Wrap(err, "meta file")
}
files = append(files, &File{
name: "meta.go",
buf: buf.Bytes(),
})
files = append(files, &File{name: "meta.go", buf: buf.Bytes()})

buf, err = newPredicatesFile(schema)
if err != nil {
return nil, errors.Wrap(err, "predicates file")
}
files = append(files, &File{
name: "predicates.go",
buf: buf.Bytes(),
})
files = append(files, &File{name: "predicates.go", buf: buf.Bytes()})

buf, err = newSortsFile(schema)
if err != nil {
return nil, errors.Wrap(err, "sorts file")
}
files = append(files, &File{
name: "sorts.go",
buf: buf.Bytes(),
})
files = append(files, &File{name: "sorts.go", buf: buf.Bytes()})

buf, err = newAggregatesFile(schema)
if err != nil {
Expand All @@ -50,21 +39,15 @@ func Generate(schemaer nero.Schemaer) ([]*File, error) {
if err != nil {
return nil, errors.Wrap(err, "repository file")
}
files = append(files, &File{
name: "repository.go",
buf: buf.Bytes(),
})
files = append(files, &File{name: "repository.go", buf: buf.Bytes()})

for _, tmpltr := range schema.Templaters() {
buf, err = newTemplater(schema, tmpltr)
for _, tmpl := range schema.Templaters() {
buf, err = newTemplater(schema, tmpl)
if err != nil {
return nil, errors.Wrap(err, "repository implementation file")
return nil, errors.Wrap(err, "templater file")
}

files = append(files, &File{
name: tmpltr.Filename(),
buf: buf.Bytes(),
})
files = append(files, &File{name: tmpl.Filename(), buf: buf.Bytes()})
}

return files, nil
Expand Down
2 changes: 1 addition & 1 deletion gen/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func TestGenerate(t *testing.T) {
files, err := gen.Generate(&example.User{})
files, err := gen.Generate((example.User{}).Schema())
assert.NoError(t, err)
assert.Len(t, files, 6)

Expand Down
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ go 1.15

require (
github.com/Masterminds/squirrel v1.5.0
github.com/google/uuid v1.2.0
github.com/hashicorp/go-multierror v1.1.0
github.com/jinzhu/inflection v1.0.0
github.com/lib/pq v1.9.0
github.com/pkg/errors v0.9.1
github.com/segmentio/ksuid v1.0.3
github.com/sf9v/mira v0.1.0
github.com/sf9v/mira v0.2.0
github.com/stretchr/testify v1.7.0
golang.org/x/tools v0.1.0
)
10 changes: 2 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ github.com/Masterminds/squirrel v1.5.0/go.mod h1:NNaOrjSoIDfDA40n7sr2tPNZRfjzjA4
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs=
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-multierror v1.1.0 h1:B9UzwGQJehnUY1yNrnwREHc3fGbC2xefo8g4TbElacI=
Expand All @@ -22,13 +19,10 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/segmentio/ksuid v1.0.3 h1:FoResxvleQwYiPAVKe1tMUlEirodZqlqglIuFsdDntY=
github.com/segmentio/ksuid v1.0.3/go.mod h1:/XUiZBD3kVx5SmUOl55voK5yeAbBNNIed+2O73XgrPE=
github.com/sf9v/mira v0.1.0 h1:ohZF25vrhJysBEXmdZhg51aJn6Xeh0pbP2H7uyvBO7E=
github.com/sf9v/mira v0.1.0/go.mod h1:zeNW4eWcS+HUH99UqwRG7qGFkyD9uvrKOMPExqKjAc0=
github.com/sf9v/mira v0.2.0 h1:uRdI2ylEeFbAH7V4N9FtczqnRR4nqfuuWWl/f2aVybc=
github.com/sf9v/mira v0.2.0/go.mod h1:aYgakH2Pd7Fwsjmd8zhFvZyZjfcItnx9DxGeTfRMzqY=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
4 changes: 2 additions & 2 deletions postgres_template.go

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

9 changes: 2 additions & 7 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,14 @@ import (
stringsx "github.com/sf9v/nero/x/strings"
)

// Schemaer is an interface that wraps the Schema method
type Schemaer interface {
Schema() *Schema
}

// Schema is a schema used for generating the repository
type Schema struct {
// pkgName is the package name of the generated files
pkgName string
// Collection is the name of the collection/table
collection string
// typeInfo is the type info the schema model
typeInfo *mira.Type
typeInfo *mira.TypeInfo
// Identity is the identity column
identity *Column
// Columns is the list of columns
Expand Down Expand Up @@ -60,7 +55,7 @@ func (s *Schema) Templaters() []Templater {
}

// TypeInfo returns the type info
func (s *Schema) TypeInfo() *mira.Type {
func (s *Schema) TypeInfo() *mira.TypeInfo {
return s.typeInfo
}

Expand Down
2 changes: 1 addition & 1 deletion schema_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type SchemaBuilder struct {
// NewSchemaBuilder takes a model struct value and returns a SchemaBuilder
func NewSchemaBuilder(v interface{}) *SchemaBuilder {
return &SchemaBuilder{&Schema{
typeInfo: mira.NewType(v),
typeInfo: mira.NewTypeInfo(v),
columns: []*Column{},
templaters: []Templater{},
}}
Expand Down
8 changes: 4 additions & 4 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,17 @@ func resolveType(t reflect.Type) reflect.Type {
}

func zeroFunc(v interface{}) string {
tt := mira.NewType(v)
ti := mira.NewTypeInfo(v)

if tt.IsNillable() {
if ti.IsNillable() {
return "nil"
}

if tt.Kind() == mira.Numeric {
if ti.IsNumeric() {
return "0"
}

switch tt.T().Kind() {
switch ti.T().Kind() {
case reflect.Bool:
return "false"
case reflect.Struct,
Expand Down
Loading

0 comments on commit 16e7d71

Please sign in to comment.