feat(strct-tags): add support for tag options (#126)

* feat(struct_tags): add options support

* refactor(struct-tags): give input field better name

* feat(struct-tag): add default option

* refactor: make it work on neovim version below 0.12

* chore(struct-tags): update the demo

* refactor: unite struct_tags util with main logic
This commit is contained in:
Oleksandr Smirnov 2025-10-30 12:25:42 +02:00
parent 0de1892ca9
commit 7a18d9f7bd
No known key found for this signature in database
19 changed files with 301 additions and 15 deletions

View file

@ -0,0 +1,8 @@
package main
type Test struct {
ID int
Another struct {
Second string
}
}

View file

@ -0,0 +1,8 @@
package main
type Test struct {
ID int `xml:"id,otheroption"`
Another struct {
Second string `xml:"second,otheroption"`
} `xml:"another,otheroption"`
}

View file

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

View file

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

View file

@ -0,0 +1,8 @@
package main
type Test struct {
ID int
Another struct {
Second string
}
}

View file

@ -0,0 +1,8 @@
package main
type Test struct {
ID int `xml:"id,theoption"`
Another struct {
Second string `xml:"second,theoption"`
} `xml:"another,theoption"`
}

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,52 @@ 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
struct_tags["should add tags with default option"] = function()
child.lua [[
require("gopher").setup {
gotag = { option = "xml=theoption" },
}
]]
local rs = t.setup_test("tags/with_default_option", child, { 3, 6 })
child.cmd "GoTagAdd xml"
child.cmd "write"
t.eq(t.readfile(rs.tmp), rs.fixtures.output)
t.cleanup(rs)
end
struct_tags["should add tags and overwrite default option"] = function()
child.lua [[
require("gopher").setup {
gotag = { option = "xml=theoption" },
}
]]
local rs = t.setup_test("tags/overwrite_default_option", child, { 3, 6 })
child.cmd "GoTagAdd xml=otheroption"
child.cmd "write"
t.eq(t.readfile(rs.tmp), rs.fixtures.output)
t.cleanup(rs)
end
struct_tags["should remove tag with specified option"] = function()
local rs = t.setup_test("tags/remove_with_option", child, { 3, 6 })
child.cmd "GoTagRm 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.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.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.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.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.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.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.struct_tags").parse_args { "json,yaml,json=omitempty" }
t.eq(out.tags, "json,yaml")
t.eq(out.options, "json=omitempty")
end
return T

View file

@ -46,4 +46,14 @@ utils["should add .indent() 2 tabs"] = function()
t.eq("\t\t", u.indent(line, indent))
end
utils["should .list_unique on list with duplicates"] = function()
local u = require "gopher._utils"
t.eq({ "json", "xml" }, u.list_unique { "json", "xml", "json" })
end
utils["should .list_unique on list with no duplicates"] = function()
local u = require "gopher._utils"
t.eq({ "json", "xml" }, u.list_unique { "json", "xml" })
end
return T