41 lines
780 B
Go
41 lines
780 B
Go
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, "", " ")
|
|
}
|