Something went wrong. Try again.
Work with Cumulus NVUE Config schemas
Something went wrong. Try again.
nvueschema schema.go
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182package nvueschema
import ( "fmt" "maps" "reflect" "slices" "strings")
// TypeSegment is a piece of a type annotation, either a type name or a literal value.type TypeSegment struct { Text string Literal bool // true for enum/literal values, false for type names}
// FlattenComposite merges NVUE composition groups into one schema, combining// properties and leaf metadata. Enum constraints depend on the composition kind.func FlattenComposite(s *Config) *Config { if s == nil { return &Config{} } if len(s.AllOf) == 0 && len(s.AnyOf) == 0 && len(s.OneOf) == 0 { return s } merged := &Config{ Description: s.Description, Type: s.Type, Nullable: s.Nullable, Enum: s.Enum, Default: s.Default, Format: s.Format, Pattern: s.Pattern, Minimum: s.Minimum, Maximum: s.Maximum, MinLength: s.MinLength, MaxLength: s.MaxLength, Items: s.Items, Properties: make(map[string]*Config), }
// Merge all composition variants — they all contribute properties // and leaf-level fields when not already set on the top-level schema. for groupIndex, group := range [3][]*Config{s.AllOf, s.AnyOf, s.OneOf} { var enum []any allEnums := true for _, sub := range group { flat := FlattenComposite(sub) if flat.Description != "" && merged.Description == "" { merged.Description = flat.Description } if flat.Type != "" && merged.Type == "" { merged.Type = flat.Type } if flat.Format != "" && merged.Format == "" { merged.Format = flat.Format } if flat.Default != nil && merged.Default == nil { merged.Default = flat.Default } if flat.Pattern != "" && merged.Pattern == "" { merged.Pattern = flat.Pattern } if flat.Minimum != nil && merged.Minimum == nil { merged.Minimum = flat.Minimum } if flat.Maximum != nil && merged.Maximum == nil { merged.Maximum = flat.Maximum } if flat.MinLength != nil && merged.MinLength == nil { merged.MinLength = flat.MinLength } if flat.MaxLength != nil && merged.MaxLength == nil { merged.MaxLength = flat.MaxLength } if flat.Items != nil && merged.Items == nil { merged.Items = flat.Items } maps.Copy(merged.Properties, flat.Properties) merged.Required = append(merged.Required, flat.Required...) if flat.AdditionalProperties != nil { merged.AdditionalProperties = flat.AdditionalProperties } if len(merged.Enum) > 0 { continue } if groupIndex == 0 { merged.Enum = flat.Enum } else if allEnums { switch { case len(flat.Enum) == 0: allEnums = false enum = nil case len(enum) == 0: // Appending must not overwrite a branch's shared backing array. enum = slices.Clip(flat.Enum) default: enum = compositeEnum(enum, flat.Enum) } } } if groupIndex > 0 && allEnums && len(merged.Enum) == 0 { merged.Enum = enum } }
// Also merge properties from the top-level schema itself. maps.Copy(merged.Properties, s.Properties) merged.Required = append(merged.Required, s.Required...) return merged}
func compositeEnum(values, branch []any) []any { for _, value := range branch { if !slices.ContainsFunc(values, func(candidate any) bool { return reflect.DeepEqual(candidate, value) }) { values = append(values, value) } } return values}
// isScalarUnion returns true if the schema is an anyOf/oneOf where every// branch is a scalar type (no properties, no additionalProperties).func isScalarUnion(s *Config) bool { variants := scalarUnionVariants(s) if len(variants) == 0 { return false } for _, v := range variants { if v.Properties != nil || v.AdditionalProperties != nil || len(v.AllOf) > 0 || len(v.AnyOf) > 0 || len(v.OneOf) > 0 { return false } } return true}
// hasProps returns true if the schema has at least one property.func hasProps(s *Config) bool { return len(s.Properties) > 0}
// sourceRefFor finds the best SourceRef from a schema or its composition branches.func sourceRefFor(s *Config) string { if s.SourceRef != "" { return s.SourceRef } for _, group := range [][]*Config{s.AllOf, s.AnyOf, s.OneOf} { for _, sub := range group { if sub.SourceRef != "" { return sub.SourceRef } } } return ""}
// shortDesc returns the first line of a description, truncated to 60 chars.func shortDesc(s string) string { if s == "" { return "" } first, _, _ := strings.Cut(strings.TrimRight(s, "\n"), "\n") if len(first) > 60 { first = first[:57] + "..." } return first}
// enumString formats enum values as a comma-separated string.func enumString(vals []any) string { var parts []string for _, v := range vals { if v != nil { parts = append(parts, fmt.Sprint(v)) } } return strings.Join(parts, ",")}