CommandHelper

CommandHelper

46.5k Downloads

problem with array_get() and empty arrays

LadyCailinBot opened this issue ยท 1 comments

commented

CMDHELPER-2754 - Reported by Hekta

If an empty array is got from persistance, it is always considered as not associative, and array_get(get_value('an.empty.array'), 'stringKey', 'default') will always throw a CastException instead of returning 'default'.

commented

Comment by LadyCailin

Hmm. This is interesting. An array being associative is not information that is inherently stored with the array, it's inferred, based on the keys in the array. To change this would require a massive change, and require everybody to run an upgrade routine on their databases. I think I'm going to decline to do anything about this right now, but I will keep it in mind for the future, once I get true objects in place (and will likely require database upgrades anyways). In the meantime, you should be able to work around this easily enough with a proc:

proc(_getArrayFromStorage, @addr, @key, @def,
    @array = get_value(@addr)
    if(!is_integral(@key) && array_size(@array) == 0){
        # It's an empty array, and we're requesting a non integral value, so return the default
        return(@def)
    } else {
        return(array_get(@array, @key, @def))
    }
)
_getArrayFromStorage('value.to.get', 'stringValue', 'default')

I think that should work, anyways.