Skip to content

Conversation

hiranya911
Copy link
Contributor

This PR introduces the database package for interacting with the Firebase Database. This is a programming layer on top of the REST interface of Database. It closely mirrors the database API found in the Firebase Python Admin SDK, and does not include real time event listeners.

go/firebase-go-admin-db

Copy link
Collaborator

@bklimt bklimt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really good code. I just have a bunch of nits.

firebase.go Outdated
@@ -55,16 +48,20 @@ const firebaseEnvName = "FIREBASE_CONFIG"

// An App holds configuration and state common to all Firebase services that are exposed from the SDK.
type App struct {
ao map[string]interface{}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we rename this something more descriptive, like maybe authOverride?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

typeNull = 0
typeBoolFalse = 1
typeBoolTrue = 2
typeNumeric = 3
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does RTDB have any special cases like Infinity or NaN? I remember those required some tricky sorting logic in Firestore.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. Since all values are essentially constrained by the json type system, this doesn't affect RTDB as far as i can tell.

)

const userAgentFormat = "Firebase/HTTP/%s/%s/AdminGo"
const invalidChars = "[].#$"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For such a small set, this is the right way to do this. But I want to bring unicode.RangeTable to your attention, as it would be the right way to do this if the set were much more complicated.

https://golang.org/pkg/unicode/#Is

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack

db/db.go Outdated
type Client struct {
hc *internal.HTTPClient
url string
ao string
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a longer name would be more appropriate here. With hc, it's at least obvious that it's an HTTPClient. But ao is just a string, and has no comment, so it's pretty unclear.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

db/db.go Outdated
} else if p.Scheme != "https" {
return nil, fmt.Errorf("invalid database URL (incorrect scheme): %q", c.URL)
} else if !strings.HasSuffix(p.Host, ".firebaseio.com") {
return nil, fmt.Errorf("invalid database URL (incorrest host): %q", c.URL)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"incorrect"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

db/query.go Outdated
type Query struct {
client *Client
path string
ob orderBy
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we maybe call this "order"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

db/query_test.go Outdated
want []interface{}
}{
{
resp: map[string]interface{}{"k1": 1, "k2": 2, "k3": 3},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wish it could do the left-to-right type inference that would let you omit the types from these composite literals, but this seems like the correct way to do it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack

db/ref.go Outdated
"golang.org/x/net/context"
)

const txnRetries = 25
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should comment on this. Is 25 what the clients use as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added comment. 25 is the magic number used in other SDKs as well.

db/ref_test.go Outdated
})
}

func TestWerlformedHttpError(t *testing.T) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol "Werlformed" isn't. 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done :)

Opts []option.ClientOption
URL string
Version string
AO map[string]interface{}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comments as above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@hiranya911 hiranya911 assigned avishalom and unassigned bklimt Feb 16, 2018
db/db.go Outdated
} else if p.Scheme != "https" {
return nil, fmt.Errorf("invalid database URL (incorrect scheme): %q", c.URL)
} else if !strings.HasSuffix(p.Host, ".firebaseio.com") {
return nil, fmt.Errorf("invalid database URL (incorrest host): %q", c.URL)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are requiring a specific suffix, and a specific scheme, why not state them in the error message?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

db/query.go Outdated
//
// The resulting Query will only return child nodes with a value greater than or equal to v.
func (q *Query) StartAt(v interface{}) *Query {
q2 := new(Query)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and below, &Query{} would be more consistent with our current SDK.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

"stegosaurus": 5,
"triceratops": 22
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

newline?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor

@avishalom avishalom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!
(general question for this and the other SDKs, do we want to clean up after the integration tests. not for this PR... but maybe later)

@hiranya911 hiranya911 assigned hiranya911 and unassigned avishalom Feb 16, 2018
* Adding database snippets

* Adding query snippets

* Added complex query samples

* Updated variable name

* Fixing a typo

* Fixing query example

* Updated DB snippets to use GetOrdered()

* Removing unnecessary placeholders in Fatalln() calls

* Removing unnecessary placeholders in Fatalln() calls
@hiranya911 hiranya911 merged commit c9be1e9 into dev Feb 28, 2018
@hiranya911 hiranya911 deleted the hkj-rtdb branch February 28, 2018 01:13
hiranya911 added a commit that referenced this pull request Feb 28, 2018
* Lint (#96)

* fix misspelling

* add check error

* missing copyright

* Doc (#97)

* update readme with Authentication Guide & Release Notes

* fix a misspelling : separately

* fix missing newline before package

* add Go Report Card + update doc

* add travis build for go versions 1.7.x -> 1.10.x (#98)

* add build for go version 1.6.x -> 1.10.x

* fix 1.10 version

* fix context to golang.org/x/net/context for go 1.6 compatibility

* add race detector + go vet on build + build without failure on go unstable

* add go16 et go17 file due to req.withcontext which is only go 1.7

* fix context package

* update go16.go to remove WithContext

* update bad import

* remove unused func

* finally use ctxhttp.Do with multiple build version

* ignore integration package for install

* fix go get command

* put go 1.6.X in allow_failures dur to test failure

* fix inversion of code

* remove go 1.6 support

* revert initial version with req.WithContext

* fix travis to support go 1.10.x

* nits

* Import context from standard package (#101)

* Import context from standard package.

* Firebase Database API (#92)

* Experimental RTDB code

* Added ref.Set()

* Added Push(), Update(), Remove() and tests

* Adding Transaction() support

* Fixed Transaction() API

* Code cleanup

* Implemented Query() API

* Added GetIfChanged() and integration tests

* More integration tests

* Updated unit test

* More integration tests

* Integration tests for queries

* Auth override support and more tests

* More test cases; AuthOverride support in App

* Implemented AuthOverride support; Added tests

* Implementing the new API

* More code cleanup

* Code clean up

* Refactored the http client code

* More tests

* Boosted test coverage to 97%

* Better error messages in tests; Added license headers

* Added documentatioon and cleaned up tests

* Fixing a build break

* Finishing up documentation

* More test cases

* Implemented a reusable HTTP client API

* Added test cases

* Comment clean up

* Using the shared http client API

* Simplified the usage by adding HTTPClient

* using the new client API

* Using the old ctx import

* Using the old context import

* Refactored db code

* More refactoring

* Support for arbitrary entity types in the request

* Renamed fields; Added documentation

* Removing a redundant else case

* Code readability improvements

* Cleaned up the RTDB HTTP client code

* Added shallow reads support; Added the new txn API

* Implementing GetOrdered() for queries

* Adding more sorting tests

* Added Query ordering tests

* Fixing some lint errors and compilation errors

* Removing unused function

* Cleaned up unit tests for db

* Updated query impl and tests

* Added integration tests for ordered queries

* Removed With*() from query functions

* Updated change log; Added more tests

* Support for database url in auto init

* Support for loading auth overrides from env

* Removed db.AuthOverride type

* Renamed ao to authOverride everywhere; Other code review nits

* Introducing the QueryNode interface to handle ordered query results (#100)

* Database Sample Snippets (#102)

* Adding database snippets

* Adding query snippets

* Added complex query samples

* Updated variable name

* Fixing a typo

* Fixing query example

* Updated DB snippets to use GetOrdered()

* Removing unnecessary placeholders in Fatalln() calls

* Removing unnecessary placeholders in Fatalln() calls

* Handling FCM canonical error codes (#103)

* Formatting test file with gofmt (#104)

* Bumped version to 2.6.0 (#105)
hiranya911 pushed a commit that referenced this pull request Mar 15, 2018
* Renamed some tests and test parameters for clarity, and adhere to Go conventions (#74)

* clean unused types (#76)

* Create CHANGELOG.md (#75) (#79)

* Create CHANGELOG.md

Initial changelog based on https://firebase.google.com/support/release-notes/admin/go

* change instance ID format (#82)

Changing the format of the "non-existing" instance ID in the integration tests to comply with the expected iid format.

* Import context from golang.org/x/net/ for 1.6 compatibility (#87)

* import golang.org/x/net/context instead of context for 1.6 compatibility

* Document non existing name in integration tests for iid (#85)

* Revoke Tokens (#77)

Adding TokensValidAfterMillis property, RevokeRefreshTokens(), and VerifyIDTokenAndCheckRevoked().

* Firebase Cloud Messaging API (#81)

* Adding Firebase Cloud Messaging (#62)

* initial commit for adding Firebase Cloud Messaging

* add validator

* use http const in messaging test

* add client version header for stats

* init integration test

* add integration test (validated on IOS today)

* add comment with URL to enable Firebase Cloud Messaging API

* fix broken test

* add integration tests

* accept a Message instead of RequestMessage + and rename method + send  / sendDryRun

* update fcm url

* rollback url endpoint

* fix http constants, change responseMessage visibility, change map[string]interface{} as map[string]string

* fix http constants

* fix integration tests

* fix APNS naming

* add validators

* Added APNS types; Updated tests

* Added more tests; Fixed APNS serialization

* Updated documentation

* Improved error handling inFCM

* Added utils file

* Updated integration tests

* Implemented topic management operations

* Added integration tests

* Updated CHANGELOG

* Addressing code review comments

* Supporting 0 valued Aps.Badge

* Addressing some review comments

* Removed some unused vars

* Accepting prefixed topic names (#84)

* Accepting prefixed topic named

* Added a comment

* Using new FCM error codes (#89)

* Bumped version to 2.5.0 (#90)

* Lint (#96)

* fix misspelling

* add check error

* missing copyright

* Doc (#97)

* update readme with Authentication Guide & Release Notes

* fix a misspelling : separately

* fix missing newline before package

* add Go Report Card + update doc

* add travis build for go versions 1.7.x -> 1.10.x (#98)

* add build for go version 1.6.x -> 1.10.x

* fix 1.10 version

* fix context to golang.org/x/net/context for go 1.6 compatibility

* add race detector + go vet on build + build without failure on go unstable

* add go16 et go17 file due to req.withcontext which is only go 1.7

* fix context package

* update go16.go to remove WithContext

* update bad import

* remove unused func

* finally use ctxhttp.Do with multiple build version

* ignore integration package for install

* fix go get command

* put go 1.6.X in allow_failures dur to test failure

* fix inversion of code

* remove go 1.6 support

* revert initial version with req.WithContext

* fix travis to support go 1.10.x

* nits

* Import context from standard package (#101)

* Import context from standard package.

* Firebase Database API (#92)

* Experimental RTDB code

* Added ref.Set()

* Added Push(), Update(), Remove() and tests

* Adding Transaction() support

* Fixed Transaction() API

* Code cleanup

* Implemented Query() API

* Added GetIfChanged() and integration tests

* More integration tests

* Updated unit test

* More integration tests

* Integration tests for queries

* Auth override support and more tests

* More test cases; AuthOverride support in App

* Implemented AuthOverride support; Added tests

* Implementing the new API

* More code cleanup

* Code clean up

* Refactored the http client code

* More tests

* Boosted test coverage to 97%

* Better error messages in tests; Added license headers

* Added documentatioon and cleaned up tests

* Fixing a build break

* Finishing up documentation

* More test cases

* Implemented a reusable HTTP client API

* Added test cases

* Comment clean up

* Using the shared http client API

* Simplified the usage by adding HTTPClient

* using the new client API

* Using the old ctx import

* Using the old context import

* Refactored db code

* More refactoring

* Support for arbitrary entity types in the request

* Renamed fields; Added documentation

* Removing a redundant else case

* Code readability improvements

* Cleaned up the RTDB HTTP client code

* Added shallow reads support; Added the new txn API

* Implementing GetOrdered() for queries

* Adding more sorting tests

* Added Query ordering tests

* Fixing some lint errors and compilation errors

* Removing unused function

* Cleaned up unit tests for db

* Updated query impl and tests

* Added integration tests for ordered queries

* Removed With*() from query functions

* Updated change log; Added more tests

* Support for database url in auto init

* Support for loading auth overrides from env

* Removed db.AuthOverride type

* Renamed ao to authOverride everywhere; Other code review nits

* Introducing the QueryNode interface to handle ordered query results (#100)

* Database Sample Snippets (#102)

* Adding database snippets

* Adding query snippets

* Added complex query samples

* Updated variable name

* Fixing a typo

* Fixing query example

* Updated DB snippets to use GetOrdered()

* Removing unnecessary placeholders in Fatalln() calls

* Removing unnecessary placeholders in Fatalln() calls

* Handling FCM canonical error codes (#103)

* Formatting test file with gofmt (#104)

* Bumped version to 2.6.0 (#105)

* Formatting (simplification) changes (#107)

* Checking for unformatted files in CI (#108)

* Checking for unformatted files in CI

* Adding newline at eof

* Document Minimum Go Version (#111)

* Fix invalid endpoint URL for topic unsubscribe (#114)

* Fix error message for missing user (#113)

* Update CHANGELOG.md (#117)

* Removing unused member from auth.Client (#118)

* Support Go 1.6 (#120)

* all: use golang.org/x/net/context

* internal: use ctxhttp to use /x/ context

The 1.6 Request type doesn't have WithContext.

* all: don't use subtests to keep 1.6 compatibility

* integration: use float64 for fields with exp value

Values like -7e+07 cannot be parsed into ints in Go 1.6. So, use
floats instead.

* integration/messaging: use t.Fatal not log.Fatal

* travis: add 1.6.x

* changelog: mention addition of 1.6 support

* readme: mention go version support

* Bumped version to 2.6.1 (#121)

* Changlog updates (#123)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants