schema generator working

This commit is contained in:
2022-06-21 22:17:16 -04:00
parent 9bf1eeb379
commit 0c878468f5
3 changed files with 136 additions and 125 deletions

View File

@ -2,54 +2,60 @@ package main
import (
"encoding/json"
"fmt"
"os"
"github.com/invopop/jsonschema"
"go.balki.me/tss/app"
"gopkg.in/yaml.v3"
)
func fix(s string) string {
// s := "go.balki.me/tss/home/balki/projects/tss/app.Config.Proxy"
return fmt.Sprintf("%s%s", s[:16], s[40:])
}
var refsDoc = `Placeholder to put any yaml anchors to use elsewhere, E.g.
refs:
myvpsproxy: &myvpsproxy "socks5://vps.example.com:1080"
twiceaday: &twiceaday "00 1,13 * * *"`
type config struct {
app.Config
// Placeholder to put any yaml anchors to use elsewhere, E.g.
// refs:
// myvpsproxy: &myvpsproxy "socks5://vps.example.com:1080"
// twiceaday: &twiceaday "00 1,13 * * *"
Refs map[string]any `yaml:"refs" jsonschema:"refs"`
}
func main() {
// "go.balki.me/tss/app.FeedCfg.Channel": "Telegram channel name in the form \"@foobar\" or \"-1004242442\"",
//s go.balki.me/tss/app.FeedCfg.Channel
// foo()
// return
fmt.Println("vim-go")
cmmap := map[string]string{}
err := jsonschema.ExtractGoComments("go.balki.me/tss", "/home/balki/projects/tss", cmmap)
cmmapF := map[string]string{}
for k, v := range cmmap {
cmmapF[fix(k)] = v
}
cmmap = cmmapF
fmt.Println("err", err)
d, err := json.Marshal(cmmap)
fmt.Printf("%s\n", string(d))
os.WriteFile("/tmp/cmmap.json", d, 0644)
var c config
r := jsonschema.Reflector{
RequiredFromJSONSchemaTags: true,
DoNotReference: true,
ExpandedStruct: true,
CommentMap: cmmap,
}
s := r.Reflect(c)
j, _ := json.Marshal(s)
os.WriteFile("/home/balki/projects/tss/docs/schema.json", j, 0644)
fmt.Println(string(j))
err := r.AddGoComments("go.balki.me/tss", ".")
if err != nil {
panic(err)
}
r.CommentMap["main.config.Refs"] = refsDoc
s := r.Reflect(config{})
j, err := json.Marshal(s)
if err != nil {
panic(err)
}
err = os.WriteFile("./docs/schema.json", j, 0644)
if err != nil {
panic(err)
}
var a any
err = yaml.Unmarshal(j, &a)
if err != nil {
panic(err)
}
y, err := yaml.Marshal(a)
if err != nil {
panic(err)
}
err = os.WriteFile("./docs/schema.yaml", y, 0644)
if err != nil {
panic(err)
}
}