Skip to content

Commit

Permalink
fix: create helper functions to safely deref pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielg2020 committed Nov 3, 2024
1 parent af29513 commit 975c35b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
11 changes: 6 additions & 5 deletions api/entity/move.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package entity

import (
"errors"
"github.com/gabrielg2020/chess-api/api/service/helper_service"
"github.com/gabrielg2020/chess-api/pkg/logger"
"github.com/sirupsen/logrus"
"strconv"
Expand Down Expand Up @@ -32,11 +33,11 @@ type MoveEntity struct {

func NewMoveEntity(fromX *int, fromY *int, toX *int, toY *int, promotion *int, isCastling *bool, isEnPassant *bool, captured *int) *MoveEntity {
logger.Log.WithFields(logrus.Fields{
"fromX": *fromX, "fromY": *fromY,
"toX": *toX, "toY": *toY,
"promotion": *promotion,
"isCastling": *isCastling, "isEnPassant": *isEnPassant,
"captured": *captured,
"fromX": HelperService.IntValue(fromX), "fromY": HelperService.IntValue(fromY),
"toX": HelperService.IntValue(toX), "toY": HelperService.IntValue(toY),
"promotion": HelperService.IntValue(promotion),
"isCastling": HelperService.BoolValue(isCastling), "isEnPassant": HelperService.BoolValue(isEnPassant),
"captured": HelperService.IntValue(captured),
}).Debug("NewMoveEntity: Created a new MoveEntity")

return &MoveEntity{
Expand Down
14 changes: 14 additions & 0 deletions api/service/helper_service/helper_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,17 @@ func BoolPtr(b bool) *bool {
func IntBoardArrayPtr(a [8][8]int) *[8][8]int {
return &a
}

func IntValue(p *int) interface{} {
if p != nil {
return *p
}
return nil
}

func BoolValue(p *bool) interface{} {
if p != nil {
return *p
}
return nil
}

0 comments on commit 975c35b

Please sign in to comment.