@@ -2,6 +2,7 @@ package model
2
2
3
3
import (
4
4
"encoding/json"
5
+ "errors"
5
6
"fmt"
6
7
"reflect"
7
8
"strconv"
@@ -12,6 +13,8 @@ import (
12
13
geojson "github.com/paulmach/go.geojson"
13
14
)
14
15
16
+ var ErrInvalidCastingAttributeEntity = errors .New ("could not cast the attribute of the entity" )
17
+
15
18
// Entity is a context entity, i.e. a thing in the NGSI model.
16
19
type Entity struct {
17
20
Id string `json:"id"`
@@ -684,7 +687,11 @@ func (a *Attribute) GetAsString() (string, error) {
684
687
if a .Type != StringType && a .Type != TextType {
685
688
return "" , fmt .Errorf ("Attribute is nor String or Text, but %s" , a .Type )
686
689
}
687
- return a .Value .(string ), nil
690
+ rawString , ok := a .Value .(string )
691
+ if ! ok {
692
+ return "" , ErrInvalidCastingAttributeEntity
693
+ }
694
+ return rawString , nil
688
695
}
689
696
690
697
func (a * Attribute ) GetAsInteger () (int , error ) {
@@ -697,21 +704,28 @@ func (a *Attribute) GetAsInteger() (int, error) {
697
704
} else {
698
705
return int (fValue ), nil
699
706
}
700
- return int (a .Value .(float64 )), nil
701
707
}
702
708
703
709
func (a * Attribute ) GetAsFloat () (float64 , error ) {
704
710
if a .Type != FloatType && a .Type != NumberType {
705
711
return 0 , fmt .Errorf ("Attribute is nor Float or Number, but %s" , a .Type )
706
712
}
707
- return a .Value .(float64 ), nil
713
+ rawFloat , ok := a .Value .(float64 )
714
+ if ! ok {
715
+ return 0 , ErrInvalidCastingAttributeEntity
716
+ }
717
+ return rawFloat , nil
708
718
}
709
719
710
720
func (a * Attribute ) GetAsBoolean () (bool , error ) {
711
721
if a .Type != BooleanType {
712
722
return false , fmt .Errorf ("Attribute is not Boolean, but %s" , a .Type )
713
723
}
714
- return a .Value .(bool ), nil
724
+ rawBool , ok := a .Value .(bool )
725
+ if ! ok {
726
+ return false , ErrInvalidCastingAttributeEntity
727
+ }
728
+ return rawBool , nil
715
729
}
716
730
717
731
func (a * Attribute ) GetAsDateTime () (time.Time , error ) {
0 commit comments