mirror of
https://github.com/ent/ent.git
synced 2026-05-22 09:31:45 +03:00
entc/gen: revet the new format for enums (#581)
Some acronyms don't exist in entc, and we define them as uppercase to control the Go constant name
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -115,7 +115,7 @@ const (
|
||||
// {{ $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 }}{{ lower $e | pascal }}{{ end }}:
|
||||
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", {{ $receiver }})
|
||||
|
||||
@@ -615,7 +615,7 @@ func (f Field) Enums() []string {
|
||||
|
||||
// EnumName returns the constant name of the enum value.
|
||||
func (f Field) EnumName(enum string) string {
|
||||
return pascal(f.Name) + pascal(strings.ToLower(enum))
|
||||
return pascal(f.Name) + pascal(enum)
|
||||
}
|
||||
|
||||
// Validator returns the validator name.
|
||||
|
||||
@@ -136,6 +136,23 @@ func TestType_Receiver(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestField_EnumName(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
enum string
|
||||
}{
|
||||
{"GIF", "TypeGIF"},
|
||||
{"SVG", "TypeSVG"},
|
||||
{"PNG", "TypePNG"},
|
||||
{"MP4", "TypeMP4"},
|
||||
{"unknown", "TypeUnknown"},
|
||||
{"user_data", "TypeUserData"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
require.Equal(t, tt.enum, Field{Name: "Type"}.EnumName(tt.name))
|
||||
}
|
||||
}
|
||||
|
||||
func TestType_WithRuntimeMixin(t *testing.T) {
|
||||
position := &load.Position{MixedIn: true}
|
||||
typ := &Type{
|
||||
|
||||
Reference in New Issue
Block a user