github.com/Jeffail/benthos/v3@v3.65.0/internal/config/schema/schema.go (about)

     1  package schema
     2  
     3  import (
     4  	"sort"
     5  
     6  	"github.com/Jeffail/benthos/v3/internal/bloblang/query"
     7  	"github.com/Jeffail/benthos/v3/internal/bundle"
     8  	"github.com/Jeffail/benthos/v3/internal/docs"
     9  	"github.com/Jeffail/benthos/v3/lib/condition"
    10  	"github.com/Jeffail/benthos/v3/lib/config"
    11  )
    12  
    13  // Full represents the entirety of the Benthos instances configuration spec and
    14  // all plugins.
    15  type Full struct {
    16  	Version           string               `json:"version"`
    17  	Date              string               `json:"date"`
    18  	Config            docs.FieldSpecs      `json:"config,omitempty"`
    19  	Buffers           []docs.ComponentSpec `json:"buffers,omitempty"`
    20  	Caches            []docs.ComponentSpec `json:"caches,omitempty"`
    21  	Inputs            []docs.ComponentSpec `json:"inputs,omitempty"`
    22  	Outputs           []docs.ComponentSpec `json:"outputs,omitempty"`
    23  	Processors        []docs.ComponentSpec `json:"processors,omitempty"`
    24  	RateLimits        []docs.ComponentSpec `json:"rate-limits,omitempty"`
    25  	Metrics           []docs.ComponentSpec `json:"metrics,omitempty"`
    26  	Tracers           []docs.ComponentSpec `json:"tracers,omitempty"`
    27  	conditions        []string
    28  	BloblangFunctions []query.FunctionSpec `json:"bloblang-functions,omitempty"`
    29  	BloblangMethods   []query.MethodSpec   `json:"bloblang-methods,omitempty"`
    30  }
    31  
    32  // New walks all registered Benthos components and creates a full schema
    33  // definition of it.
    34  func New(version, date string) Full {
    35  	s := Full{
    36  		Version:           version,
    37  		Date:              date,
    38  		Config:            config.Spec(),
    39  		Buffers:           bundle.AllBuffers.Docs(),
    40  		Caches:            bundle.AllCaches.Docs(),
    41  		Inputs:            bundle.AllInputs.Docs(),
    42  		Outputs:           bundle.AllOutputs.Docs(),
    43  		Processors:        bundle.AllProcessors.Docs(),
    44  		RateLimits:        bundle.AllRateLimits.Docs(),
    45  		Metrics:           bundle.AllMetrics.Docs(),
    46  		Tracers:           bundle.AllTracers.Docs(),
    47  		BloblangFunctions: query.FunctionDocs(),
    48  		BloblangMethods:   query.MethodDocs(),
    49  	}
    50  	for t := range condition.Constructors {
    51  		s.conditions = append(s.conditions, t)
    52  	}
    53  	sort.Strings(s.conditions)
    54  	return s
    55  }
    56  
    57  // Flattened returns a flattened representation of all registered plugin types
    58  // and names.
    59  func (f *Full) Flattened() map[string][]string {
    60  	justNames := func(components []docs.ComponentSpec) []string {
    61  		names := []string{}
    62  		for _, c := range components {
    63  			if c.Status != docs.StatusDeprecated {
    64  				names = append(names, c.Name)
    65  			}
    66  		}
    67  		return names
    68  	}
    69  	justNamesBloblFuncs := func(fns []query.FunctionSpec) []string {
    70  		names := []string{}
    71  		for _, c := range fns {
    72  			if c.Status != query.StatusDeprecated {
    73  				names = append(names, c.Name)
    74  			}
    75  		}
    76  		return names
    77  	}
    78  	justNamesBloblMethods := func(fns []query.MethodSpec) []string {
    79  		names := []string{}
    80  		for _, c := range fns {
    81  			if c.Status != query.StatusDeprecated {
    82  				names = append(names, c.Name)
    83  			}
    84  		}
    85  		return names
    86  	}
    87  	return map[string][]string{
    88  		"buffers":            justNames(f.Buffers),
    89  		"caches":             justNames(f.Caches),
    90  		"inputs":             justNames(f.Inputs),
    91  		"outputs":            justNames(f.Outputs),
    92  		"processors":         justNames(f.Processors),
    93  		"rate-limits":        justNames(f.RateLimits),
    94  		"metrics":            justNames(f.Metrics),
    95  		"tracers":            justNames(f.Tracers),
    96  		"conditions":         f.conditions,
    97  		"bloblang-functions": justNamesBloblFuncs(f.BloblangFunctions),
    98  		"bloblang-methods":   justNamesBloblMethods(f.BloblangMethods),
    99  	}
   100  }
   101  
   102  // Scrub walks the schema and removes all descriptions and other long-form
   103  // documentation, reducing the overall size.
   104  func (f *Full) Scrub() {
   105  	scrubFieldSpecs(f.Config)
   106  	scrubComponentSpecs(f.Buffers)
   107  	scrubComponentSpecs(f.Caches)
   108  	scrubComponentSpecs(f.Inputs)
   109  	scrubComponentSpecs(f.Outputs)
   110  	scrubComponentSpecs(f.Processors)
   111  	scrubComponentSpecs(f.RateLimits)
   112  	scrubComponentSpecs(f.Metrics)
   113  	scrubComponentSpecs(f.Tracers)
   114  
   115  	for i := range f.BloblangFunctions {
   116  		f.BloblangFunctions[i].Description = ""
   117  		f.BloblangFunctions[i].Examples = nil
   118  		scrubParams(f.BloblangFunctions[i].Params.Definitions)
   119  	}
   120  	for i := range f.BloblangMethods {
   121  		f.BloblangMethods[i].Description = ""
   122  		f.BloblangMethods[i].Examples = nil
   123  		f.BloblangMethods[i].Categories = nil
   124  		scrubParams(f.BloblangMethods[i].Params.Definitions)
   125  	}
   126  }
   127  
   128  func scrubParams(p []query.ParamDefinition) {
   129  	for i := range p {
   130  		p[i].Description = ""
   131  	}
   132  }
   133  
   134  func scrubFieldSpecs(fs []docs.FieldSpec) {
   135  	for i := range fs {
   136  		fs[i].Description = ""
   137  		fs[i].Examples = nil
   138  		for j := range fs[i].AnnotatedOptions {
   139  			fs[i].AnnotatedOptions[j][1] = ""
   140  		}
   141  		scrubFieldSpecs(fs[i].Children)
   142  	}
   143  }
   144  
   145  func scrubFieldSpec(fs *docs.FieldSpec) {
   146  	fs.Description = ""
   147  	scrubFieldSpecs(fs.Children)
   148  }
   149  
   150  func scrubComponentSpecs(cs []docs.ComponentSpec) {
   151  	for i := range cs {
   152  		cs[i].Description = ""
   153  		cs[i].Summary = ""
   154  		cs[i].Footnotes = ""
   155  		cs[i].Examples = nil
   156  		scrubFieldSpec(&cs[i].Config)
   157  	}
   158  }