github.com/Jeffail/benthos/v3@v3.65.0/public/bloblang/spec.go (about)

     1  package bloblang
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"github.com/Jeffail/benthos/v3/internal/bloblang/query"
     7  )
     8  
     9  // ParamDefinition describes a single parameter for a function or method.
    10  type ParamDefinition struct {
    11  	def query.ParamDefinition
    12  }
    13  
    14  // NewStringParam creates a new string typed parameter. Parameter names must
    15  // match the regular expression /^[a-z0-9]+(_[a-z0-9]+)*$/ (snake case).
    16  func NewStringParam(name string) ParamDefinition {
    17  	return ParamDefinition{
    18  		def: query.ParamString(name, ""),
    19  	}
    20  }
    21  
    22  // NewInt64Param creates a new 64-bit integer typed parameter. Parameter names
    23  // must match the regular expression /^[a-z0-9]+(_[a-z0-9]+)*$/ (snake case).
    24  func NewInt64Param(name string) ParamDefinition {
    25  	return ParamDefinition{
    26  		def: query.ParamInt64(name, ""),
    27  	}
    28  }
    29  
    30  // NewFloat64Param creates a new float64 typed parameter. Parameter names must
    31  // match the regular expression /^[a-z0-9]+(_[a-z0-9]+)*$/ (snake case).
    32  func NewFloat64Param(name string) ParamDefinition {
    33  	return ParamDefinition{
    34  		def: query.ParamFloat(name, ""),
    35  	}
    36  }
    37  
    38  // NewBoolParam creates a new bool typed parameter. Parameter names must match
    39  // the regular expression /^[a-z0-9]+(_[a-z0-9]+)*$/ (snake case).
    40  func NewBoolParam(name string) ParamDefinition {
    41  	return ParamDefinition{
    42  		def: query.ParamBool(name, ""),
    43  	}
    44  }
    45  
    46  // NewAnyParam creates a new parameter that can be any type. Parameter names
    47  // must match the regular expression /^[a-z0-9]+(_[a-z0-9]+)*$/ (snake case).
    48  func NewAnyParam(name string) ParamDefinition {
    49  	return ParamDefinition{
    50  		def: query.ParamAny(name, ""),
    51  	}
    52  }
    53  
    54  // Description adds an optional description to the parameter definition, this is
    55  // used when generating documentation for the parameter to describe what the
    56  // parameter is for.
    57  func (d ParamDefinition) Description(str string) ParamDefinition {
    58  	d.def.Description = str
    59  	return d
    60  }
    61  
    62  // Optional marks the parameter as optional.
    63  func (d ParamDefinition) Optional() ParamDefinition {
    64  	d.def = d.def.Optional()
    65  	return d
    66  }
    67  
    68  // Default adds a default value to a parameter, also making it implicitly
    69  // optional.
    70  func (d ParamDefinition) Default(v interface{}) ParamDefinition {
    71  	d.def = d.def.Default(v)
    72  	return d
    73  }
    74  
    75  //------------------------------------------------------------------------------
    76  
    77  // PluginSpec documents and defines the parameters of a function or method and
    78  // the way in which it should be used.
    79  //
    80  // Using a plugin spec with explicit parameters means that instantiations of the
    81  // plugin can be done using either classic argument types (foo, bar, baz),
    82  // following the order in which the parameters are added, or named style
    83  // (c: baz, a: foo).
    84  type PluginSpec struct {
    85  	category    string
    86  	description string
    87  	params      query.Params
    88  	examples    []pluginExample
    89  }
    90  
    91  type pluginExample struct {
    92  	summary      string
    93  	mapping      string
    94  	inputOutputs [][2]string
    95  }
    96  
    97  // NewPluginSpec creates a new plugin definition for a function or method
    98  // plugin that describes the arguments that the plugin expects.
    99  func NewPluginSpec() *PluginSpec {
   100  	return &PluginSpec{
   101  		params: query.NewParams(),
   102  	}
   103  }
   104  
   105  // Category adds an optional category string to the plugin spec, this is used
   106  // when generating documentation for the plugin.
   107  func (p *PluginSpec) Category(str string) *PluginSpec {
   108  	p.category = str
   109  	return p
   110  }
   111  
   112  // Description adds an optional description to the plugin spec, this is used
   113  // when generating documentation for the plugin.
   114  func (p *PluginSpec) Description(str string) *PluginSpec {
   115  	p.description = str
   116  	return p
   117  }
   118  
   119  // Example adds an optional example to the plugin spec, this is used when
   120  // generating documentation for the plugin. An example consists of a short
   121  // summary, a mapping demonstrating the plugin, and one or more input/output
   122  // combinations.
   123  func (p *PluginSpec) Example(summary, mapping string, inputOutputs ...[2]string) *PluginSpec {
   124  	p.examples = append(p.examples, pluginExample{
   125  		summary:      summary,
   126  		mapping:      mapping,
   127  		inputOutputs: inputOutputs,
   128  	})
   129  	return p
   130  }
   131  
   132  // Param adds a parameter to the spec. Instantiations of the plugin with
   133  // nameless arguments (foo, bar, baz) must follow the order in which fields are
   134  // added to the spec.
   135  func (p *PluginSpec) Param(def ParamDefinition) *PluginSpec {
   136  	p.params = p.params.Add(def.def)
   137  	return p
   138  }
   139  
   140  // EncodeJSON attempts to parse a JSON object as a byte slice and uses it to
   141  // populate the configuration spec. The schema of this method is undocumented
   142  // and is not intended for general use.
   143  //
   144  // Experimental: This method is not intended for general use and could have its
   145  // signature and/or behaviour changed outside of major version bumps.
   146  func (p *PluginSpec) EncodeJSON(v []byte) error {
   147  	def := struct {
   148  		Description string       `json:"description"`
   149  		Params      query.Params `json:"params"`
   150  	}{}
   151  	if err := json.Unmarshal(v, &def); err != nil {
   152  		return err
   153  	}
   154  	p.description = def.Description
   155  	p.params = def.Params
   156  	return nil
   157  }
   158  
   159  //------------------------------------------------------------------------------
   160  
   161  // ParsedParams is a reference to the arguments of a method or function
   162  // instantiation.
   163  type ParsedParams struct {
   164  	par *query.ParsedParams
   165  }
   166  
   167  // Get an argument value with a given name and return it boxed within an empty
   168  // interface.
   169  func (p *ParsedParams) Get(name string) (interface{}, error) {
   170  	return p.par.Field(name)
   171  }
   172  
   173  // GetString returns a string argument value with a given name.
   174  func (p *ParsedParams) GetString(name string) (string, error) {
   175  	return p.par.FieldString(name)
   176  }
   177  
   178  // GetOptionalString returns a string argument value with a given name if it
   179  // was defined, otherwise nil.
   180  func (p *ParsedParams) GetOptionalString(name string) (*string, error) {
   181  	return p.par.FieldOptionalString(name)
   182  }
   183  
   184  // GetInt64 returns an integer argument value with a given name.
   185  func (p *ParsedParams) GetInt64(name string) (int64, error) {
   186  	return p.par.FieldInt64(name)
   187  }
   188  
   189  // GetOptionalInt64 returns an int argument value with a given name if it was
   190  // defined, otherwise nil.
   191  func (p *ParsedParams) GetOptionalInt64(name string) (*int64, error) {
   192  	return p.par.FieldOptionalInt64(name)
   193  }
   194  
   195  // GetFloat64 returns a float argument value with a given name.
   196  func (p *ParsedParams) GetFloat64(name string) (float64, error) {
   197  	return p.par.FieldFloat(name)
   198  }
   199  
   200  // GetOptionalFloat64 returns a float argument value with a given name if it
   201  // was defined, otherwise nil.
   202  func (p *ParsedParams) GetOptionalFloat64(name string) (*float64, error) {
   203  	return p.par.FieldOptionalFloat(name)
   204  }
   205  
   206  // GetBool returns a bool argument value with a given name.
   207  func (p *ParsedParams) GetBool(name string) (bool, error) {
   208  	return p.par.FieldBool(name)
   209  }
   210  
   211  // GetOptionalBool returns a bool argument value with a given name if it was
   212  // defined, otherwise nil.
   213  func (p *ParsedParams) GetOptionalBool(name string) (*bool, error) {
   214  	return p.par.FieldOptionalBool(name)
   215  }