Skip to content

Commit

Permalink
Document source code better and add pkg level comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Miguel Molina committed Feb 8, 2017
1 parent cff692b commit cb126bb
Show file tree
Hide file tree
Showing 12 changed files with 291 additions and 239 deletions.
6 changes: 3 additions & 3 deletions batcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type batchQueryRunner struct {
records []Record
}

var ErrNoMoreRows = errors.New("kallax: there are no more rows in the result set")
var errNoMoreRows = errors.New("kallax: there are no more rows in the result set")

func newBatchQueryRunner(schema Schema, db squirrel.DBProxy, q Query) *batchQueryRunner {
cols, builder := q.compile()
Expand Down Expand Up @@ -53,7 +53,7 @@ func newBatchQueryRunner(schema Schema, db squirrel.DBProxy, q Query) *batchQuer

func (r *batchQueryRunner) next() (Record, error) {
if r.eof {
return nil, ErrNoMoreRows
return nil, errNoMoreRows
}

if len(r.records) == 0 {
Expand All @@ -64,7 +64,7 @@ func (r *batchQueryRunner) next() (Record, error) {

if len(records) == 0 {
r.eof = true
return nil, ErrNoMoreRows
return nil, errNoMoreRows
}

r.total += len(records)
Expand Down
13 changes: 13 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Kallax is a PostgreSQL typesafe ORM for the Go language.
//
// Kallax aims to provide a way of programmatically write queries and interact
// with a PostgreSQL database without having to write a single line of SQL,
// use strings to refer to columns and use values of any type in queries.
// For that reason, the first priority of kallax is to provide type safety to
// the data access layer.
// Another of the goals of kallax is make sure all models are, first and
// foremost, Go structs without having to use database-specific types such as,
// for example, `sql.NullInt64.
// Support for arrays of all basic Go types and all JSON and arrays operators is
// provided as well.
package kallax
57 changes: 0 additions & 57 deletions fixtures/fixtures.go

This file was deleted.

2 changes: 2 additions & 0 deletions generator/generator.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Package generator implements the processor of source code and generator of
// kallax models based on Go source code.
package generator

import (
Expand Down
83 changes: 78 additions & 5 deletions generator/types_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package generator

import (
"os"
"path/filepath"
"go/ast"
"go/importer"
"go/parser"
"go/token"
"go/types"
"reflect"
"testing"

Expand Down Expand Up @@ -160,10 +163,80 @@ type ModelSuite struct {
variadic *Model
}

const fixturesSource = `
package fixtures
import (
"errors"
"strings"
kallax "github.com/src-d/go-kallax"
)
type User struct {
kallax.Model ` + "`table:\"users\"`" + `
Username string
Email string
Password Password
Websites []string
Emails []*Email
Settings *Settings
}
func newUser(username, email string) (*User, error) {
if strings.Contains(email, "@spam.org") {
return nil, errors.New("kallax: is spam!")
}
return &User{Username: username, Email: email}, nil
}
type Email struct {
kallax.Model ` + "`table:\"models\"`" + `
Address string
Primary bool
}
func newProfile(address string, primary bool) *Email {
return &Email{Address: address, Primary: primary}
}
type Password string
// Kids, don't do this at home
func (p *Password) Set(pwd string) {
*p = Password("such cypher" + pwd + "much secure")
}
type Settings struct {
NotificationsActive bool
NotifyByEmail bool
}
type Variadic struct {
kallax.Model
Foo []string
Bar string
}
func newVariadic(bar string, foo ...string) *Variadic {
return &Variadic{Foo: foo, Bar: bar}
}
`

func (s *ModelSuite) SetupSuite() {
path := filepath.Join(os.Getenv("GOPATH"), "src", "github.com/src-d/go-kallax/fixtures")
p := NewProcessor(path, nil)
pkg, err := p.Do()
fset := &token.FileSet{}
astFile, err := parser.ParseFile(fset, "fixture.go", fixturesSource, 0)
s.Nil(err)

cfg := &types.Config{
Importer: importer.For("gc", nil),
}
p, err := cfg.Check("foo", fset, []*ast.File{astFile}, nil)
s.Nil(err)

prc := NewProcessor("fixture", []string{"foo.go"})
prc.Package = p
pkg, err := prc.processPackage()
s.Nil(err)

s.Len(pkg.Models, 3, "there should exist 3 models")
Expand Down
Loading

0 comments on commit cb126bb

Please sign in to comment.