Commit 6b7121bdf6e6b24406f65ccf0671a704274b37d7

Authored by Marko Tikvić
1 parent 70f2e7ead2
Exists in master

nil safe default values

Showing 1 changed file with 49 additions and 0 deletions   Show diff stats
default_values.go
... ... @@ -63,3 +63,52 @@ func Float64OrDefault(v *float64) float64 {
63 63 }
64 64 return 0.0
65 65 }
  66 +
  67 +func NilSafeInt32OrDefault(val *interface{}) int32 {
  68 + if *val != nil {
  69 + return (*val).(int32)
  70 + }
  71 + return 0
  72 +}
  73 +
  74 +func NilSafeInt64OrDefault(val *interface{}) int64 {
  75 + if *val != nil {
  76 + return (*val).(int64)
  77 + }
  78 + return 0
  79 +}
  80 +
  81 +func NilSafeUint32OrDefault(val *interface{}) uint32 {
  82 + if *val != nil {
  83 + return (*val).(uint32)
  84 + }
  85 + return 0
  86 +}
  87 +
  88 +func NilSafeUint64OrDefault(val *interface{}) uint64 {
  89 + if *val != nil {
  90 + return (*val).(uint64)
  91 + }
  92 + return 0
  93 +}
  94 +
  95 +func NilSafeFloat32OrDefault(val *interface{}) float32 {
  96 + if *val != nil {
  97 + return (*val).(float32)
  98 + }
  99 + return 0.0
  100 +}
  101 +
  102 +func NilSafeFloat64OrDefault(val *interface{}) float64 {
  103 + if *val != nil {
  104 + return (*val).(float64)
  105 + }
  106 + return 0.0
  107 +}
  108 +
  109 +func NilSafeStringOrDefault(val *interface{}) string {
  110 + if *val != nil {
  111 + return (*val).(string)
  112 + }
  113 + return ""
  114 +}
... ...