Skip to content

Commit

Permalink
feat: added some duration cel funcs with tests (#12)
Browse files Browse the repository at this point in the history
* feat: added some duration cel funcs with tests

* chore: lint fixes

* feat: load other extensions

* chore: bumped cel-go and added more extensions
  • Loading branch information
adityathebe authored Jul 20, 2023
1 parent b1c2122 commit 19f81d0
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 126 deletions.
4 changes: 2 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ linters:
# - gci
# - gochecknoglobals
# - gochecknoinits
- gocognit
# - gocognit
- goconst
- gocritic
# - gocyclo
Expand All @@ -63,7 +63,7 @@ linters:
# - lll
- misspell
- nakedret
- nestif
# - nestif
# - nlreturn
# - noctx
# - nolintlint
Expand Down
10 changes: 5 additions & 5 deletions funcs/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,19 @@ func CreateStringFuncs(ctx context.Context) map[string]interface{} {
return f
}

func (ns StringFuncs) HumanDuration(in interface{}) (string, error) {
func (f StringFuncs) HumanDuration(in interface{}) (string, error) {
return gompstrings.HumanDuration(in), nil
}

func (ns StringFuncs) HumanSize(in interface{}) (string, error) {
func (f StringFuncs) HumanSize(in interface{}) (string, error) {
return gompstrings.HumanBytes(in), nil
}

func (ns StringFuncs) Semver(in string) (*semver.Version, error) {
func (f StringFuncs) Semver(in string) (*semver.Version, error) {
return gompstrings.Semver(in)
}

func (ns StringFuncs) SemverMap(in string) (map[string]string, error) {
func (f StringFuncs) SemverMap(in string) (map[string]string, error) {
v, err := gompstrings.Semver(in)
if err != nil {
return nil, err
Expand All @@ -102,7 +102,7 @@ func (ns StringFuncs) SemverMap(in string) (map[string]string, error) {
return res, nil
}

func (ns StringFuncs) SemverCompare(v1, v2 string) (bool, error) {
func (f StringFuncs) SemverCompare(v1, v2 string) (bool, error) {
return gompstrings.SemverCompare(v1, v2)
}

Expand Down
14 changes: 4 additions & 10 deletions funcs/strings_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/Masterminds/goutils v1.1.1
github.com/Masterminds/semver/v3 v3.2.1
github.com/flanksource/is-healthy v0.0.0-20230705092916-3b4cf510c5fc
github.com/google/cel-go v0.16.0
github.com/google/cel-go v0.17.1
github.com/google/uuid v1.3.0
github.com/gosimple/slug v1.13.1
github.com/hairyhenderson/toml v0.4.2-0.20210923231440-40456b8e66cf
Expand Down
13 changes: 13 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ github.com/Masterminds/semver/v3 v3.2.1 h1:RN9w6+7QoMeJVGyfmbcgs28Br8cvmnucEXnY0
github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230305170008-8188dc5388df h1:7RFfzj4SSt6nnvCPbCqijJi1nWCd+TqAT3bYCStRC18=
github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230305170008-8188dc5388df/go.mod h1:pSwJ0fSY5KhvocuWSx4fz3BA8OrA1bQn+K1Eli3BRwM=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/elazarl/goproxy v0.0.0-20180725130230-947c36da3153/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc=
github.com/evanphx/json-patch v4.12.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/flanksource/is-healthy v0.0.0-20230705092916-3b4cf510c5fc h1:CPUNUw2pHnlF4ucBHx44vLTcCa4FlEEu6PkNo5rCvD4=
github.com/flanksource/is-healthy v0.0.0-20230705092916-3b4cf510c5fc/go.mod h1:4pQhmF+TnVqJroQKY8wSnSp+T18oLson6YQ2M0qPHfQ=
github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
Expand All @@ -19,6 +22,9 @@ github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/cel-go v0.16.0 h1:DG9YQ8nFCFXAs/FDDwBxmL1tpKNrdlGUM9U3537bX/Y=
github.com/google/cel-go v0.16.0/go.mod h1:HXZKzB0LXqer5lHHgfWAnlYwJaQBDKMjxjulNQzhwhY=
github.com/google/cel-go v0.17.1 h1:s2151PDGy/eqpCI80/8dl4VL3xTkqI/YubXLXCFw0mw=
github.com/google/cel-go v0.17.1/go.mod h1:HXZKzB0LXqer5lHHgfWAnlYwJaQBDKMjxjulNQzhwhY=
github.com/google/gnostic v0.5.7-v3refs/go.mod h1:73MKFl6jIHelAJNaBGFzt3SPtZULs9dYrGFt8OiIsHQ=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
Expand All @@ -44,22 +50,28 @@ github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHm
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/moby/spdystream v0.2.0/go.mod h1:f7i0iNDQJ059oMTcWxx8MA/zKFIuD/lY+0GqbN2Wy8c=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+o7JKHSa8/e818NopupXU1YMK5fe1lsApnBw=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/robertkrimen/otto v0.2.1 h1:FVP0PJ0AHIjC+N4pKCG9yCDz6LHNPCwi/GKID5pGGF0=
github.com/robertkrimen/otto v0.2.1/go.mod h1:UPwtJ1Xu7JrLcZjNWN8orJaM5n5YEtqL//farB5FlRY=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stoewer/go-strcase v1.2.0 h1:Z2iHWqGXH00XYgqDmNgQbIBxf3wrNq0F3feEy0ainaU=
github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag1KpM8ahLw8=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
Expand Down Expand Up @@ -119,6 +131,7 @@ google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cn
google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
gopkg.in/sourcemap.v1 v1.0.5 h1:inv58fC9f9J3TK2Y2R1NPntXEn3/wjWHkonhIUODNTI=
Expand Down
12 changes: 6 additions & 6 deletions strings/bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,22 +86,22 @@ func HumanBytes(size interface{}) string {
switch {
case bytes >= EXABYTE:
unit = "E"
value = value / EXABYTE
value /= EXABYTE
case bytes >= PETABYTE:
unit = "P"
value = value / PETABYTE
value /= PETABYTE
case bytes >= TERABYTE:
unit = "T"
value = value / TERABYTE
value /= TERABYTE
case bytes >= GIGABYTE:
unit = "G"
value = value / GIGABYTE
value /= GIGABYTE
case bytes >= MEGABYTE:
unit = "M"
value = value / MEGABYTE
value /= MEGABYTE
case bytes >= KILOBYTE:
unit = "K"
value = value / KILOBYTE
value /= KILOBYTE
case bytes >= BYTE:
unit = "B"
case bytes == 0:
Expand Down
10 changes: 3 additions & 7 deletions strings/cel_gen_exports.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 14 additions & 1 deletion strings/duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"errors"
"strings"
"time"

"github.com/google/cel-go/common/types"
)

// Duration is a standard unit of time.
Expand All @@ -38,6 +40,7 @@ func (d Duration) String() string {
u = -u
}

//nolint:gocritic
if u < uint64(Second) {
// Special case: if duration is smaller than a second,
// use smaller units, like 1.2ms
Expand Down Expand Up @@ -407,11 +410,13 @@ func parseDuration(s string) (Duration, error) {
func HumanDuration(duration interface{}) string {
switch v := duration.(type) {
case int64:
return Duration(time.Duration(int64(v))).String()
return Duration(time.Duration(v)).String()
case float64:
return Duration(time.Duration(int64(v))).String()
case time.Duration:
return Duration(v).String()
case types.Duration:
return Duration(v.Duration).String()
}
return ""
}
Expand All @@ -424,3 +429,11 @@ func ParseDuration(val string) (*time.Duration, error) {
t := time.Duration(d)
return &t, err
}

func Age(s string) time.Duration {
t, err := time.Parse(time.RFC3339, s)
if err != nil {
panic(err)
}
return time.Since(t)
}
125 changes: 31 additions & 94 deletions strings/duration_gen.go
Original file line number Diff line number Diff line change
@@ -1,105 +1,42 @@
// Code generated by gencel. DO NOT EDIT.

package strings

import "github.com/google/cel-go/cel"
import "github.com/google/cel-go/common/types"
import "github.com/google/cel-go/common/types/ref"

var durationStringGen = cel.Function("duration.String",
cel.Overload("duration.String_",
nil,
cel.StringType,
cel.FunctionBinding(func(args ...ref.Val) ref.Val {

var x Duration

return types.DefaultTypeAdapter.NativeToValue(x.String())

}),
),
import (
"github.com/google/cel-go/cel"
"github.com/google/cel-go/common/types"
"github.com/google/cel-go/common/types/ref"
)

var durationNanosecondsGen = cel.Function("duration.Nanoseconds",
cel.Overload("duration.Nanoseconds_",
nil,
cel.IntType,
cel.FunctionBinding(func(args ...ref.Val) ref.Val {

var x Duration

return types.DefaultTypeAdapter.NativeToValue(x.Nanoseconds())

// var durationHumanize = cel.Function("HumanDuration",
// cel.Overload("duration.HumanDuration",
// []*cel.Type{cel.DynType},
// cel.StringType,
// cel.UnaryBinding(func(value ref.Val) ref.Val {
// return types.String(HumanDuration(value.Value()))
// }),
// ),
// )

var durationAgeGen = cel.Function("Age",
cel.Overload("duration.Age",
[]*cel.Type{cel.StringType},
cel.DurationType,
cel.UnaryBinding(func(value ref.Val) ref.Val {
a := Age(value.Value().(string))
return types.Duration{Duration: a}
}),
),
)

var durationSecondsGen = cel.Function("duration.Seconds",
cel.Overload("duration.Seconds_",
nil,
cel.DoubleType,
cel.FunctionBinding(func(args ...ref.Val) ref.Val {

var x Duration

return types.DefaultTypeAdapter.NativeToValue(x.Seconds())

}),
),
)

var durationHoursGen = cel.Function("duration.Hours",
cel.Overload("duration.Hours_",
nil,
cel.DoubleType,
cel.FunctionBinding(func(args ...ref.Val) ref.Val {

var x Duration

return types.DefaultTypeAdapter.NativeToValue(x.Hours())

}),
),
)

var durationDaysGen = cel.Function("duration.Days",
cel.Overload("duration.Days_",
nil,
cel.DoubleType,
cel.FunctionBinding(func(args ...ref.Val) ref.Val {

var x Duration

return types.DefaultTypeAdapter.NativeToValue(x.Days())

}),
),
)

var durationWeeksGen = cel.Function("duration.Weeks",
cel.Overload("duration.Weeks_",
nil,
cel.DoubleType,
cel.FunctionBinding(func(args ...ref.Val) ref.Val {

var x Duration

return types.DefaultTypeAdapter.NativeToValue(x.Weeks())

}),
),
)

var durationMinutesGen = cel.Function("duration.Minutes",
cel.Overload("duration.Minutes_",
nil,
cel.DoubleType,
cel.FunctionBinding(func(args ...ref.Val) ref.Val {

var x Duration

return types.DefaultTypeAdapter.NativeToValue(x.Minutes())

var durationDurationGen = cel.Function("Duration",
cel.Overload("duration.Duration",
[]*cel.Type{cel.StringType},
cel.DurationType,
cel.UnaryBinding(func(value ref.Val) ref.Val {
a, err := ParseDuration(value.Value().(string))
if err != nil || a == nil {
return types.Duration{}
}
return types.Duration{Duration: *a}
}),
),
)
8 changes: 8 additions & 0 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import (

"github.com/flanksource/gomplate/v3/funcs"
_ "github.com/flanksource/gomplate/v3/js"
pkgStrings "github.com/flanksource/gomplate/v3/strings"
"github.com/google/cel-go/cel"
"github.com/google/cel-go/ext"
"github.com/robertkrimen/otto"
"github.com/robertkrimen/otto/registry"
_ "github.com/robertkrimen/otto/underscore"
Expand Down Expand Up @@ -81,6 +83,12 @@ func RunTemplate(environment map[string]any, template Template) (string, error)
// cel-go
if template.Expression != "" {
var opts = funcs.CelEnvOption
opts = append(opts, pkgStrings.CelEnvOption...)

// load other cel-go extensions that aren't available by default
extensions := []cel.EnvOption{ext.Math(), ext.Encoders(), ext.Strings(), ext.Sets(), ext.Lists()}
opts = append(opts, extensions...)

for k := range environment {
opts = append(opts, cel.Variable(k, cel.AnyType))
}
Expand Down
Loading

0 comments on commit 19f81d0

Please sign in to comment.