Skip to content

[Feature] Implement string.contains() per CEL spec section 6.2.5 #180

@Quantum0uasar

Description

@Quantum0uasar

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions