From 3b9fe3f4b02a6cf7d367a03fabb1277f0f2b4b55 Mon Sep 17 00:00:00 2001 From: Don Cote Date: Fri, 19 Jun 2020 13:35:42 -0400 Subject: [PATCH] add error to dynamic query --- client_test.go | 29 +++++++++++++++++++++++++---- dataset.go | 16 ++++++++-------- dynamic_query.go | 7 +++++-- go.sum | 16 ---------------- partition.go | 20 ++++++++++---------- refresh_job.go | 18 +++++++++--------- rpc.go | 4 ++-- segment.go | 8 ++++---- 8 files changed, 63 insertions(+), 55 deletions(-) diff --git a/client_test.go b/client_test.go index b6d845b..fec0bf9 100644 --- a/client_test.go +++ b/client_test.go @@ -96,16 +96,37 @@ func TestClient_IntersectMembersToSegment(t *testing.T) { func TestClient_ExecuteDynamicQuery(t *testing.T) { client := getClient(t) - c, err := client.ExecuteDynamicQuery(makeQuery(makeCondition()), false) + resp, err := client.ExecuteDynamicQuery(makeQuery(makeCondition()), false) if err != nil { t.Fatalf("Unexpected error: %v", err) } - if c == nil { - t.Fatal("count is nil") + if resp == nil { + t.Fatal("resp is nil") } fmt.Fprintln(os.Stdout, "ExecuteDynamicQuery") - fmt.Fprintln(os.Stdout, *c) + fmt.Fprintln(os.Stdout, *resp) + fmt.Println("") +} + +func TestClient_ExecuteDynamicQueryWithError(t *testing.T) { + client := getClient(t) + query := makeQuery(makeCondition()) + query.Table = "foobar" + resp, err := client.ExecuteDynamicQuery(query, false) + + if err != nil { + t.Fatalf("Unexpected error: %v", err) + } + if resp == nil { + t.Fatal("resp is nil") + } + if resp.Error == "" { + t.Fatal("error should not be nil") + } + + fmt.Fprintln(os.Stdout, "ExecuteDynamicQuery") + fmt.Fprintln(os.Stdout, *resp) fmt.Println("") } diff --git a/dataset.go b/dataset.go index 37222ce..5d8fd46 100644 --- a/dataset.go +++ b/dataset.go @@ -7,12 +7,12 @@ import ( // Dataset is a set of rules that define attributes over a distinct set of members type Dataset struct { - Identifier string `mapstructure:"identifier",json:"identifier"` - Name string `mapstructure:"name",json:"name"` - OutputEngine string `mapstructure:"output_engine",json:"output_engine"` - Description string `mapstructure:"description",json:"description"` - Tags []string `mapstructure:"tags",json:"tags"` - Partitions []Partition `mapstructure:"partitions",json:"partitions"` + Identifier string `mapstructure:"identifier" json:"identifier"` + Name string `mapstructure:"name" json:"name"` + OutputEngine string `mapstructure:"output_engine" json:"output_engine"` + Description string `mapstructure:"description" json:"description"` + Tags []string `mapstructure:"tags" json:"tags"` + Partitions []Partition `mapstructure:"partitions" json:"partitions"` } // GetDatasetsResponse returns the RPC response @@ -33,12 +33,12 @@ func (c Client) GetDatasets(entityID string, ownerID string) (*GetDatasetsRespon var response GetDatasetsResponse - result, err := c.CallRPC("RPCService.GetDatasets", input) + rpc, err := c.CallRPC("RPCService.GetDatasets", input) if err != nil { return &response, err } - err = mapstructure.Decode(result, &response) + err = mapstructure.Decode(rpc.Result, &response) if err != nil { return &response, errors.Wrap(err, "error decoding into `GetDatasets`") } diff --git a/dynamic_query.go b/dynamic_query.go index 80e0c4c..b86131b 100644 --- a/dynamic_query.go +++ b/dynamic_query.go @@ -11,6 +11,7 @@ type ExecuteDynamicQueryResponse struct { Results []interface{} `mapstructure:"results"` ResultCount int `mapstructure:"result_count"` CompiledSQL string `mapstructure:"compiled_sql"` + Error string `mapstructure:"error,omitempty"` } // ExecuteDynamicQuery returns the query results @@ -21,15 +22,17 @@ func (c Client) ExecuteDynamicQuery(query dynamicquery.Query, debug bool) (*Exec var response ExecuteDynamicQueryResponse - result, err := c.CallRPC("RPCService.ExecuteDynamicQuery", input) + rpc, err := c.CallRPC("RPCService.ExecuteDynamicQuery", input) + if err != nil { return &response, err } - err = mapstructure.Decode(result, &response) + err = mapstructure.Decode(rpc.Result, &response) if err != nil { return &response, errors.Wrap(err, "error decoding into `ExecuteDynamicQueryResponse`") } + response.Error = rpc.Error return &response, nil } diff --git a/go.sum b/go.sum index ba6c998..9e0ae29 100644 --- a/go.sum +++ b/go.sum @@ -1,20 +1,4 @@ -github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/mitchellh/mapstructure v1.3.1 h1:cCBH2gTD2K0OtLlv/Y5H01VQCqmlDxz30kS5Y5bqfLA= github.com/mitchellh/mapstructure v1.3.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= -github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= -github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= -gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/partition.go b/partition.go index a428d24..80ead23 100644 --- a/partition.go +++ b/partition.go @@ -7,14 +7,14 @@ import ( // Partition is a slice of a dataset corresponding to a particular member attribute type Partition struct { - ID string `mapstructure:"id",json:"id"` - EntityID string `mapstructure:"entity_id",json:"entity_id"` - OwnerID string `mapstructure:"owner_id",json:"owner_id"` - Name string `mapstructure:"name",json:"name"` - IsRanked bool `mapstructure:"is_ranked",json:"is_ranked"` - Cardinality int `mapstructure:"cardinality",json:"cardinality"` - MaxRank int `mapstructure:"max_rank",json:"max_rank"` - MinRank int `mapstructure:"min_rank",json:"min_rank"` + ID string `mapstructure:"id" json:"id"` + EntityID string `mapstructure:"entity_id" json:"entity_id"` + OwnerID string `mapstructure:"owner_id" json:"owner_id"` + Name string `mapstructure:"name" json:"name"` + IsRanked bool `mapstructure:"is_ranked" json:"is_ranked"` + Cardinality int `mapstructure:"cardinality" json:"cardinality"` + MaxRank int `mapstructure:"max_rank" json:"max_rank"` + MinRank int `mapstructure:"min_rank" json:"min_rank"` } // GetMemberPartitionsResponse returns the RPC response @@ -31,12 +31,12 @@ func (c Client) GetMemberPartitions(entityID string, memberID string) (*GetMembe var response GetMemberPartitionsResponse - result, err := c.CallRPC("RPCService.GetMemberPartitions", input) + rpc, err := c.CallRPC("RPCService.GetMemberPartitions", input) if err != nil { return &response, err } - err = mapstructure.Decode(result, &response) + err = mapstructure.Decode(rpc.Result, &response) if err != nil { return &response, errors.Wrap(err, "error decoding into `GetMemberPartitions`") } diff --git a/refresh_job.go b/refresh_job.go index 9eb0bd0..36cc69d 100644 --- a/refresh_job.go +++ b/refresh_job.go @@ -7,11 +7,11 @@ import ( // RefreshArgs are used as filters to determine what to refresh type RefreshArgs struct { - EntityID string `mapstructure:"entity_id",json:"entity_id"` - MetricID string `mapstructure:"metric_id",json:"metric_id"` - DatasetID string `mapstructure:"dataset_id",json:"dataset_id"` - Tags []string `mapstructure:"tags",json:"tags"` - Stage string `mapstructure:"stage",json:"stage"` + EntityID string `mapstructure:"entity_id" json:"entity_id"` + MetricID string `mapstructure:"metric_id" json:"metric_id"` + DatasetID string `mapstructure:"dataset_id" json:"dataset_id"` + Tags []string `mapstructure:"tags" json:"tags"` + Stage string `mapstructure:"stage" json:"stage"` } // RefreshResponse returns the RPC response @@ -43,12 +43,12 @@ func (c Client) Refresh(refreshArgs RefreshArgs) (*RefreshResponse, error) { var response RefreshResponse - result, err := c.CallRPC("RPCService.Refresh", input) + rpc, err := c.CallRPC("RPCService.Refresh", input) if err != nil { return &response, err } - err = mapstructure.Decode(result, &response) + err = mapstructure.Decode(rpc.Result, &response) if err != nil { return &response, errors.Wrap(err, "error decoding into `RefreshResponse`") } @@ -63,12 +63,12 @@ func (c Client) GetRefreshJob(jobID string) (*RefreshResponse, error) { var response RefreshResponse - result, err := c.CallRPC("RPCService.GetRefreshJob", input) + rpc, err := c.CallRPC("RPCService.GetRefreshJob", input) if err != nil { return &response, err } - err = mapstructure.Decode(result, &response) + err = mapstructure.Decode(rpc.Result, &response) if err != nil { return &response, errors.Wrap(err, "error decoding into `GetRefreshJob`") } diff --git a/rpc.go b/rpc.go index d7788ca..f91d473 100644 --- a/rpc.go +++ b/rpc.go @@ -25,7 +25,7 @@ type RPCResponse struct { } // CallRPC returns a response from the RPC interface -func (c Client) CallRPC(method string, param interface{}) (interface{}, error) { +func (c Client) CallRPC(method string, param interface{}) (*RPCResponse, error) { params := make([]interface{}, 0) params = append(params, param) @@ -62,5 +62,5 @@ func (c Client) CallRPC(method string, param interface{}) (interface{}, error) { return nil, errors.New(fmt.Sprintf("flip rpc responded with non-200 for %s, err: %s", c.RPCURL, rpcResponse.Error)) } - return rpcResponse.Result, nil + return &rpcResponse, nil } diff --git a/segment.go b/segment.go index 0e28b94..b40433e 100644 --- a/segment.go +++ b/segment.go @@ -19,12 +19,12 @@ func (c Client) GetSegmentMembers(condition segment.Condition) (*GetSegmentMembe var segmentMembers GetSegmentMembersResponse - result, err := c.CallRPC("RPCService.GetSegmentMembers", input) + rpc, err := c.CallRPC("RPCService.GetSegmentMembers", input) if err != nil { return &segmentMembers, err } - err = mapstructure.Decode(result, &segmentMembers) + err = mapstructure.Decode(rpc.Result, &segmentMembers) if err != nil { return &segmentMembers, errors.Wrap(err, "error decoding into `GetSegmentMembersResponse`") } @@ -47,12 +47,12 @@ func (c Client) IntersectMembersToSegment(members []string, condition segment.Co var intersectResponse IntersectMembersToSegmentResponse - result, err := c.CallRPC("RPCService.IntersectMembersToSegment", input) + rpc, err := c.CallRPC("RPCService.IntersectMembersToSegment", input) if err != nil { return &intersectResponse, err } - err = mapstructure.Decode(result, &intersectResponse) + err = mapstructure.Decode(rpc.Result, &intersectResponse) if err != nil { return &intersectResponse, errors.Wrap(err, "error decoding into `IntersectMembersToSegmentResponse`") }