mirror of
https://github.com/ent/ent.git
synced 2026-03-05 19:35:23 +03:00
schema/field: store reflect type info in json fields
This commit is contained in:
committed by
Ariel Mashraki
parent
5eb5f501f0
commit
46cf7ad36a
@@ -72,20 +72,21 @@ func Time(name string) *timeBuilder {
|
||||
//
|
||||
func JSON(name string, typ interface{}) *jsonBuilder {
|
||||
t := reflect.TypeOf(typ)
|
||||
info := &TypeInfo{
|
||||
Type: TypeJSON,
|
||||
Ident: t.String(),
|
||||
PkgPath: t.PkgPath(),
|
||||
}
|
||||
b := &jsonBuilder{&Descriptor{
|
||||
Name: name,
|
||||
Info: &TypeInfo{
|
||||
Type: TypeJSON,
|
||||
Ident: t.String(),
|
||||
PkgPath: t.PkgPath(),
|
||||
},
|
||||
}}
|
||||
b.desc.goType(typ, t)
|
||||
switch t.Kind() {
|
||||
case reflect.Slice, reflect.Array, reflect.Ptr, reflect.Map:
|
||||
info.Nillable = true
|
||||
info.PkgPath = pkgPath(t)
|
||||
b.desc.Info.Nillable = true
|
||||
b.desc.Info.PkgPath = pkgPath(t)
|
||||
}
|
||||
return &jsonBuilder{&Descriptor{
|
||||
Name: name,
|
||||
Info: info,
|
||||
}}
|
||||
return b
|
||||
}
|
||||
|
||||
// Strings returns a new JSON Field with type []string.
|
||||
|
||||
@@ -427,6 +427,18 @@ func TestJSON(t *testing.T) {
|
||||
assert.Equal(t, field.TypeJSON, fd.Info.Type)
|
||||
assert.Equal(t, "map[string]string", fd.Info.String())
|
||||
assert.Equal(t, "comment", fd.Comment)
|
||||
assert.True(t, fd.Info.Nillable)
|
||||
assert.False(t, fd.Info.RType.IsPtr())
|
||||
|
||||
type T struct{ S string }
|
||||
fd = field.JSON("name", &T{}).
|
||||
Descriptor()
|
||||
assert.True(t, fd.Info.Nillable)
|
||||
assert.Equal(t, "*field_test.T", fd.Info.Ident)
|
||||
assert.Equal(t, "entgo.io/ent/schema/field_test", fd.Info.PkgPath)
|
||||
assert.True(t, fd.Info.RType.IsPtr())
|
||||
assert.Equal(t, "T", fd.Info.RType.Name)
|
||||
assert.Equal(t, "entgo.io/ent/schema/field_test", fd.Info.RType.PkgPath)
|
||||
|
||||
fd = field.JSON("dir", http.Dir("dir")).
|
||||
Optional().
|
||||
@@ -436,6 +448,7 @@ func TestJSON(t *testing.T) {
|
||||
assert.Equal(t, "dir", fd.Name)
|
||||
assert.Equal(t, "net/http", fd.Info.PkgPath)
|
||||
assert.Equal(t, "http.Dir", fd.Info.String())
|
||||
assert.False(t, fd.Info.Nillable)
|
||||
|
||||
fd = field.Strings("strings").
|
||||
Optional().
|
||||
|
||||
Reference in New Issue
Block a user