I'm trying to do something like this:
Struct to save setting:
type Setting struct{
Name string
DataType types.Type
}
Setting definition:
var SETTING_DebugLogging Setting = Setting{
Name: "DebugLogging",
DataType: bool, <-- This is wrong
}
and then, get the value from a Map using reflection and the value on DataType:
// Get a value setting, or his default
func (s Setting) Get() interface{}{
val, ok := settings[s].(s.DataType) <-- This is wrong
if !ok{
return s.DefaultValue.(s.DataType) <-- This is wrong
}
return val
}
I wan't to do this way, to ensure "Get" is always returning the value expected for that setting. If it's an invalid one, returns the default value, so I don't have to check again in code.
Just doing var := setting.Get().(setting.DataType) would be enough, no checks needed.
I've also tried to use reflection.kind to save the type of the setting. And that works for storing it, but I can't use it to parse latter:
This is ok with reflect.Kind:
// Game to launch
var SETTING_GameToLaunch Setting = Setting{
Name: "GameToLaunch",
DataType: reflect.Bool,
DefaultValue: 2,
}
But this doesn't work latter:
func (s Setting) Get() interface{}{
val, ok := settings[s].(s.DataType)
So. Any ideas on how I can do that? Store a type, and use latter with reflection?