File tree Expand file tree Collapse file tree 4 files changed +55
-3
lines changed Expand file tree Collapse file tree 4 files changed +55
-3
lines changed Original file line number Diff line number Diff line change @@ -16,8 +16,12 @@ const (
16
16
columns = "columns"
17
17
)
18
18
19
- // ErrorNotImplemented is returned if the function is not implemented by the provided Driver (the Completable pointer is nil)
20
- var ErrorNotImplemented = errors .New ("not implemented" )
19
+ var (
20
+ // ErrorNotImplemented is returned if the function is not implemented by the provided Driver (the Completable pointer is nil)
21
+ ErrorNotImplemented = errors .New ("not implemented" )
22
+ // ErrorWrongOptions when trying to parse Options with a invalid JSON
23
+ ErrorWrongOptions = errors .New ("error reading query options" )
24
+ )
21
25
22
26
// Options are used to query schemas, tables and columns. They will be encoded in the request body (e.g. {"database": "mydb"})
23
27
type Options map [string ]string
@@ -99,3 +103,14 @@ func (ds *sqldatasource) registerRoutes(mux *http.ServeMux) error {
99
103
}
100
104
return nil
101
105
}
106
+
107
+ func ParseOptions (rawOptions json.RawMessage ) (Options , error ) {
108
+ args := Options {}
109
+ if rawOptions != nil {
110
+ err := json .Unmarshal (rawOptions , & args )
111
+ if err != nil {
112
+ return nil , fmt .Errorf ("%w: %v" , ErrorWrongOptions , err )
113
+ }
114
+ }
115
+ return args , nil
116
+ }
Original file line number Diff line number Diff line change @@ -3,12 +3,16 @@ package sqlds
3
3
import (
4
4
"bytes"
5
5
"context"
6
+ "encoding/json"
7
+ "errors"
6
8
"fmt"
7
9
"io"
8
10
"io/ioutil"
9
11
"net/http"
10
12
"net/http/httptest"
11
13
"testing"
14
+
15
+ "github.com/google/go-cmp/cmp"
12
16
)
13
17
14
18
func Test_handleError (t * testing.T ) {
@@ -169,3 +173,34 @@ func Test_registerRoutes(t *testing.T) {
169
173
}
170
174
})
171
175
}
176
+
177
+ func TestParseOptions (t * testing.T ) {
178
+ tests := []struct {
179
+ description string
180
+ input json.RawMessage
181
+ result Options
182
+ err error
183
+ }{
184
+ {
185
+ description : "parses input" ,
186
+ input : json .RawMessage (`{"foo":"bar"}` ),
187
+ result : Options {"foo" : "bar" },
188
+ },
189
+ {
190
+ description : "returns an error" ,
191
+ input : json .RawMessage (`not a json` ),
192
+ err : ErrorWrongOptions ,
193
+ },
194
+ }
195
+ for _ , tc := range tests {
196
+ t .Run (tc .description , func (t * testing.T ) {
197
+ res , err := ParseOptions (tc .input )
198
+ if (err != nil || tc .err != nil ) && ! errors .Is (err , tc .err ) {
199
+ t .Errorf ("unexpected error %v" , err )
200
+ }
201
+ if ! cmp .Equal (res , tc .result ) {
202
+ t .Errorf ("unexpected result: %v" , cmp .Diff (res , tc .result ))
203
+ }
204
+ })
205
+ }
206
+ }
Original file line number Diff line number Diff line change 5
5
require (
6
6
github.com/DATA-DOG/go-sqlmock v1.5.0
7
7
github.com/go-sql-driver/mysql v1.4.0
8
+ github.com/google/go-cmp v0.5.6
8
9
github.com/grafana/grafana-plugin-sdk-go v0.94.0
9
10
github.com/stretchr/testify v1.7.0
10
11
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 // indirect
Original file line number Diff line number Diff line change @@ -112,8 +112,9 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
112
112
github.com/google/go-cmp v0.4.0 /go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE =
113
113
github.com/google/go-cmp v0.5.0 /go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE =
114
114
github.com/google/go-cmp v0.5.4 /go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE =
115
- github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU =
116
115
github.com/google/go-cmp v0.5.5 /go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE =
116
+ github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ =
117
+ github.com/google/go-cmp v0.5.6 /go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE =
117
118
github.com/google/gofuzz v1.0.0 /go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg =
118
119
github.com/google/renameio v0.1.0 /go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI =
119
120
github.com/google/uuid v1.0.0 /go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo =
You can’t perform that action at this time.
0 commit comments