Skip to content

Commit

Permalink
Support time.Time annotation values
Browse files Browse the repository at this point in the history
The new yaml library for values formated as date/timestamps now returns
a `time.Time` typed value, this change makes sure we support it.
  • Loading branch information
zregvart committed Nov 5, 2024
1 parent 029c054 commit 7e482bc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
12 changes: 10 additions & 2 deletions internal/opa/rule/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"regexp"
"strings"
"time"

"github.com/open-policy-agent/opa/ast"
)
Expand Down Expand Up @@ -48,9 +49,16 @@ func customAnnotationString(a *ast.AnnotationsRef, fieldName string) string {
if a == nil || a.Annotations == nil || a.Annotations.Custom == nil {
return ""
}
if value, ok := a.Annotations.Custom[fieldName].(string); ok {
return value

if value, ok := a.Annotations.Custom[fieldName]; ok {
switch value := value.(type) {
case string:
return value
case time.Time:
return value.Format(time.RFC3339)
}
}

return ""
}

Expand Down
10 changes: 10 additions & 0 deletions internal/opa/rule/rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,16 @@ func TestEffectiveOn(t *testing.T) {
},
{
name: "with effective_on annotation",
annotation: annotationRef(heredoc.Doc(`
package a
# METADATA
# custom:
# effective_on: 2022-01-01T00:00:00Z
deny() { true }`)),
expected: "2022-01-01T00:00:00Z",
},
{
name: "with effective_on annotation as string",
annotation: annotationRef(heredoc.Doc(`
package a
# METADATA
Expand Down

0 comments on commit 7e482bc

Please sign in to comment.