json to go struct

pull/196/head
ray-x 2 years ago
parent 5516cf7fff
commit 3f2e7f49bd

@ -521,6 +521,13 @@ Please use jsonls/null-ls check your launch.json is valid json file. Following s
Here is a sample [launch.json](https://github.com/ray-x/go.nvim/blob/master/playground/sampleApp/.vscode/launch.json)
### Json to Go struct
* ["x]GoJson2Struct!
Visual select the json and run `GoJson2Struct youStructName`
-bang will put result to register `a`
if ["x] specified, will put get json from clipboard
### Commands
| Command | Description |
@ -561,7 +568,7 @@ Sample vimrc for DAP
## Commands
Check [go.lua](https://github.com/ray-x/go.nvim/blob/master/lua/go.lua) on all the commands provided
Check [commands.lua](https://github.com/ray-x/go.nvim/blob/master/lua/go/commands.lua) on all the commands provided
## configuration

@ -323,6 +323,11 @@ COMMANDS *go-nvim-commands*
:GoTermClose
Closes the floating term.
:["x]GoJson2Struct
Convert json (visual select) to go struct.
bang: put result to register
\"x : get json from register x
==============================================================================
OPTIONS *go-nvim-options*

@ -348,10 +348,24 @@ return {
create_cmd('GoMockGen', require('go.mockgen').run, {
nargs = '*',
-- bang = true,
complete = function(ArgLead, CmdLine, CursorPos)
complete = function(_, _, _)
-- return completion candidates as a list-like table
return { '-p', '-d', '-i', '-s' }
end,
})
create_cmd('GoEnv', function(opts)
require('go.env').load_env(unpack(opts.fargs))
end, { nargs = '*' })
create_cmd('GoJson2Struct', function(opts) require('go.json2struct').run(opts) end, {
nargs = '*',
bang = true,
register = true,
-- complete = function(ArgLead, CmdLine, CursorPos)
complete = function(_, _, _)
return { 'myStruct'}
end,
range = true,
})
end,
}

@ -1,28 +1,29 @@
local uv = vim.loop
local DIR_SEP = package.config:sub(1, 1)
local utils = require("go.utils")
local utils = require('go.utils')
local log = utils.log
local url = {
gofumpt = "mvdan.cc/gofumpt",
golines = "github.com/segmentio/golines",
["golangci-lint"] = "github.com/golangci/golangci-lint/cmd/golangci-lint",
goimports = "golang.org/x/tools/cmd/goimports",
gorename = "golang.org/x/tools/cmd/gorename",
gomodifytags = "github.com/fatih/gomodifytags",
gopls = "golang.org/x/tools/gopls",
gotests = "github.com/cweill/gotests/...",
iferr = "github.com/koron/iferr",
callgraph = "golang.org/x/tools/cmd/callgraph",
guru = "golang.org/x/tools/cmd/guru",
impl = "github.com/josharian/impl",
fillstruct = "github.com/davidrjenni/reftools/cmd/fillstruct",
fillswitch = "github.com/davidrjenni/reftools/cmd/fillswitch",
dlv = "github.com/go-delve/delve/cmd/dlv",
ginkgo = "github.com/onsi/ginkgo/v2/ginkgo",
richgo = "github.com/kyoh86/richgo",
gotestsum = "gotest.tools/gotestsum",
mockgen = "github.com/golang/mock"
gofumpt = 'mvdan.cc/gofumpt',
golines = 'github.com/segmentio/golines',
['golangci-lint'] = 'github.com/golangci/golangci-lint/cmd/golangci-lint',
goimports = 'golang.org/x/tools/cmd/goimports',
gorename = 'golang.org/x/tools/cmd/gorename',
gomodifytags = 'github.com/fatih/gomodifytags',
gopls = 'golang.org/x/tools/gopls',
gotests = 'github.com/cweill/gotests/...',
iferr = 'github.com/koron/iferr',
callgraph = 'golang.org/x/tools/cmd/callgraph',
guru = 'golang.org/x/tools/cmd/guru',
impl = 'github.com/josharian/impl',
fillstruct = 'github.com/davidrjenni/reftools/cmd/fillstruct',
fillswitch = 'github.com/davidrjenni/reftools/cmd/fillswitch',
dlv = 'github.com/go-delve/delve/cmd/dlv',
ginkgo = 'github.com/onsi/ginkgo/v2/ginkgo',
richgo = 'github.com/kyoh86/richgo',
gotestsum = 'gotest.tools/gotestsum',
mockgen = 'github.com/golang/mock',
['json-to-struct'] = 'github.com/tmc/json-to-struct',
}
local tools = {}
@ -31,7 +32,7 @@ for tool, _ in pairs(url) do
end
local function is_installed(bin)
local env_path = os.getenv("PATH")
local env_path = os.getenv('PATH')
local sep = utils.sep2()
local ext = utils.ext()
local base_paths = vim.split(env_path, sep, true)
@ -40,7 +41,6 @@ local function is_installed(bin)
if uv.fs_stat(value .. DIR_SEP .. bin .. ext) then
return true
end
end
return false
end
@ -49,23 +49,23 @@ local function go_install(pkg)
local u = url[pkg]
if u == nil then
vim.notify(
"command " .. pkg .. " not supported, please update install.lua, or manually install it",
'command ' .. pkg .. ' not supported, please update install.lua, or manually install it',
vim.lsp.log_levels.WARN
)
return
end
u = u .. "@latest"
local setup = { "go", "install", u }
u = u .. '@latest'
local setup = { 'go', 'install', u }
vim.fn.jobstart(setup, {
on_stdout = function(_, data, _)
log(setup)
if type(data) == "table" and #data > 0 then
data = table.concat(data, " ")
if type(data) == 'table' and #data > 0 then
data = table.concat(data, ' ')
end
local msg = "install " .. u .. " finished"
local msg = 'install ' .. u .. ' finished'
if #data > 1 then
msg = msg .. data
end
@ -79,11 +79,11 @@ local function install(bin, verbose)
verbose = _GO_NVIM_CFG.verbose
end
if not is_installed(bin) then
vim.notify("installing " .. bin, vim.lsp.log_levels.INFO)
vim.notify('installing ' .. bin, vim.lsp.log_levels.INFO)
go_install(bin)
else
if verbose then
vim.notify(bin .. " already install, use GoUpdateBinary to update it", vim.lsp.log_levels.DEBUG)
vim.notify(bin .. ' already install, use GoUpdateBinary to update it', vim.lsp.log_levels.DEBUG)
end
end
return is_installed(bin)

@ -0,0 +1,58 @@
local runner = require('go.runner')
local utils = require('go.utils')
local log = utils.log
local uv, api = vim.loop, vim.api
local M = {}
-- visual select json text and run the command
function M.run(opts)
local args, bang = opts.args, opts.bang
local register = opts.register
local json
if register then
json = vim.fn.getreg(register)
else
local range = vim.lsp.util.make_given_range_params().range
log(range)
json = vim.api.nvim_buf_get_lines(0, range.start.line, range['end'].line + 1, true)
json[1] = json[1]:sub(range.start.character + 1)
json[#json] = json[#json]:sub(1, range['end'].character + 1)
end
if #json == 1 then
json = vim.split(json[1], '\n')
end
log(json)
local cmd = { 'json-to-struct', '-name', args[1] or 'myStruct' }
local opts = {
update_buffer = true,
on_exit = function(code, signal, output_buf)
log(code, signal, output_buf)
if code ~= 0 or signal ~= 0 then
vim.notify(
'error code' .. tostring(code) .. ' ' .. tostring(signal) .. vim.inspect(output_buf or ''),
vim.lsp.log_levels.WARN
)
end
local output = vim.split(output_buf, '\n')
if output[1] == 'package main' then
table.remove(output, 1)
end
vim.schedule(function()
if not bang then
api.nvim_buf_set_lines(0, range['end'].line + 1, range['end'].line + 1, false, output)
else
vim.fn.setreg('g', table.concat(output, '\n'))
vim.notify('Json To Struct JSON converted and placed in register "g"')
end
end)
end,
}
stdin, _, _ = runner.run(cmd, opts)
uv.write(stdin, json)
return cmd, opts
end
return M

@ -103,6 +103,7 @@ local run = function(cmd, opts)
end)
stdout:read_start(update_chunk)
-- stderr:read_start(update_chunk)
return stdin, stdout, stderr
end
local function make(...)

Loading…
Cancel
Save