Skip to content
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: allow users change their property name style #359

Open
peterstev-fm opened this issue May 29, 2021 · 12 comments
Open

Feature: allow users change their property name style #359

peterstev-fm opened this issue May 29, 2021 · 12 comments
Assignees

Comments

@peterstev-fm
Copy link

Allow users to determine what style they want their property naming to follow.

Camel case
raw json property names (as is)
maybe both camelCase and raw types if possible

for example I want my class properties to be defined as

data class JsonLogin(
    val id: Int,
    val token: String,
    val user_avatar: String,
    val user_display_name: String,
    val user_email: String,
    val user_nicename: String
)

instead of

data class Login(
    val id: Int,
    val token: String,
    val userAvatar: String,
    val userDisplayName: String,
    val userEmail: String,
    val userNicename: String
)

so I do not have to explicitly change every generated property name to suite my style, because if I have to do all that I might as well define the class manually myself.

@wuseal
Copy link
Owner

wuseal commented May 29, 2021

Hi.if you choose any annotation in annotation tab in advanced config. It will generate properties' name as camelcase

@wuseal
Copy link
Owner

wuseal commented May 29, 2021

@peterstev-fm for your case, refer here : #17

@peterstev-fm
Copy link
Author

peterstev-fm commented May 29, 2021

that is the point, I do not want camelCase, I want to use snake_case in my data layer and camelCase in domain.

data layer: 
data class JsonLogin(
    val id: Int,
    val token: String,
    val user_avatar: String,
    val user_display_name: String,
    val user_email: String,
    val user_nicename: String
)

domain layer:
data class Login(
    val id: Int,
    val token: String,
    val userAvatar: String,
    val userDisplayName: String,
    val userEmail: String,
    val userNicename: String
)

with with this, I would not need to annotate every field with @JSON("field_name") PS I use converter moshi. I also use mappers to map my data layer objects to the domain equivalent.

so I need to be able to config the generator to use snake_case or camelCase

@wuseal
Copy link
Owner

wuseal commented May 29, 2021

Hey, is that your json is in snake_case style? If it is true, the default behavior this plugin does Will generate your data class as snake case style, And with selecting 'None(came case)' config. The data class Will generate as came case style. With these action, you could switch to snake case or camel case style. @peterstev-fm

Is that enough for you?

@peterstev-fm
Copy link
Author

peterstev-fm commented May 29, 2021

oh I see, it does that, but I use 'other by customize' because I want to set my class like this:

import com.squareup.moshi.JsonClass

@JsonClass(generateAdapter = true)
data class JsonUser(
    val Id: Int,
    val token: String,
    val user_avatar: String,
    val user_display_name: String,
    val user_email: String,
    val user_nicename: String
)

I use 'other by customize' because I want to add imports for the @JsonClass annotation for moshi, because if I select moshi directly from the options list, it adds the @JSON("field_name") which I do not want since I am using snake_case

so how can I use 'other by customize' and still use snake_case. Thanks

@wuseal
Copy link
Owner

wuseal commented May 29, 2021

Got it. Currently plugin doesn't support your requst, And I want to know that why you choose snake case style, as I know it's not kotlin standard style. Does it means that your code all in snake case style? @peterstev-fm

@wuseal
Copy link
Owner

wuseal commented May 29, 2021

To achieve your requst feature, We need creat a new extension to implement it, it's not hard

@peterstev-fm
Copy link
Author

Got it. Currently plugin doesn't support your requst, And I want to know that why you choose snake case style, as I know it's not kotlin standard style. Does it means that your code all in snake case style? @peterstev-fm

No my entire code isn't like this, I use this in the data layer of my application, so that I don't have to clutter my class with @serializedName("name") or @JSON("name")

The domain layer uses camelCase, then I use mappers to map the data layer classes to the domain layer.

@peterstev-fm
Copy link
Author

To achieve your requst feature, We need creat a new extension to implement it, it's not hard

Oh nice, good to know it's not hard. How soon can it be done 😁

@wuseal
Copy link
Owner

wuseal commented May 29, 2021

So, what you want is just not use the property annotation with moshi😅?

It can take several hours to implement it, But I have other tasks prefer to do, If you have interest,Hope you can do it by yourself, it's easy. Refer here: src/main/kotlin/extensions/yuan/varenyzc/CamelCaseSupport.kt

Write one class like that(we may named it as let all property' name to be snake case style) and add it into here is ok

'src/main/kotlin/extensions/ExtensionsCollector.kt'

That's all need to be done

@peterstev-fm

@peterstev-fm
Copy link
Author

So, what you want is just not use the property annotation with moshi😅?

It can take several hours to implement it, But I have other tasks prefer to do, If you have interest,Hope you can do it by yourself, it's easy. Refer here: src/main/kotlin/extensions/yuan/varenyzc/CamelCaseSupport.kt

Okay I've seen the CamelCaseSupport file, I'll build the feature and create a PR.

That's good?

@wuseal
Copy link
Owner

wuseal commented May 29, 2021

Good! Very good!😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants