Use IfaceAs* inside FieldAs* for CGREvent

This commit is contained in:
TeoV
2019-05-03 14:42:13 +03:00
committed by Dan Christian Bogos
parent d5e5cdd972
commit 0e5e759b89
2 changed files with 7 additions and 47 deletions

View File

@@ -19,9 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package utils
import (
"fmt"
"reflect"
"strconv"
"strings"
"time"
)
@@ -54,11 +52,7 @@ func (ev *CGREvent) FieldAsString(fldName string) (val string, err error) {
if !has {
return "", ErrNotFound
}
val, err = IfaceAsString(iface)
if err != nil {
return "", fmt.Errorf("cannot cast %s to string", fldName)
}
return val, nil
return IfaceAsString(iface)
}
// FieldAsTime returns a field as Time instance
@@ -68,16 +62,7 @@ func (ev *CGREvent) FieldAsTime(fldName string, timezone string) (t time.Time, e
err = ErrNotFound
return
}
var canCast bool
if t, canCast = iface.(time.Time); canCast {
return
}
s, canCast := iface.(string)
if !canCast {
err = fmt.Errorf("cannot cast %s to string", fldName)
return
}
return ParseTimeDetectLayout(s, timezone)
return IfaceAsTime(iface, timezone)
}
// FieldAsDuration returns a field as Duration instance
@@ -87,19 +72,7 @@ func (ev *CGREvent) FieldAsDuration(fldName string) (d time.Duration, err error)
err = ErrNotFound
return
}
var canCast bool
if d, canCast = iface.(time.Duration); canCast {
return
}
if f, canCast := iface.(float64); canCast {
return time.Duration(int64(f)), nil
}
s, canCast := iface.(string)
if !canCast {
err = fmt.Errorf("cannot cast %s to string", fldName)
return
}
return ParseDurationWithNanosecs(s)
return IfaceAsDuration(iface)
}
// FieldAsFloat64 returns a field as float64 instance
@@ -108,15 +81,7 @@ func (ev *CGREvent) FieldAsFloat64(fldName string) (f float64, err error) {
if !has {
return f, ErrNotFound
}
if val, canCast := iface.(float64); canCast {
return val, nil
}
csStr, canCast := iface.(string)
if !canCast {
err = fmt.Errorf("cannot cast %s to string", fldName)
return
}
return strconv.ParseFloat(csStr, 64)
return IfaceAsFloat64(iface)
}
func (ev *CGREvent) TenantID() string {

View File

@@ -18,8 +18,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package utils
import (
"fmt"
"reflect"
"strings"
"testing"
"time"
)
@@ -118,7 +118,6 @@ func TestCGREventFieldAsString(t *testing.T) {
}
func TestCGREventFieldAsFloat64(t *testing.T) {
err1 := fmt.Errorf("cannot cast %s to string", AnswerTime)
se := &CGREvent{
Tenant: "cgrates.org",
ID: "supplierEvent1",
@@ -154,12 +153,8 @@ func TestCGREventFieldAsFloat64(t *testing.T) {
t.Errorf("Expecting: %+v, received: %+v", 0, answ)
}
answ, err = se.FieldAsFloat64(AnswerTime)
if !reflect.DeepEqual(err, err1) {
t.Error(err)
}
if answ != 0 {
t.Errorf("Expecting: %+v, received: %+v", 0, answ)
if _, err := se.FieldAsFloat64(AnswerTime); err == nil || !strings.HasPrefix(err.Error(), "cannot convert field") {
t.Errorf("Unexpected error : %+v", err)
}
}
func TestCGREventClone(t *testing.T) {