From a30d805f9b0d5ffd8673c82fb373d812846dd3d6 Mon Sep 17 00:00:00 2001 From: Elara Musayelyan Date: Thu, 22 Jun 2023 14:53:42 -0700 Subject: [PATCH] Add utils.ver_cmp() builtin --- go.mod | 2 ++ go.sum | 4 ++++ internal/builtins/reader.go | 2 +- internal/builtins/register.go | 1 + internal/builtins/utils.go | 23 +++++++++++++++++++++++ 5 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 internal/builtins/utils.go diff --git a/go.mod b/go.mod index 1efcc90..b6ecb81 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( github.com/vmihailenco/msgpack/v5 v5.3.5 go.elara.ws/logger v0.0.0-20230421022458-e80700db2090 go.elara.ws/pcre v0.0.0-20230421030233-daf2d2e6973f + go.elara.ws/vercmp v0.0.0-20230622214216-0b2b067575c4 go.etcd.io/bbolt v1.3.7 go.starlark.net v0.0.0-20230525235612-a134d8f9ddca golang.org/x/crypto v0.9.0 @@ -38,6 +39,7 @@ require ( github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect github.com/xanzy/ssh-agent v0.3.3 // indirect github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect + golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 // indirect golang.org/x/net v0.10.0 // indirect golang.org/x/sys v0.8.0 // indirect gopkg.in/warnings.v0 v0.1.2 // indirect diff --git a/go.sum b/go.sum index 9269ced..925bddd 100644 --- a/go.sum +++ b/go.sum @@ -124,6 +124,8 @@ go.elara.ws/logger v0.0.0-20230421022458-e80700db2090 h1:RVC8XvWo6Yw4HUshqx4TSzu go.elara.ws/logger v0.0.0-20230421022458-e80700db2090/go.mod h1:qng49owViqsW5Aey93lwBXONw20oGbJIoLVscB16mPM= go.elara.ws/pcre v0.0.0-20230421030233-daf2d2e6973f h1:ZwR0xvBeP5BHHv63fgfuwhZIj+Si5rp79WSDUE73ZVA= go.elara.ws/pcre v0.0.0-20230421030233-daf2d2e6973f/go.mod h1:EF48C6VnP4wBayzFGk6lXqbiLucH7EfiaYOgiiCe5k4= +go.elara.ws/vercmp v0.0.0-20230622213404-1ef50f776de8 h1:Q4fiNpQH1qOlPUVt/cVoW3VtIvOe+jo7mFTX0Sor7O4= +go.elara.ws/vercmp v0.0.0-20230622213404-1ef50f776de8/go.mod h1:/7PNW7nFnDR5W7UXZVc04gdVLR/wBNgkm33KgIz0OBk= go.etcd.io/bbolt v1.3.7 h1:j+zJOnnEjF/kyHlDDgGnVL/AIqIJPq8UoB2GSNfkUfQ= go.etcd.io/bbolt v1.3.7/go.mod h1:N9Mkw9X8x5fupy0IKsmuqVtoGDyxsaDlbk4Rd05IAQw= go.starlark.net v0.0.0-20230525235612-a134d8f9ddca h1:VdD38733bfYv5tUZwEIskMM93VanwNIi5bIKnDrJdEY= @@ -137,6 +139,8 @@ golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g= golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 h1:k/i9J1pBpvlfR+9QsetwPyERsqu1GIbi967PQMq3Ivc= +golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= diff --git a/internal/builtins/reader.go b/internal/builtins/reader.go index aab96ba..ee05757 100644 --- a/internal/builtins/reader.go +++ b/internal/builtins/reader.go @@ -34,7 +34,7 @@ func newStarlarkReader(r io.Reader) starlarkReader { "read_all": starlark.NewBuiltin("reader.read_all", sr.readAll), "read_all_string": starlark.NewBuiltin("reader.read_all_string", sr.readAllString), "read_json": starlark.NewBuiltin("reader.read_json", sr.readJSON), - "read_msgpack": starlark.NewBuiltin("read_msgpack", sr.readMsgpack), + "read_msgpack": starlark.NewBuiltin("reader.read_msgpack", sr.readMsgpack), "close": starlark.NewBuiltin("reader.close", sr.closeReader), }) diff --git a/internal/builtins/register.go b/internal/builtins/register.go index 76408eb..11bca43 100644 --- a/internal/builtins/register.go +++ b/internal/builtins/register.go @@ -43,5 +43,6 @@ func Register(sd starlark.StringDict, opts *Options) { sd["updater"] = updaterModule(opts.Config) sd["log"] = logModule(opts.Name) sd["json"] = starlarkjson.Module + sd["utils"] = utilsModule sd["register_webhook"] = registerWebhook(opts.Mux, opts.Config, opts.Name) } diff --git a/internal/builtins/utils.go b/internal/builtins/utils.go new file mode 100644 index 0000000..d0c4588 --- /dev/null +++ b/internal/builtins/utils.go @@ -0,0 +1,23 @@ +package builtins + +import ( + "go.elara.ws/vercmp" + "go.starlark.net/starlark" + "go.starlark.net/starlarkstruct" +) + +var utilsModule = &starlarkstruct.Module{ + Name: "utils", + Members: starlark.StringDict{ + "ver_cmp": starlark.NewBuiltin("utils.ver_cmp", utilsVerCmp), + }, +} + +func utilsVerCmp(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { + var v1, v2 string + err := starlark.UnpackArgs("utils.ver_cmp", args, kwargs, "v1", &v1, "v2", &v2) + if err != nil { + return nil, err + } + return starlark.MakeInt(vercmp.Compare(v1, v2)), nil +}