VOL-3190 throw error message when filter or order on struct
Change-Id: If15983350d3f1a6fd21e5bfcec33ae57808cc31f
diff --git a/pkg/order/order.go b/pkg/order/order.go
index bcf097b..09ef1fc 100644
--- a/pkg/order/order.go
+++ b/pkg/order/order.go
@@ -110,6 +110,16 @@
if !field.IsValid() {
return field, fmt.Errorf("Failed to find field %s while sorting", name)
}
+
+ // we might have a pointer to a struct at this time, so dereference it
+ if field.Kind() == reflect.Ptr {
+ field = reflect.Indirect(field)
+ }
+
+ if field.Kind() == reflect.Struct {
+ return val, fmt.Errorf("Cannot sort on a field that is a struct")
+ }
+
return field, nil
}
diff --git a/pkg/order/order_test.go b/pkg/order/order_test.go
index 28132d9..f2d28b4 100644
--- a/pkg/order/order_test.go
+++ b/pkg/order/order_test.go
@@ -32,6 +32,7 @@
Three uint
Four int
Six SortIncludedStruct
+ Eight *SortIncludedStruct
}
var testSetOne = []SortTestStruct{
@@ -42,6 +43,7 @@
Three: 10,
Four: 1,
Six: SortIncludedStruct{Seven: "o"},
+ Eight: &SortIncludedStruct{Seven: "o"},
},
{
Id: 1,
@@ -50,6 +52,7 @@
Three: 1,
Four: 10,
Six: SortIncludedStruct{Seven: "p"},
+ Eight: &SortIncludedStruct{Seven: "p"},
},
{
Id: 2,
@@ -58,6 +61,7 @@
Three: 2,
Four: 1000,
Six: SortIncludedStruct{Seven: "q"},
+ Eight: &SortIncludedStruct{Seven: "q"},
},
{
Id: 3,
@@ -66,6 +70,7 @@
Three: 3,
Four: 100,
Six: SortIncludedStruct{Seven: "r"},
+ Eight: &SortIncludedStruct{Seven: "r"},
},
{
Id: 4,
@@ -74,6 +79,7 @@
Three: 3,
Four: 0,
Six: SortIncludedStruct{Seven: "s"},
+ Eight: &SortIncludedStruct{Seven: "s"},
},
}
@@ -272,7 +278,22 @@
}
}
-func TestInvaliodDotted(t *testing.T) {
+func TestSortDottedPointer(t *testing.T) {
+ s, err := Parse("+Eight.Seven")
+ if err != nil {
+ t.Errorf("Unable to parse sort specification")
+ }
+ o, err := s.Process(testSetOne)
+ if err != nil {
+ t.Errorf("Sort failed: %s", err.Error())
+ }
+
+ if !Verify(o.([]SortTestStruct), []int{0, 1, 2, 3, 4}) {
+ t.Errorf("incorrect sort")
+ }
+}
+
+func TestInvalidDotted(t *testing.T) {
s, err := Parse("+Six.Nonexistent")
if err != nil {
t.Errorf("Unable to parse sort specification")
@@ -296,6 +317,30 @@
}
}
+func TestSortOnStuct(t *testing.T) {
+ s, err := Parse("+Six")
+ if err != nil {
+ t.Errorf("Unable to parse sort specification")
+ }
+ o, err := s.Process(testSetOne)
+ assert.EqualError(t, err, "Cannot sort on a field that is a struct")
+ if o != nil {
+ t.Errorf("expected no results, got some")
+ }
+}
+
+func TestSortOnPointerStuct(t *testing.T) {
+ s, err := Parse("+Eight")
+ if err != nil {
+ t.Errorf("Unable to parse sort specification")
+ }
+ o, err := s.Process(testSetOne)
+ assert.EqualError(t, err, "Cannot sort on a field that is a struct")
+ if o != nil {
+ t.Errorf("expected no results, got some")
+ }
+}
+
func TestTrailingDot(t *testing.T) {
s, err := Parse("+Six.Seven.")
if err != nil {