forked from thetaepsilon-gamedev/minetest-devsupport-modpack
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
modns: [+] add subloader helper for abstracting access to sub-components
- Loading branch information
thetaepsilon
committed
Sep 30, 2018
1 parent
aeb16d2
commit 130cd05
Showing
3 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
--[[ | ||
A subloader represents an abstraction of accessing sub-components of a given path. | ||
You can pass it a list of sub-path segments | ||
(e.g. {"some", "child"} as opposed to "some.child") | ||
or a single segment string to load a component relative to a base path; | ||
this base path is baked into the subloader at construction time. | ||
]] | ||
|
||
|
||
|
||
|
||
-- concat a path passed as a table of components, | ||
-- or accept just a string to act as a list of length one. | ||
-- data SubPath = Single Segment | Deep [Segment] | ||
-- concat_path :: SubPath -> PathSep -> SubPathStr | ||
local msg_concat = "path concatenation: expected a string or table, got " | ||
local concat_path = function(path, sep) | ||
local t = type(path) | ||
if t == "string" then | ||
return path | ||
elseif t == "table" then | ||
return table.concat(path, sep) | ||
else | ||
error(msg_concat .. t) | ||
end | ||
end | ||
|
||
|
||
|
||
|
||
|
||
local checkstr = function(v) assert(type(v) == "string") end | ||
|
||
-- it's closures all the way dooooooooown | ||
local attach_loader = function(loader) | ||
local create_subloader = function(base, sep) | ||
checkstr(base) | ||
checkstr(sep) | ||
base = base .. sep | ||
|
||
return function(subpath) | ||
local subs = concat_path(subpath, sep) | ||
local fullpath = base..subs | ||
return loader:get(fullpath) | ||
end | ||
end | ||
return create_subloader | ||
end | ||
|
||
|
||
|
||
return attach_loader | ||
|