mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
Merge branch 'tmc-tmc-relax-signature-with-GoType' into gotypedefault
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -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 }}
|
||||
|
||||
@@ -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 }
|
||||
|
||||
|
||||
@@ -213,7 +213,7 @@ func (b *stringBuilder) Default(s string) *stringBuilder {
|
||||
// field.String("cuid").
|
||||
// DefaultFunc(cuid.New)
|
||||
//
|
||||
func (b *stringBuilder) DefaultFunc(fn func() string) *stringBuilder {
|
||||
func (b *stringBuilder) DefaultFunc(fn interface{}) *stringBuilder {
|
||||
b.desc.Default = fn
|
||||
return b
|
||||
}
|
||||
@@ -295,6 +295,13 @@ func (b *stringBuilder) Annotations(annotations ...schema.Annotation) *stringBui
|
||||
|
||||
// Descriptor implements the ent.Field interface by returning its descriptor.
|
||||
func (b *stringBuilder) Descriptor() *Descriptor {
|
||||
// If the Default is present and a function, check that it's signtaure is appropriate.
|
||||
if b.desc.Default != nil {
|
||||
typ := reflect.TypeOf(b.desc.Default)
|
||||
if typ.Kind() == reflect.Func && (typ.NumIn() != 0 || typ.NumOut() != 1 || typ.Out(0).String() != b.desc.Info.String()) {
|
||||
b.desc.Err = fmt.Errorf("expect type (func() %s) for default value", b.desc.Info)
|
||||
}
|
||||
}
|
||||
return b.desc
|
||||
}
|
||||
|
||||
@@ -500,7 +507,7 @@ func (b *bytesBuilder) Default(v []byte) *bytesBuilder {
|
||||
// field.Bytes("cuid").
|
||||
// DefaultFunc(cuid.New)
|
||||
//
|
||||
func (b *bytesBuilder) DefaultFunc(fn func() []byte) *bytesBuilder {
|
||||
func (b *bytesBuilder) DefaultFunc(fn interface{}) *bytesBuilder {
|
||||
b.desc.Default = fn
|
||||
return b
|
||||
}
|
||||
@@ -590,6 +597,14 @@ func (b *bytesBuilder) SchemaType(types map[string]string) *bytesBuilder {
|
||||
|
||||
// Descriptor implements the ent.Field interface by returning its descriptor.
|
||||
func (b *bytesBuilder) Descriptor() *Descriptor {
|
||||
// If the Default is present and a function, check that it's signtaure is appropriate.
|
||||
if b.desc.Default != nil {
|
||||
typ := reflect.TypeOf(b.desc.Default)
|
||||
// We check against "[]uint8" here because the reflect package treats byte as an alias to uint8.
|
||||
if typ.Kind() == reflect.Func && (typ.NumIn() != 0 || typ.NumOut() != 1 || typ.Out(0).String() != "[]uint8") {
|
||||
b.desc.Err = fmt.Errorf("expect type (func() %s) for default value", b.desc.Info)
|
||||
}
|
||||
}
|
||||
return b.desc
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user