Skip to content

Conversation

mikelehen
Copy link
Contributor

  • Creates a C++ api::Settings class as a container for the settings we pass
    into the core client.
  • Adds [FIRFirestoreSettings internalSettings] to convert from public API
    settings to api::Settings.

Input validation, etc. still happens in FIRFirestoreSettings.

* Creates a C++ api::Settings class as a container for the settings we pass
  into the core client.
* Adds [FIRFirestoreSettings internalSettings] to convert from public API
  settings to api::Settings.

Input validation, etc. still happens in FIRFirestoreSettings.
public:
Settings() = default;

void set_host(const std::string_view& value) {
Copy link
Contributor

Choose a reason for hiding this comment

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

There is no std::string_view yet.

Since you're going to make copy when you retain the value, you can make this take a std::string and then move it. If the caller passes a temporary (or std::moves) this will save a copy.

void set_host(std::string value) {
  host_ = std::move(value);
}

Copy link
Contributor

Choose a reason for hiding this comment

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

To add some background, std::string_view is from C++17. You could use absl::string_view, but since you're copying the value, Gil's suggestion is strictly better.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ahh, whoops! So I guess we're stuck on C++11 but for some reason our compilers (whatever xcode, cmake, and travis are using) happily accept this, but we can't rely on it because we need to be compilable with other compilers that might not support it (like MSVC or something?)? Is there no way to limit ourselves to c++11 features other than code review audits?

Anyway, I'll remove it but https://goto.google.com/totw/117 says:

passing by const reference is simpler and safer, so it's still a good default choice. However, if you're working in an area that's known to be performance-sensitive, or your benchmarks show that you're spending too much time copying function parameters, passing by value can be a very useful tool to have in your toolbox.

This is very much not performance sensitive, so I'll just do const std::string &. I'm actually not sure why I tried to use string_view in the first place.

@wilhuff
Copy link
Contributor

wilhuff commented Apr 11, 2019

You might consider moving some of the validation into the new C++ class to share it with the public C++ implementation.

@wilhuff wilhuff assigned mikelehen and unassigned wilhuff Apr 11, 2019
public:
Settings() = default;

void set_host(const std::string_view& value) {
Copy link
Contributor

Choose a reason for hiding this comment

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

To add some background, std::string_view is from C++17. You could use absl::string_view, but since you're copying the value, Gil's suggestion is strictly better.


private:
std::string host_;
bool ssl_enabled_;
Copy link
Contributor

Choose a reason for hiding this comment

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

Note: all the primitives will be uninitialized by the default constructor. I understand you initialize them right after creating a Settings object, but this still seems error-prone. I'd suggest using in-class initializers to prevent that (only for bool/int64_t, something like bool ssl_enabled_ = false;).

(Very optional: you could make set methods return a reference to Settings to enable chaining of the calls)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ooooh, good catch. I totally forgot that C++ thinks playing with uninitialized memory is a fun game.

I actually like the chaining idea, but I don't think it's probably worth re-working. And actually, if I were to rework it I think I'd go back and forego having setters and go the giant constructor route since that avoids the uninitialized problem too...

@mikelehen mikelehen changed the base branch from master to firestore-master April 11, 2019 22:03
@mikelehen mikelehen assigned wilhuff and unassigned mikelehen Apr 11, 2019
@mikelehen
Copy link
Contributor Author

You might consider moving some of the validation into the new C++ class to share it with the public C++ implementation.

This was part of why I was going back/forth on whether to make FIRFirestoreSettings a wrapper over api::FirestoreSettings or just create api::FirestoreSettings at the point that they set the settings. Doing the latter, I can't perform API validation at the point that the developer is setting the individual settings, and so I can't really port the validation without some amount of behavior change.

It's also just a handful of lines, so I don't think it's a big deal to duplicate (or refactor later) or whatever.

Copy link
Contributor

@wilhuff wilhuff left a comment

Choose a reason for hiding this comment

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

LGTM

@mikelehen mikelehen changed the base branch from firestore-master to master April 14, 2019 18:58
Copy link
Contributor Author

@mikelehen mikelehen left a comment

Choose a reason for hiding this comment

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

Thanks! Comments addressed.

@mikelehen mikelehen merged commit 6dca5e6 into master Apr 14, 2019
@mikelehen mikelehen deleted the mikelehen/port-settings branch April 14, 2019 19:28
Corrob pushed a commit that referenced this pull request Apr 25, 2019
* Creates a C++ api::Settings class as a container for the settings we pass
  into the core client.
* Adds [FIRFirestoreSettings internalSettings] to convert from public API
  settings to api::Settings.

Input validation, etc. still happens in FIRFirestoreSettings.
@firebase firebase locked and limited conversation to collaborators Oct 17, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants