feat(struct_tags): add options support

This commit is contained in:
Oleksandr Smirnov 2025-10-29 13:09:19 +02:00
parent 0de1892ca9
commit 92625cc6e3
No known key found for this signature in database
6 changed files with 158 additions and 14 deletions

11
spec/fixtures/tags/with_option_input.go vendored Normal file
View file

@ -0,0 +1,11 @@
package main
type Test struct {
ID int
Name string
Num int64
Another struct {
First int
Second string
}
}

View file

@ -0,0 +1,11 @@
package main
type Test struct {
ID int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Num int64 `json:"num,omitempty"`
Another struct {
First int `json:"first,omitempty"`
Second string `json:"second,omitempty"`
} `json:"another,omitempty"`
}

View file

@ -96,4 +96,13 @@ struct_tags["should remove tag with range"] = function()
t.cleanup(rs)
end
struct_tags["should add tags with option"] = function()
local rs = t.setup_test("tags/with_option", child, { 3, 6 })
child.cmd "GoTagAdd json=omitempty"
child.cmd "write"
t.eq(t.readfile(rs.tmp), rs.fixtures.output)
t.cleanup(rs)
end
return T

View file

@ -0,0 +1,68 @@
local t = require "spec.testutils"
local _, T, st = t.setup "struct_tags"
st["should parse tags"] = function()
local out = require("gopher._utils.struct_tags").parse_args { "json", "yaml", "etc" }
t.eq(out.tags, "json,yaml,etc")
t.eq(out.options, "")
end
st["should parse tags separated by commas"] = function()
local out = require("gopher._utils.struct_tags").parse_args { "json,yaml,etc" }
t.eq(out.tags, "json,yaml,etc")
t.eq(out.options, "")
end
st["should parse tags separated by command and spaces"] = function()
local out = require("gopher.struct_tags").parse_args {
"json,yaml",
"json=omitempty",
"xml=something",
}
t.eq(out.tags, "json,yaml,xml")
t.eq(out.options, "json=omitempty,xml=something")
end
st["should parse tag with an option"] = function()
local out = require("gopher._utils.struct_tags").parse_args {
"json=omitempty",
"xml",
"xml=theoption",
}
t.eq(out.tags, "json,xml")
t.eq(out.options, "json=omitempty,xml=theoption")
end
st["should parse tags with an option"] = function()
local out = require("gopher._utils.struct_tags").parse_args { "json=omitempty", "yaml" }
t.eq(out.tags, "json,yaml")
t.eq(out.options, "json=omitempty")
end
st["should parse tags with an option separated with comma"] = function()
local out = require("gopher._utils.struct_tags").parse_args { "json=omitempty,yaml" }
t.eq(out.tags, "json,yaml")
t.eq(out.options, "json=omitempty")
end
st["should parse tags with options specified separately"] = function()
local out = require("gopher._utils.struct_tags").parse_args { "json", "yaml", "json=omitempty" }
t.eq(out.tags, "json,yaml")
t.eq(out.options, "json=omitempty")
end
st["should parse tags with options specified separately and separated by comma"] = function()
local out = require("gopher._utils.struct_tags").parse_args { "json,yaml,json=omitempty" }
t.eq(out.tags, "json,yaml")
t.eq(out.options, "json=omitempty")
end
return T