Compare commits

..

1 commit
v0.4.0 ... main

Author SHA1 Message Date
6a3924cee5
feat: json2go (#130)
* feat(json2go): implement

* feat(json2go): support manual input

* chore(readme): add json2go

* chore(docs): add docs
2025-12-08 21:29:34 +02:00
16 changed files with 312 additions and 1 deletions

View file

@ -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)
</details>
<details>
@ -185,6 +186,24 @@ Requirements:
```
</details>
<details>
<summary>
<b>Convert json to Go types</b>
</summary>
![Convert JSON to Go types](./vhs/json2go.gif)
`: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.
</details>
<details>
<summary>
@ -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,
},
}
```

View file

@ -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*

View file

@ -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, {

View file

@ -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

137
lua/gopher/json2go.lua Normal file
View file

@ -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

View file

@ -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, "*")

View file

@ -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",

View file

@ -0,0 +1,2 @@
package main

View file

@ -0,0 +1,5 @@
package main
type AutoGenerated struct {
Json bool `json:"json"`
}

2
spec/fixtures/json2go/manual_input.go vendored Normal file
View file

@ -0,0 +1,2 @@
package main

View file

@ -0,0 +1,7 @@
package main
type AutoGenerated struct {
User struct {
Name string `json:"name"`
} `json:"user"`
}

View file

@ -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 "<Esc>"
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

View file

@ -18,3 +18,6 @@ tasks:
impl:
cmd: vhs impl.tape
json2go:
cmd: vhs json2go.tape

BIN
vhs/json2go.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 282 KiB

2
vhs/json2go.go Normal file
View file

@ -0,0 +1,2 @@
package main

27
vhs/json2go.tape Normal file
View file

@ -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