procapi

package module
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2019 License: MIT Imports: 10 Imported by: 1

README

English | Pусский


apisite/procapi

Caller of postgresql stored functions, which intended to use via http and in templates

GoDoc codecov Build Status GoCard GitHub Release LoC GitHub code size in bytes GitHub license

Project status: WIP

License

The MIT License (MIT), see LICENSE.

Copyright (c) 2018 Aleksei Kovrizhkin lekovr+apisite@gmail.com

Documentation

Index

Constants

View Source
const (
	SQLInit  = `CREATE SCHEMA %s; SET SEARCH_PATH = %s, public;`
	SQLReset = `RESET SEARCH_PATH;` // TODO: reset after load
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	DSN           string    `long:"dsn" default:"postgres://?sslmode=disable" description:"Database connect string"`
	Driver        string    `long:"driver" default:"postgres" description:"Database driver"`
	InDefFunc     string    `long:"indef" default:"func_args" description:"Argument definition function"`
	OutDefFunc    string    `long:"outdef" default:"func_result" description:"Result row definition function"`
	IndexFunc     string    `long:"index" default:"index" description:"Available functions list"`
	FuncSchema    string    `long:"schema" default:"rpc" description:"Definition functions schema"`
	ArgSyntax     string    `long:"arg_syntax" default:":=" description:"Default named args syntax (:= or =>)"`
	ArgTrimPrefix string    `long:"arg_prefix" default:"a_" description:"Trim prefix from arg name"`
	NameSpaces    *[]string `long:"nsp" description:"Proc namespace(s)"`
}

Config defines local application flags

type DB

type DB interface {
	Beginx() (Tx, error)
	Close() error
}

DB holds all of database methods, used for database mocking

type InDef

type InDef struct {
	Name     string  `db:"arg"`
	Type     string  `db:"type"`
	Required bool    `db:"required"` // TODO: is_required
	Default  *string `db:"def_val" json:",omitempty"`
	Anno     *string `db:"anno" json:",omitempty"`
}

InDef holds function argument attributes

type Marshaller

type Marshaller interface {
	Marshal(typ string, v interface{}) (interface{}, error)
	Unmarshal(typ string, data interface{}) (rv interface{}, err error)
}

Marshaller holds methods for database values marshalling

type Method

type Method struct {
	Name     string           `db:"code"`
	Class    string           `db:"nspname"`
	Func     string           `db:"proname"`
	Anno     string           `db:"anno"`
	IsRO     bool             `db:"is_ro"`
	IsSet    bool             `db:"is_set"`
	IsStruct bool             `db:"is_struct"`
	Sample   *string          `db:"sample" json:",omitempty"`
	Result   *string          `db:"result" json:",omitempty"`
	In       map[string]InDef //`json:",omitempty"`
	Out      []OutDef         //`json:",omitempty"`
}

Method holds method attributes

type OutDef

type OutDef struct {
	Name string  `db:"arg"`
	Type string  `db:"type"`
	Anno *string `db:"anno" json:",omitempty"`
}

OutDef holds function result attributes

type Result

type Result interface {
	RowsAffected() (int64, error)
}

Result is a local copy of sql.Result for mocking

type Rows

type Rows interface {
	StructScan(dest interface{}) error
	MapScan(dest map[string]interface{}) error
	Scan(dest ...interface{}) error
	Close() error
	Next() bool
}

Rows is a local copy of *sql[.Result for mocking Rows holds all of Rows methods used

type Service

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

Service holds API service methods

func New

func New(cfg Config, log loggers.Contextual, dbh DB) *Service

New returns procapi service

func (*Service) Call

func (srv *Service) Call(
	r *http.Request,
	method string,
	args map[string]interface{},
) (interface{}, error)

Call calls postgresql stored function

func (*Service) CallTx

func (srv *Service) CallTx(
	tx Tx,
	method string,
	args map[string]interface{},
) (interface{}, error)

CallTx calls postgresql stored function within given transaction

func (*Service) DB

func (srv *Service) DB() DB

Method returns method by name

func (*Service) FetchMethods

func (srv *Service) FetchMethods(tx Tx, nsp *[]string) (map[string]Method, error)

FetchMethods fetches from DB methods definition for given namespaces

func (*Service) LoadMethods

func (srv *Service) LoadMethods() error

LoadMethods load methods for nsp if given, all of methods otherwise

func (*Service) LoadMethodsTx

func (srv *Service) LoadMethodsTx(tx Tx) error

LoadMethodsTx load methods within given transaction for nsp if given, all of methods otherwise

func (*Service) Method

func (srv *Service) Method(name string) (Method, bool)

Method returns method by name

func (*Service) Open

func (srv *Service) Open() error

Open opens DB connection

func (*Service) SetMarshaller added in v0.3.1

func (srv *Service) SetMarshaller(m Marshaller) *Service

SetMarshaller sets marshaller

func (*Service) SetSchemaSuffix

func (srv *Service) SetSchemaSuffix(suffix string) *Service

SetSchemaSuffix sets suffix for all of used shemas names

type Tx

type Tx interface {
	Queryx(query string, args ...interface{}) (Rows, error)
	Exec(query string, args ...interface{}) (Result, error)
	Rollback() error
	Commit() error
}

Tx is a local copy of *sqlx.Tx for mocking Tx holds all of database transaction methods used

Directories

Path Synopsis
ginproc module
pgtype module

Jump to

Keyboard shortcuts

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