-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
.NET Connector: Simplifies config using custom attributes (#100)
Using a custom attribute for `MyAgentConfig` properties to reduce the boilerplate code necessary to implement IAgentConfig. MyAgentConfig now extend and `AgentConflict` class that uses reflection to read the custom attributes and implements all the IAgentConfig logic.
- Loading branch information
1 parent
c18d48f
commit ddf9733
Showing
10 changed files
with
164 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
libraries/dotnet/WorkbenchConnector/AgentConfig/AgentConfig.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
|
||
// ReSharper disable once CheckNamespace | ||
namespace Microsoft.SemanticWorkbench.Connector; | ||
|
||
public class AgentConfig : IAgentConfig | ||
{ | ||
public object? ToWorkbenchFormat() | ||
{ | ||
Dictionary<string, object> result = new(); | ||
Dictionary<string, object> defs = new(); | ||
Dictionary<string, object> properties = new(); | ||
Dictionary<string, object> jsonSchema = new(); | ||
Dictionary<string, object> uiSchema = new(); | ||
|
||
foreach (var property in this.GetType().GetProperties()) | ||
{ | ||
var config = new Dictionary<string, object>(); | ||
var attributes = property.GetCustomAttributes<AgentConfigPropertyAttribute>(); | ||
foreach (var attribute in attributes) | ||
{ | ||
config[attribute.Name] = attribute.Value; | ||
} | ||
|
||
properties[property.Name] = config; | ||
|
||
if (config.TryGetValue("uischema", out var uiSchemaValue)) | ||
{ | ||
switch (uiSchemaValue) | ||
{ | ||
case "textarea": | ||
ConfigUtils.UseTextAreaFor(property.Name, uiSchema); | ||
break; | ||
case "radiobutton": | ||
ConfigUtils.UseRadioButtonsFor(property.Name, uiSchema); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
} | ||
|
||
jsonSchema["type"] = "object"; | ||
jsonSchema["title"] = "ConfigStateModel"; | ||
jsonSchema["additionalProperties"] = false; | ||
jsonSchema["properties"] = properties; | ||
jsonSchema["$defs"] = defs; | ||
|
||
result["json_schema"] = jsonSchema; | ||
result["ui_schema"] = uiSchema; | ||
result["config"] = this; | ||
|
||
return result; | ||
} | ||
|
||
public void Update(object? config) | ||
{ | ||
if (config == null) | ||
{ | ||
throw new ArgumentException("Empty configuration"); | ||
} | ||
|
||
if (config is not AgentConfig cfg) | ||
{ | ||
throw new ArgumentException("Incompatible configuration type"); | ||
} | ||
|
||
foreach (var property in this.GetType().GetProperties()) | ||
{ | ||
property.SetValue(this, property.GetValue(cfg)); | ||
} | ||
} | ||
} |
Oops, something went wrong.