lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Ah, good catch! Would read the rest of the comments and see what I miss :)

Cheers!

On 6/7/2012 10:51 AM, Thijs Schreijer wrote:
I made one as well once, and failed on numeric keys like this one;

    local dk = dotkey
    local t = { one = { two = "12", t = true, f = false, ["end"] = "end" },
[1] = { two = "23" } }
    assert( dk.get(t, "1.two") == "23" )

so do you :)



-----Original Message-----
From: lua-l-bounces@lists.lua.org [mailto:lua-l-bounces@lists.lua.org] On
Behalf Of Dimiter 'malkia' Stanev
Sent: donderdag 7 juni 2012 1:14
To: lua
Subject: dot encoded string key

I wrote a little lua library that can read keys, which are dot encoded
("one.two.three") with some minimal checking:

It solves the problem, when the key (actually multiple keys) comes from a
string and needs to be looked in a table.

Here it is: (better formatted version than the one below)
https://gist.github.com/2885376

local dotkey = {}

----------------------------------------------------------
-- Returns the value of a dot encoded key from the table t
-- Returns nil if the key is not found.
-- Example: local t = { one = { two = 12 } }
--          print( dotkey.get( t, "one.two" ) ) -- prints 12
function dotkey.get(t, key)
     local b, s, p = string.byte, string.sub, 1
     for i=1, key:len()+1 do
        local c = b(key, i)
        if c==nil or c==46 then
           if p==i then
              error( "DOTKEY: Invalid character '" .. string.char(c) ..
"' (" .. c ..
		   ") found in key '"  .. tostring(key) .. "' at position: "
... p)
           end
	 -- Don't shortcut here. Check the whole key for errors!
           t,p = type(t)~="table" and nil or t[s(key, p, i-1)],i+1
        end
     end
     return t
end

if #{...}==0 then
     print( "dotkey: standalone test..." )
     local dk = dotkey
     local t = { one = { two = "12", t = true, f = false, ["end"] = "end" } }
     assert( dk.get(t, "one.two") == "12" )
     assert( dk.get(t, "one.t") == true )
     assert( dk.get(t, "one.t1") == nil )
     assert( dk.get(t, "one.f") == false )
     assert( dk.get(t, "one.f1") == nil )
     assert( dk.get(t, "one.end") == "end" )
     assert( type(dk.get(t, "one"))=="table" )
     assert( dk.get(t, "blah") == nil )
     assert( not xpcall(function() dk.get({}, "bad key.") end, type), "this
should fail" )
     assert( not xpcall(function() dk.get({}, ".bad key") end, type), "this
should fail" )
     assert( not xpcall(function() dk.get({}, ".") end, type), "this should
fail" )
     assert( not xpcall(function() dk.get({}, "") end, type), "this should
fail" ) end

return dotkey