GitRoot

craft your forge, build your project, grow your community freely
 1// SPDX-FileCopyrightText: 2025 Romain Maneschi <romain@gitroot.dev>
 2//
 3// SPDX-License-Identifier: MIT
 4
 5package model
 6
 7import (
 8	"github.com/tidwall/gjson"
 9)
10
11type Call struct {
12	Plugin string            `json:"plugin"`
13	Name   string            `json:"name"`
14	Args   map[string]string `json:"args"`
15}
16
17func CallFromString(call string) Call {
18	jsonCall := gjson.Parse(call)
19	args := map[string]string{}
20	for key, value := range jsonCall.Get("args").Map() {
21		args[key] = value.String()
22	}
23	res := Call{
24		Plugin: jsonCall.Get("plugin").String(),
25		Name:   jsonCall.Get("name").String(),
26		Args:   args,
27	}
28	return res
29}
30
31type CallRes struct {
32	Res map[string]string `json:"res"`
33	Err string            `json:"err"`
34}
35
36func CallResFromString(res string) CallRes {
37	jsonRes := gjson.Parse(res)
38	ret := map[string]string{}
39	for key, value := range jsonRes.Get("res").Map() {
40		ret[key] = value.String()
41	}
42	return CallRes{
43		Res: ret,
44		Err: jsonRes.Get("err").String(),
45	}
46}