Skip to content

Commit

Permalink
Add ArgVal signature helper to extract a generic type from event ar…
Browse files Browse the repository at this point in the history
…guments. (#3954)

This function is simply copied from `pkg/events/parse/params.go` so it is now available for signatures.
  • Loading branch information
oshaked1 committed Apr 4, 2024
1 parent 9e44157 commit 37dc481
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions signatures/helpers/arguments_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,25 @@ import (
"github.com/aquasecurity/tracee/types/trace"
)

func ArgVal[T any](args []trace.Argument, argName string) (T, error) {
for _, arg := range args {
if arg.Name == argName {
val, ok := arg.Value.(T)
if !ok {
zeroVal := *new(T)
return zeroVal, fmt.Errorf(
"argument %s is not of type %T, is of type %T",
argName,
zeroVal,
arg.Value,
)
}
return val, nil
}
}
return *new(T), fmt.Errorf("argument %s not found", argName)
}

// GetArgOps represents options for arguments getters
type GetArgOps struct {
DefaultArgs bool // Receive default args value (value equals 'nil'). If set to false, will return error if arg not initialized.
Expand Down

0 comments on commit 37dc481

Please sign in to comment.