camelCase json for Golang struct tags

https://intellij-support.jetbrains.com/hc/en-us/community/posts/360000525304-camelCase-json-for-Golang-struct-tads?flash_digest=8f577179f1e291b4e260f78426d40965cb05a154

Hi! How to convert SomeValue `json:"some_value"` auto completion to SomeValue `json:"someValue"?

I have the same question with the thread above. However upon following the instructions, I still get the snakeCase() result .

I'm using Goland 2019.2.2. Can someone confirm whether this is a bug? 

0
14 comments
Official comment

It works fine for me in 2019.2.3. Please ensure that you configure the template variable as on picture below.

Hi, I have Goland 2021.1.1 and the only place I could find this setting is in Go Struct Tags / json. However, modifying it using "Edit Variables" does not seem to be working and I continue to get snakecase instead of camelcase when auto completing for json tags.

Could you pl. confirm instructions for obtaining json struct tags in camelcase format for Goland 2021.1.1? Thanks.

1

Hey, it's much easier in 2021.1. You can convert existing tags by hitting Alt+Enter on them. After that, GoLand will automatically infer style from existing tags. Here you can find more details https://www.jetbrains.com/go/whatsnew/#working-with-json

1

Hello @...,

You can still use custom tags in the structs. We have several usability problems, and it may look a bit difficult to use, please see GO-11442 & GO-11465. I see that you voted for GO-11005 as well.

There is one hint that allows you to use json custom tag with camelCase.

  • Navigate to Preferences / Settings | Editor | Live Templates | Go Struct Tags and delete the current json template.
  • Create a new one with the following parameters: Abbreviation: json, Template text: json:"$FIELD_NAME$"$END$, Applicable in Go: tag literal, Expression: camelCase(fieldName()).
  • Save changes and type a full abbreviation in backticks, then press the Tab key. It is important that you shouldn't select json from the completion pop-up due to it is a pre-defined template for now.

 

1
Avatar
Permanently deleted user

Thanks for the explanation Daniil. This is exactly the workaround I needed for the workflow I have. I've voted for the other 2 issues that you've linked. Appreciate your help!

1

For anyone still encountering this, on the latest version, it seems that it's possible to make the autocomplete pop up by exiting the initial window that appears when writing the tag name, and then writing the tag name that you want to autocomplete.

I'm not sure why this behavior is like that, but I'm guessing the initial autocomplete takes its data from somewhere else, and doesn't take Live Templates into consideration.

It's working like this for me on IDEA 2021.2.2, not sure about Goland. 

1

Hello, I have done that, and now is not auto-completing anymore, even if I revert to `snake case` it still does not autocomplete.

Could you please help me?

0

There's a button on the right which is called Restore deleted defaults. Does it help if you apply it to json?

0

Ok, I have manage to fix it with camelCase, thanks

0

Cool, I like the new feature and it would be so much more powerful if we can configure the default formatting behavior. I am still wondering if there is a way I can change the default behavior from snake-case to camel-case? I used to be able to do that in previous versions, so it is much easier to simply do back-tick+json+tab and have these struct tags autocomplete with desired format configured in settings. Updating the format in Live Template / Go Struct Tags / json panel does not seem to be working for me and the option to configure the same in Live Template / Go / json panel seems to have been removed in 2021.1.

0

Hi Saurabh Deoras, the new templates inside backticks always produce struct tags with snake_case and there is no way to configure it yet.

You can create your own template under Preferences / Settings | Editor | Live Templates | Go Struct Tags with the following parameters:

  • Abbreviation: json
  • Description: json:""
  • Template text: `json:"$FIELD_NAME$,omitempty"` (pay attention to backticks)
  • Edit variables button => Expression: camelCase(fieldName())
  • Applicable in Go: tag

After that, you can invoke json abbreviation without backticks. Please see the example below.

type Person struct {
FirstName string // start typing json on the line
}
type Person struct {
FirstName string `json:"firstName,omitempty"`
}

I've filled out a new ticket, please see GO-11005. Autodetection can help your write the correct struct tag once, then GoLand inserts new fields with your camelCase/snake_case/etc preferences instead of configuring the default behavior for each struct tag.

I hope it helps.

0

Hi Daniil, thanks so much, that worked!

0
Avatar
Permanently deleted user

This appears to be a regression from how struct tags previously worked. It used to be possible to apply additional tags that conformed to the live templates defined in Preferences / Settings | Editor | Live Templates | Go Struct Tags.

Previously when I needed to add additional tags I could type the tag name and hit tab to apply. Now custom tags defined in the Live Template settings are no longer picked up and are unusable. If I attempt to use the custom tag first and then type "json" it applies the snake case form of the field name.

Is there any way to achieve what I'm describing above?

0

Please sign in to leave a comment.