integrates Dynamic ENUM Metadata within Rules #3

Merged
florian merged 1 commits from feature/allow-enums-in-rules into main 2024-02-06 21:52:00 +01:00
4 changed files with 81 additions and 26 deletions
Showing only changes of commit 565f920b10 - Show all commits

View File

@ -56,24 +56,38 @@ func getHostnameRuleByCategory(category string) (rules.HostnameRule, error) {
// @Router /rules/:rule [get] // @Router /rules/:rule [get]
func GetRuleStruct(c *gin.Context) { func GetRuleStruct(c *gin.Context) {
ruleName := c.Param("rule") ruleName := c.Param("rule")
descriptor, exists := rules.RulesRegistry[ruleName] descriptor, exists := rules.RulesRegistry[ruleName]
if !exists { if !exists {
c.JSON(http.StatusNotFound, gin.H{"error": "Rule not found"}) c.JSON(http.StatusNotFound, gin.H{"error": "Rule not found"})
return return
} }
// Create instances of the rule and its input struct // Create an instance of the rule struct for output
ruleInstance := descriptor.Factory() 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) ruleJSON, err := rules.StructToJSON(ruleInstance)
if err != nil { if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error serializing rule struct"}) c.JSON(http.StatusInternalServerError, gin.H{"error": "Error serializing rule struct"})
return 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) inputJSON, err := rules.StructToJSON(inputInstance)
if err != nil { if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error serializing input struct"}) 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{ c.JSON(http.StatusOK, gin.H{
"input": inputJSON, "input": inputJSON, // Input struct with possible ENUM values
"output": ruleJSON, "enums": enumInfo, // ENUM metadata if available
"output": ruleJSON, // Serialized rule instance
}) })
} }

25
rules/enum.go Normal file
View File

@ -0,0 +1,25 @@
// Copyright 2024 Florian Beisel
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package rules
type EnumFieldMetadata struct {
FieldName string
Values []string
}
// EnumProvider interface that rules should implement if they provide enum values.
type EnumProvider interface {
EnumOptions() []EnumFieldMetadata
}

View File

@ -14,21 +14,26 @@
package rules package rules
import "reflect"
type RuleFactoryFunc func() HostnameRule type RuleFactoryFunc func() HostnameRule
type RuleDescriptor struct { type RuleDescriptor struct {
Description string Description string
Factory RuleFactoryFunc Factory RuleFactoryFunc
InputStruct reflect.Type
} }
var RulesRegistry = map[string]RuleDescriptor{ var RulesRegistry = map[string]RuleDescriptor{
"notebook": { "notebook": {
Description: "Generates hostnames for notebooks.", Description: "Generates hostnames for notebooks.",
Factory: func() HostnameRule { return &NotebookRule{} }, Factory: func() HostnameRule { return &NotebookRule{} },
InputStruct: reflect.TypeOf(NotebookRuleInput{}),
}, },
"server": { "server": {
Description: "Generates hostnames for servers.", Description: "Generates hostnames for servers.",
Factory: func() HostnameRule { return &ServerRule{} }, Factory: func() HostnameRule { return &ServerRule{} },
InputStruct: reflect.TypeOf(ServerRuleInput{}),
}, },
// ... other rules ... // ... other rules ...
} }

View File

@ -43,6 +43,16 @@ type ServerRuleInput struct {
Responsible string `json:"Responsible"` Responsible string `json:"Responsible"`
} }
func (sr *ServerRule) EnumOptions() []EnumFieldMetadata {
return []EnumFieldMetadata{
{
FieldName: "OrgUnit",
Values: []string{"IDG", "IDE", "SSG"},
},
// Add more fields as necessary
}
}
// Ensure that ServerRule implements HostnameRule interface // Ensure that ServerRule implements HostnameRule interface
var _ HostnameRule = &ServerRule{} var _ HostnameRule = &ServerRule{}