Skip to content

Commit

Permalink
ruby: add CEL example
Browse files Browse the repository at this point in the history
  • Loading branch information
satreix committed Jun 25, 2024
1 parent 1b2865a commit 46fafcd
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 15 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,21 @@ GEM
remote: https://rubygems.org/
specs:
ast (2.4.2)
bigdecimal (3.1.8)
cel (0.2.3)
cgi (0.3.6)
diff-lcs (1.5.1)
erb (4.0.4)
cgi (>= 0.3.3)
google-protobuf (4.27.1-arm64-darwin)
bigdecimal
rake (>= 13)
google-protobuf (4.27.1-x86_64-darwin)
bigdecimal
rake (>= 13)
google-protobuf (4.27.1-x86_64-linux)
bigdecimal
rake (>= 13)
haml (5.2.2)
temple (>= 0.8.0)
tilt
Expand All @@ -17,6 +28,7 @@ GEM
racc
racc (1.8.0)
rainbow (3.1.1)
rake (13.2.1)
regexp_parser (2.9.2)
rexml (3.3.0)
strscan
Expand Down Expand Up @@ -69,7 +81,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
Expand All @@ -78,4 +92,4 @@ DEPENDENCIES
slim (~> 5.0)

BUNDLED WITH
2.5.3
2.5.11
10 changes: 10 additions & 0 deletions src/ruby/cel/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
],
)
19 changes: 19 additions & 0 deletions src/ruby/cel/cel.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 46fafcd

Please sign in to comment.