Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion golang/config/initializers.config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func InitLogger(appName string) {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}

if utils.IsLocalEnv() {
if utils.IsLocalEnv() || utils.IsDevelopmentEnv() {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
} else {
writer := &loggers.InfoDebugWriter{
Expand Down
18 changes: 15 additions & 3 deletions golang/gql/float32.gqlScalar.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"math"
"strconv"

"github.com/rs/zerolog/log"
)

type Float32 float32
Expand All @@ -19,7 +21,7 @@ func (f *Float32) UnmarshalGraphQL(input interface{}) error {
case float32:
*f = Float32(v)
case float64:
if v < -math.MaxFloat64 || v > math.MaxFloat64 {
if v < -math.MaxFloat32 || v > math.MaxFloat32 {
return fmt.Errorf("value out of range for float32")
}
*f = Float32(v)
Expand All @@ -32,9 +34,14 @@ func (f *Float32) UnmarshalGraphQL(input interface{}) error {
case int32:
*f = Float32(v)
case int64:
if v < math.MinInt64 || v > math.MaxInt64 {
if float64(v) < -math.MaxFloat32 || float64(v) > math.MaxFloat32 {
return fmt.Errorf("value out of range for float32")
}

if v < -16777216 || v > 16777216 {
log.Warn().Int64("value", v).Msg("Warning: value may lose precision in float32")
}

*f = Float32(v)
case uint8:
*f = Float32(v)
Expand All @@ -43,9 +50,14 @@ func (f *Float32) UnmarshalGraphQL(input interface{}) error {
case uint32:
*f = Float32(v)
case uint64:
if v > ^uint64(0) {
if float64(v) > float64(math.MaxFloat32) {
return fmt.Errorf("value out of range for float32")
}

if v > 16777216 {
log.Warn().Uint64("value", v).Msg("Warning: value may lose precision in float32")
}

*f = Float32(v)
case json.Number:
val, err := v.Float64()
Expand Down
45 changes: 45 additions & 0 deletions golang/gql/metadata.payload.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package gql

import (
"github.com/BehemothLtd/behemoth-pkg/golang/pagination"
)

type MetadataPayload struct {
Metadata pagination.Metadata
}

func (mt *MetadataPayload) Total() *Uint32 {
return (*Uint32)(&mt.Metadata.Total)
}

func (mt *MetadataPayload) PerPage() *Uint32 {
return (*Uint32)(&mt.Metadata.PerPage)
}

func (mt *MetadataPayload) Page() *Uint32 {
return (*Uint32)(&mt.Metadata.Page)
}

func (mt *MetadataPayload) Pages() *Uint32 {
return (*Uint32)(&mt.Metadata.Pages)
}

func (mt *MetadataPayload) Count() *Uint32 {
return (*Uint32)(&mt.Metadata.Count)
}

func (mt *MetadataPayload) Next() *Uint32 {
return (*Uint32)(&mt.Metadata.Next)
}

func (mt *MetadataPayload) Prev() *Uint32 {
return (*Uint32)(&mt.Metadata.Prev)
}

func (mt *MetadataPayload) From() *Uint32 {
return (*Uint32)(&mt.Metadata.From)
}

func (mt *MetadataPayload) To() *Uint32 {
return (*Uint32)(&mt.Metadata.To)
}
32 changes: 32 additions & 0 deletions golang/gql/pagy.input.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package gql

import (
"github.com/BehemothLtd/behemoth-pkg/golang/pagination"
)

type PagyInput struct {
PerPage *int32
Page *int32
}

// ToPaginationInput converts PagyInput to models.PaginationData.
func ToPaginationInput[T any](input *PagyInput) pagination.PaginationData[T] {
paginationInput := pagination.PaginationData[T]{
Metadata: pagination.Metadata{
Page: 1,
PerPage: 10,
},
}

if input != nil {
if input.Page != nil && *input.Page >= 1 {
paginationInput.Metadata.Page = uint32(*input.Page)
}

if input.PerPage != nil && *input.PerPage >= 1 {
paginationInput.Metadata.PerPage = uint32(*input.PerPage)
}
}

return paginationInput
}
10 changes: 6 additions & 4 deletions golang/pagination/pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,22 @@ type Metadata struct {
To uint32
}

type PaginationData struct {
type PaginationData[T any] struct {
Metadata Metadata
Collection interface{}
Collection T
}

func Paginate(ctx context.Context, db *gorm.DB, p *PaginationData, tableName string) (func(db *gorm.DB) *gorm.DB, error) {
func Paginate[T any](ctx context.Context, db *gorm.DB, p *PaginationData[T], tableName string) (func(db *gorm.DB) *gorm.DB, error) {
log.Debug().Ctx(ctx).Msg("pagination.Paginate")

// Create a new session to avoid side effects
dbClone := db.Session(&gorm.Session{})

// Calculate the total number of records
var totalRecords uint32
dbClone.Select(fmt.Sprintf("COUNT(DISTINCT %s.id)", tableName)).Scan(&totalRecords)
if err := dbClone.Select(fmt.Sprintf("COUNT(DISTINCT %s.id)", tableName)).Scan(&totalRecords).Error; err != nil {
return nil, err
}

// Update pagination metadata
p.Metadata.Total = totalRecords
Expand Down