Skip to content

Commit

Permalink
improve error in bindings.go
Browse files Browse the repository at this point in the history
Signed-off-by: Adrian Moreno <[email protected]>
  • Loading branch information
amorenoz committed Mar 5, 2021
1 parent 7cfd014 commit bdd34c4
Show file tree
Hide file tree
Showing 2 changed files with 211 additions and 44 deletions.
65 changes: 55 additions & 10 deletions bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,43 @@ var (
nilType = reflect.TypeOf(nil)
)

// ErrWrongType describes typing error
type ErrWrongType struct {
from string
expected string
got interface{}
}

func (e *ErrWrongType) Error() string {
return fmt.Sprintf("Wrong Type (%s): expected %s but got %s (%s)",
e.from, e.expected, e.got, reflect.TypeOf(e.got))
}

// NewErrWrongType creates a new ErrWrongType
func NewErrWrongType(from, expected string, got interface{}) error {
return &ErrWrongType{
from: from,
expected: expected,
got: got,
}
}

// ErrBadSchema describes an error in the provided columnSchema
type ErrBadSchema struct {
column *ColumnSchema
}

func (e *ErrBadSchema) Error() string {
return fmt.Sprintf("Bad Schema: %v", e.column)
}

// NewErrBadSchema creates a new ErrBadSchema
func NewErrBadSchema(column *ColumnSchema) error {
return &ErrBadSchema{
column: column,
}
}

// nativeTypeFromExtended returns the native type that can hold a value of an
// Extended type
func nativeTypeFromExtended(extended ExtendedType) (reflect.Type, error) {
Expand All @@ -28,7 +65,7 @@ func nativeTypeFromExtended(extended ExtendedType) (reflect.Type, error) {
case TypeUUID:
return strType, nil
default:
return nilType, fmt.Errorf("Failed to determine type of column %s", extended)
return nilType, fmt.Errorf("%s is not an native type", extended)
}
}

Expand All @@ -38,7 +75,7 @@ func NativeValueOf(elem interface{}, elemType ExtendedType) (reflect.Value, erro
if elemType == TypeUUID {
uuid, ok := elem.(UUID)
if !ok {
return reflect.ValueOf(nil), fmt.Errorf("Element in should be convertible to UUID. Instead got %s", reflect.TypeOf(elem))
return reflect.ValueOf(nil), NewErrWrongType("NativeValueOf", "UUID", elem)
}
return reflect.ValueOf(uuid.GoUUID), nil
}
Expand Down Expand Up @@ -75,7 +112,7 @@ func NativeType(column *ColumnSchema) (reflect.Type, error) {
}
return reflect.SliceOf(kType), nil
default:
return reflect.TypeOf(nil), fmt.Errorf("Failed to determine type of column %s", column)
return reflect.TypeOf(nil), NewErrBadSchema(column)
}
}

Expand All @@ -87,12 +124,15 @@ func OvsToNative(column *ColumnSchema, ovsElem interface{}) (interface{}, error)
}
switch column.Type {
case TypeInteger, TypeReal, TypeString, TypeBoolean, TypeEnum:
if reflect.TypeOf(ovsElem) != naType {
return nil, NewErrWrongType("OvsToNative", naType.String(), ovsElem)
}
// Atomic types should have the same underlying type
return ovsElem, nil
case TypeUUID:
uuid, ok := ovsElem.(UUID)
if !ok {
return nil, fmt.Errorf("Element in column should be convertible to UUID. Instead got %s", reflect.TypeOf(ovsElem))
return nil, NewErrWrongType("OvsToNative", "UUID", ovsElem)
}
return uuid.GoUUID, nil
case TypeSet:
Expand All @@ -110,6 +150,9 @@ func OvsToNative(column *ColumnSchema, ovsElem interface{}) (interface{}, error)
if err != nil {
return nil, err
}
if vv.Type() != naType.Elem() {
return nil, NewErrWrongType("OvsToNative", fmt.Sprintf("convertible to %s", naType), ovsElem)
}
nativeSet = reflect.Append(nativeSet, vv)
}

Expand All @@ -126,7 +169,7 @@ func OvsToNative(column *ColumnSchema, ovsElem interface{}) (interface{}, error)
}

if !vv.Type().ConvertibleTo(keyType) {
return nil, fmt.Errorf("Element in column should be convertible to Set of %s. Instead got %s", column.TypeObj.Key.Type, reflect.TypeOf(ovsElem))
return nil, NewErrWrongType("OvsToNative", keyType.String(), ovsElem)
}
nativeSet = reflect.Append(nativeSet, vv)
}
Expand All @@ -135,7 +178,7 @@ func OvsToNative(column *ColumnSchema, ovsElem interface{}) (interface{}, error)
case TypeMap:
ovsMap, ok := ovsElem.(OvsMap)
if !ok {
return nil, fmt.Errorf("Element in column should be convertible to Map. Instead got %s", reflect.TypeOf(ovsElem))
return nil, NewErrWrongType("OvsToNative", "OvsMap", ovsElem)
}
// The inner slice is map[interface]interface{}
// We need to convert it to the real type os slice
Expand All @@ -149,11 +192,14 @@ func OvsToNative(column *ColumnSchema, ovsElem interface{}) (interface{}, error)
if err != nil {
return nil, err
}
if vv.Type() != naType.Elem() || kk.Type() != naType.Key() {
return nil, NewErrWrongType("OvsToNative", fmt.Sprintf("convertible to %s", naType), ovsElem)
}
nativeMap.SetMapIndex(kk, vv)
}
return nativeMap.Interface(), nil
default:
return nil, fmt.Errorf("Unknown type %s", column.Type)
return nil, NewErrBadSchema(column)
}
}

Expand All @@ -165,8 +211,7 @@ func NativeToOvs(column *ColumnSchema, rawElem interface{}) (interface{}, error)
}

if t := reflect.TypeOf(rawElem); t != naType {
return nil, fmt.Errorf("Bad Type in column expected %s, got %s (%v)",
naType.String(), t.String(), rawElem)
return nil, NewErrWrongType("NativeToOvs", naType.String(), rawElem)
}

switch column.Type {
Expand Down Expand Up @@ -198,6 +243,6 @@ func NativeToOvs(column *ColumnSchema, rawElem interface{}) (interface{}, error)
}
return ovsMap, nil
default:
return nil, fmt.Errorf("Unsuppored type %s", column.Type)
return nil, NewErrBadSchema(column)
}
}
Loading

0 comments on commit bdd34c4

Please sign in to comment.