From 6b7121bdf6e6b24406f65ccf0671a704274b37d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Tikvi=C4=87?= Date: Thu, 4 Jul 2019 11:44:53 +0200 Subject: [PATCH] nil safe default values --- default_values.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/default_values.go b/default_values.go index ab1d24b..396bf70 100644 --- a/default_values.go +++ b/default_values.go @@ -63,3 +63,52 @@ func Float64OrDefault(v *float64) float64 { } return 0.0 } + +func NilSafeInt32OrDefault(val *interface{}) int32 { + if *val != nil { + return (*val).(int32) + } + return 0 +} + +func NilSafeInt64OrDefault(val *interface{}) int64 { + if *val != nil { + return (*val).(int64) + } + return 0 +} + +func NilSafeUint32OrDefault(val *interface{}) uint32 { + if *val != nil { + return (*val).(uint32) + } + return 0 +} + +func NilSafeUint64OrDefault(val *interface{}) uint64 { + if *val != nil { + return (*val).(uint64) + } + return 0 +} + +func NilSafeFloat32OrDefault(val *interface{}) float32 { + if *val != nil { + return (*val).(float32) + } + return 0.0 +} + +func NilSafeFloat64OrDefault(val *interface{}) float64 { + if *val != nil { + return (*val).(float64) + } + return 0.0 +} + +func NilSafeStringOrDefault(val *interface{}) string { + if *val != nil { + return (*val).(string) + } + return "" +} -- 1.8.1.2