Skip to content

Commit

Permalink
feat: make enums act as proper strings in Expr-lang scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
jippi committed Sep 3, 2024
1 parent 38d3075 commit 7a327e7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
27 changes: 27 additions & 0 deletions pkg/scm/gitlab/context_valuers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package gitlab

// Extending the EvalContext with Expr-lang "valuer" interface support
// allowing custom types to act as native Expr-lang types
//
// See: https://github.com/expr-lang/expr/blob/master/patcher/value/value.go
// See: pkg/stdlib/stdlib.go

// MergeRequestState is a ENUM type
func (d MergeRequestState) AsString() string {
return d.String()
}

// UserState is a ENUM type
func (d UserState) AsString() string {
return d.String()
}

// MergeStatus is a ENUM type
func (d MergeStatus) AsString() string {
return d.String()
}

// DetailedMergeStatus is a ENUM type
func (d DetailedMergeStatus) AsString() string {
return d.String()
}
8 changes: 8 additions & 0 deletions pkg/stdlib/stdlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@ package stdlib

import (
"github.com/expr-lang/expr"
"github.com/expr-lang/expr/patcher/value"
)

var Functions = []expr.Option{
// Replace built-in duration function with one that supports "d" (days) and "w" (weeks)
expr.DisableBuiltin("duration"),

// Add Expr-lang support for a wider range of "valuers" for custom types, such as
//
// - "AsString()" interface for custom types wanting to be used as a String (useful for Enum types!)
// - "AsBool()" interface for custom types wanting to be used as a boolean comparison
value.ValueGetter,

Duration,
Since,

Expand Down

0 comments on commit 7a327e7

Please sign in to comment.