sdk

package
v0.0.0-...-890d1bf Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2023 License: GPL-3.0 Imports: 45 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoResult      = errors.New("No result")
	ErrAlreadyClosed = errors.New("Already closed")

	ErrOverflow     = errors.New("Overflow")
	ErrInvalidValue = errors.New("Invalid value")

	ErrTracerServiceNameRequired = errors.New("tracer: service name required")
	ErrTracerEndpointRequired    = errors.New("tracer: endpoint required")
)
View Source
var (
	ErrInvalidTransaction   = errors.New("Invalid transaction")
	ErrInvalidArgumentsScan = errors.New("Invalid arguments for scan")
	ErrNoColumnsReturned    = errors.New("No columns returned")
	ErrInvalidDatabase      = errors.New("Invalid database")
)
View Source
var (
	ErrMultipleCommands = errors.New("Multiple commands")
	ErrInvalidCommand   = errors.New("Invalid command")
)

nolint: gochecknoglobals

View Source
var (
	OTel open_telemetry
)

nolint: gochecknoglobals

Functions

func CancelRequest

func CancelRequest(r *http.Request) *http.Request

CancelRequest will cancel the underlying context from *http.Request.

func Middleware

func Middleware(h ...http.Handler) http.Handler

Middleware create a stack of http.Handler that is cancelable via CancelRequest.

func MuxMatcherAnd

func MuxMatcherAnd(priority float64, muxes ...MuxMatcher) *muxMatcherAnd

func MuxMatcherMethods

func MuxMatcherMethods(priority float64, methods ...string) *muxMatcherMethods

MuxMatcherMethods receive multiple methods, if contains asterisk `*` then the priority should be set to 0.

func MuxMatcherMock

func MuxMatcherMock(priority float64, test, match bool) *muxMatcherMock

func MuxMatcherOr

func MuxMatcherOr(priority float64, muxes ...MuxMatcher) *muxMatcherOr

func MuxMatcherPattern

func MuxMatcherPattern(priority float64, pattern, start, end string, caseSensitive bool) *muxMatcherPattern

MuxMatcherPattern receive pattern of named arguments using a pair of start and end string; if start is empty string, then assuming start is colon `:`, when end is empty string, then assuming end is slash `/`

`/:args1/:args2/:args3` // colon at start of arguments
`/:args1:/:args2:/:args3:` // colon at both start and end
`/{args1}/{args2}/{args3}` // curly-braces at both start and end

func NamedArgsFromRequest

func NamedArgsFromRequest(r *http.Request) url.Values

NamedArgsFromRequest is a helper function that extract url.Values that have been parsed using MuxMatcherPattern, url.Values should not be empty if parsing is successful and should be able to extract further following url.Values, same keys in the pattern result in new value added in url.Values.

func PanicIf

func PanicIf(cond bool, v interface{})

func PanicRecoveryFromRequest

func PanicRecoveryFromRequest(r *http.Request) interface{}

PanicRecoveryFromRequest is a helper function that extract error value when panic occurred, the value is saved to *http.Request after recovery process and right before calling mux.PanicHandler.

func PostgreSQLArray

func PostgreSQLArray(array interface{}) interface {
	driver.Valuer
	sql.Scanner
}

func SQLNoScan

func SQLNoScan() interface{}

Types

type BeginTx

type BeginTx interface {
	BeginTx(ctx context.Context, opts *sql.TxOptions) (tx *sql.Tx, err error)
}

type BoxExec

type BoxExec interface {
	Scan(rowsAffected *int, lastInsertID *int) (err error)
}

type BoxQuery

type BoxQuery interface {
	// Scan accept do, a func that accept `i int` as index and returns a List
	// of pointer.
	//  List == nil   // break the loop
	//  len(List) < 1 // skip the current loop
	//  len(List) > 0 // assign the pointer, must be same as the length of columns
	Scan(row func(i int) List) (err error)
}

type Decoder

type Decoder interface{ Decode(v interface{}) error }

type Encoder

type Encoder interface{ Encode(v interface{}) error }

type ExecContext

type ExecContext interface {
	ExecContext(ctx context.Context, query string, args ...interface{}) (res sql.Result, err error)
}

type List

type List []interface{}

List extends `[]interface{}`, and it should be used to represent json-like data.

func (*List) Add

func (l *List) Add(v ...interface{})

Add new element.

func (*List) Delete

func (l *List) Delete(k int)

Delete an element.

func (*List) Map

func (l *List) Map(fn func(k int, v interface{}))

Map is an alternative of for loop.

type ListError

type ListError struct{ Errors []error }

ListError is a wrapper to slice of `error`.

func (*ListError) Add

func (e *ListError) Add(errs ...error) *ListError

Add will append the given error to the list.

func (*ListError) Err

func (e *ListError) Err() error

Err will validate & remove any nil error in the list.

func (*ListError) Error

func (e *ListError) Error() string

Error is a method that satisfied an error interface.

func (*ListError) Pop

func (e *ListError) Pop(i int) error

Pop will remove the errors from the given index, use i = -1 to remove from last index.

func (*ListError) Unwrap

func (e *ListError) Unwrap() error

type Logger

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

func (*Logger) Level

func (l *Logger) Level(level string) *Logger

func (*Logger) S

func (l *Logger) S() *log.Logger

func (*Logger) Swap

func (l *Logger) Swap() (readOUT, readERR func() ([]byte, error))

func (*Logger) Unswap

func (l *Logger) Unswap()

func (*Logger) WithContext

func (l *Logger) WithContext(ctx context.Context) context.Context

func (*Logger) Z

func (l *Logger) Z() *zerolog.Logger

type Meter

type Meter struct {
	metric.Meter
	// contains filtered or unexported fields
}

func (*Meter) Must

func (m *Meter) Must() metric.MeterMust

func (*Meter) WithContext

func (m *Meter) WithContext(ctx context.Context) context.Context

type MeterConfiguration

type MeterConfiguration struct {
	Name       string
	Prometheus struct {
		HTTPHandlerCallback func(http.Handler)
	}
}

type Mux

type Mux struct {

	// PanicHandler can access the error recovered via PanicRecoveryFromRequest,
	// PanicRecoveryFromRequest is a helper under httphandler package
	PanicHandler    http.Handler
	NotFoundHandler http.Handler
	Middleware      func(next http.Handler) http.Handler
	// contains filtered or unexported fields
}

Mux holds a map of entries.

func (*Mux) Handle

func (m *Mux) Handle(method string, pattern string, next http.Handler) *Mux

Handle will register http.Handler with MuxMatcherMethods on method and MuxMatcherPattern on pattern, see more details on each mux matcher implementation.

func (*Mux) ServeHTTP

func (m *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implement http.Handler interface.

func (*Mux) With

func (m *Mux) With(next http.Handler, matcher MuxMatcher) *Mux

With will register http.Handler with any implementation of MuxMatcher.

type MuxMatcher

type MuxMatcher interface {
	// Test is called when MuxMatcher is added to Mux, this should be an
	// opportunity to set priority and test the implementation parameter
	// e.g. MuxMatcherPattern should check pattern, start, end
	Test() bool

	// Priority is called after Test() returning true to set a priority queue
	Priority() float64

	// Match is called by order of Priority, after its turn, it will validate
	// the *http.Request and if the result is true, the http.Handler registered
	// in the entry will be served
	Match(*http.Request) bool
}

MuxMatcher is an incoming *http.Request matcher.

type Parser

type Parser interface {
	Marshal(v interface{}) (p []byte, err error)
	Unmarshal(p []byte, v interface{}) (err error)
	NewDecoder(r io.Reader) Decoder
	NewEncoder(w io.Writer) Encoder
}
var (
	JSON Parser = json_{jsoniter.ConfigFastest}
	TOML Parser = toml_{}
	YAML Parser = yaml_{}
	XML  Parser = xml_{}
)

nolint: gochecknoglobals

type PingContext

type PingContext interface {
	PingContext(ctx context.Context) (err error)
}

type PrepareContext

type PrepareContext interface {
	PrepareContext(ctx context.Context, query string) (stmt *sql.Stmt, err error)
}

type QueryContext

type QueryContext interface {
	QueryContext(ctx context.Context, query string, args ...interface{}) (rows *sql.Rows, err error)
}

type QueryRowContext

type QueryRowContext interface {
	QueryRowContext(ctx context.Context, query string, args ...interface{}) (row *sql.Row)
}

type SQL

type SQL struct{}

func (SQL) BoxExec

func (SQL) BoxExec(sqlResult sql.Result, err error) BoxExec

BoxExec will wrap `ExecContext` so that we can Scan later

BoxExec(cmd.ExecContext(ctx, "..."))

Scan the result of ExecContext that usually return numbers of rowsAffected and lastInsertID.

func (SQL) BoxQuery

func (SQL) BoxQuery(sqlRows *sql.Rows, err error) BoxQuery

BoxQuery will wrap `QueryContext` so that we can Scan later

BoxQuery(cmd.QueryContext(ctx, "..."))

Scan all the rows and map it into []map[string]interface{} using column name as key in the map and then parse it into json format, after that unmarshal into designated dest.

func (SQL) EndTx

func (SQL) EndTx(tx *sql.Tx, err error) error

EndTx will end transaction with provided *sql.Tx and error. The tx argument should be valid, and then will check the err, if any error occurred, will commencing the ROLLBACK else will COMMIT the transaction.

txc := XSQLTxConn(db) // shared between *sql.Tx, *sql.DB and *sql.Conn
if tx, err := db.BeginTx(ctx, nil); err == nil && tx != nil {
  defer func() { err = xsql.EndTx(tx, err) }()
  txc = tx
}

func (SQL) IsDDLCommand

func (SQL) IsDDLCommand(query string) (ok bool)

IsDDLCommand only valid if starts with CREATE, ALTER, DROP, USE, ADD, EXEC, TRUNCATE.

func (SQL) IsDMLCommand

func (SQL) IsDMLCommand(query string) (ok bool)

IsDMLCommand only valid if starts with INSERT, UPDATE, DELETE.

func (SQL) IsMultipleCommand

func (SQL) IsMultipleCommand(query string) (ok bool)

IsMultipleCommand is a naive implementation of checking multiple sql command.

func (SQL) IsSELECTCommand

func (SQL) IsSELECTCommand(query string) (ok bool)

IsSELECTCommand only valid if starts with SELECT.

func (SQL) IsValidCommand

func (SQL) IsValidCommand(query string) (ok bool)

func (SQL) NewRoundRobin

func (SQL) NewRoundRobin(ctx context.Context, conns ...SQLConn) SQLConn

NewRoundRobin will reduce multiple connections into one with RoundRobin style.

func (SQL) OpenWithDSN

func (SQL) OpenWithDSN(ctx context.Context, dsn string) (conn *sql.DB, err error)

OpenWithDSN will open connection from the given dsn string with URL format, note that any error when opening the database should result in a panic.

func (SQL) RemoveComment

func (SQL) RemoveComment(query string) (query_ string)

RemoveComment from sql command.

func (SQL) SetupOrTeardown

func (SQL) SetupOrTeardown(ctx context.Context, conn ExecContext, queries ...string) error

SetupOrTeardown will execute multiple queries and useful in SETUP/TEARDOWN phase of Behavior Driven Development (BDD). BDD approach is useful to make sure that the SQL Query should satisfied the syntax and reduce integration issues.

type SQLConn

type SQLConn interface {
	BeginTx
	io.Closer
	PingContext
	SQLTxConn
}

SQLConn is a common interface of *sql.DB and *sql.Conn.

type SQLRoundRobinError

type SQLRoundRobinError struct{ Total, Index int }

SQLRoundRobinError reporting issue when getting from set of Conn from SQLRoundRobin.

func (*SQLRoundRobinError) Error

func (e *SQLRoundRobinError) Error() string

type SQLTxConn

SQLTxConn is a common interface of *sql.DB, *sql.Conn, and *sql.Tx.

type Tracer

type Tracer struct {
	trace.Tracer
	// contains filtered or unexported fields
}

func (Tracer) ForceFlush

func (x Tracer) ForceFlush(ctx context.Context) error

func (Tracer) RegisterSpanProcessor

func (x Tracer) RegisterSpanProcessor(s sdk_trace.SpanProcessor)

func (Tracer) Shutdown

func (x Tracer) Shutdown(ctx context.Context) error

func (Tracer) UnregisterSpanProcessor

func (x Tracer) UnregisterSpanProcessor(s sdk_trace.SpanProcessor)

func (*Tracer) WithContext

func (t *Tracer) WithContext(ctx context.Context) context.Context

type TracerConfiguration

type TracerConfiguration struct {
	Name   string
	Jaeger struct{ URL string }
	OTLP   struct {
		GRPC struct{ URL string }
		HTTP struct{ URL string }
	}
}

Jump to

Keyboard shortcuts

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