-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[FEATURE] @FieldNameConstants add custom generation strategy for fieldname function #2157
Comments
@SummerXSH I'm rather sure, you can't. As Lombok runs during compilation, it can't rely on your In case you're asking if Lombok provides such strategies, the answer is no as well. Nonetheless, there may be a way: Use
I haven't tried this. This way you won't get compile-time constants, actually, you wan't get any constants. In case it works, you could avoid code repetitions with
and default methods in your interface. I haven't tried this either. |
Thanks for your answer,I understand what you said.If can't customize generate strategy, can provide a choice for generate field name ? @FieldNameConstants(type = "UpToUnderline")
@Data
public class Test {
private Integer userId;
private String userName;
private Date birthday;
public static class Fields {
public static String userId = "user_id";
public static String userName = "user_name";
public static String birthday = "birthday";
}
} Or at lombok.config file configuration just like |
@SummerXSH This is a working example printing import java.time.LocalDate;
import lombok.Data;
import lombok.experimental.FieldNameConstants;
import com.google.common.base.CaseFormat;
@FieldNameConstants(asEnum=true)
@Data
public class SummerXsh {
private Integer userId;
private String userName;
private LocalDate birthday; // much better than `Date`
public enum Fields {
; // This is necessary!
public String underlinedName() {
// https://guava.dev/releases/snapshot-jre/api/docs/com/google/common/base/CaseFormat.html
return CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, name());
// or your own implementation
}
}
public static void main(String[] args) {
System.out.println(Fields.userName.underlinedName());
}
} However, the docs says literally
which most probably does exactly what you want. It needs v1.18.8, which I don't have yet, so try it yourself. Please format your post using three backticks like this:
This will be rendered as: class Foo {
// formatted java code
} |
Your example need add unnecessary code to each required class。I think it is not good. I think |
@SummerXSH, can you explain how you would use this? I mean, I know most mapping frameworks have already a way to map field naming strategies, and most databases don't care about the casing. It is not obvious to me that being able to generate lowercase-with_underscores would reduce a lot of boilerplate. And having it configurable does increase the maintenance load. |
I have a similar case where I would like to have custom naming: @FieldNameConstants(asEnum = true)
public class User {
@JsonProperty("_id")
private String id;
private String name;
// The generated enum
public enum Fields {
_id,
name
}
} Ideally to make it more generic, it would be nice to have a separate field annotation e.g. |
In addition add another string containing the original camel-case name (at least to enum). Otherwise there is no simple mapping from |
Now
@FieldNameConstants
generate field name iscan add custom generate strategy function for field name ?
From example :
"UpToUnderline" this way is friendly for mapping of DB field .
The text was updated successfully, but these errors were encountered: