-
Notifications
You must be signed in to change notification settings - Fork 57
Env config #509
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
Env config #509
Changes from 2 commits
51d492c
5665732
b7536f2
e22dde7
baf24c0
535b8a0
8c80846
3284607
58f06b7
049b3d0
8ba428c
33aadd9
05d7cf6
28eb602
9afb69b
0cb850b
99ca3e8
4a621e1
dd51f40
a8b4305
f563daa
28a93d8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
|
Check failure on line 3 in src/Temporalio/Client/Configuration/ClientConfig.cs
|
||
| using System.Text.Json; | ||
|
|
||
| namespace Temporalio.Client.Configuration | ||
| { | ||
| /// <summary> | ||
| /// Represents the overall client configuration. | ||
| /// </summary> | ||
| public sealed class ClientConfig : ICloneable | ||
|
THardy98 marked this conversation as resolved.
Outdated
|
||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="ClientConfig"/> class. | ||
| /// </summary> | ||
| /// <param name="profiles">The configuration profiles.</param> | ||
| public ClientConfig(IReadOnlyDictionary<string, ClientConfigProfile> profiles) | ||
| { | ||
| Profiles = profiles ?? new Dictionary<string, ClientConfigProfile>(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the configuration profiles. | ||
| /// </summary> | ||
| public IReadOnlyDictionary<string, ClientConfigProfile> Profiles { get; private set; } | ||
|
THardy98 marked this conversation as resolved.
Outdated
|
||
|
|
||
| /// <summary> | ||
| /// Load client configuration from environment variables and configuration files. | ||
| /// </summary> | ||
| /// <param name="configSource">The data source to load from.</param> | ||
| /// <param name="disableFile">If true, do not load from file (only from environment).</param> | ||
| /// <param name="configFileStrict">If true, fail if configuration file is invalid.</param> | ||
| /// <param name="overrideEnvVars">Environment variables to use, or null to use system environment.</param> | ||
| /// <returns>Loaded configuration data.</returns> | ||
| public static ClientConfig Load( | ||
| DataSource? configSource = null, | ||
| bool disableFile = false, | ||
| bool configFileStrict = false, | ||
| Dictionary<string, string>? overrideEnvVars = null) | ||
|
THardy98 marked this conversation as resolved.
Outdated
|
||
| { | ||
| var runtime = Runtime.TemporalRuntime.Default.Runtime; | ||
| var profiles = Bridge.EnvConfig.LoadClientConfig( | ||
| runtime, | ||
| configSource ?? DataSource.FromDefault(), | ||
| disableFile, | ||
| configFileStrict, | ||
| overrideEnvVars); | ||
| return new ClientConfig(profiles); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Load client connection options directly from configuration. | ||
| /// </summary> | ||
| /// <param name="profile">Name of the profile to load. If null, "default" is used.</param> | ||
| /// <param name="configSource">The data source to load from.</param> | ||
| /// <param name="disableFile">If true, do not load from file (only from environment).</param> | ||
| /// <param name="disableEnv">If true, disable environment variable overrides.</param> | ||
| /// <param name="configFileStrict">If true, fail if configuration file is invalid.</param> | ||
| /// <param name="overrideEnvVars">Environment variables to use, or null to use system environment.</param> | ||
| /// <returns>Client connection options.</returns> | ||
| public static TemporalClientConnectOptions LoadClientConnectOptions( | ||
| string? profile = null, | ||
| DataSource? configSource = null, | ||
| bool disableFile = false, | ||
| bool disableEnv = false, | ||
| bool configFileStrict = false, | ||
| Dictionary<string, string>? overrideEnvVars = null) | ||
| { | ||
| var profileName = profile ?? "default"; | ||
| var clientProfile = ClientConfigProfile.Load( | ||
| profileName, | ||
| configSource, | ||
| disableFile, | ||
| disableEnv, | ||
| configFileStrict, | ||
| overrideEnvVars); | ||
| return clientProfile.ToConnectionOptions(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Deserialize client configuration from JSON. | ||
| /// </summary> | ||
| /// <param name="json">JSON string to deserialize.</param> | ||
| /// <param name="options">Optional JSON serializer options.</param> | ||
| /// <returns>Client configuration instance.</returns> | ||
| public static ClientConfig FromJson(string json, JsonSerializerOptions? options = null) | ||
|
THardy98 marked this conversation as resolved.
Outdated
|
||
| { | ||
| return JsonSerializer.Deserialize<ClientConfig>(json, options ?? GetDefaultJsonOptions()) | ||
| ?? throw new JsonException("Failed to deserialize ClientConfig from JSON."); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Serialize this client configuration to JSON. | ||
| /// </summary> | ||
| /// <param name="options">Optional JSON serializer options.</param> | ||
| /// <returns>JSON string representation.</returns> | ||
| public string ToJson(JsonSerializerOptions? options = null) | ||
| { | ||
| return JsonSerializer.Serialize(this, options ?? GetDefaultJsonOptions()); | ||
| } | ||
|
THardy98 marked this conversation as resolved.
Outdated
|
||
|
|
||
| /// <inheritdoc /> | ||
| public object Clone() | ||
| { | ||
| var newConfig = (ClientConfig)MemberwiseClone(); | ||
| var clonedProfiles = new Dictionary<string, ClientConfigProfile>(); | ||
| foreach (var kvp in Profiles) | ||
| { | ||
| clonedProfiles[kvp.Key] = (ClientConfigProfile)kvp.Value.Clone(); | ||
| } | ||
| newConfig.Profiles = clonedProfiles; | ||
| return newConfig; | ||
| } | ||
|
|
||
| private static JsonSerializerOptions GetDefaultJsonOptions() | ||
|
THardy98 marked this conversation as resolved.
Outdated
|
||
| { | ||
| return new JsonSerializerOptions | ||
| { | ||
| PropertyNamingPolicy = JsonNamingPolicy.CamelCase, | ||
| WriteIndented = true, | ||
| }; | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.