@@ -9,22 +9,25 @@ import (
99 "strings"
1010
1111 "gopkg.in/yaml.v3"
12+
13+ "github.com/elastic/elastic-package/internal/common"
1214)
1315
1416// FieldDefinition describes a single field with its properties.
1517type FieldDefinition struct {
16- Name string `yaml:"name"`
17- Description string `yaml:"description"`
18- Type string `yaml:"type"`
19- Value string `yaml:"value"` // The value to associate with a constant_keyword field.
20- Pattern string `yaml:"pattern"`
21- Unit string `yaml:"unit"`
22- MetricType string `yaml:"metric_type"`
23- External string `yaml:"external"`
24- Index * bool `yaml:"index"`
25- DocValues * bool `yaml:"doc_values"`
26- Fields FieldDefinitions `yaml:"fields,omitempty"`
27- MultiFields []FieldDefinition `yaml:"multi_fields,omitempty"`
18+ Name string `yaml:"name"`
19+ Description string `yaml:"description"`
20+ Type string `yaml:"type"`
21+ Value string `yaml:"value"` // The value to associate with a constant_keyword field.
22+ AllowedValues AllowedValues `yaml:"allowed_values"`
23+ Pattern string `yaml:"pattern"`
24+ Unit string `yaml:"unit"`
25+ MetricType string `yaml:"metric_type"`
26+ External string `yaml:"external"`
27+ Index * bool `yaml:"index"`
28+ DocValues * bool `yaml:"doc_values"`
29+ Fields FieldDefinitions `yaml:"fields,omitempty"`
30+ MultiFields []FieldDefinition `yaml:"multi_fields,omitempty"`
2831}
2932
3033func (orig * FieldDefinition ) Update (fd FieldDefinition ) {
@@ -40,6 +43,9 @@ func (orig *FieldDefinition) Update(fd FieldDefinition) {
4043 if fd .Value != "" {
4144 orig .Value = fd .Value
4245 }
46+ if len (fd .AllowedValues ) > 0 {
47+ orig .AllowedValues = fd .AllowedValues
48+ }
4349 if fd .Pattern != "" {
4450 orig .Pattern = fd .Pattern
4551 }
@@ -182,3 +188,31 @@ func cleanNested(parent *FieldDefinition) (base []FieldDefinition) {
182188 parent .Fields = nested
183189 return base
184190}
191+
192+ // AllowedValues is the list of allowed values for a field.
193+ type AllowedValues []AllowedValue
194+
195+ // Allowed returns true if a given value is allowed.
196+ func (avs AllowedValues ) IsAllowed (value string ) bool {
197+ if len (avs ) == 0 {
198+ // No configured allowed values, any value is allowed.
199+ return true
200+ }
201+ return common .StringSliceContains (avs .Values (), value )
202+ }
203+
204+ // Values returns the list of allowed values.
205+ func (avs AllowedValues ) Values () []string {
206+ var values []string
207+ for _ , v := range avs {
208+ values = append (values , v .Name )
209+ }
210+ return values
211+ }
212+
213+ // AllowedValue is one of the allowed values for a field.
214+ type AllowedValue struct {
215+ Name string `yaml:"name"`
216+ Description string `yaml:"description"`
217+ ExpectedEventTypes []string `yaml:"expected_event_types"`
218+ }
0 commit comments