Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
1 change: 1 addition & 0 deletions entc/integration/json/ent/schema/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func (User) Fields() []ent.Field {
field.Strings("strings").
Optional(),
field.JSON("addr", Addr{}).
Sensitive().
Optional(),
}
}
Expand Down
5 changes: 2 additions & 3 deletions entc/integration/json/ent/user.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions entc/integration/json/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net"
"net/http"
"net/url"
"reflect"
"testing"

"entgo.io/ent/dialect"
Expand Down Expand Up @@ -199,6 +200,11 @@ func NetAddr(t *testing.T, client *ent.Client) {
require.Equal(t, "127.0.0.1:80", client.User.GetX(ctx, usr.ID).Addr.String())
usr.Update().SetAddr(schema.Addr{Addr: &net.UDPAddr{IP: ip, Port: 1812}}).ExecX(ctx)
require.Equal(t, "127.0.0.1:1812", client.User.GetX(ctx, usr.ID).Addr.String())

// Ensure sensitive fields are not marshalled.
f, ok := reflect.TypeOf(ent.User{}).FieldByName("Addr")
require.True(t, ok)
require.Equal(t, "-", f.Tag.Get("json"))
}

func Dirs(t *testing.T, client *ent.Client) {
Expand Down
6 changes: 6 additions & 0 deletions schema/field/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,12 @@ func (b *jsonBuilder) Comment(c string) *jsonBuilder {
return b
}

// Sensitive fields not printable and not serializable.
func (b *jsonBuilder) Sensitive() *jsonBuilder {
b.desc.Sensitive = true
return b
}

// StructTag sets the struct tag of the field.
func (b *jsonBuilder) StructTag(s string) *jsonBuilder {
b.desc.Tag = s
Expand Down
2 changes: 2 additions & 0 deletions schema/field/field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,9 +474,11 @@ func TestJSON(t *testing.T) {
fd = field.Strings("strings").
Optional().
Default([]string{"a", "b"}).
Sensitive().
Descriptor()
assert.NoError(t, fd.Err)
assert.True(t, fd.Optional)
assert.True(t, fd.Sensitive)
assert.Empty(t, fd.Info.PkgPath)
assert.Equal(t, "strings", fd.Name)
assert.Equal(t, []string{"a", "b"}, fd.Default)
Expand Down