Implement string.contains() per CEL spec section 6.2.5

Open Beginner friendly
#180 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Quiet
Tech stack
python
Domain
backend

Research direction

Start in src/celpy/celtypes.py with StringType and then inspect the function dispatch table mentioned in the issue. Run the provided celpy expression to reproduce the missing overload. Done means .contains() returns true for a present substring and false otherwise, matching the CEL specification.

Written by the indexing model from the issue text.

Description

enhancement

Summary

The CEL specification (section 6.2.5) defines string.contains(string) -> bool as a standard string function. Currently, calling .contains() on a StringType raises CELEvalError: no such overload in cel-python.

Steps to reproduce

import celpy

env = celpy.Environment()
ast = env.compile('"hello world".contains("world")')
prog = env.program(ast)
result = prog.evaluate({})
print(result)  # Expected: true

Actual behaviour

celpy.celenv.CELEvalError: no such overload

Expected behaviour

contains() should return true when the substring is present and false otherwise, matching the Go and Java CEL implementations.

Proposed fix

Add a contains method to StringType in src/celpy/celtypes.py:

def contains(self, other: 'StringType') -> BoolType:
    return BoolType(str(other) in str(self))

And register the overload in the function dispatch table so that "x".contains("y") is correctly dispatched.

Related

Dominant language
Python
Stars
174
Forks
40
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from cloud-custodian/cel-python

All issues in cloud-custodian/cel-python

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.