Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow selecting columns with NULL values #18

Merged
merged 3 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions pkg/select/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,14 @@ func readColumnValue(document *firestore.DocumentSnapshot, data *map[string]inte
var colData interface{}
colData = *data
for _, fPath := range fieldPaths {
colData = colData.(map[string]interface{})[fPath]
if colData == nil {
fieldVal, ok := colData.(map[string]interface{})[fPath]
if !ok {
return nil, fmt.Errorf(`unknown field "%s" in doc "%s"`, column.field, document.Ref.ID)
}
colData = fieldVal
if colData == nil {
break
}
}
val = colData
}
Expand Down
44 changes: 25 additions & 19 deletions pkg/select/select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,76 +30,82 @@ type TestExpect struct {
const FirestoreEmulatorHost = "FIRESTORE_EMULATOR_HOST"

var selectTests = []TestExpect{
TestExpect{
{
query: "select * from users",
columns: []string{"id", "email", "username", "address", "name"},
length: "21",
},
TestExpect{
{
query: "select * from `users`",
columns: []string{"id", "email", "username", "address", "name"},
length: "21",
},
TestExpect{
{
query: "select id as uid, * from users",
columns: []string{"uid", "id", "email", "username", "address", "name"},
length: "21",
},
TestExpect{
{
query: "select *, username as uname from users",
columns: []string{"id", "email", "username", "address", "name", "uname"},
length: "21",
},
TestExpect{
{
query: "select id as uid, *, username as uname from users",
columns: []string{"uid", "id", "email", "username", "address", "name", "uname"},
length: "21",
},
TestExpect{
{
query: "select id, email, address from users",
columns: []string{"id", "email", "address"},
length: "21",
},
TestExpect{
{
query: "select id, email, address from users limit 5",
columns: []string{"id", "email", "address"},
length: "5",
},
TestExpect{
{
query: "select id from users where email='[email protected]'",
columns: []string{"id"},
length: "1",
records: [][]interface{}{[]interface{}{float64(20)}},
records: [][]interface{}{{float64(20)}},
},
TestExpect{
{
query: "select id from users order by id desc limit 1",
columns: []string{"id"},
length: "1",
records: [][]interface{}{[]interface{}{float64(21)}},
records: [][]interface{}{{float64(21)}},
},
TestExpect{
{
query: "select LENGTH(username) as uLen from users where id = 8",
columns: []string{"uLen"},
length: "1",
records: [][]interface{}{[]interface{}{float64(6)}},
records: [][]interface{}{{float64(6)}},
},
TestExpect{
{
query: "select id from users where `address.city` = 'Glendale' and name = 'Eleanora'",
columns: []string{"id"},
length: "1",
records: [][]interface{}{[]interface{}{float64(10)}},
records: [][]interface{}{{float64(10)}},
},
TestExpect{
{
query: "select id > 0 as has_id from users where `address.city` = 'Glendale' and name = 'Eleanora'",
columns: []string{"has_id"},
length: "1",
records: [][]interface{}{[]interface{}{true}},
records: [][]interface{}{{true}},
},
TestExpect{
{
query: "select __name__ from users where id = 1",
columns: []string{"__name__"},
length: "1",
records: [][]interface{}{[]interface{}{"1"}},
records: [][]interface{}{{"1"}},
},
{
query: "select id, email, username from users where id = 21",
columns: []string{"id", "email", "username"},
length: "1",
records: [][]interface{}{{float64(21), nil, "ckensleyk"}},
},
}

Expand Down
2 changes: 1 addition & 1 deletion test/data/users.json
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@
"id": 21,
"name": "Doyle",
"username": "ckensleyk",
"email": "[email protected]",
"email": null,
"address": {
"city": "Fayetteville"
}
Expand Down
Loading