qbg

package module
v2.0.0+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 15, 2022 License: MIT Imports: 6 Imported by: 0

README

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BuildField

type BuildField struct {
	Name string
	Tag  *BuildTag
	// contains filtered or unexported fields
}

BuildField represents field of BuildStruct.

type BuildSource

type BuildSource struct {
	InlineInterfaces    bool
	UseDatastoreWrapper bool
	Structs             []*BuildStruct
	// contains filtered or unexported fields
}

BuildSource represents source code of assembling..

func Parse

func Parse(pkg *genbase.PackageInfo, typeInfos genbase.TypeInfos) (*BuildSource, error)

Parse construct *BuildSource from package & type information. deprecated. use *BuildSource#Parse instead.

func (*BuildSource) Emit

func (b *BuildSource) Emit(args *[]string) ([]byte, error)

Emit generate wrapper code.

func (*BuildSource) Parse added in v1.1.0

func (b *BuildSource) Parse(pkg *genbase.PackageInfo, typeInfos genbase.TypeInfos) error

Parse construct *BuildSource from package & type information.

type BuildStruct

type BuildStruct struct {
	Private bool
	Fields  []*BuildField
	// contains filtered or unexported fields
}

BuildStruct represents struct of assembling..

func (*BuildStruct) Kind

func (st *BuildStruct) Kind() string

Kind returns kind from struct.

func (*BuildStruct) Name

func (st *BuildStruct) Name() string

Name returns struct type name.

func (*BuildStruct) SimpleName

func (st *BuildStruct) SimpleName() string

Name returns struct type name.

type BuildTag

type BuildTag struct {
	Kind              string // e.g. `goon:"kind,FooKind"`
	Name              string
	PropertyNameAlter string // e.g. `qbg:"StartAt"`
	ID                bool
	Ignore            bool // e.g. Secret string `datastore:"-"`
	NoIndex           bool // e.g. no index `datastore:",noindex"`
	// contains filtered or unexported fields
}

BuildTag represents tag of BuildField.

Directories

Path Synopsis
cmd
qbg
misc

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL