From 10d253660a46408f8e3f98b83ea0620955540054 Mon Sep 17 00:00:00 2001 From: satreix Date: Sun, 16 Jun 2024 15:38:14 +0100 Subject: [PATCH] ruby: add CEL example --- .ruby-version | 2 +- Gemfile | 4 +++- Gemfile.lock | 26 +++++++++++++++++++++++++- MODULE.bazel | 10 ++++++++++ MODULE.bazel.lock | 12 +++++++++++- src/ruby/cel/BUILD.bazel | 10 ++++++++++ src/ruby/cel/cel.rb | 19 +++++++++++++++++++ 7 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 src/ruby/cel/BUILD.bazel create mode 100644 src/ruby/cel/cel.rb diff --git a/.ruby-version b/.ruby-version index dac625a8..0117b3e1 100644 --- a/.ruby-version +++ b/.ruby-version @@ -1 +1 @@ -jruby-9.4.8.0 +jruby-9.4.7.0 diff --git a/Gemfile b/Gemfile index a2b796db..076eb30f 100644 --- a/Gemfile +++ b/Gemfile @@ -2,10 +2,12 @@ source 'https://rubygems.org' +gem 'cel', '~> 0.2.3' gem 'erb', '~> 4.0' +gem 'google-protobuf', '~> 4.27' gem 'haml', '~> 5.2' -gem 'rspec-its' gem 'rspec', '~> 3.13.0' +gem 'rspec-its' gem 'slim', '~> 5.0' gem 'rubocop', require: false diff --git a/Gemfile.lock b/Gemfile.lock index 4c4504b4..485d3fb2 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,6 +2,9 @@ GEM remote: https://rubygems.org/ specs: ast (2.4.2) + bigdecimal (3.1.8) + bigdecimal (3.1.8-java) + cel (0.2.3) cgi (0.3.6) cgi (0.3.6-java) diff-lcs (1.5.1) @@ -9,6 +12,24 @@ GEM cgi (>= 0.3.3) erb (4.0.4-java) cgi (>= 0.3.3) + ffi (1.17.0-java) + ffi-compiler (1.3.2) + ffi (>= 1.15.5) + rake + google-protobuf (4.27.2-arm64-darwin) + bigdecimal + rake (>= 13) + google-protobuf (4.27.2-java) + bigdecimal + ffi (~> 1) + ffi-compiler (~> 1) + rake (>= 13) + google-protobuf (4.27.2-x86_64-darwin) + bigdecimal + rake (>= 13) + google-protobuf (4.27.2-x86_64-linux) + bigdecimal + rake (>= 13) haml (5.2.2) temple (>= 0.8.0) tilt @@ -22,6 +43,7 @@ GEM racc (1.8.0) racc (1.8.0-java) rainbow (3.1.1) + rake (13.2.1) regexp_parser (2.9.2) rexml (3.3.1) strscan @@ -76,7 +98,9 @@ PLATFORMS x86_64-linux DEPENDENCIES + cel (~> 0.2.3) erb (~> 4.0) + google-protobuf (~> 4.27) haml (~> 5.2) rspec (~> 3.13.0) rspec-its @@ -85,4 +109,4 @@ DEPENDENCIES slim (~> 5.0) BUNDLED WITH - 2.5.3 + 2.5.15 diff --git a/MODULE.bazel b/MODULE.bazel index 1b233603..09b1e1b0 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -44,11 +44,20 @@ ruby.bundle_fetch( name = "bundle", gem_checksums = { "ast-2.4.2": "1e280232e6a33754cde542bc5ef85520b74db2aac73ec14acef453784447cc12", + "bigdecimal-3.1.8": "a89467ed5a44f8ae01824af49cbc575871fa078332e8f77ea425725c1ffe27be", + "bigdecimal-3.1.8-java": "b9e94c14623fff8575f17a10320852219bbba92ecff4977571503d942687326e", + "cel-0.2.3": "eacd66662224b688fa9d4a169d9c8ed292edfb487b040dd6a2ab34f246abaa77", "cgi-0.3.6": "c5e2c301437116b28fdfbb565452b9c1ee597901e68611367b4126b5d10feca3", "cgi-0.3.6-java": "f94886a613c101f2f60c2e33ea8a42d6ad0b9a5fcda3d550e01f35b0271585af", "diff-lcs-1.5.1": "273223dfb40685548436d32b4733aa67351769c7dea621da7d9dd4813e63ddfe", "erb-4.0.4": "de116e106205c46bc01918789b611aaad1328dcc6e9f12cf8cd2cc60ef619717", "erb-4.0.4-java": "3767088f230194a84316a5879f0c3dc5f2a96fb1c9a198c3e27d3f35d416c597", + "ffi-1.17.0-java": "f65f022616970fcde83fe176393eb873a7b959ef2703f94931cf0af6ab55ec7a", + "ffi-compiler-1.3.2": "a94f3d81d12caf5c5d4ecf13980a70d0aeaa72268f3b9cc13358bcc6509184a0", + "google-protobuf-4.27.2-arm64-darwin": "36cbd6620ef822f8a93870cc837212b302cbbdd7c900f8fa6701c735b7ff7f97", + "google-protobuf-4.27.2-java": "e70eda54bf6bcd659c4e685c91cb7f00b7f4b3675467eca085a8409c8f4dfe53", + "google-protobuf-4.27.2-x86_64-darwin": "689f921050489d3cc2537c48bee98047414ad099bb980538562955a3bc9fdced", + "google-protobuf-4.27.2-x86_64-linux": "126183186de79f709a0792f31eddf1fcebbfb5ded29538d184ece8fae73148c2", "haml-5.2.2": "6e759246556145642ef832d670fc06f9bd8539159a0e600847a00291dd7aae0c", "json-2.7.2": "1898b5cbc81cd36c0fd4d0b7ad2682c39fb07c5ff682fc6265f678f550d4982c", "json-2.7.2-java": "138e3038b5361b3d06ee2e8aa2be00bed0d0de4ef5f1553fc5935e5b93aca7ee", @@ -58,6 +67,7 @@ ruby.bundle_fetch( "racc-1.8.0": "09349a65c37c4fe710a435f25c9f1652e39f29ad6b1fa08d4a8d30c0553d3a08", "racc-1.8.0-java": "3688448632150682bbd708b199961129aed2266107e26fcacb45c997395c19f9", "rainbow-3.1.1": "039491aa3a89f42efa1d6dec2fc4e62ede96eb6acd95e52f1ad581182b79bc6a", + "rake-13.2.1": "46cb38dae65d7d74b6020a4ac9d48afed8eb8149c040eccf0523bec91907059d", "regexp_parser-2.9.2": "5a27e767ad634f8a4b544520d5cd28a0db7aa1198a5d7c9d7e11d7b3d9066446", "rexml-3.3.1": "34af9fb38eff6c451abd187c53fded98378aa91766d4c62fbbce10e40ed7c325", "rspec-3.13.0": "d490914ac1d5a5a64a0e1400c1d54ddd2a501324d703b8cfe83f458337bab993", diff --git a/MODULE.bazel.lock b/MODULE.bazel.lock index fd7a4ac1..63eb573f 100644 --- a/MODULE.bazel.lock +++ b/MODULE.bazel.lock @@ -2776,7 +2776,7 @@ "@@rules_ruby~//ruby:extensions.bzl%ruby": { "general": { "bzlTransitiveDigest": "j8Z4sRl//8oTbAI/GgSSNRDJQErEPVfHpzHmEOm6cZ4=", - "usagesDigest": "bDOwPYVGMiP+q9UxsDj4jEFfAY36LJKJ4gantER3qkY=", + "usagesDigest": "qdo4kx/vcr6NMkBEhTaXQSTv8uiRra8odknbEc6jP/I=", "recordedFileInputs": {}, "recordedDirentsInputs": {}, "envVariables": {}, @@ -2799,11 +2799,20 @@ "gemfile_lock": "@@//:Gemfile.lock", "gem_checksums": { "ast-2.4.2": "1e280232e6a33754cde542bc5ef85520b74db2aac73ec14acef453784447cc12", + "bigdecimal-3.1.8": "a89467ed5a44f8ae01824af49cbc575871fa078332e8f77ea425725c1ffe27be", + "bigdecimal-3.1.8-java": "b9e94c14623fff8575f17a10320852219bbba92ecff4977571503d942687326e", + "cel-0.2.3": "eacd66662224b688fa9d4a169d9c8ed292edfb487b040dd6a2ab34f246abaa77", "cgi-0.3.6": "c5e2c301437116b28fdfbb565452b9c1ee597901e68611367b4126b5d10feca3", "cgi-0.3.6-java": "f94886a613c101f2f60c2e33ea8a42d6ad0b9a5fcda3d550e01f35b0271585af", "diff-lcs-1.5.1": "273223dfb40685548436d32b4733aa67351769c7dea621da7d9dd4813e63ddfe", "erb-4.0.4": "de116e106205c46bc01918789b611aaad1328dcc6e9f12cf8cd2cc60ef619717", "erb-4.0.4-java": "3767088f230194a84316a5879f0c3dc5f2a96fb1c9a198c3e27d3f35d416c597", + "ffi-1.17.0-java": "f65f022616970fcde83fe176393eb873a7b959ef2703f94931cf0af6ab55ec7a", + "ffi-compiler-1.3.2": "a94f3d81d12caf5c5d4ecf13980a70d0aeaa72268f3b9cc13358bcc6509184a0", + "google-protobuf-4.27.2-arm64-darwin": "36cbd6620ef822f8a93870cc837212b302cbbdd7c900f8fa6701c735b7ff7f97", + "google-protobuf-4.27.2-java": "e70eda54bf6bcd659c4e685c91cb7f00b7f4b3675467eca085a8409c8f4dfe53", + "google-protobuf-4.27.2-x86_64-darwin": "689f921050489d3cc2537c48bee98047414ad099bb980538562955a3bc9fdced", + "google-protobuf-4.27.2-x86_64-linux": "126183186de79f709a0792f31eddf1fcebbfb5ded29538d184ece8fae73148c2", "haml-5.2.2": "6e759246556145642ef832d670fc06f9bd8539159a0e600847a00291dd7aae0c", "json-2.7.2": "1898b5cbc81cd36c0fd4d0b7ad2682c39fb07c5ff682fc6265f678f550d4982c", "json-2.7.2-java": "138e3038b5361b3d06ee2e8aa2be00bed0d0de4ef5f1553fc5935e5b93aca7ee", @@ -2813,6 +2822,7 @@ "racc-1.8.0": "09349a65c37c4fe710a435f25c9f1652e39f29ad6b1fa08d4a8d30c0553d3a08", "racc-1.8.0-java": "3688448632150682bbd708b199961129aed2266107e26fcacb45c997395c19f9", "rainbow-3.1.1": "039491aa3a89f42efa1d6dec2fc4e62ede96eb6acd95e52f1ad581182b79bc6a", + "rake-13.2.1": "46cb38dae65d7d74b6020a4ac9d48afed8eb8149c040eccf0523bec91907059d", "regexp_parser-2.9.2": "5a27e767ad634f8a4b544520d5cd28a0db7aa1198a5d7c9d7e11d7b3d9066446", "rexml-3.3.1": "34af9fb38eff6c451abd187c53fded98378aa91766d4c62fbbce10e40ed7c325", "rspec-3.13.0": "d490914ac1d5a5a64a0e1400c1d54ddd2a501324d703b8cfe83f458337bab993", diff --git a/src/ruby/cel/BUILD.bazel b/src/ruby/cel/BUILD.bazel new file mode 100644 index 00000000..33e7c60b --- /dev/null +++ b/src/ruby/cel/BUILD.bazel @@ -0,0 +1,10 @@ +load("@rules_ruby//ruby:defs.bzl", "rb_binary", "rb_test") + +rb_binary( + name = "cel", + srcs = ["cel.rb"], + main = "cel.rb", + deps = [ + "@bundle", + ], +) diff --git a/src/ruby/cel/cel.rb b/src/ruby/cel/cel.rb new file mode 100644 index 00000000..83fefe8b --- /dev/null +++ b/src/ruby/cel/cel.rb @@ -0,0 +1,19 @@ +require "cel" + +env = Cel::Environment.new(name: :string, group: :string) + +begin + ast = env.compile('name.startsWith("/groups/" + group)') #=> Cel::Types[:bool], which is == :bool +rescue Cel::Error => e + STDERR.puts("type-check error: #{e.message}") + raise e +end + +prg = env.program(ast) + +return_value = prg.evaluate( + name: Cel::String.new("/groups/acme.co/documents/secret-stuff"), + group: Cel::String.new("acme.co"), +) + +puts return_value #=> true