-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from gatewayd-io/act-system
Add types and basic functionality of the act system
- Loading branch information
Showing
8 changed files
with
214 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package act | ||
|
||
import "github.com/rs/zerolog" | ||
|
||
// ActionFunc is a function that can be used to run an action. | ||
type ActionFunc func(logger zerolog.Logger, data map[string]any) (any, error) | ||
|
||
// Action is any action that can be performed by the system. | ||
type Action struct { | ||
Name string `json:"name"` | ||
Metadata map[string]any `json:"metadata"` | ||
Sync bool `json:"sync"` | ||
Terminal bool `json:"terminal"` | ||
Run ActionFunc `json:"-"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package act | ||
|
||
const ( | ||
Signals = "__signals__" | ||
Outputs = "__outputs__" | ||
Terminal = "__terminal__" | ||
Name = "name" | ||
Metadata = "metadata" | ||
Sync = "sync" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package act | ||
|
||
type Input struct { | ||
Name string `json:"name"` | ||
Policy map[string]any `json:"policy"` | ||
Signal map[string]any `json:"signal"` | ||
Sync bool `json:"sync"` | ||
} | ||
|
||
type Output struct { | ||
MatchedPolicy string `json:"matchedPolicy"` | ||
Metadata map[string]any `json:"metadata"` | ||
Verdict bool `json:"verdict"` | ||
Terminal bool `json:"terminal"` | ||
Sync bool `json:"sync"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package act | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/expr-lang/expr" | ||
"github.com/expr-lang/expr/vm" | ||
"github.com/spf13/cast" | ||
) | ||
|
||
type IPolicy interface { | ||
MustCompile(opts ...expr.Option) error | ||
Eval(ctx context.Context, input Input) (bool, error) | ||
} | ||
|
||
type Policy struct { | ||
prg *vm.Program `json:"-"` // Compiled policy | ||
opts []expr.Option `json:"-"` // Extra options for compilation | ||
|
||
Name string `json:"name"` | ||
Policy string `json:"policy"` | ||
Metadata map[string]any `json:"metadata"` | ||
} | ||
|
||
var _ IPolicy = (*Policy)(nil) | ||
|
||
func (p *Policy) MustCompile(extraOpts ...expr.Option) error { | ||
var err error | ||
|
||
extraOpts = append(p.opts, extraOpts...) | ||
|
||
p.prg, err = expr.Compile(p.Policy, extraOpts...) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (p *Policy) Eval(ctx context.Context, input Input) (bool, error) { | ||
output, err := expr.Run(p.prg, input) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
return cast.ToBool(output), nil | ||
} | ||
|
||
func MustNewPolicy( | ||
name string, policy string, metadata map[string]any, opts ...expr.Option, | ||
) *Policy { | ||
plc := Policy{ | ||
Name: name, | ||
Policy: policy, | ||
Metadata: metadata, | ||
opts: opts, | ||
} | ||
|
||
if err := plc.MustCompile(opts...); err != nil { | ||
return nil | ||
} | ||
|
||
return &plc | ||
} | ||
|
||
func NewPolicy( | ||
name string, policy string, metadata map[string]any, opts ...expr.Option, | ||
) (*Policy, error) { | ||
plc := &Policy{ | ||
Name: name, | ||
Policy: policy, | ||
Metadata: metadata, | ||
opts: opts, | ||
} | ||
|
||
if err := plc.MustCompile(opts...); err != nil { | ||
return nil, err | ||
} | ||
|
||
return plc, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package act | ||
|
||
type Signal struct { | ||
Name string `json:"name"` | ||
Metadata map[string]any `json:"metadata"` | ||
} | ||
|
||
func (s *Signal) ToMap() map[string]any { | ||
return map[string]any{ | ||
Name: s.Name, | ||
Metadata: s.Metadata, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters