cookbook

package module
v1.11.0 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2021 License: MIT Imports: 8 Imported by: 18

README

Cookbook

Build Status Coverage Status Go Report Card GoDoc

Toolkit or helper library for Go(Golang) development

Documentation

Index

Constants

View Source
const (
	// Bearer
	Bearer = "Bearer"

	// XRequestID HTTP request header key X-Request-ID
	XRequestID    = "X-Request-ID"
	HContentType  = "Content-Type"
	HAccept       = "Accept"
	HJSONType     = "application/json"
	HJSONTypeUTF8 = "application/json; charset=utf-8"
)

Variables

This section is empty.

Functions

func BoolEnv

func BoolEnv(key string) (bool, error)

BoolEnv used for get bool value from environment

func IntEnv

func IntEnv(key string) (int, error)

IntEnv used for get bool value from environment

func ParsePtrGRPCTimeToPtrTime added in v1.11.0

func ParsePtrGRPCTimeToPtrTime(t *timestamppb.Timestamp) *time.Time

ParsePtrGRPCTimeToPtrTime Parse pointer timestamp from GRPC struct and return nil if empty or invalid timestamp

func ParsePtrGRPCTimeToTime added in v1.11.0

func ParsePtrGRPCTimeToTime(t *timestamppb.Timestamp) time.Time

ParsePtrGRPCTimeToTime parse pointer timestamp from GRPC and return new timestamp if nil or error

func ParsePtrString added in v1.8.5

func ParsePtrString(s *string) string

ParsePtrString function check pointer string, if nil return empty string

func ParsePtrTimeToTime added in v1.11.0

func ParsePtrTimeToTime(t *time.Time) time.Time

ParsePtrTimeToTime parse pointer time to time, timezone UTC

func ParseStringNil added in v1.8.5

func ParseStringNil(s string) *string

ParseStringNil function to check string if empty return nil

func ParseStringToFloat64

func ParseStringToFloat64(str string, def float64) float64

ParseStringToFloat64 Parse string to float64, error return default value

func ParseStringToInt

func ParseStringToInt(str string, def int) int

ParseStringToInt Parse string to int, error return default value

func ParseStringToPhone added in v1.8.6

func ParseStringToPhone(phone, defaultRegionCode string, phoneFormat phonenumbers.PhoneNumberFormat) (string, error)

ParseStringToPhone Function to parse string to formatted phone number See details documentation in https://github.com/nyaruka/phonenumbers defaultRegionCode using ISO 3166-Alpha 2

func ParseTimeToPtrTime added in v1.11.0

func ParseTimeToPtrTime(t time.Time) *time.Time

ParseTimeToPtrTime parse time to pointer time, timezone UTC

func StringEnv

func StringEnv(key string) (string, error)

StringEnv used for get string value from environment

func Stringify

func Stringify(str interface{}) string

Stringify Convert interface to string, error will return string error message

Types

type JSON added in v1.8.0

type JSON map[string]interface{}

type JSend

type JSend struct {
	Status  string      `json:"status"`
	Message string      `json:"message,omitempty"`
	Data    interface{} `json:"data,omitempty"`
	Meta    *Meta       `json:"meta,omitempty"`
	Link    *Links      `json:"links,omitempty"`
	Code    interface{} `json:"code,omitempty"`
}

JSend used JSend format with some modification See https://jsonapi.org/ and https://github.com/omniti-labs/jsend for references

{
	"meta": {
		"page": {
			"number": "page number",
			"size": "page size",
			"total": "total page"
		}
	},
	"links": {
		"first": "first page",
		"next": "next page",
		"prev": "previous page",
		"last": "last page"
	},
	"status": "message status return with success, failed, or error",
	"message": "return message if error",
	"data": "return data if success with data or failed",
	"code": "error status code"
}

func ErrorResponse added in v1.7.0

func ErrorResponse(msg string, code interface{}) JSend

ErrorResponse is used return response with JSON format if failure in server side See https://jsonapi.org/ and https://github.com/omniti-labs/jsend for references

{
	"status": "error",
	"message": "error message",
	"code": "error code"
}

func FailResponse

func FailResponse(data interface{}) JSend

FailResponse is used to return response with JSON format if failure from client side See https://jsonapi.org/ and https://github.com/omniti-labs/jsend for references

{
	"status": "failed",
	"data": [
		{
			"field": "field",
			"code": "failed code",
			"message": "error message"
		}
	],
}

func SuccessDataResponse

func SuccessDataResponse(data interface{}, meta *Meta, links *Links) JSend

SuccessDataResponse used to return response JSON format if have data value See https://jsonapi.org/ and https://github.com/omniti-labs/jsend for references

{
	"status": "success",
	"data": "response data",
	"meta": "optional",
	"links": "optional"
}

func SuccessResponse

func SuccessResponse() JSend

SuccessResponse used to return response with JSON format success See https://jsonapi.org/ and https://github.com/omniti-labs/jsend for references

{
	"status": "success"
}

func (JSend) Stringify

func (j JSend) Stringify() string

Stringify make JSend struct to string

type Links struct {
	First string `json:"first,omitempty"`
	Next  string `json:"next,omitempty"`
	Prev  string `json:"prev,omitempty"`
	Last  string `json:"last,omitempty"`
}

Links for meta data JSON response See https://jsonapi.org/ and https://github.com/omniti-labs/jsend for references

{
	"first": "first page",
	"next": "next page",
	"prev": "previous page",
	"last": "last page"
}
func NewLinks(next, prev, first, last string) *Links

NewLinks build new links struct See https://jsonapi.org/ and https://github.com/omniti-labs/jsend for references

{
	"first": "first page",
	"next": "next page",
	"prev": "previous page",
	"last": "last page"
}

type Meta

type Meta struct {
	Page *Page `json:"page,omitempty"`
}

Meta data used for JSON response See https://jsonapi.org/ and https://github.com/omniti-labs/jsend for references

{
	"meta": {
		"page": {
			"number": "page number",
			"size": "page size",
			"total": "total page"
		}
	}
}

func NewMeta

func NewMeta(page *Page) *Meta

NewMeta build new meta struct See https://jsonapi.org/ and https://github.com/omniti-labs/jsend for references

{
	"meta": {
		"page": {
			"number": "page number",
			"size": "page size",
			"total": "total page"
		}
	}
}

type Page added in v1.11.0

type Page struct {
	Number int `json:"number,omitempty"`
	Size   int `json:"size,omitempty"`
	Total  int `json:"total,omitempty"`
}

Page pagination information data See https://jsonapi.org/ and https://github.com/omniti-labs/jsend for references

func NewPage added in v1.11.0

func NewPage(number, size, total int) *Page

NewPage of return data with total of data, size per page, and number page See https://jsonapi.org/ and https://github.com/omniti-labs/jsend for references

{
	"page": {
		"number": "page number",
		"size": "page size",
		"total": "total page"
	}
}

Directories

Path Synopsis
negroni

Jump to

Keyboard shortcuts

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