helper

package module
v0.0.0-...-214db93 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2024 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddDatetime

func AddDatetime(datetime string, year, month, day, hour, min, sec int) string

AddDatetime add date time only format 2006-01-02 15:04:05

func ConvertDateTime

func ConvertDateTime(dateTime, position string) string

func CreateMap

func CreateMap[K comparable, V any, T any](items []T, keyFunc func(T) K, valueFunc func(T) V) map[K]V

func CreateSlice

func CreateSlice[V any, T any](items []T, valueFunc func(T) V) []V

ฟังก์ชันที่สร้าง slice จาก struct ใด ๆ โดยกำหนดค่า value ที่จะนำมาใช้ใน slice นั้นได้

อธิบายโค้ด:

  1. ฟังก์ชัน CreateSlice: ฟังก์ชันนี้ใช้ generics เพื่อสร้าง slice จาก struct ใดๆ โดยกำหนดค่า value ที่จะนำมาใช้ใน slice: • V: ประเภทของ value. • T: ประเภทของ struct.
  2. ตัวอย่างโครงสร้างข้อมูล UserLogin และ Product: สร้าง struct UserLogin และ Product เพื่อใช้เป็นตัวอย่างข้อมูล.
  3. การใช้งานใน main function: • สร้าง slice ของ UserLogin และ Product. • ใช้ CreateSlice เพื่อสร้าง slice โดยกำหนดค่า value ที่จะนำมาใช้ใน slice: • สร้าง slice ที่มี UserName จาก UserLogin (userNames). • สร้าง slice ที่มี ProfileId จาก UserLogin (profileIds). • สร้าง slice ที่มี ProductName จาก Product (productNames). • สร้าง slice ที่มี Price จาก Product (productPrices).
  4. แสดงผลลัพธ์: พิมพ์ผลลัพธ์ของ slice ที่สร้างขึ้นเพื่อแสดงข้อมูลที่ถูกจัดเก็บใน slice.

โค้ดนี้ทำให้คุณสามารถสร้าง slice จาก struct ใดๆ ได้โดยกำหนดค่า value ตามที่ต้องการ, ทำให้มีความยืดหยุ่นในการใช้งาน.

func DBConnectionString

func DBConnectionString(dbConfig DBConfig) (dsn string, driver string)

สร้าง connect string การเชื่อมต่อของฐานข้อมูล

func DBConnects

func DBConnects(dsn string, dbType string) (*gorm.DB, error)

เชื่อมต่อฐานข้อมูล

func Date

func Date(s string) time.Time

func DateTime

func DateTime(s string) time.Time

func DaysBetween

func DaysBetween(a, b time.Time) int

func DaysIn

func DaysIn(year int, m time.Month) int

DaysIn returns the number of days in a month for a given year.

func FormatDate

func FormatDate(dt *time.Time) (string, string)

FormatDate format date

func GetDate

func GetDate(date string) string

GetDate get date from string datetime format

func GetDateTime

func GetDateTime(datetime string) string

GetDate get date from string datetime format

func GetDateTimeByDate

func GetDateTimeByDate(day time.Time) time.Time

GetDateTimeByDate get datetime by date timezone (th)

func GetNextDateTime

func GetNextDateTime(day int) time.Time

GetNextDateTime get datetime tomorrow timezone (th)

func GetPreviousDateTime

func GetPreviousDateTime(day int) time.Time

GetPreviousDateTime get datetime yesterday timezone (th)

func GetTime

func GetTime(datetime string) string

func GetToday

func GetToday() time.Time

GetToday get datetime today timezone (th)

func GetYesterday

func GetYesterday() time.Time

GetYesterday get datetime yesterday timezone (th)

func InTimeSpan

func InTimeSpan(start, end, check time.Time) bool

func IsValidDay

func IsValidDay(value time.Weekday) bool

IsValidDay check if day is more than 6(Saturday time.Weekday)

func IsValidTime

func IsValidTime(openTime, closeTime string) bool

IsValidTime check if open and close time is in hours: 00-23 and minutes: 00-59

func LoadLocation

func LoadLocation() *time.Location

LoadLocation returns The time zone.

func ReDatetime

func ReDatetime(datetime string, year, month, day, hour, min, sec int) string

ReduceDatetime reduce re date time only format 2006-01-02 15:04:05

func RenderJSON

func RenderJSON(c *fiber.Ctx, err error, successResponse interface{}) error

ฟังก์ชันที่จัดการ error response และ success response

func RespJson

func RespJson(c *fiber.Ctx, fn interface{}, input interface{}) error

func RespJsonNoReq

func RespJsonNoReq(c *fiber.Ctx, fn interface{}) error

func RespSuccess

func RespSuccess(c *fiber.Ctx, fn interface{}, input interface{}) error

func ShortDate

func ShortDate(date time.Time, language string) string

ShortDate short date

func ShortMonth

func ShortMonth(date time.Time, language string) string

ShortYearMonth short month

func ShortYearMonth

func ShortYearMonth(date time.Time, language string) string

ShortYearMonth short month

func TimeNowLocationTH

func TimeNowLocationTH() time.Time

TimeNowLocationTH get time location thai

func ToMap

func ToMap[K comparable, T any](items []T, keyFunc func(T) K) map[K]T

ToMap แปลง slice ของ struct ใด ๆ ที่ใช้ key เป็นประเภทเดียวกันกับค่าที่ส่งเข้ามา K comparable: แทนที่ด้วย type ใด ๆ ก็ได้ที่สามารถใช้เป็น key ใน map (เช่น int, string, etc.) T any: แทนที่ด้วย type ใด ๆ ก็ได้ (Generics) items []T: slice ของ struct ที่ต้องการแปลง keyFunc func(T) K: ฟังก์ชันที่ใช้ในการดึง key จาก struct

อธิบายการทำงานของโค้ด:

  1. การประกาศฟังก์ชัน ToMap: • ฟังก์ชันนี้ใช้ Generics เพื่อให้สามารถทำงานกับ key และ value ที่เป็น type ใดก็ได้ • K comparable: กำหนดให้ K เป็นประเภทที่สามารถใช้เป็น key ใน map ได้ (ต้องสามารถเปรียบเทียบได้ เช่น int, string เป็นต้น) • T any: กำหนดให้ T เป็นประเภทใดก็ได้ • items []T: รับ slice ของ struct ที่ต้องการแปลง • keyFunc func(T) K: รับฟังก์ชันที่ใช้ในการดึง key จาก struct โดยคืนค่าเป็นประเภท K
  2. การสร้าง map: • สร้าง map ที่ชื่อ result โดยมี key เป็นประเภท K และ value เป็นประเภท T
  3. การวน loop ผ่านแต่ละ item ของ slice: • ใช้ for loop เพื่อวนผ่านแต่ละ item ใน slice items • ใช้ keyFunc เพื่อดึง key จาก item • นำ item ไปเก็บใน map result โดยใช้ key ที่ดึงมาได้
  4. การคืนค่า: • คืนค่า map ที่สร้างขึ้น

การใช้งาน:

ในฟังก์ชัน main:

  • สร้าง instance ของ struct User และกำหนดค่า field ต่าง ๆ
  • เรียกฟังก์ชัน ToMap เพื่อแปลง struct เป็น map โดยใช้ ID (ประเภท uint) เป็น key
  • เรียกฟังก์ชัน ToMap เพื่อแปลง struct เป็น map โดยใช้ Username (ประเภท string) เป็น key
  • พิมพ์ผลลัพธ์ที่ได้ออกมา

ผลลัพธ์จะเป็น map ที่มี key เป็นประเภทที่กำหนด (เช่น uint, string) และค่าของ map เป็น struct User ซึ่งสามารถใช้งานต่อไปได้ตามต้องการ

func ToTripleNestedMap

func ToTripleNestedMap[K1 comparable, K2 comparable, K3 comparable, T any](items []T, keyFunc1 func(T) K1, keyFunc2 func(T) K2, keyFunc3 func(T) K3) map[K1]map[K2]map[K3]T

ToTripleNestedMap แปลง slice ของ struct ใด ๆ ที่ใช้ key เป็นประเภทเดียวกันกับค่าที่ส่งเข้ามาเป็น map ซ้อนกันสามชั้น K1, K2, K3 comparable: แทนที่ด้วย type ใด ๆ ก็ได้ที่สามารถใช้เป็น key ใน map (เช่น int, string, etc.) T any: แทนที่ด้วย type ใด ๆ ก็ได้ (Generics) items []T: slice ของ struct ที่ต้องการแปลง keyFunc1, keyFunc2, keyFunc3 func(T) K: ฟังก์ชันที่ใช้ในการดึง key จาก struct

func Transaction

func Transaction(db *gorm.DB, handler func(*gorm.DB) error) error

สำหรับ Transaction

func ValidateDate

func ValidateDate(fl validator.FieldLevel) bool

ValidateDate คือ custom validation function สำหรับการตรวจสอบวันที่

Types

type CustomValidator

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

CustomValidator คือ struct ที่ใช้เพื่อสร้าง custom validator

func NewValidator

func NewValidator() *CustomValidator

NewValidator คือฟังก์ชั่นที่ใช้สร้าง custom validator

type DBConfig

type DBConfig struct {
	Host         string //Host Database
	Port         int    //Port ที่ใช้
	UserName     string //ชื่อผู้ใช้
	Password     string //รหัสผ่าน
	DatabaseName string //ชื่อฐานข้อมูล
	DriverName   string //ชื่อของ DRIVER ของฐานข้อมูล (postgres,mysql,sqlserver)
	ConnectName  string //ชื่อการเชื่อมต่อ (กำหนดเองได้)
}

type ErrorResponse

type ErrorResponse struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
}

ErrorResponse represents the structure of a JSON error response

type Message

type Message struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
}

func NewSuccessMessage

func NewSuccessMessage() *Message

type ResponseResult

type ResponseResult struct {
	Code    int         `json:"code"`
	Message string      `json:"message"`
	Result  interface{} `json:"result"`
}

Jump to

Keyboard shortcuts

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