From 2f30e210462caeb1b43802bb7a480a009a067be7 Mon Sep 17 00:00:00 2001 From: Prasanna Kumar <6123002+pgollangi@users.noreply.github.com> Date: Thu, 16 Nov 2023 17:55:42 +0530 Subject: [PATCH] feat: Allow selecting columns with NULL values (#18) * feat: Allow selecting columns with NULL values --- pkg/select/select.go | 8 +++++-- pkg/select/select_test.go | 44 ++++++++++++++++++++++----------------- test/data/users.json | 2 +- 3 files changed, 32 insertions(+), 22 deletions(-) diff --git a/pkg/select/select.go b/pkg/select/select.go index ef195a8..df53d83 100644 --- a/pkg/select/select.go +++ b/pkg/select/select.go @@ -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 } diff --git a/pkg/select/select_test.go b/pkg/select/select_test.go index a225daf..37b5c12 100644 --- a/pkg/select/select_test.go +++ b/pkg/select/select_test.go @@ -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='aeatockj@psu.edu'", 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"}}, }, } diff --git a/test/data/users.json b/test/data/users.json index 38e17c5..70ca98a 100644 --- a/test/data/users.json +++ b/test/data/users.json @@ -183,7 +183,7 @@ "id": 21, "name": "Doyle", "username": "ckensleyk", - "email": "ckensleyk@pen.io", + "email": null, "address": { "city": "Fayetteville" }