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

GoAddTags: tag options #985

Closed
tobstarr opened this issue Aug 1, 2016 · 7 comments
Closed

GoAddTags: tag options #985

tobstarr opened this issue Aug 1, 2016 · 7 comments

Comments

@tobstarr
Copy link

tobstarr commented Aug 1, 2016

At least for my workflow it would be nice to be able to also provide e.g. the omitempty option when adding a json tag to a struct field.

I would propose to change the behaviour of GoAddTags json,omitempty to produce json:"name,omitempty" instead of json,omitempty:"name" but I do not know if there are cases where one actually would want to add a tag including a comma character.

@fatih
Copy link
Owner

fatih commented Aug 1, 2016

Hi @tobstarr

This and the second PR (#984) you opened, I wanted to do in the next release ;) I didn't wanted to put a lot of features. So adding them one by one is the way to go 👍

Tag options is easy, however for the second one we need to parse the string (might be a little bit complicated, but doable). I'll keep an eye on this. If someone else want to tackle it feel free to pick it up, but let me know before if you do it.

fatih added a commit that referenced this issue Feb 13, 2017
* Use new `gomodifytags` tool to manipulate and modify tags
* Adds new `:GoRenameTags`
* `:GoAddTags` is able to add options
* `:GoRemoveTags` is able to remove options

Usage:

Following command add multiple tags. Does not modify if the key already
exist. If `,optionaname` is given, it adds the given option to that key.
If called without arguments, it adds by default tags with `json` key.
```
:GoAddTags
:GoAddTags json
:GoAddTags json,omitempty
:GoAddTags json hcl bson
:GoAddTags json,omitempty hcl bson
```

Following command removes multiple tags and options. If `,optionname` is
given, it removes the option belonging to that key instead of removing
the key. If called without arguments, it removes all tags belonging to a
struct.

```
:GoRemoveTags
:GoRemoveTags json
:GoRemoveTags json,omitempty
:GoRemoveTags json hcl bson
:GoRemoveTags json,omitempty hcl bson
```

Fixes following issues:

 #984
 #985
 #990
 #1064
 #1091
fatih added a commit that referenced this issue Feb 13, 2017
* Use new `gomodifytags` tool to manipulate and modify tags
* Adds new `:GoRenameTags`
* `:GoAddTags` is able to add options
* `:GoRemoveTags` is able to remove options

Usage:

Following command add multiple tags. Does not modify if the key already
exist. If `,optionaname` is given, it adds the given option to that key.
If called without arguments, it adds by default tags with `json` key.
```
:GoAddTags
:GoAddTags json
:GoAddTags json,omitempty
:GoAddTags json hcl bson
:GoAddTags json,omitempty hcl bson
```

Following command removes multiple tags and options. If `,optionname` is
given, it removes the option belonging to that key instead of removing
the key. If called without arguments, it removes all tags belonging to a
struct.

```
:GoRemoveTags
:GoRemoveTags json
:GoRemoveTags json,omitempty
:GoRemoveTags json hcl bson
:GoRemoveTags json,omitempty hcl bson
```

Fixes following issues:

 #984
 #985
 #990
 #1064
 #1091
fatih added a commit that referenced this issue Feb 13, 2017
* Use new `gomodifytags` tool to manipulate and modify tags
* `:GoAddTags` is able to add tags and options
* `:GoRemoveTags` is able to remove tags and options

Usage:

Following command add multiple tags. Does not modify if the key already
exist. If `,optionaname` is given, it adds the given option to that key.
If called without arguments, it adds by default tags with `json` key.
```
:GoAddTags
:GoAddTags json
:GoAddTags json,omitempty
:GoAddTags json hcl bson
:GoAddTags json,omitempty hcl bson
```

Following command removes multiple tags and options. If `,optionname` is
given, it removes the option belonging to that key instead of removing
the key. If called without arguments, it removes all tags belonging to a
struct.

```
:GoRemoveTags
:GoRemoveTags json
:GoRemoveTags json,omitempty
:GoRemoveTags json hcl bson
:GoRemoveTags json,omitempty hcl bson
```

Fixes following issues:

 #984
 #985
 #990
 #1064
 #1091
fatih added a commit that referenced this issue Feb 13, 2017
* Use new `gomodifytags` tool to manipulate and modify tags
* `:GoAddTags` is able to add tags and options
* `:GoRemoveTags` is able to remove tags and options

Usage:

Following command add multiple tags. Does not modify if the key already
exist. If `,optionaname` is given, it adds the given option to that key.
If called without arguments, it adds by default tags with `json` key.
```
:GoAddTags
:GoAddTags json
:GoAddTags json,omitempty
:GoAddTags json hcl bson
:GoAddTags json,omitempty hcl bson
```

Following command removes multiple tags and options. If `,optionname` is
given, it removes the option belonging to that key instead of removing
the key. If called without arguments, it removes all tags belonging to a
struct.

```
:GoRemoveTags
:GoRemoveTags json
:GoRemoveTags json,omitempty
:GoRemoveTags json hcl bson
:GoRemoveTags json,omitempty hcl bson
```

Fixes following issues:

 #984
 #985
 #990
 #1064
 #1091
@fatih
Copy link
Owner

fatih commented Feb 14, 2017

Fixed with #1204 please, feel free to test and reopen if it's not the case.

@fatih fatih closed this as completed Feb 14, 2017
@tobstarr
Copy link
Author

@fatih is it intended that GoAddTags json,omitempty only works if the tag is already present? In that case things work fine, otherwise nothing happens (but something is happening as the vim buffer is then marked as changed - +)

@fatih
Copy link
Owner

fatih commented Feb 14, 2017

Yeah it only works if it exists. Previously it was the opposite. The buffer changes because gomodifytags changes the file again, thus the creation time changes as well. Open an issue to https://github.com/fatih/gomodifytags to track if you want :)

@tobstarr
Copy link
Author

Any particular reason for that? I would find it really handy for GoAddTags json,omitemty to also create the tag of it does not exist.

I just checked gomodifytag and the call gomodifytags -file main.go -add-tags json -struct User -add-options json=omitempty would do the trick (add the tag with the option if it does not exist).

Behaviour before was not the opposite, but that it created this: json,omitempty:"name".

@fatih
Copy link
Owner

fatih commented Feb 15, 2017

When I mean oppposite, the current implementation was like you said. I've removed it because I wanted to make it explicitly. It's very easy to change it because I've like that previously. I'm not sure why I reverted it (don't remember now). Let me check it again.

@gitsang
Copy link

gitsang commented Nov 3, 2022

I write a function to implement it (using fatih/gomodifytags)

:command -range -nargs=* GoTagsAdd <line1>,<line2>call GoTagsAdd(<f-args>)
function! GoTagsAdd(...) range
    let filename = expand('%:t')
    let line = a:firstline . ',' . a:lastline
    let cmds = ['!gomodifytags']
    call add(cmds, '-file')
    call add(cmds, filename)
    call add(cmds, '-line')
    call add(cmds, line)
    call add(cmds, '--skip-unexported')
    call add(cmds, '-w --quiet')
    let cmd = join(cmds, " ")

    if a:0 > 0
        let tags = a:1
        if tags != '--'
            call add(cmds, '-add-tags')
            call add(cmds, tags)
        endif
    else
        call add(cmds, '-add-tags json')
    endif

    if a:0 > 1
        let options = a:2
        if options != '--'
            call add(cmds, '-add-options')
            call add(cmds, options)
        endif
    else
        call add(cmds, '-add-options json=omitempty')
    endif

    if a:0 > 2
        let transform = a:2
        if transform != '--'
            call add(cmds, '-transform')
            call add(cmds, transform)
        endif
    else
        call add(cmds, '-transform camelcase')
    endif

    execute join(cmds, " ")
endfunction

It seems like

GIF 2022-11-3 20-53-10

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

3 participants