Skip to content

Commit

Permalink
util: move isnilish function to util package
Browse files Browse the repository at this point in the history
  • Loading branch information
wuriyanto authored and wuriyanto committed Nov 1, 2024
1 parent 02bbf59 commit 51abcb8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 23 deletions.
5 changes: 3 additions & 2 deletions runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/telkomdev/tob/services/postgres"
"github.com/telkomdev/tob/services/redisdb"
"github.com/telkomdev/tob/services/web"
"github.com/telkomdev/tob/util"
)

// Runner the tob runner
Expand Down Expand Up @@ -199,7 +200,7 @@ func healthCheck(n string, s tob.Service, t *time.Ticker, waiter tob.Waiter) {
}

for _, notificator := range s.GetNotificators() {
if !tob.IsNilish(notificator) {
if !util.IsNilish(notificator) {
if notificator.IsEnabled() {
err := notificator.Send(notificatorMessage)
if err != nil {
Expand All @@ -221,7 +222,7 @@ func healthCheck(n string, s tob.Service, t *time.Ticker, waiter tob.Waiter) {
}

for _, notificator := range s.GetNotificators() {
if !tob.IsNilish(notificator) {
if !util.IsNilish(notificator) {
if notificator.IsEnabled() {
err := notificator.Send(notificatorMessage)
if err != nil {
Expand Down
21 changes: 0 additions & 21 deletions tob.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package tob

import (
"reflect"
)

const (
// Version number

Expand All @@ -15,20 +11,3 @@ const (
// NotOk service status
NotOk = "NOT_OK"
)

// IsNilish function is a function to check for nullability on the interface
func IsNilish(val any) bool {
if val == nil {
return true
}

v := reflect.ValueOf(val)
k := v.Kind()
switch k {
case reflect.Chan, reflect.Func, reflect.Map, reflect.Pointer,
reflect.UnsafePointer, reflect.Interface, reflect.Slice:
return v.IsNil()
}

return false
}
18 changes: 18 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package util

import (
"reflect"
"strconv"
)

Expand Down Expand Up @@ -56,3 +57,20 @@ func InterfaceToFloat64(val interface{}) float64 {

return i
}

// IsNilish function is a function to check for nullability on the interface
func IsNilish(val any) bool {
if val == nil {
return true
}

v := reflect.ValueOf(val)
k := v.Kind()
switch k {
case reflect.Chan, reflect.Func, reflect.Map, reflect.Pointer,
reflect.UnsafePointer, reflect.Interface, reflect.Slice:
return v.IsNil()
}

return false
}

0 comments on commit 51abcb8

Please sign in to comment.