package catalog import ( "reflect" "strings" "gopkg.in/yaml.v3" ) // SchemaKinds maps a schema file name to the manifest it describes. var SchemaKinds = map[string]struct { Kind string Spec any }{ "catalog": {KindCatalog, CatalogSpec{}}, "capabilities": {KindCapabilities, CapabilitiesSpec{}}, "package": {KindPackage, PackageSpec{}}, "profile": {KindProfile, ProfileSpec{}}, "solution": {KindSolution, SolutionSpec{}}, "example": {KindExample, ExampleSpec{}}, "knowledge-source": {KindKnowledgeSource, KnowledgeSourceSpec{}}, } // JSONSchema returns the JSON Schema (draft 2020-12) of a manifest kind, // derived from the Go types the loader decodes with KnownFields -- so the // editor's check and the loader cannot disagree (CAT-05). Several // documents per file ("---") are not expressible in JSON Schema; the // schema describes one document. Solution and Task share a schema. func JSONSchema(name string) map[string]any { k := SchemaKinds[name] kinds := []any{k.Kind} if k.Kind == KindSolution { kinds = append(kinds, KindTask) } return map[string]any{ "$schema": "https://json-schema.org/draft/2020-12/schema", "$id": "https://yetano.app/schema/solutions/" + name + ".schema.json", "title": "Yetano " + k.Kind + " manifest", "type": "object", "additionalProperties": false, "required": []any{"apiVersion", "kind", "metadata"}, "properties": map[string]any{ "apiVersion": map[string]any{"const": APIVersion}, "kind": map[string]any{"enum": kinds}, "metadata": schemaOf(reflect.TypeOf(Meta{}), 0), "spec": schemaOf(reflect.TypeOf(k.Spec), 0), }, } } var ( nodeType = reflect.TypeOf(yaml.Node{}) condType = reflect.TypeOf(Cond{}) ) func schemaOf(t reflect.Type, depth int) map[string]any { if depth > 12 { return map[string]any{} } if t.Kind() == reflect.Pointer { t = t.Elem() } switch { case t == nodeType: return map[string]any{"description": "native configuration (any YAML)"} case t == condType: return map[string]any{"type": "object", "description": "condition over earlier answers (ADR-0003)"} case t == reflect.TypeOf(Support("")): return map[string]any{"enum": []any{"supported", "experimental", "untested", "discouraged", "needs-prerequisites", "not-implemented", "not-supported", "impossible", "insecure"}} case t == reflect.TypeOf(Level("")): return map[string]any{"enum": []any{"quick", "guided", "advanced"}} } switch t.Kind() { case reflect.String: return map[string]any{"type": "string"} case reflect.Bool: return map[string]any{"type": "boolean"} case reflect.Int, reflect.Int64: return map[string]any{"type": "integer"} case reflect.Float64: return map[string]any{"type": "number"} case reflect.Slice: return map[string]any{"type": "array", "items": schemaOf(t.Elem(), depth+1)} case reflect.Map: return map[string]any{"type": "object", "additionalProperties": schemaOf(t.Elem(), depth+1)} case reflect.Interface: return map[string]any{} case reflect.Struct: props := map[string]any{} addFields(t, props, depth) return map[string]any{"type": "object", "additionalProperties": false, "properties": props} } return map[string]any{} } func addFields(t reflect.Type, props map[string]any, depth int) { for i := 0; i < t.NumField(); i++ { f := t.Field(i) if !f.IsExported() { continue } tag := f.Tag.Get("yaml") name, opts, _ := strings.Cut(tag, ",") if name == "-" { continue } if strings.Contains(opts, "inline") { addFields(f.Type, props, depth) continue } if name == "" { name = strings.ToLower(f.Name) } props[name] = schemaOf(f.Type, depth+1) } }