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

Support branch connection arg #79

Merged
merged 1 commit into from
Apr 18, 2024
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
2 changes: 1 addition & 1 deletion src/EdgeDB.Net.Driver/Binary/Common/ConnectionParam.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public int Size

public string Name { get; init; }

public string Value { get; init; }
public string? Value { get; init; }

public void Write(ref PacketWriter writer)
{
Expand Down
2 changes: 1 addition & 1 deletion src/EdgeDB.Net.Driver/Binary/PacketWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public void Write(ref ReadOnlyMemory<byte> memory)
pin.Dispose();
}

public void Write(string value)
public void Write(string? value)
{
if (value is null)
Write((uint)0);
Expand Down
26 changes: 12 additions & 14 deletions src/EdgeDB.Net.Driver/Binary/Protocol/V1.0/V1ProtocolProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -420,24 +420,22 @@ public virtual ITypeDescriptor GetDescriptor(ref PacketReader reader)
return descriptor;
}

public virtual Sendable Handshake() =>
new ClientHandshake
public virtual Sendable Handshake()
{
return new ClientHandshake
{
MajorVersion = Version.Major,
MinorVersion = Version.Minor,
ConnectionParameters = _client.Connection.SecretKey is not null
? new ConnectionParam[]
{
new() {Name = "user", Value = _client.Connection.Username!},
new() {Name = "database", Value = _client.Connection.Database!},
new() {Name = "secret_key", Value = _client.Connection.SecretKey}
}
: new ConnectionParam[]
{
new() {Name = "user", Value = _client.Connection.Username!},
new() {Name = "database", Value = _client.Connection.Database!}
}
ConnectionParameters = new ConnectionParam[]
{
new() {Name = "user", Value = _client.Connection.Username},
new() {Name = "database", Value = _client.Connection.Database},
new() {Name = "secret_key", Value = _client.Connection.SecretKey},
new() {Name = "branch", Value = _client.Connection.Branch}
}
};
}


public virtual ValueTask ProcessAsync<T>(in T message) where T : IReceiveable
{
Expand Down
61 changes: 56 additions & 5 deletions src/EdgeDB.Net.Driver/EdgeDBConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public sealed class EdgeDBConnection
private const string EDGEDB_USER_ENV_NAME = "EDGEDB_USER";
private const string EDGEDB_PASSWORD_ENV_NAME = "EDGEDB_PASSWORD";
private const string EDGEDB_DATABASE_ENV_NAME = "EDGEDB_DATABASE";
private const string EDGEDB_BRANCH_ENV_NAME = "EDGEDB_BRANCH";
private const string EDGEDB_HOST_ENV_NAME = "EDGEDB_HOST";
private const string EDGEDB_PORT_ENV_NAME = "EDGEDB_PORT";
private const string EDGEDB_CLOUD_PROFILE_ENV_NAME = "EDGEDB_CLOUD_PROFILE";
Expand Down Expand Up @@ -142,13 +143,46 @@ public int Port
/// Gets or sets the database name to use when connecting.
/// </summary>
/// <remarks>
/// This property defaults to edgedb
/// This property defaults to <c>edgedb</c>. It is mutually exclusive with <see cref="Branch"/>.
/// </remarks>
/// <exception cref="InvalidOperationException"><see cref="Branch"/> already contains a value; they're mutually exclusive</exception>
[JsonProperty("database")]
public string? Database
{
get => _database ?? "edgedb";
set => _database = value;
get => _database ?? _branch ?? "edgedb";
set
{
if (_branch is not null)
{
throw new InvalidOperationException(
"Cannot set database: database conflicts with already provided branch");
}

_database = value;
}
}

/// <summary>
/// Gets or sets the branch name to use when connecting.
/// </summary>
/// <remarks>
/// This property defaults to <c>__default__</c>. It is mutually exclusive with <see cref="Database"/>
/// </remarks>
/// <exception cref="InvalidOperationException"><see cref="Database"/> already contains a value; they're mutually exclusive</exception>
[JsonProperty("branch")]
public string? Branch
{
get => _database ?? _branch ?? "__default__";
set
{
if (_database is not null)
{
throw new InvalidOperationException(
"Cannot set branch: branch conflicts with already provided database");
}

_branch = value;
}
}

/// <summary>
Expand Down Expand Up @@ -213,6 +247,7 @@ public string CloudProfile

private string? _user;
private string? _database;
private string? _branch;
private string? _hostname;
private string? _cloudProfile;
private int? _port;
Expand Down Expand Up @@ -366,7 +401,7 @@ void SetArgument(string name, string? value, EdgeDBConnection conn)

conn.Hostname = value;
break;
case "database":
case "database" or "branch":
if (database is not null)
throw new ArgumentException("Database ambiguity mismatch");

Expand Down Expand Up @@ -404,6 +439,13 @@ void SetArgument(string name, string? value, EdgeDBConnection conn)
}
}

if (args.Any(x => x.Key.StartsWith("branch", StringComparison.InvariantCultureIgnoreCase)) && args.Any(x =>
x.Key.StartsWith("database", StringComparison.InvariantCultureIgnoreCase)))
{
throw new ArgumentException("branch conflicts with database");
}


// query arguments
foreach (var arg in args)
{
Expand All @@ -421,7 +463,7 @@ void SetArgument(string name, string? value, EdgeDBConnection conn)
var val = Environment.GetEnvironmentVariable(arg.Value, EnvironmentVariableTarget.Process);

if (val == null)
throw new KeyNotFoundException($"Enviroment variable \"{arg.Value}\" couldn't be found");
throw new KeyNotFoundException($"Environment variable \"{arg.Value}\" couldn't be found");

SetArgument(envMatch.Groups[1].Value, val, conn);
}
Expand Down Expand Up @@ -699,10 +741,19 @@ public static EdgeDBConnection Parse(string? instance = null, string? dsn = null

if (env.Contains(EDGEDB_DATABASE_ENV_NAME))
{
if (env.Contains(EDGEDB_BRANCH_ENV_NAME))
throw new ArgumentException($"{EDGEDB_DATABASE_ENV_NAME} conflicts with {EDGEDB_BRANCH_ENV_NAME}");

connection ??= new EdgeDBConnection();
connection.Database = (string)env[EDGEDB_DATABASE_ENV_NAME]!;
}

if (env.Contains(EDGEDB_BRANCH_ENV_NAME))
{
connection ??= new EdgeDBConnection();
connection.Branch = (string)env[EDGEDB_BRANCH_ENV_NAME]!;
}

#endregion

if (instance is not null)
Expand Down
Loading