Semaphore/db/config.go

170 lines
4.2 KiB
Go
Raw Permalink Normal View History

2024-07-07 19:12:21 +02:00
package db
import (
"fmt"
"github.com/ansible-semaphore/semaphore/util"
"reflect"
"strings"
)
func ConvertFlatToNested(flatMap map[string]string) map[string]interface{} {
2024-07-07 20:08:23 +02:00
nestedMap := make(map[string]interface{})
for key, value := range flatMap {
parts := strings.Split(key, ".")
currentMap := nestedMap
for i, part := range parts {
if i == len(parts)-1 {
currentMap[part] = value
} else {
if _, exists := currentMap[part]; !exists {
currentMap[part] = make(map[string]interface{})
}
currentMap = currentMap[part].(map[string]interface{})
}
}
}
return nestedMap
}
func AssignMapToStruct[P *S, S any](m map[string]interface{}, s P) error {
2024-07-07 19:12:21 +02:00
v := reflect.ValueOf(s).Elem()
return assignMapToStructRecursive(m, v)
}
func cloneStruct(origValue reflect.Value) reflect.Value {
// Create a new instance of the same type as the original struct
cloneValue := reflect.New(origValue.Type()).Elem()
// Iterate over the fields of the struct
for i := 0; i < origValue.NumField(); i++ {
// Get the field value
fieldValue := origValue.Field(i)
// Set the field value in the clone
cloneValue.Field(i).Set(fieldValue)
}
// Return the cloned struct
return cloneValue
}
2024-07-07 19:12:21 +02:00
func assignMapToStructRecursive(m map[string]interface{}, structValue reflect.Value) error {
structType := structValue.Type()
for i := 0; i < structType.NumField(); i++ {
field := structType.Field(i)
jsonTag := field.Tag.Get("json")
if jsonTag == "" {
jsonTag = field.Name
} else {
jsonTag = strings.Split(jsonTag, ",")[0]
}
2024-07-07 20:08:23 +02:00
if value, ok := m[jsonTag]; ok {
fieldValue := structValue.FieldByName(field.Name)
if fieldValue.CanSet() {
2024-07-09 22:18:25 +02:00
2024-07-07 20:08:23 +02:00
val := reflect.ValueOf(value)
2024-07-07 20:53:32 +02:00
2024-07-07 20:08:23 +02:00
switch fieldValue.Kind() {
case reflect.Struct:
2024-07-07 20:53:32 +02:00
if val.Kind() != reflect.Map {
2024-07-07 20:08:23 +02:00
return fmt.Errorf("expected map for nested struct field %s but got %T", field.Name, value)
}
2024-07-07 20:53:32 +02:00
mapValue, ok := value.(map[string]interface{})
if !ok {
return fmt.Errorf("cannot assign value of type %T to field %s of type %s", value, field.Name, field.Type)
}
err := assignMapToStructRecursive(mapValue, fieldValue)
if err != nil {
return err
}
2024-07-07 20:08:23 +02:00
case reflect.Map:
2024-07-09 22:18:25 +02:00
if fieldValue.IsNil() {
mapValue := reflect.MakeMap(fieldValue.Type())
fieldValue.Set(mapValue)
}
2024-07-07 20:08:23 +02:00
// Handle map
2024-07-07 20:53:32 +02:00
if val.Kind() != reflect.Map {
return fmt.Errorf("expected map for field %s but got %T", field.Name, value)
}
2024-07-07 20:53:32 +02:00
for _, key := range val.MapKeys() {
mapElemValue := val.MapIndex(key)
mapElemType := fieldValue.Type().Elem()
srcVal := fieldValue.MapIndex(key)
var mapElem reflect.Value
if srcVal.IsValid() {
mapElem = cloneStruct(srcVal)
} else {
mapElem = reflect.New(mapElemType).Elem()
}
2024-07-07 20:53:32 +02:00
if mapElemType.Kind() == reflect.Struct {
if err := assignMapToStructRecursive(mapElemValue.Interface().(map[string]interface{}), mapElem); err != nil {
return err
}
} else {
if mapElemValue.Type().ConvertibleTo(mapElemType) {
mapElem.Set(mapElemValue.Convert(mapElemType))
2024-07-07 20:08:23 +02:00
} else {
2024-07-07 20:53:32 +02:00
newVal, converted := util.CastValueToKind(mapElemValue.Interface(), mapElemType.Kind())
if !converted {
2024-07-07 20:08:23 +02:00
return fmt.Errorf("cannot assign value of type %s to map element of type %s",
mapElemValue.Type(), mapElemType)
}
2024-07-07 20:53:32 +02:00
mapElem.Set(reflect.ValueOf(newVal))
2024-07-07 20:08:23 +02:00
}
}
2024-07-07 20:53:32 +02:00
fieldValue.SetMapIndex(key, mapElem)
2024-07-07 20:08:23 +02:00
}
2024-07-07 20:08:23 +02:00
default:
// Handle simple types
if val.Type().ConvertibleTo(fieldValue.Type()) {
fieldValue.Set(val.Convert(fieldValue.Type()))
} else {
2024-07-07 20:53:32 +02:00
newVal, converted := util.CastValueToKind(val.Interface(), fieldValue.Type().Kind())
if !converted {
return fmt.Errorf("cannot assign value of type %s to map element of type %s",
val.Type(), val)
}
fieldValue.Set(reflect.ValueOf(newVal))
2024-07-07 20:08:23 +02:00
}
}
2024-07-07 19:12:21 +02:00
}
}
}
return nil
}
func FillConfigFromDB(store Store) (err error) {
opts, err := store.GetOptions(RetrieveQueryParams{})
if err != nil {
return
}
options := ConvertFlatToNested(opts)
2024-07-07 19:12:21 +02:00
if options["apps"] == nil {
options["apps"] = make(map[string]interface{})
}
err = AssignMapToStruct(options, util.Config)
2024-07-07 19:12:21 +02:00
return
}