common

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Common tools and helper functions

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInternalServerError = errors.New("internalServerError")
	ErrNotFound            = errors.New("notFound")
	ErrConflict            = errors.New("alreadyExist")
	ErrBadParamInput       = errors.New("badParamInput")
	ErrEmailDuplicate      = errors.New("emailDuplicated")
	ErrTokenInvalid        = errors.New("tokenInvalid")
	ErrUnauthentication    = errors.New("unAuthentication")
	ErrUnauthorization     = errors.New("unAuthorization")
	ErrInvalidCredential   = errors.New("invalidCredential")
)

Functions

func Bind

func Bind(c *gin.Context, obj interface{}) error

Changed the c.MustBindWith() -> c.ShouldBindWith(). I don't want to auto return 400 when error happened. origin function is here: https://github.com/gin-gonic/gin/blob/master/context.go

func ConvertMapToStruct

func ConvertMapToStruct(m map[string]interface{}, s interface{}) error

func ConvertToDistinctArray

func ConvertToDistinctArray(edges []domain.WordsLink) []string

func InitConfig

func InitConfig()

func RandString

func RandString(n int) string

A helper function to generate random string

func ToPointer

func ToPointer[T any](x T) *T

Types

type CommonError

type CommonError struct {
	Errors map[string]interface{} `json:"errors"`
}

My own Error type that will help return my customized Error info

{"database": {"hello":"no such table", error: "not_exists"}}

func NewError

func NewError(key string, err error) CommonError

Warp the error info in a object

func NewValidatorError

func NewValidatorError(err error) CommonError

To handle the error returned by c.Bind in gin framework https://github.com/go-playground/validator/blob/v9/_examples/translations/main.go

type Configuration

type Configuration struct {
	AppName            string
	AppDomain          string
	AppEmail           string
	TokenSecret        string
	RefreshTokenSecret string
	//database
	DBHost     string
	DBPort     string
	DBUsername string
	DBPassword string
	//mail server
	SMTPHost     *string
	SMTPPort     *string
	SMTPUsername *string
	SMTPPassword *string
	//mailjet
	MailjetPublicKey  *string
	MailjetPrivateKey *string
}
var AppConfig *Configuration

type Nullable

type Nullable struct {
	Value any
}

func (Nullable) ToInt64Ptr

func (n Nullable) ToInt64Ptr() *int64

func (Nullable) ToStringArrayPtr

func (n Nullable) ToStringArrayPtr() *[]string

func (Nullable) ToStringPtr

func (n Nullable) ToStringPtr() *string

type Stream

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

func StreamOf

func StreamOf(slice any) *Stream

func (*Stream) Contains

func (s *Stream) Contains(fn any) bool

dst := StreamOf(hoges).

    Contains(func(hoge *Hoge) bool {
		return hoge.ID == "abc"
	})

要素の存在確認

func (*Stream) Count

func (s *Stream) Count() int

dst := StreamOf(hoges).Count()

要素数を取得

func (*Stream) Filter

func (s *Stream) Filter(fn any) *Stream

dst := StreamOf(hoges).

Filter(func(hoge *Hoge) bool {
	return hoge.Num > 3
}).Out().([]*Hoge)

要素のフィルタリング

func (*Stream) ForEach

func (s *Stream) ForEach(fn any) *Stream

dst := StreamOf(hoges).

    ForEach(func(hoge *Hoge, i int) {
		hoge.ID = "abc"
	})

要素のループ

func (*Stream) Map

func (s *Stream) Map(fn any) *Stream

dst := StreamOf(hoges).

Map(func(hoge *Hoge) string {
	return hoge.ID
}).Out().([]string)

要素の変換

func (*Stream) Out

func (s *Stream) Out() any

結果を出力する

func (*Stream) Reduce

func (s *Stream) Reduce(fn any) any

dst := StreamOf(hoges).

Reduce(func(dst int, num int) int {
	return dst + num
}).(int)

要素の集計

func (*Stream) Sort

func (s *Stream) Sort(fn any) *Stream

dst := StreamOf(hoges).

Sort(func(prev, next *Hoge) bool {
	return prev.SortNum < next.SortNum
}).Out().([]*Hoge)

ソート

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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