Giter Site home page Giter Site logo

smg's Introduction

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

smg's People

Contributors

favclip-develop avatar shota-takahashi-tg avatar vvakame avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

smg's Issues

build error by auto-generated file `imported and not used: "strconv"`

Hello,
Thank you a lot for this package. It will be a great help to our Japanese full-text search function.

By the way, I encountered a problem as the title of this issue when I start to use this (latest version 812be11 ).
I only tried to autogen one struct as below:

// +smg
type ChatLog struct {
	ApplicantID string    `search:",string"`
	Body        string    `search:",ngram"`
	Created     time.Time `search:",unixtime"`
}

I checked into codes and this line seem to cause this.
https://github.com/favclip/smg/blob/master/generator.go#L82

thanks again

build query from json

Hello,
Thank you for this package. The query builder is good to construct it at the server, but it would be great to have a way to build query from a specification, like json object. say, we have a query struct

type filter struct {
    fieldName string
    op        string
    value     interface{}
}

type Query struct {
    filter []filter
    order  []order

    limit  int
    offset int

    keysOnly bool
}

if, there is a way to build a query, from this struct, then we can construct query from a json/client request. Do you think it is viable, and any plan for it. Else, how do you typically handle a query from client?

thanks again,
bsr.

Question: text search

Hi,

I don't know a lot about the terminology, but what I want is full text search on my entities. For example, in the usecase test, I want to search by a string and it should match against all the fields in the entity (like ProductName, Description, Shops). I could maintain a separate field for this, but please let me know is it easily possible with this package?

thanks.
bsr.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.