proxy

package
v0.0.0-...-c18a219 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2023 License: MIT Imports: 8 Imported by: 0

README

proxy

proxy implements a dynamic proxy for gsd, like JPA in Java.

Usage

With proxy, you only need add interface and tags, proxy create these methods for you automatically.

type User struct {
	ID   int32 `gsd:",auto"`
	Name string
}

type UserDao struct {
	Get    func(id int32) (*User, error) `gsd:"find,cache:user.load"`
	Load   func(u *User) error           `gsd:"load,cache:user.load"`
	Remove func(u *User) error           `gsd:"remove"`
	Create func(u *User) error           `gsd:"create"`
	Update func(u *User) error           `gsd:"modify"`
}

func ProxyTest() {
	dao := &UserDao{}
	proxy.Apply("test", dao)

	user, err := dao.Get(1)
	if err != nil {
		panic(err)
	}

	err = dao.Load(user)
	if err != nil {
		panic(err)
	}

	user = &User{ID: 2, Name: "noname"}
	err = dao.Create(user)
	if err != nil {
		panic(err)
	}

	user.Name = "noname"
	err = dao.Update(user)
	if err != nil {
		panic(err)
	}

	err = dao.Remove(user)
	if err != nil {
		panic(err)
	}
}

Create custom proxy method

proxy support 5 proxy methods now, you can create your own proxy method easily.

proxy.Register("search", func(db *gsd.LazyDB, ft reflect.Type, options data.Options) reflect.Value {
    // todo: impliment proxy method
})

Once you register it, you can use it in any dao struct.

type UserDao struct {
	Search func(name string) ([]*User, error) `gsd:"search"`
}

Documentation

Index

Examples

Constants

View Source
const PkgName = "auxo.db.gsd.proxy"

Variables

View Source
var (
	Default = New(nil)
)

Functions

func Apply

func Apply(dbName string, i interface{})

Apply generate data access methods according to fields of i.

func ApplyLazy

func ApplyLazy(db *gsd.LazyDB, i interface{})

func Register

func Register(action string, builder Builder)

Types

type Builder

type Builder func(db *gsd.LazyDB, ft reflect.Type, options data.Options) reflect.Value

type Options

type Options struct {
}

type Proxy

type Proxy struct {
	// contains filtered or unexported fields
}

func New

func New(options *Options) *Proxy

func (*Proxy) Apply

func (p *Proxy) Apply(dbName string, i interface{})

Apply generate data access methods according to fields of i.

Example
package main

import (
	"github.com/cuigh/auxo/db/gsd/proxy"
)

func main() {
	type User struct {
		ID   int32 `gsd:",auto"`
		Name string
	}
	type UserDao struct {
		Get    func(id int32) (*User, error) `gsd:"find,cache:user.load"`
		Load   func(u *User) error           `gsd:"load,cache:user.load"`
		Remove func(u *User) error           `gsd:"remove"`
		Create func(u *User) error           `gsd:"create"`
		Update func(u *User) error           `gsd:"modify"`
	}

	dao := &UserDao{}
	proxy.Apply("test", dao)

	user, err := dao.Get(1)
	if err != nil {
		panic(err)
	}

	err = dao.Load(user)
	if err != nil {
		panic(err)
	}

	user = &User{ID: 2, Name: "noname"}
	err = dao.Create(user)
	if err != nil {
		panic(err)
	}

	user.Name = "noname"
	err = dao.Update(user)
	if err != nil {
		panic(err)
	}

	err = dao.Remove(user)
	if err != nil {
		panic(err)
	}
}
Output:

func (*Proxy) ApplyLazy

func (p *Proxy) ApplyLazy(db *gsd.LazyDB, i interface{})

func (*Proxy) Register

func (p *Proxy) Register(action string, builder Builder)

Jump to

Keyboard shortcuts

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