golf

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

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

Go to latest
Published: Dec 12, 2021 License: Apache-2.0 Imports: 7 Imported by: 0

README

GOLF (WIP)

中文文档

GOLF(Go Light Filter), golf dependents Gorm . golf can help you build model query as fast as,build model query like Django Rest Framework.

usage

define model
type testModel struct {
    ID       int    `json:"id"`
    UserID   int    `json:"user_id"`
    Username string `json:"username"`
}

implement GolfQuery interface

use golf must implement this interface , map key is go struct member name


func (m *testModel) Field() map[string][]golf.Filter {
    return map[string][]golf.Filter{
        "ID":       {golf.Equal},
        "UserID":   {golf.Equal, golf.Gte},
        "Username": {golf.Equal, golf.Like},
    }
}

use with request url

TODO you can read the example

// URL path /ping?eq_id=1&like_username=test
// sql log should be  SELECT * FROM "test_model" WHERE username LIKE 'test' AND id = 1 LIMIT 10
golfQ := golf.NewGolf(globalDB)
var tests testModel
if err := golfQ.Build(&testModel{}, Request.URL.Query()).Find(&tests).Error; err != nil {
    log.Println("find failed", err)
}

Documentation

Index

Constants

View Source
const (
	Equal    Filter = "eq"
	NotEqual Filter = "not_eq"
	Lt       Filter = "lt"
	Lte      Filter = "lte"
	Gt       Filter = "gt"
	Gte      Filter = "gte"
	Like     Filter = "like"
	NotLike  Filter = "not_like"
	In       Filter = "in"
	NotIn    Filter = "not_in"

	// TODO sort
	Desc SortOrder = "desc"
	Asc  SortOrder = "asc"
)

Variables

View Source
var OperationMap = map[Filter]Filter{
	Equal:   Equal,
	Lt:      Lt,
	Lte:     Lte,
	Gt:      Gt,
	Gte:     Gte,
	Like:    Like,
	NotLike: NotLike,
	In:      In,
	NotIn:   NotIn,
}

Functions

This section is empty.

Types

type Filter

type Filter string

type Golf

type Golf struct {
	Error error
	// contains filtered or unexported fields
}

func NewGolf

func NewGolf(db *gorm.DB) *Golf

func (*Golf) Build

func (g *Golf) Build(model GolfQuery, query map[string][]string) *Golf

Build Golf wile call checkQuery before generate real query

func (*Golf) Find

func (g *Golf) Find(dest interface{}, conds ...interface{}) *Golf

func (*Golf) First

func (g *Golf) First(dest interface{}, conds ...interface{}) *Golf

func (*Golf) GetGormDB

func (g *Golf) GetGormDB() *gorm.DB

type GolfQuery

type GolfQuery interface {
	// Field map key is target column, value is support operation slice
	Field() map[string][]Filter
}

type OperationWithType

type OperationWithType struct {
	ValueOperation
	Type reflect.Type
}

type SortOrder

type SortOrder string

type ValueOperation

type ValueOperation struct {
	Column string
	Filter Filter
	Value  interface{}
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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