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, "", " ")
|
||||
}
|
@ -19,7 +19,7 @@ import (
|
||||
// are up to the caller.
|
||||
func MustLoadConfigInto[T any](ctx context.Context, into T) (context.Context, T) {
|
||||
// Step 1: Check our custom type for required *config.AppConfig
|
||||
if err := hasAppConfig(into); err != nil {
|
||||
if err := HasAppConfig(into); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
@ -66,7 +66,7 @@ func setAppConfig[T any](target T, appConfig *config.AppConfig) error {
|
||||
}
|
||||
|
||||
// Replace *config.AppConfig
|
||||
for i := 0; i < v.NumField(); i++ {
|
||||
for i := range v.NumField() {
|
||||
field := v.Field(i)
|
||||
if field.Type() == reflect.TypeOf((*config.AppConfig)(nil)) {
|
||||
if !field.CanSet() {
|
||||
@ -80,7 +80,7 @@ func setAppConfig[T any](target T, appConfig *config.AppConfig) error {
|
||||
return errors.New("no *config.AppConfig field found in target struct")
|
||||
}
|
||||
|
||||
func hasAppConfig[T any](target T) error {
|
||||
func HasAppConfig[T any](target T) error {
|
||||
v := reflect.ValueOf(target)
|
||||
if v.Kind() != reflect.Ptr || v.IsNil() {
|
||||
return errors.New("target must be a non-nil pointer to a struct")
|
||||
@ -92,7 +92,7 @@ func hasAppConfig[T any](target T) error {
|
||||
}
|
||||
|
||||
hasAppConfig := false
|
||||
for i := 0; i < v.NumField(); i++ {
|
||||
for i := range v.NumField() {
|
||||
field := v.Type().Field(i)
|
||||
if field.Type == reflect.TypeOf((*config.AppConfig)(nil)) {
|
||||
hasAppConfig = true
|
||||
|
Reference in New Issue
Block a user