support json schema
This commit is contained in:
40
pkg/app/schema.go
Normal file
40
pkg/app/schema.go
Normal 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, "", " ")
|
||||
}
|
Reference in New Issue
Block a user