Don't trigger warning / error if configuration value is nil.

This makes getStringFromTable() and getIntFromTable() only return errors
if there's a type mismatch.

Example: 'name' should be a 'string'. If it's a number, an error is
triggered.
This commit is contained in:
Noah 2024-04-15 15:10:34 -05:00
parent 2e5c3dbed6
commit c19cb14ade

View File

@ -20,27 +20,27 @@ type Configuration struct {
} }
// getStringFromTable retrieves a string from a table on the lua stack using the key passed in. // getStringFromTable retrieves a string from a table on the lua stack using the key passed in.
//
// If the value is nil, 'out' is unchanged.
func getStringFromTable(L *lua.LState, lobj lua.LValue, key string, out *string) (bool, error) { func getStringFromTable(L *lua.LState, lobj lua.LValue, key string, out *string) (bool, error) {
lv := L.GetTable(lobj, lua.LString(key)) lv := L.GetTable(lobj, lua.LString(key))
if text, ok := lv.(lua.LString); ok { // Value is string if text, ok := lv.(lua.LString); ok { // Value is string
*out = string(text) *out = string(text)
} else if _, ok := lv.(*lua.LNilType); !ok { // Value is not a string, and is not nil } else if _, ok := lv.(*lua.LNilType); !ok { // Value is not a string, and is not nil
return true, errors.New("'" + key + "' is not a string") return true, errors.New("'" + key + "' is not a string")
} else { // Value is nil
return false, errors.New("'" + key + "' is nil")
} }
return true, nil return true, nil
} }
// getIntFromTable retrieves an integer from a table on the Lua stack using the key passed in. // getIntFromTable retrieves an integer from a table on the Lua stack using the key passed in.
//
// If the value is nil, 'out' is unchanged.
func getIntFromTable(L *lua.LState, lobj lua.LValue, key string, out *int64) (bool, error) { func getIntFromTable(L *lua.LState, lobj lua.LValue, key string, out *int64) (bool, error) {
lv := L.GetTable(lobj, lua.LString(key)) lv := L.GetTable(lobj, lua.LString(key))
if int, ok := lv.(lua.LNumber); ok { // Value is number if int, ok := lv.(lua.LNumber); ok { // Value is number
*out = int64(int) *out = int64(int)
} else if _, ok := lv.(*lua.LNilType); !ok { // Value is not a number, and is not nil } else if _, ok := lv.(*lua.LNilType); !ok { // Value is not a number, and is not nil
return true, errors.New("'" + key + "' is not a number") return true, errors.New("'" + key + "' is not a number")
} else { // Value is nil
return false, errors.New("'" + key + "' is nil")
} }
return true, nil return true, nil
} }