examples

538 views
Skip to first unread message

ged...@gmail.com

unread,
Jun 4, 2018, 4:06:03 PM6/4/18
to CEL Go Discussion Forum
are there any examples in github ?

Tristan Swadell

unread,
Jun 4, 2018, 7:18:10 PM6/4/18
to ged...@gmail.com, CEL Go Discussion Forum
Hello there,

Apart from the README.md and the github.com/google/cel-spec there aren't a lot of examples at the moment. What would you like to see? I'd be happy to file an issue and update the docs.

-Tristan

On Mon, Jun 4, 2018 at 1:06 PM <ged...@gmail.com> wrote:
are there any examples in github ?

--
You received this message because you are subscribed to the Google Groups "CEL Go Discussion Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cel-go-discus...@googlegroups.com.
To post to this group, send email to cel-go-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cel-go-discuss/009bb2fb-c636-40cc-bf2c-8fb381b3f6cf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ged Wed

unread,
Jun 5, 2018, 6:26:26 AM6/5/18
to Tristan Swadell, CEL Go Discussion Forum
Hey,

I think super simple example demonstrating just basic basic stuff for now is good.

The intent is to see how to write code for it and how to compile it. CEL is pretty new concept since its a runtime interpreter and so basic example or two is enough for me to just see the big picture. Maybe a more advanced example would be a calculator ?

kpacha

unread,
Dec 14, 2018, 2:57:00 PM12/14/18
to CEL Go Discussion Forum
Hi all,

I'd love some examples regarding the timestamp type and its functions (getDayOfWeek and so on)

cheers

Tristan Swadell

unread,
Dec 18, 2018, 11:17:33 AM12/18/18
to kpacha, CEL Go Discussion Forum
Okay, so timestamp examples. I will hopefully find a bit more time to sit down and write actual decent docs for the various ways you can use CEL. For now, here's what I have ...

timestamp('YYYY-MM-DDTHH:MM:SSZ") will create a timestamp from a string. Most of the timestamps have helper functions that let you ask for different features of the timestamp (with or without a timezone). The timestamp format follows RFC3339 and may include a timezone, though the helper methods which accept a string based timezone may be more user friendly since these methods account for daylight savings. The timezone format is based on the IANA.org timezone database: https://data.iana.org/time-zones/releases. Though, I can't recall which exact version this would be, I can say it's one of those. 

-Tristan

kpacha

unread,
Dec 18, 2018, 5:01:42 PM12/18/18
to CEL Go Discussion Forum
Hi, Tristan

the constructor part was the easy one... the tricky one for me is being able to use the overloads. And I'm pretty sure I'm missing something very obvious... but when I add this test to interpreter/interpreter_test.go, I get the error commented at every variation (notice I tried some crazy combinations just in case I did not get the documentation right)

func TestInterpreter_TimestampDayOfTheWeek(t *testing.T) {
expression := "timestamp(now).getDayOfWeek()"     // no such overload
expression = "timestamp(now).getDayOfWeek('UTC')" // no such overload
// expression = `timestamp(now).getDayOfWeek`    // type 'well_known:TIMESTAMP ' does not support field selection
// expression = `getDayOfWeek(timestamp(now))`   // found no matching overload for 'getDayOfWeek' applied to '(timestamp)'
expr := compileExpr(t, expression, decls.NewIdent("now", decls.String, nil))
prog := NewCheckedProgram(expr)
eval := interpreter.NewInterpretable(prog)

for now, expectedResult := range map[string]int{
"2018-12-18T01:00:00Z": 2,
} {
result, _ := eval.Eval(NewActivation(map[string]interface{}{
"now": now,
}))
if v, ok := result.Value().(int); !ok || v != expectedResult {
t.Errorf("Got %v, wanted %v for expr %s with input %s", result.Value(), expectedResult, expression, now)
}
}
}

What's the proper way to access to the timestamp overloads?

Thanks

Tristan Swadell

unread,
Dec 18, 2018, 6:28:20 PM12/18/18
to kpacha, CEL Go Discussion Forum
Thanks for the test cases. The proper format is expression := "timestamp(now).getDayOfWeek()",but there was a bug in the function dispatch that was causing the function to be called but not executed. Should be fixed now.

kpacha

unread,
Dec 19, 2018, 2:48:22 PM12/19/18
to CEL Go Discussion Forum
it's perfect now. thank you very much!

Ethan Cheng

unread,
Nov 18, 2019, 5:40:32 PM11/18/19
to CEL Go Discussion Forum
Hi Tristan,
  I really would like to see some examples showing how to put protobuf message in the environment and do some expression evaluations.


On Monday, June 4, 2018 at 4:18:10 PM UTC-7, Tristan Swadell wrote:
Hello there,

Apart from the README.md and the github.com/google/cel-spec there aren't a lot of examples at the moment. What would you like to see? I'd be happy to file an issue and update the docs.

-Tristan

On Mon, Jun 4, 2018 at 1:06 PM <ged...@gmail.com> wrote:
are there any examples in github ?

--
You received this message because you are subscribed to the Google Groups "CEL Go Discussion Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cel-go-...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages