Skip to content

Commit ab727a8

Browse files
authored
ParseOptions helper function (#50)
1 parent 7dafc4b commit ab727a8

File tree

4 files changed

+55
-3
lines changed

4 files changed

+55
-3
lines changed

completion.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@ const (
1616
columns = "columns"
1717
)
1818

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+
)
2125

2226
// Options are used to query schemas, tables and columns. They will be encoded in the request body (e.g. {"database": "mydb"})
2327
type Options map[string]string
@@ -99,3 +103,14 @@ func (ds *sqldatasource) registerRoutes(mux *http.ServeMux) error {
99103
}
100104
return nil
101105
}
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+
}

completion_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ package sqlds
33
import (
44
"bytes"
55
"context"
6+
"encoding/json"
7+
"errors"
68
"fmt"
79
"io"
810
"io/ioutil"
911
"net/http"
1012
"net/http/httptest"
1113
"testing"
14+
15+
"github.com/google/go-cmp/cmp"
1216
)
1317

1418
func Test_handleError(t *testing.T) {
@@ -169,3 +173,34 @@ func Test_registerRoutes(t *testing.T) {
169173
}
170174
})
171175
}
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+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.15
55
require (
66
github.com/DATA-DOG/go-sqlmock v1.5.0
77
github.com/go-sql-driver/mysql v1.4.0
8+
github.com/google/go-cmp v0.5.6
89
github.com/grafana/grafana-plugin-sdk-go v0.94.0
910
github.com/stretchr/testify v1.7.0
1011
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 // indirect

go.sum

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,9 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
112112
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
113113
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
114114
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=
116115
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=
117118
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
118119
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
119120
github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=

0 commit comments

Comments
 (0)