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

Deprecate Uri conversion constructors in favor of an explicit fallible Uri::parse factory function. #579

Open
hallfox opened this issue Jan 22, 2025 · 0 comments
Labels
A-Client Area: C++ SDK

Comments

@hallfox
Copy link
Collaborator

hallfox commented Jan 22, 2025

The constructors on Uri that take a string have the following usage requirements:

/// If the `uri` input
/// string doesn't not represent a valid URI, this object is left in an
/// invalid state (isValid() will return false).

This is an easy footgun, as a URI parse failure is silent and requires the user to verify the validity of the Uri after construction. A better interface would make the fallibility of parsing a URI more directly addressable. Therefore, I propose a new interface to Uri:

class Uri {
public:
    /// Parse the specified `uriString` into the specified `result` object
    /// if `uriString` is a valid URI, otherwise load the specified
    /// `errorDescription` with a description of the syntax error present in
    /// `uriString`.  Return 0 on success and non-zero if `uriString` does
    /// not have a valid syntax.  Note that `errorDescription` may be null
    /// if the caller does not care about getting error messages.  The
    /// behavior is undefined unless `initialize` has been called
    /// previously.
  BSLA_NODISCARD
  static int parse(Uri*          result,
    const bslstl::StringRef& uriString,
    bsl::string*             errorDescription);
};

This is basically the function UriParser::parse, except that interface requires the user to call UriParser::init(allocator) prior to use. The Uri::parse function will effectively do this:

int Uri::parse(Uri*          result,
    const bslstl::StringRef& uriString,
    bsl::string*             errorDescription)
{
  UriParser::initialize();
  result->d_wasParserInitialized = true;
  return UriParser::parse(result, uriString, errorDescription);
}
@hallfox hallfox added the A-Client Area: C++ SDK label Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Client Area: C++ SDK
Projects
None yet
Development

No branches or pull requests

1 participant