Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions thorlog/jsonschema/generateschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package main
import (
"encoding/json"
"fmt"
"maps"
"os"
"reflect"
"slices"
"strings"

"github.com/NextronSystems/jsonlog"
Expand All @@ -19,11 +21,17 @@ func makeObjectSchema() (mainEntry string, defs map[string]*jsonschema.Schema) {
var allLogObjects []*jsonschema.Schema
var logObjectTypes []any
var reflector jsonschema.Reflector
reflector.AllowAdditionalProperties = true
err := reflector.AddGoComments("github.com/NextronSystems/jsonlog/thorlog/v3", "../v3")
if err != nil {
panic(err)
}
defs = map[string]*jsonschema.Schema{}

// Sort the object type names to have a stable output
var objectTypeNames = slices.Collect(maps.Keys(thorlog.LogObjectTypes))
slices.Sort(objectTypeNames)

reflector.Mapper = func(r reflect.Type) *jsonschema.Schema {
if r.Kind() == reflect.Interface {
if r.Implements(objectType) {
Expand All @@ -32,7 +40,8 @@ func makeObjectSchema() (mainEntry string, defs map[string]*jsonschema.Schema) {
// we can filter for the types that implement the interface,
// and generate a oneOf schema for them.
var implementations = &jsonschema.Schema{}
for _, t := range thorlog.LogObjectTypes {
for _, typename := range objectTypeNames {
t := thorlog.LogObjectTypes[typename]
if reflect.TypeOf(t).Implements(r) {
structName := reflect.TypeOf(t).Elem().Name()
implementations.OneOf = append(implementations.OneOf, &jsonschema.Schema{
Expand All @@ -52,8 +61,8 @@ func makeObjectSchema() (mainEntry string, defs map[string]*jsonschema.Schema) {
}
return nil
}
for typename, object := range thorlog.LogObjectTypes {
schema := reflector.Reflect(object)
for _, typename := range objectTypeNames {
schema := reflector.Reflect(thorlog.LogObjectTypes[typename])
refName := strings.TrimPrefix(schema.Ref, "#/$defs/")
typeSchema := schema.Definitions[refName]
typenameDef, ok := typeSchema.Properties.Get("type")
Expand All @@ -63,6 +72,7 @@ func makeObjectSchema() (mainEntry string, defs map[string]*jsonschema.Schema) {
typenameDef.Const = typename
allLogObjects = append(allLogObjects, schema)
logObjectTypes = append(logObjectTypes, typename)
defs[refName] = typeSchema
}
var logObjectSchema = &jsonschema.Schema{
Properties: orderedmap.New[string, *jsonschema.Schema](),
Expand Down
2 changes: 0 additions & 2 deletions thorlog/jsonschema/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
github.com/NextronSystems/jsonlog v0.0.0-20250522074152-0a205b123911 h1:jmmvwkhul9DtWWOBhyeyXV9LA4cQzqJ0UyniD3e/HZQ=
github.com/NextronSystems/jsonlog v0.0.0-20250522074152-0a205b123911/go.mod h1:Hk47VW018TX8o/0sxK+EJt16iRE7gB91zGZGiaAjcww=
github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPnH1Wvgk=
github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg=
github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs=
Expand Down