support json schema

This commit is contained in:
2025-03-22 14:02:39 -04:00
parent f0a699029a
commit 3e319b24fd
10 changed files with 82 additions and 36 deletions

40
pkg/app/schema.go Normal file
View File

@ -0,0 +1,40 @@
package app
import (
"encoding/json"
js "github.com/swaggest/jsonschema-go"
)
// Generates json schema for app's config.AppConfig
func (app *App) Schema() ([]byte, error) {
r := js.Reflector{}
s, err := r.Reflect(*app.cfg)
if err != nil {
return nil, err
}
return json.MarshalIndent(s, "", " ")
}
// Generates json schema for custom config
// which embeds *config.AppConfig into it
// Panics if no *config.AppConfig is embedded into custom
// config type
//
// See swaggest/jsonschema-go for struct tag docs
func CustomSchema[T any](target T) ([]byte, error) {
if err := HasAppConfig(target); err != nil {
panic(err.Error())
}
r := js.Reflector{}
s, err := r.Reflect(target)
if err != nil {
return nil, err
}
return json.MarshalIndent(s, "", " ")
}