8000 schema/field: ensure DefaultFunc argument is type func by a8m · Pull Request #2884 · ent/ent · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

schema/field: ensure DefaultFunc argument is type func #2884

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion schema/field/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func JSON(name string, typ any) *jsonBuilder {
}}
t := reflect.TypeOf(typ)
if t == nil {
b.desc.Err = errors.New("expect a Go value as JSON type, but got nil")
b.desc.Err = errors.New("expect a Go value as JSON type but got nil")
return b
}
b.desc.Info.Ident = t.String()
Expand Down Expand Up @@ -237,6 +237,9 @@ func (b *stringBuilder) Default(s string) *stringBuilder {
// field.String("cuid").
// DefaultFunc(cuid.New)
func (b *stringBuilder) DefaultFunc(fn any) *stringBuilder {
if t := reflect.TypeOf(fn); t.Kind() != reflect.Func {
b.desc.Err = fmt.Errorf("field.String(%q).DefaultFunc expects func but got %s", b.desc.Name, t.Kind())
}
b.desc.Default = fn
return b
}
Expand Down Expand Up @@ -528,6 +531,9 @@ func (b *bytesBuilder) Default(v []byte) *bytesBuilder {
// field.Bytes("cuid").
// DefaultFunc(cuid.New)
func (b *bytesBuilder) DefaultFunc(fn any) *bytesBuilder {
if t := reflect.TypeOf(fn); t.Kind() != reflect.Func {
b.desc.Err = fmt.Errorf("field.Bytes(%q).DefaultFunc expects func but got %s", b.desc.Name, t.Kind())
}
b.desc.Default = fn
return b
}
Expand Down
8 changes: 7 additions & 1 deletion schema/field/field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,9 @@ func TestBytes_DefaultFunc(t *testing.T) {
f4 := func() net.IPMask { return net.IPMask("ffff:ff80::") }
fd = field.Bytes("ip").GoType(net.IP("127.0.0.1")).DefaultFunc(f4).Descriptor()
assert.Error(t, fd.Err, "`var _ net.IP = f4()` should fail")

fd = field.Bytes("ip").GoType(net.IP("127.0.0.1")).DefaultFunc(net.IP("127.0.0.1")).Descriptor()
assert.EqualError(t, fd.Err, `field.Bytes("ip").DefaultFunc expects func but got slice`)
}

func TestString_DefaultFunc(t *testing.T) {
Expand All @@ -304,6 +307,9 @@ func TestString_DefaultFunc(t *testing.T) {
f4 := func() S { return "" }
fd = field.String("str").GoType(http.Dir("/tmp")).DefaultFunc(f4).Descriptor()
assert.Error(t, fd.Err, "`var _ http.Dir = f4()` should fail")

fd = field.String("str").GoType(http.Dir("/tmp")).DefaultFunc("/tmp").Descriptor()
assert.EqualError(t, fd.Err, `field.String("str").DefaultFunc expects func but got string`)
}

type VString string
Expand Down Expand Up @@ -522,7 +528,7 @@ func TestJSON(t *testing.T) {
assert.Equal(t, "net/url", fd.Info.PkgPath)
assert.Equal(t, "url", fd.Info.PkgName)
fd = field.JSON("addr", net.Addr(nil)).Descriptor()
assert.EqualError(t, fd.Err, "expect a Go value as JSON type, but got nil")
assert.EqualError(t, fd.Err, "expect a Go value as JSON type but got nil")
}

func TestField_Tag(t *testing.T) {
Expand Down
0