summaryrefslogtreecommitdiff
path: root/xmake/core/base/serialize.lua
diff options
context:
space:
mode:
authorOpportunityLiu <[email protected]>2019-07-25 19:45:45 +0800
committerOpportunityLiu <[email protected]>2019-07-25 19:46:54 +0800
commitb724d37e01b4a12d1b5eea3a9a866ba4b8c4e46d (patch)
treea8d30f410a7e99824923633c956588bcc18498b2 /xmake/core/base/serialize.lua
parent68f96b225c4da758a87a90f5723025be7b2fafc1 (diff)
fix style
Diffstat (limited to 'xmake/core/base/serialize.lua')
-rw-r--r--xmake/core/base/serialize.lua30
1 files changed, 22 insertions, 8 deletions
diff --git a/xmake/core/base/serialize.lua b/xmake/core/base/serialize.lua
index 8b5b233b1..4a9392ebe 100644
--- a/xmake/core/base/serialize.lua
+++ b/xmake/core/base/serialize.lua
@@ -86,16 +86,12 @@ function serialize._maketable(object, opt, level)
-- make indent
local indent = ""
if opt.indent then
- indent = string.rep(" ", level)
+ indent = string.rep(opt.indent, level)
end
-- make head
- local headstr
- if opt.indent then
- headstr = (level > 0 and "\n" or "") .. indent .. "{\n"
- else
- headstr = "{"
- end
+ local headstr = opt.indent and "{\n" or "{"
+
-- make tail
local tailstr
if opt.indent then
@@ -107,7 +103,7 @@ function serialize._maketable(object, opt, level)
-- make body
local s = {}
if opt.indent then
- indent = indent .. " "
+ indent = string.rep(opt.indent, level + 1)
end
if isarr then
@@ -199,6 +195,24 @@ function serialize.save(object, opt)
opt = { strip = false, binary = false, indent = true }
end
+ if not opt.indent then
+ opt.indent = false
+ elseif type(opt.indent) == "boolean" then
+ opt.indent = " "
+ elseif type(opt.indent) == "number" then
+ if opt.indent < 0 then
+ opt.indent = false
+ else
+ opt.indent = string.rep(" ", opt.indent)
+ end
+ elseif type(opt.indent) == "string" then
+ if not opt.indent:match("^%s+$") then
+ return nil, "invalid opt.indent, only whitespaces are accepted"
+ end
+ else
+ return nil, "invalid opt.indent, should be boolean, number or string"
+ end
+
-- make string
local result, errors = serialize._make(object, opt, 0)