Skip to content

Commit

Permalink
query.go: added new query format
Browse files Browse the repository at this point in the history
  • Loading branch information
jfcote87 committed Mar 14, 2021
1 parent 795a789 commit 688d0ab
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
7 changes: 4 additions & 3 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,15 +209,16 @@ func ExampleQuery_GetAll() {

var projects []Project

var filter = &intacct.Filter{}
filter.EqualTo("STATUS", "active").In("PARENTID", "ID01", "ID02")
var filter *intacct.Filter

//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,
Filter: filter.EqualTo("STATUS", "active").In("PARENTID", "ID01", "ID02"),
}

if err := stmt.GetAll(ctx, sv, &projects); err != nil {
Expand Down
24 changes: 19 additions & 5 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Query struct {
XMLName xml.Name `xml:"query"`
Object string `xml:"object"`
Select Select `xml:"select"`
Filter Filter `xml:"filter"`
Filter *Filter `xml:"filter,omitempty"`
OrderBy []OrderBy `xml:"orderby>order,omitempty"`
Options *QueryOptions `xml:"options,omitempty"`
PageSz int `xml:"pagesize,omitempty"`
Expand Down Expand Up @@ -74,8 +74,9 @@ type Select struct {

// OrderBy describes sort conditions
type OrderBy struct {
Field string `xml:"field"`
Descending bool `xml:"ascending"`
XMLName xml.Name `xml:"order"`
Field string `xml:"field,omitempty"`
Descending bool `xml:"descending,omitempty"`
}

// MarshalXML used to create <descending> tag
Expand Down Expand Up @@ -103,8 +104,8 @@ func NewFilter() *Filter {
// Filter is a heirarchy of criteria. Use function to add criteria
type Filter struct {
XMLName xml.Name
Field string `xml:"field,omitempty"`
Value []string `xml:"value,omitempty"`
Field string `xml:"field,omitempty"`
Value FilterVals `xml:"value,omitempty"`
Filters []Filter
}

Expand All @@ -129,6 +130,19 @@ func (f *Filter) newFilter(nm string) *Filter {
return &ret
}

// FilterVals handles proper marsheling of empty strings
type FilterVals []string

// MarshalXML output nothing for empty slice, value elements for all others
func (fv FilterVals) MarshalXML(e *xml.Encoder, s xml.StartElement) error {
for _, val := range fv {
e.EncodeToken(s)
e.EncodeToken(xml.CharData(val))
e.EncodeToken(s.End())
}
return nil
}

func (f *Filter) add(nm, field string, values ...string) *Filter {
if f == nil {
f = &Filter{}
Expand Down
22 changes: 13 additions & 9 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func TestFilter(t *testing.T) {
var f = intacct.NewFilter()
fa := f.And()
fa.EqualTo("FLD1", "Val1")
fa.EqualTo("FLD1a", "")
fa.NotEqualTo("FLD2", "Val2")
fa.LessThan("FLD3", "Val3").LessThanOrEqualTo("FLD4", "Val4")
fa.GreaterThan("FLD5", "Val5").GreaterThanOrEqualTo("FLD6", "Val6")
Expand All @@ -29,6 +30,8 @@ func TestFilter(t *testing.T) {
return
}
if f.Filters[0].XMLName.Local != "and" {
t.Errorf("expected and filter; got %s", f.Filters[0].XMLName.Local)
return

}
if f.Filters[1].XMLName.Local != "or" {
Expand All @@ -38,6 +41,7 @@ func TestFilter(t *testing.T) {

tests_and := []intacct.Filter{
{XMLName: xml.Name{Local: "equalto"}, Field: "FLD1", Value: []string{"Val1"}},
{XMLName: xml.Name{Local: "equalto"}, Field: "FLD1a", Value: []string{""}},
{XMLName: xml.Name{Local: "notequalto"}, Field: "FLD2", Value: []string{"Val2"}},
{XMLName: xml.Name{Local: "lessthan"}, Field: "FLD3", Value: []string{"Val3"}},
{XMLName: xml.Name{Local: "lessthanorequalto"}, Field: "FLD4", Value: []string{"Val4"}},
Expand Down Expand Up @@ -74,9 +78,8 @@ func TestOrderBy_MarshalXML(t *testing.T) {
orderby intacct.OrderBy
want string
}{
// TODO: Add test cases.
{name: "t1", orderby: intacct.OrderBy{Field: "F1"}, want: "<order><field>F1</field></order>"},
{name: "t2", orderby: intacct.OrderBy{"F2", true}, want: "<order><field>F2</field><descending></descending></order>"},
{name: "t2", orderby: intacct.OrderBy{Field: "F2", Descending: true}, want: "<order><field>F2</field><descending></descending></order>"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -95,22 +98,23 @@ func TestOrderBy_MarshalXML(t *testing.T) {
}
}

/*
func TestMarshal(t *testing.T) {
f := intacct.NewFilter().EqualTo("RECORDNO", "1").In("PROJECTID", "P1", "P2")
sx := intacct.Stmt{
var f *intacct.Filter
sx := intacct.Query{
Object: "PROJECT",
Select: intacct.Select{
Fields: []string{"RECORDNO", "PROJECTID", "NAME", "DESCRIPTION", "PARENTNAME"},
Min: "PROJECTID",
},
OrderBy: []intacct.OrderBy{{Field: "PROJECTID"}, {Field: "NAME", Descending: true}},
Filter: *f,
Filter: f.EqualTo("RECORDNO", "1").In("PROJECTID", "P1", "P2").EqualTo("NAME", ""),
}
b, err := xml.MarshalIndent(sx, "", " ")
b, err := xml.Marshal(sx)
if err != nil {
t.Fatalf("%v", err)
}
t.Errorf("%s", b)
expect := `<query><object>PROJECT</object><select><field>RECORDNO</field><field>PROJECTID</field><field>NAME</field><field>DESCRIPTION</field><field>PARENTNAME</field><min>PROJECTID</min></select><filter><equalto><field>RECORDNO</field><value>1</value></equalto><in><field>PROJECTID</field><value>P1</value><value>P2</value></in><equalto><field>NAME</field><value></value></equalto></filter><orderby><order><field>PROJECTID</field></order><order><field>NAME</field><descending></descending></order></orderby></query>`
if expect != string(b) {
t.Errorf("expected marshal of %s; got %s", expect, b)
}
}
*/

0 comments on commit 688d0ab

Please sign in to comment.