smg

package module
v0.0.0-...-a4a9cab Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2017 License: MIT Imports: 4 Imported by: 0

README

smg

Search Model Generator.

Description

smg can generate model & query builder for appengine Search API.

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

type User struct {
	Name	string
}
user := &User {
	"go-chan",
}
index := NewUserSearch()
docID, err = index.Put(c, user)
user := &User {
	"go-chan",
}
index := NewUserSearch()
docID, err = index.Put(c, user)
Example

from:

package a

// test for basic struct definition

type Sample struct {
	Foo string
}

to:

// generated!
type SampleSearch struct {
	src *Sample

	Foo string
}

func (src *Sample) Searchfy() (*SampleSearch, error) {
	if src == nil {
		return nil, nil
	}
	dest := &SampleSearch{}
	dest.src = src
	dest.Foo = src.Foo
	return dest, nil
}

func NewSampleSearch() *SampleSearchBuilder {
	op := &smgutils.Op{}
	b := &SampleSearchBuilder{
		rootOp:    op,
		currentOp: op,
	}
	b.Foo = &SampleSearchStringPropertyInfo{"Foo", b}

	return b
}

type SampleSearchBuilder struct {
	rootOp    *smgutils.Op
	currentOp *smgutils.Op // for grouping
	opts      *search.SearchOptions
	query     string
	index     *search.Index
	Foo       *SampleSearchStringPropertyInfo
}

...

usage:

src := &Sample{"Foo!"}

index := NewSampleSearch() // generated!
docID, err = index.Put(c, user)

other example.

With go generate
$ ls -la .
total 8
drwxr-xr-x@ 3 vvakame  staff  102 10 15 14:14 .
drwxr-xr-x@ 7 vvakame  staff  238  8 14 18:24 ..
-rw-r--r--@ 1 vvakame  staff  147  8 14 18:24 model.go
$ cat model.go
//go:generate smg -output model_search.go .

package d

// test for struct with tagged comment

// +smg
type Sample struct {
	A string
	B string
}
$ go generate
$ ls -la .
total 32
drwxr-xr-x@ 4 vvakame  staff   136 10 15 14:15 .
drwxr-xr-x@ 7 vvakame  staff   238  8 14 18:24 ..
-rw-r--r--@ 1 vvakame  staff   147  8 14 18:24 model.go
-rw-r--r--  1 vvakame  staff  9993 10 15 14:15 model_search.go

Installation

$ go get github.com/favclip/smg/cmd/smg
$ smg
Usage of smg:
	smg [flags] [directory]
	smg [flags] files... # Must be a single package
Flags:
  -output="": output file name; default srcdir/<type>_string.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
}
$ smg -type Sample -output misc/fixture/a/model_search.go misc/fixture/a

Model with tagged comment.

$ cat misc/fixture/d/model.go
//go:generate smg -output model_search.go .

package d

// test for struct with tagged comment

// +smg
type Sample struct {
	A string
	B string
}
$ smg -output misc/fixture/d/model_search.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 {
	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.

func (*BuildSource) Emit

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

Emit generate wrapper code.

type BuildStruct

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

BuildStruct represents struct of assembling..

func (*BuildStruct) HasID

func (st *BuildStruct) HasID() bool

HasID returns struct has id annotated field.

func (*BuildStruct) HasJSON

func (st *BuildStruct) HasJSON() bool

HasJSON returns struct has json annotated field.

func (*BuildStruct) HasNgram

func (st *BuildStruct) HasNgram() bool

HasNgram returns struct has ngram annotated field.

func (*BuildStruct) HasString

func (st *BuildStruct) HasString() bool

HasString returns struct has string annotated field.

func (*BuildStruct) HasUnixTime

func (st *BuildStruct) HasUnixTime() bool

HasUnixTime returns struct has unix time annotated field.

func (*BuildStruct) ID

func (st *BuildStruct) ID() *BuildField

ID returns field with id annotation.

func (*BuildStruct) Name

func (st *BuildStruct) Name() string

Name returns struct type name.

func (*BuildStruct) Rank

func (st *BuildStruct) Rank() *BuildField

Rank returns field with rank annotation.

type BuildTag

type BuildTag struct {
	Name     string
	ID       bool // e.g. DocID string `search:",id"`
	Ignore   bool // e.g. Secret string `search:"-"`
	Ngram    bool // e.g. Description string `search:",ngram"`
	JSON     bool // e.g. Store []*Store `search:",json"`
	Rank     bool // e.g. Stock int `search:",rank"`
	String   bool // e.g. Int64String int64 `search:",string"`
	UnixTime bool // e.g. Unix time time.Time `search:",unixtime"`
	// contains filtered or unexported fields
}

BuildTag represents tag of BuildField.

Directories

Path Synopsis
cmd
smg
misc

Jump to

Keyboard shortcuts

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