github.com/DataDog/datadog-agent/pkg/security/secl@v0.55.0-devel.0.20240517055856-10c4965fea94/compiler/generators/accessors/field_accessors.tmpl (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 2022-present Datadog, Inc.
     5  // Code generated - DO NOT EDIT.
     6  
     7  {{range .BuildTags }}//{{.}}{{end}}
     8  
     9  package {{.Name}}
    10  
    11  import (
    12      {{range .Imports }}
    13          "{{.}}"
    14      {{end}}
    15      "time"
    16  
    17      "github.com/DataDog/datadog-agent/pkg/security/secl/compiler/eval"
    18  )
    19  
    20  {{range $Name, $Field := .Fields}}
    21  
    22  {{ $pascalCaseName := PascalCaseFieldName $Name }}
    23  
    24  {{$accessorReturnType := $Field.OrigType}}
    25  {{ if $Field.Handler}}
    26      {{$accessorReturnType = $Field.ReturnType}}
    27  {{ end }}
    28  
    29  {{ if or ($Field.Iterator) ($Field.IsArray) }}
    30      {{$accessorReturnType = $accessorReturnType | printf "[]%s" }}
    31  {{ end }}
    32  
    33  // Get{{$pascalCaseName}} returns the value of the field, resolving if necessary
    34  func (ev *Event) Get{{$pascalCaseName}}() {{ $accessorReturnType }} {
    35      {{if ne $Field.Event "*"}}
    36          if ev.GetEventType().String() != "{{$Field.Event}}" {
    37              return {{ GetDefaultValueOfType $accessorReturnType}}
    38          }
    39      {{end}}
    40  
    41      {{$Field | GeneratePrefixNilChecks $.AllFields $accessorReturnType}}
    42  
    43      {{if $Field.Iterator}}
    44          var values {{ $accessorReturnType }}
    45  
    46          ctx := eval.NewContext(ev)
    47  
    48          iterator := &{{$Field.Iterator.ReturnType}}{}
    49          ptr := iterator.Front(ctx)
    50  
    51          for ptr != nil {
    52              {{if $Field.Iterator.IsOrigTypePtr}}
    53                  element := (*{{$Field.Iterator.OrigType}})(ptr)
    54              {{else}}
    55                  elementPtr := (*{{$Field.Iterator.OrigType}})(ptr)
    56                  element := *elementPtr
    57              {{end}}
    58  
    59              {{$SubName := $Field.Iterator.Name | TrimPrefix $Field.Name}}
    60  
    61              {{$Return := $SubName | printf "element%s"}}
    62              {{if $Field.Handler}}
    63                  {{$SubName = $Field.Iterator.Name | TrimPrefix $Field.Prefix}}
    64                  {{$Handler := $Field.Iterator.Name | TrimPrefix $Field.Handler}}
    65                  {{$Return = print "ev.FieldHandlers." $Handler "(ev, &element" $SubName ")"}}
    66              {{end}}
    67  
    68              {{if $Field.IsLength}}
    69              {{$Return = ".length" | TrimSuffix $Return}}
    70              {{end}}
    71  
    72              {{if and $Field.Handler (and (eq $Field.ReturnType "int") (ne $Field.OrigType "int")) }}
    73  			    result := int({{$Return}})
    74              {{else}}
    75                  {{if $Field.IsLength}}
    76                      result := len({{$Return}})
    77                  {{else}}
    78                      result := {{$Return}}
    79                  {{end}}
    80              {{end}}
    81  
    82              {{if not $Field.GetArrayPrefix}}
    83              values = append(values, result)
    84              {{else}}
    85              values = append(values, result...)
    86              {{end}}
    87  
    88              ptr = iterator.Next()
    89          }
    90  
    91          return values
    92      {{else}}
    93  
    94          {{$Return := $Field.Name | printf "ev.%s"}}
    95  
    96          {{$Checks := $Field | GetChecks $.AllFields}}
    97          {{range $Check := $Checks}}
    98          {{$Check = $Check | printf "ev.%s"}}
    99          if !{{$Check}}() {
   100              return {{ GetDefaultValueOfType $accessorReturnType}}
   101          }
   102          {{end}}
   103  
   104          {{if $Field.IsLength}}
   105              {{$Return = ".length" | TrimSuffix $Field.Name | printf "len(ev.%s)"}}
   106          {{end}}
   107  
   108          {{if $Field.Handler}}
   109              {{$Ptr := "&"}}
   110              {{$Parent := index $.AllFields $Field.Prefix}}
   111              {{- if or (not $Parent) $Parent.IsOrigTypePtr}}
   112                  {{$Ptr = ""}}
   113              {{end}}
   114  
   115              {{$Prefix := $Field.Prefix}}
   116              {{ if not $Prefix }}
   117                  {{$Return = print "ev.FieldHandlers." $Field.Handler "(ev)"}}
   118              {{- else if $Field.IsLength }}
   119                  {{$Return = print "len(ev.FieldHandlers." $Field.Handler "(ev, " $Ptr "ev." $Prefix "))"}}
   120              {{ else }}
   121                  {{$Return = print "ev.FieldHandlers." $Field.Handler "(ev, " $Ptr "ev." $Prefix ")"}}
   122              {{end}}
   123          {{end}}
   124  
   125          return {{$Return}}
   126  
   127      {{end}}
   128  }
   129  {{end}}