entc/gen: define method receiver on enum fields (#580)

Signed-off-by: Alex Snast <alexsn@fb.com>
This commit is contained in:
Alex Snast
2020-07-03 20:41:32 +03:00
committed by GitHub
parent 902b29d9bf
commit da56f8e6e8
5 changed files with 15 additions and 15 deletions

File diff suppressed because one or more lines are too long

View File

@@ -106,19 +106,19 @@ const (
{{- end }}
)
func (s {{ $enum }}) String() string {
return string(s)
{{ $receiver := receiver $f.BuilderField -}}
func ({{ $receiver }} {{ $enum }}) String() string {
return string({{ $receiver }})
}
{{ $name := $f.Validator -}}
{{ $arg := receiver $f.BuilderField }}
// {{ $name }} is a validator for the "{{ $arg }}" field enum values. It is called by the builders before save.
func {{ $name }}({{ $arg }} {{ $enum }}) error {
switch {{ $arg }} {
// {{ $name }} is a validator for the "{{ $receiver }}" field enum values. It is called by the builders before save.
func {{ $name }}({{ $receiver }} {{ $enum }}) error {
switch {{ $receiver }} {
case {{ range $i, $e := $f.Enums }}{{ if ne $i 0 }},{{ end }}{{ $f.StructField }}{{ pascal $e }}{{ end }}:
return nil
default:
return fmt.Errorf("{{ $.Package }}: invalid enum value for {{ $f.Name }} field: %q", {{ $arg }})
return fmt.Errorf("{{ $.Package }}: invalid enum value for {{ $f.Name }} field: %q", {{ $receiver }})
}
}
{{ end }}

View File

@@ -167,8 +167,8 @@ const (
RoleAdmin Role = "admin"
)
func (s Role) String() string {
return string(s)
func (r Role) String() string {
return string(r)
}
// RoleValidator is a validator for the "r" field enum values. It is called by the builders before save.

View File

@@ -100,8 +100,8 @@ const (
RoleAdmin Role = "admin"
)
func (s Role) String() string {
return string(s)
func (r Role) String() string {
return string(r)
}
// RoleValidator is a validator for the "r" field enum values. It is called by the builders before save.

View File

@@ -67,8 +67,8 @@ const (
TypeIrregular Type = "irregular"
)
func (s Type) String() string {
return string(s)
func (_type Type) String() string {
return string(_type)
}
// TypeValidator is a validator for the "_type" field enum values. It is called by the builders before save.