Skip to content

Commit

Permalink
Tailscale feedback (#28)
Browse files Browse the repository at this point in the history
* do-not-edit-line

* tailscale

* fix
  • Loading branch information
nikolaydubina authored Aug 27, 2024
1 parent cf0aa9e commit 13bf8ab
Show file tree
Hide file tree
Showing 35 changed files with 1,057 additions and 115 deletions.
15 changes: 8 additions & 7 deletions enum.go.template
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
// Code generated by go-enum-encoding DO NOT EDIT
// Code generated by go-enum-encoding DO NOT EDIT.

package {{.Package}}

import "errors"

var ErrUnknown{{.Type}} = errors.New("unknown {{.Type}}")

var vals_{{.Type}} = map[{{.Type}}][]byte{
{{.val_to_json}}
var value_to_bytes_{{.Type}} = map[{{.Type}}][]byte{
{{.value_to_bytes}}
}

var vals_inv_{{.Type}} = map[string]{{.Type}}{
{{.json_to_value}}
var string_to_value_{{.Type}} = map[string]{{.Type}}{
{{.string_to_value}}
}

func (s *{{.Type}}) UnmarshalText(text []byte) error {
var ok bool
if *s, ok = vals_inv_{{.Type}}[string(text)]; !ok {
if *s, ok = string_to_value_{{.Type}}[string(text)]; !ok {
return ErrUnknown{{.Type}}
}
return nil
}

func (s {{.Type}}) MarshalText() ([]byte, error) { return vals_{{.Type}}[s], nil }
func (s {{.Type}}) MarshalText() ([]byte, error) { return value_to_bytes_{{.Type}}[s], nil }
9 changes: 5 additions & 4 deletions enum_short.go.template
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Code generated by go-enum-encoding DO NOT EDIT
// Code generated by go-enum-encoding DO NOT EDIT.

package {{.Package}}

import "errors"
Expand All @@ -7,18 +8,18 @@ var ErrUnknown{{.Type}} = errors.New("unknown {{.Type}}")

func (s *{{.Type}}) UnmarshalText(text []byte) error {
switch string(text) {
{{.json_to_value}}
{{.string_to_value}}
default:
return ErrUnknown{{.Type}}
}
return nil
}

var json_bytes_{{.Type}} = [...][]byte{{{.json_bytes}}}
var seq_bytes_{{.Type}} = [...][]byte{{{.seq_bytes}}}

func (s {{.Type}}) MarshalText() ([]byte, error) {
switch s {
{{.val_to_json}}
{{.value_to_bytes}}
default:
return nil, ErrUnknown{{.Type}}
}
Expand Down
10 changes: 10 additions & 0 deletions enum_short_string.go.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var seq_string_{{.Type}} = [...]string{{{.seq_string}}}

func (s {{.Type}}) String() string {
switch s {
{{.value_to_string}}
default:
return ""
}
}

7 changes: 4 additions & 3 deletions enum_string.go.template
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
func (s {{.Type}}) String() string {
b, _ := s.MarshalText()
return string(b)
var value_to_string_{{.Type}} = map[{{.Type}}]string{
{{.value_to_string}}
}

func (s {{.Type}}) String() string { return value_to_string_{{.Type}}[s] }
12 changes: 12 additions & 0 deletions enum_string_test.go.template
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,15 @@ func Test{{.Type}}_String(t *testing.T) {
}
}
}

func Benchmark{{.Type}}_String(b *testing.B) {
var v string
for i := 0; i < b.N; i++ {
for _, c := range []{{.Type}}{{{.Values}}} {
v = c.String()
}
}
if len(v) > 1000 {
b.Fatal("noop")
}
}
3 changes: 2 additions & 1 deletion enum_test.go.template
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Code generated by go-enum-encoding DO NOT EDIT
// Code generated by go-enum-encoding DO NOT EDIT.

package {{.Package}}

import (
Expand Down
32 changes: 32 additions & 0 deletions internal/testdata/color2_enum_encoding.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions internal/testdata/color_enum_encoding.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 75 additions & 0 deletions internal/testdata/color_enum_encoding_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 51 additions & 0 deletions internal/testdata/colorstring_enum_encoding.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 13bf8ab

Please sign in to comment.