Skip to content

Commit

Permalink
query.go - orderby marshals with empty list
Browse files Browse the repository at this point in the history
Changed orderby to a QuerySort struct.
  • Loading branch information
jfcote87 committed Mar 15, 2021
1 parent 688d0ab commit 90ffe41
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
8 changes: 4 additions & 4 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,16 +209,16 @@ func ExampleQuery_GetAll() {

var projects []Project

var filter *intacct.Filter
var filter = intacct.NewFilter()
filter.And().EqualTo("STATUS", "active").In("PARENTID", "ID01", "ID02")

//filter.EqualTo("STATUS", "active").In("PARENTID", "ID01", "ID02")
var stmt = &intacct.Query{
Object: "PROJECT",
Select: intacct.Select{
Fields: []string{"RECORDNO", "PROJECTID", "NAME", "DESCRIPTION", "PARENTNAME"},
},
OrderBy: []intacct.OrderBy{{Field: "PROJECTID"}},
Filter: filter.EqualTo("STATUS", "active").In("PARENTID", "ID01", "ID02"),
Sort: &intacct.QuerySort{Fields: []intacct.OrderBy{{Field: "PROJECTID"}}},
Filter: filter,
}

if err := stmt.GetAll(ctx, sv, &projects); err != nil {
Expand Down
7 changes: 6 additions & 1 deletion query.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Query struct {
Object string `xml:"object"`
Select Select `xml:"select"`
Filter *Filter `xml:"filter,omitempty"`
OrderBy []OrderBy `xml:"orderby>order,omitempty"`
Sort *QuerySort `xml:"orderby,omitempty"`
Options *QueryOptions `xml:"options,omitempty"`
PageSz int `xml:"pagesize,omitempty"`
Offset int `xml:"offset,omitempty"`
Expand Down Expand Up @@ -72,6 +72,11 @@ type Select struct {
Sum string `xml:"sum,omitempty"`
}

type QuerySort struct {
XMLName xml.Name `xml:"orderby"`
Fields []OrderBy `xml:"order"`
}

// OrderBy describes sort conditions
type OrderBy struct {
XMLName xml.Name `xml:"order"`
Expand Down
16 changes: 6 additions & 10 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,17 @@ func TestFilter(t *testing.T) {
func TestOrderBy_MarshalXML(t *testing.T) {
tests := []struct {
name string
orderby intacct.OrderBy
orderby *intacct.QuerySort //intacct.OrderBy
want string
}{
{name: "t1", orderby: intacct.OrderBy{Field: "F1"}, want: "<order><field>F1</field></order>"},
{name: "t2", orderby: intacct.OrderBy{Field: "F2", Descending: true}, want: "<order><field>F2</field><descending></descending></order>"},
{name: "t1", orderby: &intacct.QuerySort{Fields: []intacct.OrderBy{{Field: "F1"}}}, want: "<orderby><order><field>F1</field></order></orderby>"},
{name: "t2", orderby: &intacct.QuerySort{Fields: []intacct.OrderBy{{Field: "F2", Descending: true}}}, want: "<orderby><order><field>F2</field><descending></descending></order></orderby>"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
buff := &bytes.Buffer{}
e := xml.NewEncoder(buff)
err := tt.orderby.MarshalXML(e, xml.StartElement{Name: xml.Name{Local: "order"}})
if err != nil {
t.Errorf("%s encode %v", tt.name, err)
return
}
e.Encode(tt.orderby)
e.Flush()
if string(buff.Bytes()) != tt.want {
t.Errorf("%s expected %s; got %s", tt.name, tt.want, buff.Bytes())
Expand All @@ -106,8 +102,8 @@ func TestMarshal(t *testing.T) {
Fields: []string{"RECORDNO", "PROJECTID", "NAME", "DESCRIPTION", "PARENTNAME"},
Min: "PROJECTID",
},
OrderBy: []intacct.OrderBy{{Field: "PROJECTID"}, {Field: "NAME", Descending: true}},
Filter: f.EqualTo("RECORDNO", "1").In("PROJECTID", "P1", "P2").EqualTo("NAME", ""),
Sort: &intacct.QuerySort{Fields: []intacct.OrderBy{{Field: "PROJECTID"}, {Field: "NAME", Descending: true}}},
Filter: f.EqualTo("RECORDNO", "1").In("PROJECTID", "P1", "P2").EqualTo("NAME", ""),
}
b, err := xml.Marshal(sx)
if err != nil {
Expand Down

0 comments on commit 90ffe41

Please sign in to comment.