func TestConvertKeys(t *testing.T) { m := New().PickAll().ConvertKeys(func(s string) string { return "dummy_" + s }).Transform(user) assert.Contains(t, m, "dummy_ID") assert.Contains(t, m, "dummy_FirstName") assert.NotContains(t, m, "ID") }
func TestPickFunc(t *testing.T) { m := New().PickFunc(func(t interface{}) interface{} { return t.(time.Time).Format(time.RFC3339) }, "CreatedAt", "UpdatedAt").Transform(user) assert.Contains(t, m, "CreatedAt") assert.Contains(t, m, "UpdatedAt") assert.Equal(t, m["CreatedAt"], user.CreatedAt.Format(time.RFC3339)) }
func TestPickFuncIf(t *testing.T) { m := New().PickFuncIf(alwaysTrue, func(t interface{}) interface{} { return t.(time.Time).Format(time.RFC3339) }, "CreatedAt", "UpdatedAt").PickFuncIf(alwaysFalse, identity, "Email").Transform(user) assert.Contains(t, m, "CreatedAt") assert.Contains(t, m, "UpdatedAt") assert.NotContains(t, m, "Email") assert.Equal(t, m["CreatedAt"], user.CreatedAt.Format(time.RFC3339)) }
func TestPickIf(t *testing.T) { m := New(). PickIf(alwaysTrue, "ID", "FirstName"). PickIf(alwaysFalse, "Email").Transform(user) assert.Contains(t, m, "ID") assert.Contains(t, m, "FirstName") assert.NotContains(t, m, "Email") assert.NotContains(t, m, "Age") }
func TestPick(t *testing.T) { m := New().Pick("ID", "Age").Transform(user) assert.Contains(t, m, "ID") assert.Contains(t, m, "Age") assert.NotContains(t, m, "FirstName") m = New().Pick("ID").Pick("Age").Transform(user) assert.Contains(t, m, "ID") assert.Contains(t, m, "Age") assert.Equal(t, m["ID"], user.ID) }
func TestDefaultCase(t *testing.T) { SetDefaultCase(SnakeCase) m := New().PickAll().Transform(user) assert.Contains(t, m, "first_name") SetDefaultCase(CamelCase) m = New().PickAll().Transform(user) assert.Contains(t, m, "firstName") SetDefaultCase(PascalCase) m = New().PickAll().Transform(user) assert.Contains(t, m, "FirstName") }
func TestOmitIf(t *testing.T) { m := New().PickAll().OmitIf(func(u interface{}) bool { return u.(User).HideName }, "FirstName", "LastName").Transform(user) assert.NotContains(t, m, "FirstName", "LastName") m = New().PickAll().OmitIf(func(u interface{}) bool { return u.(User).Age < 18 }, "Birthday", "Age").Transform(user) assert.Contains(t, m, "Birthday") assert.Contains(t, m, "Age") }
func TestAddFunc(t *testing.T) { m := New().AddFunc("FullName", func(u interface{}) interface{} { return u.(User).FirstName + " " + u.(User).LastName }).Transform(user) assert.Contains(t, m, "FullName") assert.Equal(t, user.FirstName+" "+user.LastName, m["FullName"]) }
func TestAddIf(t *testing.T) { m := New().AddIf(alwaysFalse, "Foo", "Bar").Transform(user) assert.NotContains(t, m, "Foo") m = New().AddIf(alwaysTrue, "Foo", "Bar").Transform(user) assert.Contains(t, m, "Foo") assert.Equal(t, "Bar", m["Foo"]) }
func TestAddFuncIf(t *testing.T) { m := New().AddFuncIf(alwaysTrue, "Foo", func(u interface{}) interface{} { return "Bar" }).Transform(user) assert.Contains(t, m, "Foo") assert.Equal(t, m["Foo"], "Bar") m = New().AddFuncIf(alwaysFalse, "Foo", identity).Transform(user) assert.NotContains(t, m, "Foo") }
func TestCustomSerializer(t *testing.T) { m := NewCustomSerializer().WithPrivateinfo().WithBasicInfo().Transform(user) for _, field := range []string{"ID", "FirstName", "LastName", "Email"} { assert.Contains(t, m, field) } }
func TestUsePascalCase(t *testing.T) { m := New().UsePascalCase().PickAll().Transform(user) assert.Contains(t, m, "ID") assert.Contains(t, m, "FirstName") }
func TestUseCamelCase(t *testing.T) { m := New().UseCamelCase().PickAll().Transform(user) assert.Contains(t, m, "id") assert.Contains(t, m, "firstName") assert.NotContains(t, m, "FirstName") }
func TestAdd(t *testing.T) { m := New().Add("Foo", "Bar").Transform(user) assert.Contains(t, m, "Foo") assert.Equal(t, "Bar", m["Foo"]) }
func TestOmit(t *testing.T) { m := New().PickAll().Omit("Birthday", "FirstName").Transform(user) assert.NotContains(t, m, "Birthday") assert.NotContains(t, m, "FirstName") assert.Contains(t, m, "ID") }
func TestPickAll(t *testing.T) { m := New().PickAll().Transform(user) assert.Contains(t, m, "ID") assert.Contains(t, m, "Age") assert.Contains(t, m, "FirstName") }