github.com/DataDog/datadog-agent/pkg/security/secl@v0.55.0-devel.0.20240517055856-10c4965fea94/compiler/eval/opts.go (about) 1 // Unless explicitly stated otherwise all files in this repository are licensed 2 // under the Apache License Version 2.0. 3 // This product includes software developed at Datadog (https://www.datadoghq.com/). 4 // Copyright 2016-present Datadog, Inc. 5 6 // Package eval holds eval related files 7 package eval 8 9 // MacroStore represents a store of SECL Macros 10 type MacroStore struct { 11 macros []*Macro 12 } 13 14 // Add adds a macro 15 func (s *MacroStore) Add(macro *Macro) *MacroStore { 16 s.macros = append(s.macros, macro) 17 return s 18 } 19 20 // List lists macros 21 func (s *MacroStore) List() []*Macro { 22 if s == nil { 23 return nil 24 } 25 26 return s.macros 27 } 28 29 // Get returns the marcro 30 func (s *MacroStore) Get(id string) *Macro { 31 if s == nil { 32 return nil 33 } 34 35 for _, m := range s.macros { 36 if m.ID == id { 37 return m 38 } 39 } 40 return nil 41 } 42 43 // Contains returns returns true is there is already a macro with this ID in the store 44 func (s *MacroStore) Contains(id string) bool { 45 return s.Get(id) != nil 46 } 47 48 // VariableStore represents a store of SECL variables 49 type VariableStore struct { 50 Variables map[string]VariableValue 51 } 52 53 // Add adds a variable 54 func (s *VariableStore) Add(name string, variable VariableValue) *VariableStore { 55 if s.Variables == nil { 56 s.Variables = make(map[string]VariableValue) 57 } 58 s.Variables[name] = variable 59 return s 60 } 61 62 // Get returns the variable 63 func (s *VariableStore) Get(name string) VariableValue { 64 if s == nil || s.Variables == nil { 65 return nil 66 } 67 return s.Variables[name] 68 } 69 70 // Opts are the options to be passed to the evaluator 71 type Opts struct { 72 LegacyFields map[Field]Field 73 Constants map[string]interface{} 74 VariableStore *VariableStore 75 MacroStore *MacroStore 76 } 77 78 // WithConstants set constants 79 func (o *Opts) WithConstants(constants map[string]interface{}) *Opts { 80 o.Constants = constants 81 return o 82 } 83 84 // WithVariables set variables 85 func (o *Opts) WithVariables(variables map[string]VariableValue) *Opts { 86 if o.VariableStore == nil { 87 o.VariableStore = &VariableStore{} 88 } 89 90 for n, v := range variables { 91 o.VariableStore.Add(n, v) 92 } 93 return o 94 } 95 96 // WithVariableStore set the variable store 97 func (o *Opts) WithVariableStore(store *VariableStore) *Opts { 98 o.VariableStore = store 99 return o 100 } 101 102 // WithLegacyFields set legacy fields 103 func (o *Opts) WithLegacyFields(fields map[Field]Field) *Opts { 104 o.LegacyFields = fields 105 return o 106 } 107 108 // WithMacroStore set the macro store 109 func (o *Opts) WithMacroStore(store *MacroStore) *Opts { 110 o.MacroStore = store 111 return o 112 } 113 114 // AddMacro add a macro 115 func (o *Opts) AddMacro(macro *Macro) *Opts { 116 if o.MacroStore == nil { 117 o.MacroStore = &MacroStore{} 118 } 119 o.MacroStore.Add(macro) 120 return o 121 } 122 123 // AddVariable add a variable 124 func (o *Opts) AddVariable(name string, variable VariableValue) *Opts { 125 if o.VariableStore == nil { 126 o.VariableStore = &VariableStore{} 127 } 128 o.VariableStore.Add(name, variable) 129 return o 130 }