to_camel_case#

safir.pydantic.to_camel_case(string)#

Convert a string to camel case.

Intended for use with Pydantic as an alias generator so that the model can be initialized from camel-case input, such as Kubernetes objects or settings from Helm charts.

Parameters:

string (str) – Input string.

Returns:

String converted to camel-case with the first character in lowercase.

Return type:

str

Examples

To support camelCase input to a model, use the following settings:

class Model(BaseModel):
    some_field: str

    class Config:
        alias_generator = to_camel_case
        allow_population_by_field_name = True

This must be added to every class that uses snake_case for an attribute and that needs to be initialized from camelCase. If there are a lot of those classes, consider making a derivative class of BaseModel that sets these configuration options, and having all of your model classes inherit from it.