1818package javascript
1919
2020import (
21- "errors"
2221 "fmt"
22+ "slices"
2323
2424 "github.com/dop251/goja"
2525
@@ -28,8 +28,8 @@ import (
2828)
2929
3030// IMPORTANT:
31- // This is the user facing API within Javascript processors. Do not make
32- // breaking changes to the JS methods. If you must make breaking changes then
31+ // This is the user- facing API within JavaScript processors. Do not make
32+ // breaking changes to the JS methods. If you must make breaking changes, then
3333// create a new version and require the user to specify an API version in their
3434// configuration (e.g. api_version: 2).
3535
@@ -52,48 +52,47 @@ func newBeatEventV0(s Session) (Event, error) {
5252func newBeatEventV0Constructor (s Session ) func (call goja.ConstructorCall ) * goja.Object {
5353 return func (call goja.ConstructorCall ) * goja.Object {
5454 if len (call .Arguments ) != 1 {
55- panic (errors . New ( "Event constructor requires one argument" ) )
55+ panic ("Event constructor requires one argument" )
5656 }
5757
5858 a0 := call .Argument (0 ).Export ()
5959
6060 var fields mapstr.M
6161 switch v := a0 .(type ) {
62- case map [string ]interface {} :
62+ case map [string ]any :
6363 fields = v
6464 case mapstr.M :
6565 fields = v
6666 default :
67- panic (fmt .Errorf ("Event constructor requires a " +
68- "map[string]interface{} argument but got %T" , a0 ))
67+ panic (fmt .Sprintf ("Event constructor requires map[string]interface{} argument but got %T" , a0 ))
6968 }
7069
7170 evt := & beatEventV0 {
7271 vm : s .Runtime (),
7372 obj : call .This ,
7473 }
7574 evt .init ()
76- evt .reset (& beat.Event {Fields : fields })
75+ _ = evt .reset (& beat.Event {Fields : fields })
7776 return nil
7877 }
7978}
8079
8180func (e * beatEventV0 ) init () {
82- e .obj .Set ("Get" , e .get )
83- e .obj .Set ("Put" , e .put )
84- e .obj .Set ("Rename" , e .rename )
85- e .obj .Set ("Delete" , e .delete )
86- e .obj .Set ("Cancel" , e .cancel )
87- e .obj .Set ("Tag" , e .tag )
88- e .obj .Set ("AppendTo" , e .appendTo )
81+ _ = e .obj .Set ("Get" , e .get )
82+ _ = e .obj .Set ("Put" , e .put )
83+ _ = e .obj .Set ("Rename" , e .rename )
84+ _ = e .obj .Set ("Delete" , e .delete )
85+ _ = e .obj .Set ("Cancel" , e .cancel )
86+ _ = e .obj .Set ("Tag" , e .tag )
87+ _ = e .obj .Set ("AppendTo" , e .appendTo )
8988}
9089
9190// reset the event so that it can be reused to wrap another event.
9291func (e * beatEventV0 ) reset (b * beat.Event ) error {
9392 e .inner = b
9493 e .cancelled = false
95- e .obj .Set ("_private" , e )
96- e .obj .Set ("fields" , e .vm .ToValue (e .inner .Fields ))
94+ _ = e .obj .Set ("_private" , e )
95+ _ = e .obj .Set ("fields" , e .vm .ToValue (e .inner .Fields ))
9796 return nil
9897}
9998
@@ -103,13 +102,13 @@ func (e *beatEventV0) Wrapped() *beat.Event {
103102}
104103
105104// JSObject returns the goja.Value that represents the event within the
106- // Javascript runtime.
105+ // JavaScript runtime.
107106func (e * beatEventV0 ) JSObject () goja.Value {
108107 return e .obj
109108}
110109
111- // get returns the specified field. If the field does not exist then null is
112- // returned. If no field is specified then it returns entire object.
110+ // get returns the specified field. If the field does not exist, then null is
111+ // returned. If no field is specified, then it returns an entire object.
113112//
114113// // javascript
115114// var dataset = evt.Get("event.dataset");
@@ -129,7 +128,7 @@ func (e *beatEventV0) get(call goja.FunctionCall) goja.Value {
129128}
130129
131130// put writes a value to the event. If there was a previous value assigned to
132- // the given field then the old object is returned. It throws an exception if
131+ // the given field, then the old object is returned. It throws an exception if
133132// you try to write a to a field where one of the intermediate values is not
134133// an object.
135134//
@@ -138,7 +137,7 @@ func (e *beatEventV0) get(call goja.FunctionCall) goja.Value {
138137// evt.Put("geo.location", {"lon": -73.614830, "lat": 45.505918});
139138func (e * beatEventV0 ) put (call goja.FunctionCall ) goja.Value {
140139 if len (call .Arguments ) != 2 {
141- panic (errors . New ( "Put requires two arguments (key and value)" ) )
140+ panic ("Put requires two arguments (key and value)" )
142141 }
143142
144143 key := call .Argument (0 ).String ()
@@ -157,7 +156,7 @@ func (e *beatEventV0) put(call goja.FunctionCall) goja.Value {
157156// evt.Rename("src_ip", "source.ip");
158157func (e * beatEventV0 ) rename (call goja.FunctionCall ) goja.Value {
159158 if len (call .Arguments ) != 2 {
160- panic (errors . New ( "Rename requires two arguments (from and to)" ) )
159+ panic ("Rename requires two arguments (from and to)" )
161160 }
162161
163162 from := call .Argument (0 ).String ()
@@ -181,7 +180,7 @@ func (e *beatEventV0) rename(call goja.FunctionCall) goja.Value {
181180
182181 if _ , err = e .inner .PutValue (to , fromValue ); err != nil {
183182 // Undo
184- e .inner .PutValue (from , fromValue )
183+ _ , _ = e .inner .PutValue (from , fromValue )
185184 return e .vm .ToValue (false )
186185 }
187186
@@ -194,7 +193,7 @@ func (e *beatEventV0) rename(call goja.FunctionCall) goja.Value {
194193// evt.Delete("http.request.headers.authorization");
195194func (e * beatEventV0 ) delete (call goja.FunctionCall ) goja.Value {
196195 if len (call .Arguments ) != 1 {
197- panic (errors . New ( "Delete requires one argument" ) )
196+ panic ("Delete requires one argument" )
198197 }
199198
200199 key := call .Argument (0 ).String ()
@@ -210,14 +209,14 @@ func (e *beatEventV0) IsCancelled() bool {
210209 return e .cancelled
211210}
212211
213- // Cancel marks the event as cancelled. When the processor returns the event
212+ // Cancel marks the event as cancelled. When the processor returns, the event
214213// will be dropped.
215214func (e * beatEventV0 ) Cancel () {
216215 e .cancelled = true
217216}
218217
219218// cancel marks the event as cancelled.
220- func (e * beatEventV0 ) cancel (call goja.FunctionCall ) goja.Value {
219+ func (e * beatEventV0 ) cancel (goja.FunctionCall ) goja.Value {
221220 e .cancelled = true
222221 return goja .Undefined ()
223222}
@@ -229,7 +228,7 @@ func (e *beatEventV0) cancel(call goja.FunctionCall) goja.Value {
229228// evt.Tag("_parse_failure");
230229func (e * beatEventV0 ) tag (call goja.FunctionCall ) goja.Value {
231230 if len (call .Arguments ) != 1 {
232- panic (errors . New ( "Tag requires one argument" ) )
231+ panic ("Tag requires one argument" )
233232 }
234233
235234 tag := call .Argument (0 ).String ()
@@ -242,14 +241,14 @@ func (e *beatEventV0) tag(call goja.FunctionCall) goja.Value {
242241
243242// appendTo is a specialized Put method that converts any existing value to
244243// an array and appends the value if it does not already exist. If there is an
245- // existing value that's not a string or array of strings then an exception is
244+ // existing value that's not a string or array of strings, then an exception is
246245// thrown.
247246//
248247// // javascript
249248// evt.AppendTo("error.message", "invalid file hash");
250249func (e * beatEventV0 ) appendTo (call goja.FunctionCall ) goja.Value {
251250 if len (call .Arguments ) != 2 {
252- panic (errors . New ( "AppendTo requires two arguments (field and value)" ) )
251+ panic ("AppendTo requires two arguments (field and value)" )
253252 }
254253
255254 field := call .Argument (0 ).String ()
@@ -275,14 +274,12 @@ func appendString(m mapstr.M, field, value string, alwaysArray bool) error {
275274 m .Put (field , []string {v , value })
276275 }
277276 case []string :
278- for _ , existingTag := range v {
279- if value == existingTag {
280- // Duplicate
281- return nil
282- }
277+ if slices .Contains (v , value ) {
278+ // Duplicate
279+ return nil
283280 }
284281 m .Put (field , append (v , value ))
285- case []interface {} :
282+ case []any :
286283 for _ , existingTag := range v {
287284 if value == existingTag {
288285 // Duplicate
0 commit comments