feat: config initialization routines

This commit is contained in:
nihonium 2025-10-08 00:50:48 +03:00
parent 21e19fbc36
commit a5605262b3
Signed by untrusted user: nihonium
GPG key ID: 0251623741027CFC
7 changed files with 264 additions and 0 deletions

41
modules/server/helpers.go Normal file
View file

@ -0,0 +1,41 @@
package main
import (
"fmt"
"reflect"
)
func printStructTags(f reflect.Value) {
for i := 0; i < f.NumField(); i++ {
field := f.Type().Field(i)
tag := field.Tag
fmt.Printf("Field: %v.\nEnvironment variable: %v.\n", field.Name, tag.Get("env"))
}
}
func setField(obj interface{}, name string, value interface{}) error {
v := reflect.ValueOf(obj)
if v.Kind() != reflect.Ptr || v.Elem().Kind() != reflect.Struct {
return fmt.Errorf("expected pointer to a struct")
}
v = v.Elem()
field := v.FieldByName(name)
if !field.IsValid() {
return fmt.Errorf("no such field: %s", name)
}
if !field.CanSet() {
return fmt.Errorf("cannot set field: %s", name)
}
val := reflect.ValueOf(value)
if field.Type() != val.Type() {
return fmt.Errorf("provided value type (%s) doesn't match field type (%s)", val.Type(), field.Type())
}
field.Set(val)
return nil
}