diff --git a/internal/cmd/testdata/success_csharp.golden b/internal/cmd/testdata/success_csharp.golden index bc7ea31..7a3069c 100644 --- a/internal/cmd/testdata/success_csharp.golden +++ b/internal/cmd/testdata/success_csharp.golden @@ -158,15 +158,15 @@ namespace TestNamespace /// /// /// Flag key: themeCustomization - /// Default value: new Dictionary{{ "primaryColor", "#007bff" }, { "secondaryColor", "#6c757d" }} + /// Default value: new Value(new Structure.Builder().Set("primaryColor", "#007bff").Set("secondaryColor", "#6c757d").Build()) /// Type: object /// /// Optional context for the flag evaluation /// Options for flag evaluation /// The flag value - public async Task ThemeCustomizationAsync(EvaluationContext? evaluationContext = null, FlagEvaluationOptions? options = null) + public async Task ThemeCustomizationAsync(EvaluationContext? evaluationContext = null, FlagEvaluationOptions? options = null) { - return await _client.GetObjectValueAsync("themeCustomization", new Dictionary{{ "primaryColor", "#007bff" }, { "secondaryColor", "#6c757d" }}, evaluationContext, options); + return await _client.GetObjectValueAsync("themeCustomization", new Value(new Structure.Builder().Set("primaryColor", "#007bff").Set("secondaryColor", "#6c757d").Build()), evaluationContext, options); } /// @@ -174,15 +174,15 @@ namespace TestNamespace /// /// /// Flag key: themeCustomization - /// Default value: new Dictionary{{ "primaryColor", "#007bff" }, { "secondaryColor", "#6c757d" }} + /// Default value: new Value(new Structure.Builder().Set("primaryColor", "#007bff").Set("secondaryColor", "#6c757d").Build()) /// Type: object /// /// Optional context for the flag evaluation /// Options for flag evaluation /// The evaluation details containing the flag value and metadata - public async Task> ThemeCustomizationDetailsAsync(EvaluationContext? evaluationContext = null, FlagEvaluationOptions? options = null) + public async Task> ThemeCustomizationDetailsAsync(EvaluationContext? evaluationContext = null, FlagEvaluationOptions? options = null) { - return await _client.GetObjectValueAsync("themeCustomization", new Dictionary{{ "primaryColor", "#007bff" }, { "secondaryColor", "#6c757d" }}, evaluationContext, options); + return await _client.GetObjectValueAsync("themeCustomization", new Value(new Structure.Builder().Set("primaryColor", "#007bff").Set("secondaryColor", "#6c757d").Build()), evaluationContext, options); } /// diff --git a/internal/generators/csharp/csharp.go b/internal/generators/csharp/csharp.go index 907a58f..2363a92 100644 --- a/internal/generators/csharp/csharp.go +++ b/internal/generators/csharp/csharp.go @@ -2,7 +2,6 @@ package csharp import ( _ "embed" - "encoding/json" "fmt" "maps" "slices" @@ -65,17 +64,14 @@ func toCSharpDict(value any) string { keys := slices.Sorted(maps.Keys(assertedMap)) var builder strings.Builder - builder.WriteString("new Dictionary{") + builder.WriteString("new Value(new Structure.Builder()") - for index, key := range keys { - if index > 0 { - builder.WriteString(", ") - } + for _, key := range keys { val := assertedMap[key] - builder.WriteString(fmt.Sprintf("{ %q, %s }", key, formatNestedValue(val))) + builder.WriteString(fmt.Sprintf(".Set(%q, %s)", key, formatNestedValue(val))) } - builder.WriteString("}") + builder.WriteString(".Build())") return builder.String() } @@ -110,22 +106,18 @@ func formatNestedValue(value any) string { return toCSharpDict(val) case []any: var sliceBuilder strings.Builder - sliceBuilder.WriteString("new List{") + sliceBuilder.WriteString("new Value(new List{") for index, elem := range val { if index > 0 { - sliceBuilder.WriteString(",") + sliceBuilder.WriteString(", ") } sliceBuilder.WriteString(formatNestedValue(elem)) } - sliceBuilder.WriteString("}") + sliceBuilder.WriteString("})") return sliceBuilder.String() default: - jsonBytes, err := json.Marshal(val) - if err != nil { - return "null" - } - return fmt.Sprintf("%q", string(jsonBytes)) + return fmt.Sprintf("new Value(%s)", val) } } diff --git a/internal/generators/csharp/csharp.tmpl b/internal/generators/csharp/csharp.tmpl index 4473dbf..759fa09 100644 --- a/internal/generators/csharp/csharp.tmpl +++ b/internal/generators/csharp/csharp.tmpl @@ -70,7 +70,7 @@ namespace {{ if .Params.Custom.Namespace }}{{ .Params.Custom.Namespace }}{{ else /// Optional context for the flag evaluation /// Options for flag evaluation /// The flag value - public async Task<{{ .Type | OpenFeatureType }}> {{ .Key | ToPascal }}Async(EvaluationContext? evaluationContext = null, FlagEvaluationOptions? options = null) + 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); @@ -98,7 +98,7 @@ namespace {{ if .Params.Custom.Namespace }}{{ .Params.Custom.Namespace }}{{ else /// Optional context for the flag evaluation /// Options for flag evaluation /// The evaluation details containing the flag value and metadata - public async Task> {{ .Key | ToPascal }}DetailsAsync(EvaluationContext? evaluationContext = null, FlagEvaluationOptions? options = null) + public async Task> {{ .Key | ToPascal }}DetailsAsync(EvaluationContext? evaluationContext = null, FlagEvaluationOptions? options = null) { {{- if eq .Type 1 }} return await _client.GetIntegerDetailsAsync("{{ .Key }}", {{ . | FormatDefaultValue }}, evaluationContext, options);