ext-php-rs icon indicating copy to clipboard operation
ext-php-rs copied to clipboard

#[prop] attribute should rename fields to camelCase by default

Open ju1ius opened this issue 2 years ago • 2 comments

Currently, the #[prop] attribute does not handle case conversion, so we must resort to:

#[php_class]
struct Foo {
  #[prop(rename = "isBar")]
  pub is_bar: bool,
}

which can quickly become tedious.

The #[php_class] attribute should accept a rename_fields argument similiar to rename_methods for #[php_impl], and default to camelCase.

The #[prop(rename)] argument should be kept around for corner-cases like:

#[php_class]
struct Request {
  #[prop(rename = "isXMLHttpRequest")]
  pub is_xml_http_request: bool,
}

Although on might argue that it should be #[prop(name = ..)] and not #[prop(rename = ..)]...

For ergonomics, it could even be made into a global, project-wide, configuration option?

ju1ius avatar Nov 23 '22 03:11 ju1ius

Thinking about it, it should look more like :

#[php_class(field_case = "snake", method_case = "camel", constant_case = "upper")]
struct Foo;

This would ease the burden of verbose repeated configuration.

ju1ius avatar Nov 23 '22 03:11 ju1ius

Well I think you could simply rename your function and use #[allow(non_snake_case)] :)

Dennis-Zhang-SH avatar Nov 23 '22 13:11 Dennis-Zhang-SH