fasten-onprem/backend/pkg/models/system_settings.go

71 lines
2.2 KiB
Go

package models
import (
"reflect"
)
type SystemSettings struct {
InstallationID string `json:"installation_id"`
InstallationSecret string `json:"installation_secret"`
}
// see https://gist.github.com/lelandbatey/a5c957b537bed39d1d6fb202c3b8de06
func (s *SystemSettings) FromSystemSettingsEntry(entry *SystemSettingEntry) error {
structType := reflect.ValueOf(s).Elem()
for i := 0; i < structType.NumField(); i++ {
typeField := structType.Type().Field(i)
if jsonTagValue := typeField.Tag.Get("json"); jsonTagValue == entry.SettingKeyName {
//fmt.Println("found field", field.Name)
if entry.SettingDataType == "numeric" {
structType.Field(i).SetInt(int64(entry.SettingValueNumeric))
} else if entry.SettingDataType == "string" {
structType.Field(i).SetString(entry.SettingValueString)
} else if entry.SettingDataType == "bool" {
structType.Field(i).SetBool(entry.SettingValueBool)
} else if entry.SettingDataType == "array" {
structType.Field(i).Set(reflect.ValueOf(entry.SettingValueArray))
}
break
}
}
//if entry.SettingKeyName == "dashboard_locations" {
// s.DashboardLocations = entry.SettingValueArray
//}
return nil
}
func (s *SystemSettings) ToSystemSettingsEntry(entries []SystemSettingEntry) ([]SystemSettingEntry, error) {
structType := reflect.ValueOf(s).Elem()
fieldNameNdxLookup := map[string]int{}
for i := 0; i < structType.NumField(); i++ {
typeField := structType.Type().Field(i)
jsonTagValue := typeField.Tag.Get("json")
fieldNameNdxLookup[jsonTagValue] = i
}
for ndx, entry := range entries {
fieldId := fieldNameNdxLookup[entry.SettingKeyName]
if entry.SettingDataType == "numeric" {
entries[ndx].SettingValueNumeric = int(structType.Field(fieldId).Int())
} else if entry.SettingDataType == "string" {
entries[ndx].SettingValueString = structType.Field(fieldId).String()
} else if entry.SettingDataType == "bool" {
entries[ndx].SettingValueBool = structType.Field(fieldId).Bool()
} else if entry.SettingDataType == "array" {
sliceVal := structType.Field(fieldId).Slice(0, structType.Field(fieldId).Len())
entries[ndx].SettingValueArray = sliceVal.Interface().([]string)
}
}
return entries, nil
}