-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
1,589 additions
and
528 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package cassandra | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"time" | ||
|
||
"github.com/gocql/gocql" | ||
) | ||
|
||
type Row struct { | ||
Columns []string | ||
Fields map[string]interface{} | ||
} | ||
|
||
// normalize checks the type of returned field and in case if | ||
// it is not supported by grafana tries to convert it to a supported type. | ||
// If some field has type that cannot be converted then error is returned. | ||
// Type mappings are based on these: | ||
// Cassandra gocql types: https://github.com/gocql/gocql/blob/master/marshal.go#L164 | ||
// Grafana field types: https://github.com/grafana/grafana-plugin-sdk-go/blob/main/data/field.go#L39 | ||
func (r *Row) normalize() error { | ||
for _, colName := range r.Columns { | ||
switch v := r.Fields[colName].(type) { | ||
case int8, int16, int32, int64, float32, float64, string, bool, time.Time: | ||
case int: | ||
r.Fields[colName] = int64(v) | ||
case []byte: | ||
r.Fields[colName] = string(v) | ||
case net.IP: | ||
r.Fields[colName] = v.String() | ||
case gocql.UUID: | ||
r.Fields[colName] = v.String() | ||
default: | ||
return fmt.Errorf("field %s has unsupported type %T", colName, v) | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package cassandra | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"testing" | ||
"time" | ||
|
||
"github.com/gocql/gocql" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRow_normalize(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
input *Row | ||
want *Row | ||
wantErr error | ||
}{ | ||
{ | ||
name: "empty", | ||
input: &Row{}, | ||
want: &Row{}, | ||
wantErr: nil, | ||
}, | ||
{ | ||
name: "normal row", | ||
input: &Row{ | ||
Columns: []string{"id", "time", "value"}, | ||
Fields: map[string]interface{}{"id": "id", "time": time.UnixMilli(1257894000000).UTC(), "value": 0.1}, | ||
}, | ||
want: &Row{ | ||
Columns: []string{"id", "time", "value"}, | ||
Fields: map[string]interface{}{"id": "id", "time": time.UnixMilli(1257894000000).UTC(), "value": 0.1}, | ||
}, | ||
wantErr: nil, | ||
}, | ||
{ | ||
name: "normal row with nil in the beginning", | ||
input: &Row{ | ||
Columns: []string{"field1", "id", "time", "value"}, | ||
Fields: map[string]interface{}{"field1": nil, "id": "id", "time": time.UnixMilli(1257894000000).UTC(), "value": 0.1}, | ||
}, | ||
want: &Row{ | ||
Columns: []string{"field1", "id", "time", "value"}, | ||
Fields: map[string]interface{}{"field1": nil, "id": "id", "time": time.UnixMilli(1257894000000).UTC(), "value": 0.1}, | ||
}, | ||
wantErr: fmt.Errorf("field %s has unsupported type %T", "field1", nil), | ||
}, | ||
{ | ||
name: "normal row with nil in the end", | ||
input: &Row{ | ||
Columns: []string{"id", "time", "value", "field1"}, | ||
Fields: map[string]interface{}{"id": "id", "time": time.UnixMilli(1257894000000).UTC(), "value": 0.1, "field1": nil}, | ||
}, | ||
want: &Row{ | ||
Columns: []string{"id", "time", "value", "field1"}, | ||
Fields: map[string]interface{}{"id": "id", "time": time.UnixMilli(1257894000000).UTC(), "value": 0.1, "field1": nil}, | ||
}, | ||
wantErr: fmt.Errorf("field %s has unsupported type %T", "field1", nil), | ||
}, | ||
{ | ||
name: "normal row with unsupported", | ||
input: &Row{ | ||
Columns: []string{"id", "field1", "time", "value"}, | ||
Fields: map[string]interface{}{"id": "id", "field1": struct{}{}, "time": time.UnixMilli(1257894000000).UTC(), "value": 0.1}, | ||
}, | ||
want: &Row{ | ||
Columns: []string{"id", "field1", "time", "value"}, | ||
Fields: map[string]interface{}{"id": "id", "field1": struct{}{}, "time": time.UnixMilli(1257894000000).UTC(), "value": 0.1}, | ||
}, | ||
wantErr: fmt.Errorf("field %s has unsupported type %T", "field1", struct{}{}), | ||
}, | ||
{ | ||
name: "normal row with multiple unsupported fields", | ||
input: &Row{ | ||
Columns: []string{"field1", "id", "field2", "time", "field3", "value"}, | ||
Fields: map[string]interface{}{"field1": struct{}{}, "id": "id", "field2": struct{}{}, "time": time.UnixMilli(1257894000000).UTC(), "field3": struct{}{}, "value": 0.1}, | ||
}, | ||
want: &Row{ | ||
Columns: []string{"field1", "id", "field2", "time", "field3", "value"}, | ||
Fields: map[string]interface{}{"field1": struct{}{}, "id": "id", "field2": struct{}{}, "time": time.UnixMilli(1257894000000).UTC(), "field3": struct{}{}, "value": 0.1}, | ||
}, | ||
wantErr: fmt.Errorf("field %s has unsupported type %T", "field1", struct{}{}), | ||
}, | ||
{ | ||
name: "normal row with conversion", | ||
input: &Row{ | ||
Columns: []string{"field1", "id", "field2", "time", "field3", "value"}, | ||
Fields: map[string]interface{}{"field1": gocql.UUID{}, "id": "id", "field2": net.ParseIP("127.0.0.1"), "time": time.UnixMilli(1257894000000).UTC(), "field3": []byte("some string"), "value": 0.1}, | ||
}, | ||
want: &Row{ | ||
Columns: []string{"field1", "id", "field2", "time", "field3", "value"}, | ||
Fields: map[string]interface{}{"field1": "00000000-0000-0000-0000-000000000000", "id": "id", "field2": "127.0.0.1", "time": time.UnixMilli(1257894000000).UTC(), "field3": "some string", "value": 0.1}, | ||
}, | ||
wantErr: nil, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
err := tc.input.normalize() | ||
if tc.wantErr == nil { | ||
assert.NoError(t, err) | ||
} else { | ||
assert.EqualError(t, err, tc.wantErr.Error()) | ||
} | ||
assert.Equal(t, tc.want, tc.input) | ||
}) | ||
} | ||
} |
Oops, something went wrong.