cli/internal/generators/csharp/csharp.tmpl

149 lines
7.5 KiB
Cheetah

// AUTOMATICALLY GENERATED BY OPENFEATURE CLI, DO NOT EDIT.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Threading;
using Microsoft.Extensions.DependencyInjection;
using OpenFeature;
using OpenFeature.Model;
namespace {{ if .Params.Custom.Namespace }}{{ .Params.Custom.Namespace }}{{ else }}OpenFeatureGenerated{{ end }}
{
/// <summary>
/// Service collection extensions for OpenFeature
/// </summary>
public static class OpenFeatureServiceExtensions
{
/// <summary>
/// Adds OpenFeature services to the service collection with the generated client
/// </summary>
/// <param name="services">The service collection to add services to</param>
/// <returns>The service collection for chaining</returns>
public static IServiceCollection AddOpenFeature(this IServiceCollection services)
{
return services
.AddSingleton(_ => Api.Instance)
.AddSingleton(provider => provider.GetRequiredService<Api>().GetClient())
.AddSingleton<GeneratedClient>();
}
/// <summary>
/// Adds OpenFeature services to the service collection with the generated client for a specific domain
/// </summary>
/// <param name="services">The service collection to add services to</param>
/// <param name="domain">The domain to get the client for</param>
/// <returns>The service collection for chaining</returns>
public static IServiceCollection AddOpenFeature(this IServiceCollection services, string domain)
{
return services
.AddSingleton(_ => Api.Instance)
.AddSingleton(provider => provider.GetRequiredService<Api>().GetClient(domain))
.AddSingleton<GeneratedClient>();
}
}
/// <summary>
/// Generated OpenFeature client for typesafe flag access
/// </summary>
public class GeneratedClient
{
private readonly IFeatureClient _client;
/// <summary>
/// Initializes a new instance of the <see cref="GeneratedClient"/> class.
/// </summary>
/// <param name="client">The OpenFeature client to use for flag evaluations.</param>
public GeneratedClient(IFeatureClient client)
{
_client = client ?? throw new ArgumentNullException(nameof(client));
}
{{- range .Flagset.Flags }}
/// <summary>
/// {{ .Description }}
/// </summary>
/// <remarks>
/// <para>Flag key: {{ .Key }}</para>
/// <para>Default value: {{ if eq (.Type | OpenFeatureType) "object" }}{{ .DefaultValue | ToCSharpDict }}{{ else }}{{ .DefaultValue }}{{ end }}</para>
/// <para>Type: {{ .Type | OpenFeatureType }}</para>
/// </remarks>
/// <param name="evaluationContext">Optional context for the flag evaluation</param>
/// <param name="options">Options for flag evaluation</param>
/// <returns>The flag value</returns>
public async Task<{{ if eq (.Type | OpenFeatureType) "object" }}Value{{ else }}{{ .Type | OpenFeatureType }}{{ end }}> {{ .Key | ToPascal }}Async(EvaluationContext? evaluationContext = null, FlagEvaluationOptions? options = null)
{
{{- if eq .Type 1 }}
return await _client.GetIntegerValueAsync("{{ .Key }}", {{ . | FormatDefaultValue }}, evaluationContext, options);
{{- else if eq .Type 2 }}
return await _client.GetDoubleValueAsync("{{ .Key }}", {{ . | FormatDefaultValue }}, evaluationContext, options);
{{- else if eq .Type 3 }}
return await _client.GetBooleanValueAsync("{{ .Key }}", {{ . | FormatDefaultValue }}, evaluationContext, options);
{{- else if eq .Type 4 }}
return await _client.GetStringValueAsync("{{ .Key }}", {{ . | FormatDefaultValue }}, evaluationContext, options);
{{- else if eq .Type 5 }}
return await _client.GetObjectValueAsync("{{ .Key }}", {{ if eq (.Type | OpenFeatureType) "object" }}{{ .DefaultValue | ToCSharpDict }}{{ else }}{{ . | FormatDefaultValue }}{{ end }}, evaluationContext, options);
{{- else }}
throw new NotSupportedException("Unsupported flag type");
{{- end }}
}
/// <summary>
/// {{ .Description }}
/// </summary>
/// <remarks>
/// <para>Flag key: {{ .Key }}</para>
/// <para>Default value: {{ if eq (.Type | OpenFeatureType) "object" }}{{ .DefaultValue | ToCSharpDict }}{{ else }}{{ .DefaultValue }}{{ end }}</para>
/// <para>Type: {{ .Type | OpenFeatureType }}</para>
/// </remarks>
/// <param name="evaluationContext">Optional context for the flag evaluation</param>
/// <param name="options">Options for flag evaluation</param>
/// <returns>The evaluation details containing the flag value and metadata</returns>
public async Task<FlagEvaluationDetails<{{ if eq (.Type | OpenFeatureType) "object" }}Value{{ else }}{{ .Type | OpenFeatureType }}{{ end }}>> {{ .Key | ToPascal }}DetailsAsync(EvaluationContext? evaluationContext = null, FlagEvaluationOptions? options = null)
{
{{- if eq .Type 1 }}
return await _client.GetIntegerDetailsAsync("{{ .Key }}", {{ . | FormatDefaultValue }}, evaluationContext, options);
{{- else if eq .Type 2 }}
return await _client.GetDoubleDetailsAsync("{{ .Key }}", {{ . | FormatDefaultValue }}, evaluationContext, options);
{{- else if eq .Type 3 }}
return await _client.GetBooleanDetailsAsync("{{ .Key }}", {{ . | FormatDefaultValue }}, evaluationContext, options);
{{- else if eq .Type 4 }}
return await _client.GetStringDetailsAsync("{{ .Key }}", {{ . | FormatDefaultValue }}, evaluationContext, options);
{{- else if eq .Type 5 }}
return await _client.GetObjectDetailsAsync("{{ .Key }}", {{ if eq (.Type | OpenFeatureType) "object" }}{{ .DefaultValue | ToCSharpDict }}{{ else }}{{ . | FormatDefaultValue }}{{ end }}, evaluationContext, options);
{{- else }}
throw new NotSupportedException("Unsupported flag type");
{{- end }}
}
{{ end }}
/// <summary>
/// Creates a new GeneratedClient using the default OpenFeature client
/// </summary>
/// <returns>A new GeneratedClient instance</returns>
public static GeneratedClient CreateClient()
{
return new GeneratedClient(Api.Instance.GetClient());
}
/// <summary>
/// Creates a new GeneratedClient using a domain-specific OpenFeature client
/// </summary>
/// <param name="domain">The domain to get the client for</param>
/// <returns>A new GeneratedClient instance</returns>
public static GeneratedClient CreateClient(string domain)
{
return new GeneratedClient(Api.Instance.GetClient(domain));
}
/// <summary>
/// Creates a new GeneratedClient using a domain-specific OpenFeature client with context
/// </summary>
/// <param name="domain">The domain to get the client for</param>
/// <param name="evaluationContext">Default context to use for evaluations</param>
/// <returns>A new GeneratedClient instance</returns>
public static GeneratedClient CreateClient(string domain, EvaluationContext? evaluationContext = null)
{
return new GeneratedClient(Api.Instance.GetClient(domain));
}
}
}