nikolaydubina / go-enum-encoding

Generate Go enum encoding
MIT License
12 stars 1 forks source link

Rename the comment tag to e.g. `enum:"..."` #25

Open orenl opened 3 weeks ago

orenl commented 3 weeks ago

The use of json:"..." as a tag in the comment is too generic. It may conflict with casual comments elsewhere in the code (in const or var). Consider renaming to enum:"...".

Note that once #24 get fixed, such conflicts should not happen, since this would be confined to the designated type; Nevertheless, IMO enum:"..." would still be more precise choice.

For example:

package main

type Enum uint8

//go:generate go-enum-encoding -type=Enum
const (
    Plain Enum = iota+1 // json:"one_plain"
)

var (
    Variable int // json:"something"
)
nikolaydubina commented 3 weeks ago

json

json is intentional. we are relying on the same tag that people are already familiar with. this is natural choice as it follows notation how people are declaring field names for JSON for structs. this reduces cognitive load and allows easy learning. this choice also emphasises, that our concern here is encoding/decoding, and nothing else, which makes limited scope of this tool, which is also beneficial. in fact, I would rather raise proposal to official Go language to include this as official Go language notation.

conflicts with other json tags

best I know, json comment tags can not be used in const nor var declaration. the only place standard Go (and any 3rd party packages I know) are using json tags so far are in struct declaration. this AST tool scans json tags only for const and var declarations. thus no conflicts should happen.

nikolaydubina commented 3 weeks ago

issue is open to collect feedback from others.