integrates Dynamic ENUM Metadata within Rules

Summary:
This commit revolutionizes our approach to handling ENUM metadata for
rule input structures by embedding this metadata directly within each
rule's definition. By leveraging the `EnumProvider` interface and
utilizing reflection, we've created a system where rules self-describe
their ENUM fields, enhancing the API's flexibility and maintainability.

The `GetRuleStruct` API endpoint is now capable of dynamically generating
responses that include both the input structure and ENUM options, directly
derived from the rule definitions themselves.

Key Changes:
- **Introduced `EnumProvider` Interface**:
Allows rule structs to define their own ENUM options, centralizing this
information within the rule implementations.

- **Enhanced Rule Descriptors**:
Rule descriptors are streamlined to focus on factory methods and input
struct types, with ENUM metadata being provided by the rules themselves
through the `EnumProvider` interface.

- **Dynamic ENUM Metadata Retrieval**:
The `GetRuleStruct` function dynamically extracts ENUM metadata from
rules that implement the `EnumProvider`, ensuring the API response
includes relevant ENUM options for frontend UI generation.

- **API Response Enhancement**:
Adjusted the API to provide a response that includes the rule's input
structure, possible ENUM values, and the rule instance representation,
all derived dynamically to support extensible and maintainable rule
definitions.
This commit is contained in:
2024-02-06 21:05:44 +01:00
parent 71ea9cac18
commit 565f920b10
4 changed files with 81 additions and 26 deletions

View File

@ -56,24 +56,38 @@ func getHostnameRuleByCategory(category string) (rules.HostnameRule, error) {
// @Router /rules/:rule [get]
func GetRuleStruct(c *gin.Context) {
ruleName := c.Param("rule")
descriptor, exists := rules.RulesRegistry[ruleName]
if !exists {
c.JSON(http.StatusNotFound, gin.H{"error": "Rule not found"})
return
}
// Create instances of the rule and its input struct
// Create an instance of the rule struct for output
ruleInstance := descriptor.Factory()
inputInstance := reflect.New(reflect.TypeOf(ruleInstance).Elem()).Interface()
// Serialize instances to JSON
// Check if the rule instance implements EnumProvider for ENUM metadata
enumProvider, implements := ruleInstance.(rules.EnumProvider)
var enumInfo map[string][]string
if implements {
// Retrieve ENUM metadata from the rule instance
enumMetadata := enumProvider.EnumOptions()
enumInfo = make(map[string][]string)
for _, meta := range enumMetadata {
enumInfo[meta.FieldName] = meta.Values
}
}
// Serialize the rule instance to JSON for output
ruleJSON, err := rules.StructToJSON(ruleInstance)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error serializing rule struct"})
return
}
// Create an instance of the input struct for serialization
inputInstance := reflect.New(descriptor.InputStruct).Elem().Interface()
// Serialize the input struct instance to JSON
inputJSON, err := rules.StructToJSON(inputInstance)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error serializing input struct"})
@ -81,7 +95,8 @@ func GetRuleStruct(c *gin.Context) {
}
c.JSON(http.StatusOK, gin.H{
"input": inputJSON,
"output": ruleJSON,
"input": inputJSON, // Input struct with possible ENUM values
"enums": enumInfo, // ENUM metadata if available
"output": ruleJSON, // Serialized rule instance
})
}