Skip to content

Commit

Permalink
[CWS] remove static defined field from generator (#21175)
Browse files Browse the repository at this point in the history
  • Loading branch information
safchain authored Nov 29, 2023
1 parent d13e6ec commit c8cfd6e
Show file tree
Hide file tree
Showing 4 changed files with 2,504 additions and 3,865 deletions.
4 changes: 2 additions & 2 deletions pkg/security/secl/compiler/generators/accessors/accessors.go
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ func newField(allFields map[string]*common.StructField, field *common.StructFiel
return result
}

func generatePrefixNilChecks(allFields map[string]*common.StructField, field *common.StructField) string {
func generatePrefixNilChecks(allFields map[string]*common.StructField, returnType string, field *common.StructField) string {
var fieldPath, result string
for _, node := range strings.Split(field.Name, ".") {
if fieldPath != "" {
Expand All @@ -751,7 +751,7 @@ func generatePrefixNilChecks(allFields map[string]*common.StructField, field *co

if field, ok := allFields[fieldPath]; ok {
if field.IsOrigTypePtr {
result += fmt.Sprintf("if ev.%s == nil { return zeroValue }\n", field.Name)
result += fmt.Sprintf("if ev.%s == nil { return %s }\n", field.Name, getDefaultValueOfType(returnType))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,13 @@ import (

// Get{{$pascalCaseName}} returns the value of the field, resolving if necessary
func (ev *Event) Get{{$pascalCaseName}}() {{ $accessorReturnType }} {
{{ if and (ne $Field.Handler "ResolveAsync") (and (ne $Field.Handler "ResolveEventTimestamp") (ne $Field.Handler "ResolveEventTime")) }}
zeroValue := {{ GetDefaultValueOfType $accessorReturnType}}
{{ end }}
{{if ne $Field.Event "*"}}
if ev.GetEventType().String() != "{{$Field.Event}}" {
return zeroValue
return {{ GetDefaultValueOfType $accessorReturnType}}
}
{{end}}

{{$Field | GeneratePrefixNilChecks $.AllFields}}
{{$Field | GeneratePrefixNilChecks $.AllFields $accessorReturnType}}

{{if $Field.Iterator}}
var values {{ $accessorReturnType }}
Expand Down
Loading

0 comments on commit c8cfd6e

Please sign in to comment.