Skip to content

Commit

Permalink
Merge branch 'main' into extension-ast-calls
Browse files Browse the repository at this point in the history
  • Loading branch information
patjakdev authored Nov 9, 2024
2 parents f8be512 + c3dfe27 commit 5f76c53
Show file tree
Hide file tree
Showing 26 changed files with 364 additions and 164 deletions.
49 changes: 47 additions & 2 deletions ast/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ast_test
import (
"net/netip"
"testing"
"time"

"github.com/cedar-policy/cedar-go/ast"
internalast "github.com/cedar-policy/cedar-go/internal/ast"
Expand Down Expand Up @@ -411,7 +412,51 @@ func TestASTByTable(t *testing.T) {
internalast.Permit().When(internalast.Long(42).IsInRange(internalast.Long(43))),
},
{
"decimalExtension",
"opOffset",
ast.Permit().When(ast.Datetime(time.Time{}).Offset(ast.Duration(time.Duration(100)))),
internalast.Permit().When(internalast.Datetime(time.Time{}).Offset(internalast.Duration(time.Duration(100)))),
},
{
"opDurationSince",
ast.Permit().When(ast.Datetime(time.Time{}).DurationSince(ast.Datetime(time.Time{}))),
internalast.Permit().When(internalast.Datetime(time.Time{}).DurationSince(internalast.Datetime(time.Time{}))),
},
{
"opToDate",
ast.Permit().When(ast.Datetime(time.Time{}).ToDate()),
internalast.Permit().When(internalast.Datetime(time.Time{}).ToDate()),
},
{
"opToTime",
ast.Permit().When(ast.Datetime(time.Time{}).ToTime()),
internalast.Permit().When(internalast.Datetime(time.Time{}).ToTime()),
},
{
"opToDays",
ast.Permit().When(ast.Duration(time.Duration(100)).ToDays()),
internalast.Permit().When(internalast.Duration(100).ToDays()),
},
{
"opToHours",
ast.Permit().When(ast.Duration(time.Duration(100)).ToHours()),
internalast.Permit().When(internalast.Duration(100).ToHours()),
},
{
"opToMinutes",
ast.Permit().When(ast.Duration(time.Duration(100)).ToMinutes()),
internalast.Permit().When(internalast.Duration(100).ToMinutes()),
},
{
"opToSeconds",
ast.Permit().When(ast.Duration(time.Duration(100)).ToSeconds()),
internalast.Permit().When(internalast.Duration(100).ToSeconds()),
},
{
"opToMilliseconds",
ast.Permit().When(ast.Duration(time.Duration(100)).ToMilliseconds()),
internalast.Permit().When(internalast.Duration(100).ToMilliseconds()),
},
"decimalExtension",
ast.Permit().When(ast.DecimalExtensionCall(ast.Value(types.String("3.14")))),
internalast.Permit().When(internalast.ExtensionCall("decimal", internalast.String("3.14"))),
},
Expand All @@ -429,7 +474,7 @@ func TestASTByTable(t *testing.T) {
"duration",
ast.Permit().When(ast.DurationExtensionCall(ast.Value(types.String("1d2h3m4s5ms")))),
internalast.Permit().When(internalast.ExtensionCall("duration", internalast.String("1d2h3m4s5ms"))),
},
},
}

for _, tt := range tests {

Check failure on line 480 in ast/ast_test.go

View workflow job for this annotation

GitHub Actions / lint

expected ';', found 'for' (typecheck)
Expand Down
30 changes: 30 additions & 0 deletions ast/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,33 @@ func (lhs Node) IsLoopback() Node {
func (lhs Node) IsInRange(rhs Node) Node {
return wrapNode(lhs.Node.IsInRange(rhs.Node))
}

// ____ _ _ _
// | _ \ __ _| |_ ___| |_(_)_ __ ___ ___
// | | | |/ _` | __/ _ \ __| | '_ ` _ \ / _ \
// | |_| | (_| | || __/ |_| | | | | | | __/
// |____/ \__,_|\__\___|\__|_|_| |_| |_|\___|

func (lhs Node) Offset(rhs Node) Node { return wrapNode(lhs.Node.Offset(rhs.Node)) }

func (lhs Node) DurationSince(rhs Node) Node { return wrapNode(lhs.Node.DurationSince(rhs.Node)) }

func (lhs Node) ToDate() Node { return wrapNode(lhs.Node.ToDate()) }

func (lhs Node) ToTime() Node { return wrapNode(lhs.Node.ToTime()) }

// ____ _ _
// | _ \ _ _ _ __ __ _| |_(_) ___ _ __
// | | | | | | | '__/ _` | __| |/ _ \| '_ \
// | |_| | |_| | | | (_| | |_| | (_) | | | |
// |____/ \__,_|_| \__,_|\__|_|\___/|_| |_|

func (lhs Node) ToDays() Node { return wrapNode(lhs.Node.ToDays()) }

func (lhs Node) ToHours() Node { return wrapNode(lhs.Node.ToHours()) }

func (lhs Node) ToMinutes() Node { return wrapNode(lhs.Node.ToMinutes()) }

func (lhs Node) ToSeconds() Node { return wrapNode(lhs.Node.ToSeconds()) }

func (lhs Node) ToMilliseconds() Node { return wrapNode(lhs.Node.ToMilliseconds()) }
9 changes: 9 additions & 0 deletions ast/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ast

import (
"net/netip"
"time"

"github.com/cedar-policy/cedar-go/internal/ast"
"github.com/cedar-policy/cedar-go/types"
Expand Down Expand Up @@ -79,6 +80,14 @@ func IPAddr[T netip.Prefix | types.IPAddr](i T) Node {
return wrapNode(ast.IPAddr(types.IPAddr(i)))
}

func Datetime(t time.Time) Node {
return Value(types.FromStdTime(t))
}

func Duration(d time.Duration) Node {
return Value(types.FromStdDuration(d))
}

// Value creates a value node from any value.
func Value(v types.Value) Node {
return wrapNode(ast.Value(v))
Expand Down
Loading

0 comments on commit 5f76c53

Please sign in to comment.