Twelve definitions each advertised a bare {"type":"object"} or {"type":"array"},
so no resource descriptor carried a single field: the Web UI had nothing to
label, describe or format a field with, and the one component that parsed a
schema was dead code. The constants could not be kept honest either -- a schema
written beside a struct drifts the moment a field is added, and nothing compares
the two.
Definition now names its document type and Descriptor derives the schema from it.
json tags give the keys, falling back to the Go field name because that is what
encoding/json stores; the Go type gives type and format; and a field's own
title, format and doc tags override. A duration is a string with format
"duration", which is how every duration field in the catalogue -- poll_interval,
max_uptime, rate_limit.window -- gets a client-side affordance from one rule
instead of twelve hand-written ones.
Deliberately structural: it emits no prose of its own. Field descriptions already
have one home (internal/webui/config_schema.go, keyed by dotted config path), and
a second would be two places to keep the same fact true. It also never emits
"required", because an empty string is legitimate for nearly every field here --
a blank workspace, a disabled rate limit -- so requiring most of them would
invite a client to enforce something the document does not mean.
Guarded by TestEveryResourceDescriptorDerivesProperties, which walks the catalogue
the production constructor builds and fails if any kind describes no fields --
the assertion that would have caught all twelve. It accepts a keyed collection's
additionalProperties as described, because a map document has no fixed keys.
Verified: controlplane, workflow, webui, archieui and staterpc suites, go vet,
go build ./..., golangci-lint run ./... (0 issues), docs:check.
150 lines
5.2 KiB
Go
150 lines
5.2 KiB
Go
package controlplane
|
|
|
|
import (
|
|
"encoding/json"
|
|
"reflect"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/samcharles93/archie-core/internal/config"
|
|
)
|
|
|
|
// bareObjectSchema is what a descriptor advertises when its definition names no
|
|
// document. It is the fallback, not the norm: a descriptor with no properties is
|
|
// what left the Web UI with nothing to label, describe or format a field with
|
|
// (archie-core-2xs6).
|
|
const bareObjectSchema = `{"type":"object"}`
|
|
|
|
var (
|
|
configDurationType = reflect.TypeOf(config.Duration(0))
|
|
channelDurationType = reflect.TypeOf(channelDuration(0))
|
|
timeType = reflect.TypeOf(time.Time{})
|
|
)
|
|
|
|
// schemaJSON renders the JSON Schema a resource descriptor advertises, derived
|
|
// from the document type the definition owns rather than written out beside it.
|
|
//
|
|
// Derivation is the point. A hand-written schema beside a struct drifts the
|
|
// moment a field is added, and nothing catches it, because the document and its
|
|
// schema are never compared -- which is exactly how twelve descriptors came to
|
|
// advertise `{"type":"object"}` and no field metadata at all.
|
|
//
|
|
// It describes structure only: keys, types, and the `format`, `title` and `doc`
|
|
// annotations a field declares in its own tags. It deliberately emits no prose
|
|
// of its own. Field descriptions already have one home in this tree
|
|
// (internal/webui/config_schema.go, keyed by dotted config path), and a second
|
|
// would be two places to keep the same fact true.
|
|
//
|
|
// `required` is never emitted. An empty string is a legitimate value for nearly
|
|
// every field here -- a blank workspace, a disabled rate limit -- so calling most
|
|
// fields required would invite a client to enforce something the document does
|
|
// not mean.
|
|
func schemaJSON(document any) string {
|
|
if document == nil {
|
|
return bareObjectSchema
|
|
}
|
|
encoded, err := json.Marshal(schemaOf(reflect.TypeOf(document)))
|
|
if err != nil {
|
|
// Unreachable: the tree below holds only strings, maps, slices and
|
|
// booleans. A boot must not fail over documentation, so degrade to the
|
|
// shape descriptors carried before this existed rather than panic.
|
|
return bareObjectSchema
|
|
}
|
|
return string(encoded)
|
|
}
|
|
|
|
// schemaOf is the JSON Schema for one type, following json tags for keys and the
|
|
// Go type for everything else.
|
|
func schemaOf(t reflect.Type) map[string]any {
|
|
for t.Kind() == reflect.Pointer {
|
|
t = t.Elem()
|
|
}
|
|
switch t {
|
|
case configDurationType, channelDurationType:
|
|
// Both marshal as the string time.ParseDuration reads, so the schema
|
|
// says so and a client can offer a duration affordance instead of a
|
|
// number box. This is the rule that reaches every duration field in the
|
|
// catalogue, not just the one that prompted it.
|
|
return map[string]any{"type": "string", "format": "duration"}
|
|
case timeType:
|
|
return map[string]any{"type": "string", "format": "date-time"}
|
|
}
|
|
switch t.Kind() {
|
|
case reflect.Struct:
|
|
return objectSchemaOf(t)
|
|
case reflect.Slice, reflect.Array:
|
|
return map[string]any{"type": "array", "items": schemaOf(t.Elem())}
|
|
case reflect.Map:
|
|
return map[string]any{"type": "object", "additionalProperties": schemaOf(t.Elem())}
|
|
case reflect.Bool:
|
|
return map[string]any{"type": "boolean"}
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
|
|
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
|
return map[string]any{"type": "integer"}
|
|
case reflect.Float32, reflect.Float64:
|
|
return map[string]any{"type": "number"}
|
|
default:
|
|
return map[string]any{"type": "string"}
|
|
}
|
|
}
|
|
|
|
func objectSchemaOf(t reflect.Type) map[string]any {
|
|
properties := map[string]any{}
|
|
for i := range t.NumField() {
|
|
field := t.Field(i)
|
|
if field.Anonymous {
|
|
// encoding/json promotes an embedded struct's fields into the
|
|
// enclosing object, so the schema lists them there too. Dropping
|
|
// them would leave document keys nothing describes.
|
|
embedded, ok := schemaOf(field.Type)["properties"].(map[string]any)
|
|
if !ok {
|
|
continue
|
|
}
|
|
for name, property := range embedded {
|
|
properties[name] = property
|
|
}
|
|
continue
|
|
}
|
|
if field.PkgPath != "" {
|
|
continue // unexported: not part of any document
|
|
}
|
|
name, ok := documentKey(field)
|
|
if !ok {
|
|
continue
|
|
}
|
|
properties[name] = documented(schemaOf(field.Type), field)
|
|
}
|
|
return map[string]any{"type": "object", "properties": properties}
|
|
}
|
|
|
|
// documentKey is the key a field carries in its document: the json tag's name,
|
|
// or the Go field name when the tag declares none, because that fallback is what
|
|
// encoding/json uses and therefore what the document really holds. Reporting the
|
|
// tag instead of the fallback would describe a document nobody stores.
|
|
func documentKey(field reflect.StructField) (string, bool) {
|
|
name, _, _ := strings.Cut(field.Tag.Get("json"), ",")
|
|
switch name {
|
|
case "-":
|
|
return "", false
|
|
case "":
|
|
return field.Name, true
|
|
default:
|
|
return name, true
|
|
}
|
|
}
|
|
|
|
// documented layers the three annotations a field declares in its own tags over
|
|
// the derived type.
|
|
func documented(schema map[string]any, field reflect.StructField) map[string]any {
|
|
for _, annotation := range []struct{ tag, key string }{
|
|
{"title", "title"},
|
|
{"format", "format"},
|
|
{"doc", "description"},
|
|
} {
|
|
if value := field.Tag.Get(annotation.tag); value != "" {
|
|
schema[annotation.key] = value
|
|
}
|
|
}
|
|
return schema
|
|
}
|