query builder generator
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.circleci
cmd/qbg
misc/fixture
qbgutils
.gitignore
Gopkg.lock
Gopkg.toml
LICENSE
README.md
app.yaml
generator.go
generator_test.go
setup.sh
template.go
test.sh
usage_test.go

README.md

qbg

Query Builder Generator.

Description

qbg can generate type safe wrapper for appengine datastore.

If you use string literal for building query, You shoots your foot when you do typo. qbg generate appengine datastore wrapper code. It is type safe. Your mistake will be found by go compiler.

type User struct {
	Name	string
}
user := &User {
	"go-chan",
}
builder := NewUserQueryBuilder()
builder.Name.Equal("go-chan")
cnt, err := builder.Query().Count(c)

Example

from:

type Sample struct {
	Foo string
}

to:

// generated!
// for Sample
type SampleQueryBuilder struct {
	q      *datastore.Query
	plugin qbgutils.Plugin
	Foo    *SampleQueryProperty
}

type SampleQueryProperty struct {
	bldr *SampleQueryBuilder
	name string
}

usage:

src := &Sample{"Foo!"}

builder := NewSampleQueryBuilder() // generated!
builder.Foo.GreaterThanOrEqual("Foo")
cnt, err := builder.Query().Count(c)

other example.

With go generate

$ ls -la .
total 8
drwxr-xr-x@ 3 vvakame  staff  102 10 13 17:39 .
drwxr-xr-x@ 7 vvakame  staff  238  8 14 18:26 ..
-rw-r--r--@ 1 vvakame  staff  178  8 14 18:26 model.go
$ cat model.go
//go:generate qbg -output model_query.go .

package c

import "time"

// +qbg
type Sample struct {
	ID        int64     `goon:"id"`
	CreatedAt time.Time `datastore:",noindex"`
}
$ go generate
$ ls -la .
total 16
drwxr-xr-x@ 4 vvakame  staff   136 10 13 17:40 .
drwxr-xr-x@ 7 vvakame  staff   238  8 14 18:26 ..
-rw-r--r--@ 1 vvakame  staff   178  8 14 18:26 model.go
-rw-r--r--  1 vvakame  staff  3709 10 13 17:40 model_query.go

Recommend

Please use with goon.

Installation

$ go get -u github.com/favclip/qbg/cmd/qbg
$ qbg
Usage of qbg:
	qbg [flags] [directory]
	qbg [flags] files... # Must be a single package
Flags:
  -output="": output file name; default srcdir/<type>_query.go
  -type="": comma-separated list of type names; must be set

Command sample

Model with type specific option.

$ cat misc/fixture/a/model.go
package a

// test for basic struct definition

type Sample struct {
	Foo string
}
$ qbg -type Sample -output misc/fixture/a/model_query.go misc/fixture/a

Model with tagged comment.

$ cat misc/fixture/c/model.go
//go:generate qbg -output model_query.go .

package c

import "time"

// +qbg
type Sample struct {
	ID        int64     `goon:"id"`
	CreatedAt time.Time `datastore:",noindex"`
}
$ qbg -output misc/fixture/d/model_query.go misc/fixture/d