entc/gen: move mutation-set naming to go api (#1123)

This commit is contained in:
Ariel Mashraki
2021-01-03 22:10:19 +02:00
committed by GitHub
parent eb23cd9218
commit 97c316b80a
3 changed files with 15 additions and 5 deletions

File diff suppressed because one or more lines are too long

View File

@@ -157,9 +157,9 @@ func (m *{{ $mutation }}) ID() (id {{ $n.ID.Type }}, exists bool) {
}
{{ range $f := $n.Fields }}
{{ $p := receiver $f.Type.String }}{{ if eq $p "m" }} {{ $p = "value" }} {{ end }}
{{ $func := print "Set" $f.StructField }}
{{ $const := print $n.Package "." $f.Constant }}
{{ $p := receiver $f.Type.String }}{{ if eq $p "m" }} {{ $p = "value" }} {{ end }}
{{ $func := $f.MutationSet }}
// {{ $func }} sets the {{ $f.Name }} field.
func (m *{{ $mutation }}) {{ $func }}({{ $p }} {{ $f.Type }}) {
m.{{ $f.BuilderField }} = &{{ $p }}

View File

@@ -685,7 +685,6 @@ func (t Type) RelatedTypes() []*Type {
func ValidSchemaName(name string) error {
// schema package is lower-cased (see Type.Package)
pkg := strings.ToLower(name)
if token.Lookup(pkg).IsKeyword() {
return fmt.Errorf("schema lowercase name conflicts with Go keyword %q", pkg)
}
@@ -828,6 +827,17 @@ func (f Field) MutationReset() string {
return name
}
// MutationSet returns the method name for setting the field value.
// The default name is "Set<FieldName>". If the the method conflicts
// with the mutation methods, suffix the method with "Field".
func (f Field) MutationSet() string {
name := "Set" + f.StructField()
if _, ok := mutMethods[name]; ok {
name += "Field"
}
return name
}
// IsBool returns true if the field is a bool field.
func (f Field) IsBool() bool { return f.Type != nil && f.Type.Type == field.TypeBool }