use comments of variables for help

This commit is contained in:
Christian Goll
2025-10-14 14:54:39 +02:00
committed by Jonathon Anderson
parent facde98fb5
commit 68758e3e45
7 changed files with 183 additions and 87 deletions

View File

@@ -248,7 +248,7 @@ func recursiveFlatten(obj interface{}) (hasContent bool) {
Create a string slice, where every element represents a yaml entry, used for node/profile edit
in order to get a summary of all available elements
*/
func UnmarshalConf(obj interface{}, excludeList []string) (lines []string) {
func ConfToYaml(obj interface{}, excludeList []string) (lines []string) {
objType := reflect.TypeOf(obj)
// now iterate of every field
for i := 0; i < objType.NumField(); i++ {
@@ -264,7 +264,7 @@ func UnmarshalConf(obj interface{}, excludeList []string) (lines []string) {
typeLine = strings.Split(typeLine, ",")[0] + ":"
}
lines = append(lines, typeLine)
nestedLine := UnmarshalConf(reflect.New(field.Type.Elem()).Elem().Interface(), excludeList)
nestedLine := ConfToYaml(reflect.New(field.Type.Elem()).Elem().Interface(), excludeList)
for _, ln := range nestedLine {
lines = append(lines, " "+ln)
}
@@ -274,12 +274,12 @@ func UnmarshalConf(obj interface{}, excludeList []string) (lines []string) {
typeLine = strings.Split(typeLine, ",")[0] + ":"
}
lines = append(lines, typeLine, " element:")
nestedLine := UnmarshalConf(reflect.New(field.Type.Elem().Elem()).Elem().Interface(), excludeList)
nestedLine := ConfToYaml(reflect.New(field.Type.Elem().Elem()).Elem().Interface(), excludeList)
for _, ln := range nestedLine {
lines = append(lines, " "+ln)
}
} else if field.Type.Kind() == reflect.Struct && field.Anonymous {
nestedLine := UnmarshalConf(reflect.New(field.Type).Elem().Interface(), excludeList)
nestedLine := ConfToYaml(reflect.New(field.Type).Elem().Interface(), excludeList)
lines = append(lines, nestedLine...)
}
}
@@ -316,9 +316,39 @@ func getYamlString(myType reflect.StructField, excludeList []string) ([]string,
return []string{ymlStr}, true
}
/*
Getters for unexported fields
*/
// struectred type for variable
type TemplateVarDetails struct {
Name string
Comment string
Type string
LongOpt string
}
// Type to sore a map which looks and feels like the variables in a template
type TemplateVarMap map[string]TemplateVarDetails
// Fill the map so that every key is like a template variable and the value is the comment field
func (varMap TemplateVarMap) ConfToTemplateMap(obj interface{}, prefix string) {
objType := reflect.TypeOf(obj)
// now iterate of every field
for i := 0; i < objType.NumField(); i++ {
field := objType.Field(i)
if field.Type.Kind() == reflect.Ptr && field.Type.Elem().Kind() == reflect.Struct {
varMap.ConfToTemplateMap(reflect.New(field.Type.Elem()).Elem().Interface(), field.Name)
} else if field.Type.Kind() == reflect.Map && field.Type.Elem().Kind() == reflect.Ptr {
varMap.ConfToTemplateMap(reflect.New(field.Type.Elem().Elem()).Elem().Interface(), field.Name)
} else if field.Type.Kind() == reflect.Struct && field.Anonymous {
varMap.ConfToTemplateMap(reflect.New(field.Type).Elem().Interface(), field.Name)
} else {
varMap[prefix+"."+field.Name] = TemplateVarDetails{
Name: field.Name,
Comment: field.Tag.Get("comment"),
Type: field.Type.Name(),
LongOpt: field.Tag.Get("lopt"),
}
}
}
}
/*
Returns the id of the node