Skip to content

Commit

Permalink
id: add Name function
Browse files Browse the repository at this point in the history
Add a reverse lookup table to get the name for one of the pre-defined
identifiers. We do not use a stringer here since that would mean to
define a custom type instead of just using uint32 everywhere. This seems
less intrusive.
  • Loading branch information
magiconair committed Oct 2, 2020
1 parent 2999796 commit 3272102
Show file tree
Hide file tree
Showing 2 changed files with 10,734 additions and 0 deletions.
15 changes: 15 additions & 0 deletions cmd/id/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,25 @@ var tmpl = template.Must(template.New("").Parse(`
package id
import "strconv"
func Name(id uint32) string {
if s, ok := name[id]; ok {
return s
}
return strconv.FormatUint(uint64(id), 10)
}
const (
{{range .}}{{index . 0}} = {{index . 1}}
{{end}}
)
var name = map[uint32]string{
{{- range .}}
{{index . 1}}: "{{index . 0}}",
{{- end}}
}
`))

func goName(s string) string {
Expand Down
Loading

0 comments on commit 3272102

Please sign in to comment.