diff --git a/README.md b/README.md
index a541f85..44fcfb7 100644
--- a/README.md
+++ b/README.md
@@ -62,6 +62,7 @@ Requirements:
- [impl](https://github.com/josharian/impl)
- [gotests](https://github.com/cweill/gotests)
- [iferr](https://github.com/koron/iferr)
+ - [json2go](https://github.com/olexsmir/json2go)
@@ -185,6 +186,24 @@ Requirements:
```
+
+
+ Convert json to Go types
+
+
+ 
+
+ `:GoJson` opens a temporary buffer where you can paste or write JSON.
+ Saving the buffer (`:w` or `:wq`) automatically closes it and inserts the generated Go struct into the original buffer at the cursor position.
+
+ Alternatively, you can pass JSON directly as an argument:
+ ```vim
+ :GoJson {"name": "Alice", "age": 30}
+ ```
+
+ Additionally, `gopher.json2go` provides lua api, see `:h gopher.nvim-json2go` for details.
+
+
@@ -253,6 +272,15 @@ require("gopher").setup {
-- e.g: `message = 'fmt.Errorf("failed to %w", err)'`
message = nil,
},
+ json2go = {
+ -- command used to open interactive input.
+ -- e.g: `split`, `botright split`, `tabnew`
+ interactive_cmd = "vsplit",
+
+ -- name of autogenerated struct
+ -- e.g: "MySuperCoolName"
+ type_name = nil,
+ },
}
```
diff --git a/doc/gopher.nvim.txt b/doc/gopher.nvim.txt
index 9995dd3..9232031 100644
--- a/doc/gopher.nvim.txt
+++ b/doc/gopher.nvim.txt
@@ -13,6 +13,7 @@ Table of Contents
Config ................................................ |gopher.nvim-config|
Commands ............................................ |gopher.nvim-commands|
Modify struct tags ............................... |gopher.nvim-struct-tags|
+ json2go .............................................. |gopher.nvim-json2go|
Auto implementation of interface methods ................ |gopher.nvim-impl|
Generating unit tests boilerplate .................... |gopher.nvim-gotests|
Iferr .................................................. |gopher.nvim-iferr|
@@ -69,6 +70,7 @@ or use `require("gopher").install_deps()` if you prefer lua api.
gotests = "gotests",
impl = "impl",
iferr = "iferr",
+ json2go = "json2go",
},
---@class gopher.ConfigGotests
gotests = {
@@ -96,12 +98,24 @@ or use `require("gopher").install_deps()` if you prefer lua api.
---@type string|nil
option = nil,
},
+ ---@class gopher.ConfigIfErr
iferr = {
-- choose a custom error message, nil to use default
-- e.g: `message = 'fmt.Errorf("failed to %w", err)'`
---@type string|nil
message = nil,
},
+ ---@class gopher.ConfigJson2Go
+ json2go = {
+ -- command used to open interactive input.
+ -- e.g: `split`, `botright split`, `tabnew`
+ interactive_cmd = "vsplit",
+
+ -- name of autogenerated struct, if nil none, will the default one of json2go.
+ -- e.g: "MySuperCoolName"
+ ---@type string|nil
+ type_name = nil,
+ },
}
<
Class ~
@@ -157,6 +171,42 @@ Example:
}
<
+==============================================================================
+------------------------------------------------------------------------------
+ *gopher.nvim-json2go*
+
+Convert json to go type annotations.
+
+Usage ~
+
+`:GoJson` opens a temporary buffer where you can paste or write JSON.
+Saving the buffer (`:w` or `:wq`) automatically closes it and inserts the
+generated Go struct into the original buffer at the cursor position.
+
+Alternatively, you can pass JSON directly as an argument:
+>vim
+ :GoJson {"name": "Alice", "age": 30}
+<
+------------------------------------------------------------------------------
+ *json2go.transform()*
+ `json2go.transform`({json_str})
+
+Parameters ~
+{json_str} `(string)` Json string that is going to be converted to go type.
+Return ~
+`(string)` `(optional)` Go type, or nil if failed.
+
+------------------------------------------------------------------------------
+ *json2go.json2go()*
+ `json2go.json2go`({json_str})
+Converts json string to go type, and puts result under the cursor. If
+[json_str] is nil, will open an interactive prompt (with cmd set in
+config).
+
+Parameters ~
+{json_str} `(optional)` `(string)`
+
+
==============================================================================
------------------------------------------------------------------------------
*gopher.nvim-impl*
diff --git a/lua/gopher/config.lua b/lua/gopher/config.lua
index 6081ecc..4245989 100644
--- a/lua/gopher/config.lua
+++ b/lua/gopher/config.lua
@@ -41,6 +41,7 @@ local default_config = {
gotests = "gotests",
impl = "impl",
iferr = "iferr",
+ json2go = "json2go",
},
---@class gopher.ConfigGotests
gotests = {
@@ -68,12 +69,24 @@ local default_config = {
---@type string|nil
option = nil,
},
+ ---@class gopher.ConfigIfErr
iferr = {
-- choose a custom error message, nil to use default
-- e.g: `message = 'fmt.Errorf("failed to %w", err)'`
---@type string|nil
message = nil,
},
+ ---@class gopher.ConfigJson2Go
+ json2go = {
+ -- command used to open interactive input.
+ -- e.g: `split`, `botright split`, `tabnew`
+ interactive_cmd = "vsplit",
+
+ -- name of autogenerated struct, if nil none, will the default one of json2go.
+ -- e.g: "MySuperCoolName"
+ ---@type string|nil
+ type_name = nil,
+ },
}
--minidoc_afterlines_end
@@ -104,6 +117,7 @@ function config.setup(user_config)
vim.validate("commands.gotests", _config.commands.gotests, "string")
vim.validate("commands.impl", _config.commands.impl, "string")
vim.validate("commands.iferr", _config.commands.iferr, "string")
+ vim.validate("commands.json2go", _config.commands.json2go, "string")
vim.validate("gotests", _config.gotests, "table")
vim.validate("gotests.template", _config.gotests.template, "string")
vim.validate("gotests.template_dir", _config.gotests.template_dir, { "string", "nil" })
@@ -114,6 +128,8 @@ function config.setup(user_config)
vim.validate("gotag.option", _config.gotag.option, { "string", "nil" })
vim.validate("iferr", _config.iferr, "table")
vim.validate("iferr.message", _config.iferr.message, { "string", "nil" })
+ vim.validate("json2go.installer_timeout", _config.json2go.interactive_cmd, "string")
+ vim.validate("json2go.type_name", _config.json2go.type_name, { "string", "nil" })
end
setmetatable(config, {
diff --git a/lua/gopher/installer.lua b/lua/gopher/installer.lua
index 680096a..61e1788 100644
--- a/lua/gopher/installer.lua
+++ b/lua/gopher/installer.lua
@@ -9,6 +9,7 @@ local urls = {
impl = "github.com/josharian/impl@latest",
gotests = "github.com/cweill/gotests/...@develop",
iferr = "github.com/koron/iferr@latest",
+ json2go = "olexsmir.xyz/json2go/cmd/json2go@latest",
}
---@param opt vim.SystemCompleted
diff --git a/lua/gopher/json2go.lua b/lua/gopher/json2go.lua
new file mode 100644
index 0000000..40d0e9f
--- /dev/null
+++ b/lua/gopher/json2go.lua
@@ -0,0 +1,137 @@
+---@toc_entry json2go
+---@tag gopher.nvim-json2go
+---@text
+--- Convert json to go type annotations.
+---
+---@usage
+--- `:GoJson` opens a temporary buffer where you can paste or write JSON.
+--- Saving the buffer (`:w` or `:wq`) automatically closes it and inserts the
+--- generated Go struct into the original buffer at the cursor position.
+---
+--- Alternatively, you can pass JSON directly as an argument:
+--- >vim
+--- :GoJson {"name": "Alice", "age": 30}
+--- <
+
+local c = require "gopher.config"
+local log = require "gopher._utils.log"
+local u = require "gopher._utils"
+local r = require "gopher._utils.runner"
+local json2go = {}
+
+---@dochide
+---@param bufnr integer
+---@param cpos integer
+---@param type_ string
+local function apply(bufnr, cpos, type_)
+ local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
+ local input_lines = u.remove_empty_lines(vim.split(type_, "\n"))
+ for i, line in pairs(input_lines) do
+ table.insert(lines, cpos + i, line)
+ end
+ vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, lines)
+end
+
+-- Convert json string to go type.
+---
+---@param json_str string Json string that is going to be converted to go type.
+---@return string? Go type, or nil if failed.
+function json2go.transform(json_str)
+ local cmd = { c.commands.json2go }
+ if c.json2go.type_name then
+ table.insert(cmd, "-type", c.json2go.type_name)
+ end
+
+ local rs = r.sync(cmd, { stdin = json_str })
+ if rs.code ~= 0 then
+ u.notify("json2go: got this error: " .. rs.stdout, vim.log.levels.ERROR)
+ log.error("json2go: got this error: " .. rs.stdout)
+ return
+ end
+ return rs.stdout
+end
+
+---@dochide
+---@param ocpos integer
+local function interactive(ocpos)
+ local obuf = vim.api.nvim_get_current_buf()
+ local owin = vim.api.nvim_get_current_win()
+
+ -- setup the input window
+ local buf = vim.api.nvim_create_buf(false, true)
+ vim.cmd(c.json2go.interactive_cmd)
+
+ local win = vim.api.nvim_get_current_win()
+ vim.api.nvim_win_set_buf(win, buf)
+ vim.api.nvim_buf_set_name(buf, "[GoJson input]")
+ vim.api.nvim_set_option_value("filetype", "jsonc", { buf = buf })
+ vim.api.nvim_set_option_value("buftype", "acwrite", { buf = buf })
+ vim.api.nvim_set_option_value("swapfile", false, { buf = buf })
+ vim.api.nvim_set_option_value("bufhidden", "delete", { buf = buf })
+ vim.api.nvim_buf_set_lines(buf, 0, -1, false, {
+ "// Write your json here.",
+ "// Writing and quitting (:wq), will generate go struct under the cursor.",
+ "",
+ "",
+ })
+
+ vim.api.nvim_create_autocmd("BufLeave", { buffer = buf, command = "stopinsert" })
+ vim.api.nvim_create_autocmd("BufWriteCmd", {
+ buffer = buf,
+ once = true,
+ callback = function()
+ local input = vim.api.nvim_buf_get_lines(buf, 0, -1, true)
+ local inp = table.concat(
+ vim
+ .iter(input)
+ :filter(function(line)
+ local found = string.find(line, "^//.*")
+ return (not found) or (line == "")
+ end)
+ :totable(),
+ "\n"
+ )
+
+ local go_type = json2go.transform(inp)
+ if not go_type then
+ error "cound't convert json to go type"
+ end
+
+ vim.api.nvim_set_option_value("modified", false, { buf = buf })
+ apply(obuf, ocpos, go_type)
+
+ vim.api.nvim_set_current_win(owin)
+ vim.api.nvim_win_set_cursor(owin, { ocpos + 1, 0 })
+
+ vim.schedule(function()
+ pcall(vim.api.nvim_win_close, win, true)
+ pcall(vim.api.nvim_buf_delete, buf, {})
+ end)
+ end,
+ })
+
+ vim.cmd "normal! G"
+ vim.cmd "startinsert"
+end
+
+--- Converts json string to go type, and puts result under the cursor. If
+--- [json_str] is nil, will open an interactive prompt (with cmd set in
+--- config).
+---
+---@param json_str? string
+function json2go.json2go(json_str)
+ local cur_line = vim.api.nvim_win_get_cursor(0)[1]
+ if not json_str then
+ interactive(cur_line)
+ return
+ end
+
+ local go_type = json2go.transform(json_str)
+ if not go_type then
+ error "cound't convert json to go type"
+ end
+
+ apply(0, cur_line, go_type)
+end
+
+return json2go
diff --git a/plugin/gopher.lua b/plugin/gopher.lua
index b2ccc1f..5b071b0 100644
--- a/plugin/gopher.lua
+++ b/plugin/gopher.lua
@@ -70,6 +70,12 @@ cmd("GoTagClear", function()
require("gopher").tags.clear()
end)
+-- :GoJson
+cmd("GoJson", function(opts)
+ local inp = ((opts.args ~= "" and opts.args) or nil)
+ require("gopher.json2go").json2go(inp)
+end, "*")
+
-- :GoTest
cmd("GoTestAdd", function()
require("gopher").test.add()
@@ -89,7 +95,6 @@ cmd("GoMod", function(opts)
end, "*")
cmd("GoGet", function(opts)
- vim.print(opts)
require("gopher").get(opts.fargs)
end, "*")
diff --git a/scripts/docgen.lua b/scripts/docgen.lua
index 1c66b90..48d906b 100644
--- a/scripts/docgen.lua
+++ b/scripts/docgen.lua
@@ -12,6 +12,7 @@ local files = {
"lua/gopher/config.lua",
"plugin/gopher.lua",
"lua/gopher/struct_tags.lua",
+ "lua/gopher/json2go.lua",
"lua/gopher/impl.lua",
"lua/gopher/gotests.lua",
"lua/gopher/iferr.lua",
diff --git a/spec/fixtures/json2go/interativly_input.go b/spec/fixtures/json2go/interativly_input.go
new file mode 100644
index 0000000..c9ecbf5
--- /dev/null
+++ b/spec/fixtures/json2go/interativly_input.go
@@ -0,0 +1,2 @@
+package main
+
diff --git a/spec/fixtures/json2go/interativly_output.go b/spec/fixtures/json2go/interativly_output.go
new file mode 100644
index 0000000..3314555
--- /dev/null
+++ b/spec/fixtures/json2go/interativly_output.go
@@ -0,0 +1,5 @@
+package main
+
+type AutoGenerated struct {
+ Json bool `json:"json"`
+}
diff --git a/spec/fixtures/json2go/manual_input.go b/spec/fixtures/json2go/manual_input.go
new file mode 100644
index 0000000..c9ecbf5
--- /dev/null
+++ b/spec/fixtures/json2go/manual_input.go
@@ -0,0 +1,2 @@
+package main
+
diff --git a/spec/fixtures/json2go/manual_output.go b/spec/fixtures/json2go/manual_output.go
new file mode 100644
index 0000000..bd5ac1c
--- /dev/null
+++ b/spec/fixtures/json2go/manual_output.go
@@ -0,0 +1,7 @@
+package main
+
+type AutoGenerated struct {
+ User struct {
+ Name string `json:"name"`
+ } `json:"user"`
+}
diff --git a/spec/integration/json2go_test.lua b/spec/integration/json2go_test.lua
new file mode 100644
index 0000000..83b39ba
--- /dev/null
+++ b/spec/integration/json2go_test.lua
@@ -0,0 +1,25 @@
+local t = require "spec.testutils"
+local child, T, json2go = t.setup "json2go"
+
+json2go["should convert interativly"] = function()
+ local rs = t.setup_test("json2go/interativly", child, { 2, 0 })
+ child.cmd "GoJson"
+ child.type_keys [[{"json": true}]]
+ child.type_keys ""
+ child.cmd "wq" -- quit prompt
+ child.cmd "write" -- the fixture file
+
+ t.eq(t.readfile(rs.tmp), rs.fixtures.output)
+ t.cleanup(rs)
+end
+
+json2go["should convert argument"] = function()
+ local rs = t.setup_test("json2go/manual", child, { 2, 0 })
+ child.cmd [[GoJson {"user": {"name": "user-ovic"}}]]
+ child.cmd "write"
+
+ t.eq(t.readfile(rs.tmp), rs.fixtures.output)
+ t.cleanup(rs)
+end
+
+return T
diff --git a/vhs/Taskfile.yml b/vhs/Taskfile.yml
index 2dfb107..b9cc588 100644
--- a/vhs/Taskfile.yml
+++ b/vhs/Taskfile.yml
@@ -18,3 +18,6 @@ tasks:
impl:
cmd: vhs impl.tape
+
+ json2go:
+ cmd: vhs json2go.tape
diff --git a/vhs/json2go.gif b/vhs/json2go.gif
new file mode 100644
index 0000000..908f7dc
Binary files /dev/null and b/vhs/json2go.gif differ
diff --git a/vhs/json2go.go b/vhs/json2go.go
new file mode 100644
index 0000000..c9ecbf5
--- /dev/null
+++ b/vhs/json2go.go
@@ -0,0 +1,2 @@
+package main
+
diff --git a/vhs/json2go.tape b/vhs/json2go.tape
new file mode 100644
index 0000000..309856e
--- /dev/null
+++ b/vhs/json2go.tape
@@ -0,0 +1,27 @@
+Output json2go.gif
+Require nvim
+Require json2go
+
+Set FontFamily "JetBrains Mono"
+Set Height 800
+Set Width 1500
+Set Padding 20
+Set Shell "bash"
+Set Theme "tokyonight"
+Set TypingSpeed 250ms
+
+Hide Type@0ms "./nvim.sh json2go.go" Enter Show
+
+Type@0ms "G"
+Type@400ms ":GoJson" Sleep 500ms Enter
+Type@70ms "{" Enter
+Type@70ms `"json": true,` Enter
+Type@70ms `"user": {"name": "Name", "of_age": true}` Enter
+Type@70ms "}"
+Escape Type@500ms ":wq" Enter
+Sleep 1s
+
+Type@25ms "G2o" Escape
+Type@120ms `:GoJson {"json": true}` Enter
+
+Sleep 5s