Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Commit

Permalink
feat(custom-faker): add map support to custom fakers (#70)
Browse files Browse the repository at this point in the history
* add map support to custom fakers #69

* remove map from unspported keep types #69

* test if unsupported map types are supported with custom faker #69

* test if keep works on maps #69
  • Loading branch information
jostillmanns authored and bxcodec committed Nov 15, 2019
1 parent c9820bd commit f4822c5
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 8 deletions.
16 changes: 15 additions & 1 deletion faker.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,11 @@ func getValue(a interface{}) (reflect.Value, error) {
}

func isZero(field reflect.Value) (bool, error) {
for _, kind := range []reflect.Kind{reflect.Struct, reflect.Slice, reflect.Array, reflect.Map} {
if field.Kind() == reflect.Map {
return field.Len() == 0, nil
}

for _, kind := range []reflect.Kind{reflect.Struct, reflect.Slice, reflect.Array} {
if kind == field.Kind() {
return false, fmt.Errorf("keep not allowed on struct")
}
Expand Down Expand Up @@ -558,6 +562,16 @@ func setDataWithTag(v reflect.Value, tag string) error {
}

func userDefinedMap(v reflect.Value, tag string) error {
if tagFunc, ok := mapperTag[tag]; ok {
res, err := tagFunc(v)
if err != nil {
return err
}

v.Set(reflect.ValueOf(res))
return nil
}

len := randomSliceAndMapSize()
if shouldSetNil && len == 0 {
v.Set(reflect.Zero(v.Type()))
Expand Down
47 changes: 40 additions & 7 deletions faker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,34 @@ func TestFakerData(t *testing.T) {

}

func TestCustomFakerOnUnsupportedMapStringInterface(t *testing.T) {
type Sample struct {
Map map[string]interface{} `faker:"custom"`
}

err := AddProvider("custom", func(v reflect.Value) (interface{}, error) {
return map[string]interface{}{"foo": "bar"}, nil
})
if err != nil {
t.Error("Expected NoError, but Got Err", err)
}

var sample = new(Sample)
err = FakeData(sample)
if err != nil {
t.Error("Expected NoError, but Got Err:", err)
}

actual, ok := sample.Map["foo"]
if !ok {
t.Error("map key not set by custom faker")
}

if actual != "bar" {
t.Error("map value not set by custom faker")
}
}

func TestUnsuportedMapStringInterface(t *testing.T) {
type Sample struct {
Map map[string]interface{}
Expand Down Expand Up @@ -847,13 +875,16 @@ func TestItOverwritesDefaultValueIfKeepIsSet(t *testing.T) {
}
func TestItKeepsStructPropertyWhenTagKeepIsSet(t *testing.T) {
type TestStruct struct {
FirstName string `json:"first_name,omitempty" faker:"first_name_male,keep"`
Email string `json:"email,omitempty" faker:"email,keep"`
FirstName string `json:"first_name,omitempty" faker:"first_name_male,keep"`
Email string `json:"email,omitempty" faker:"email,keep"`
Map map[string]string `json:"map,omitempty" faker:"keep"`
}

firstName := "Heino van der Laien"
m := map[string]string{"foo": "bar"}
test := TestStruct{
FirstName: firstName,
Map: m,
}

err := FakeData(&test)
Expand All @@ -865,6 +896,12 @@ func TestItKeepsStructPropertyWhenTagKeepIsSet(t *testing.T) {
t.Fatalf("expected: %s, but got: %s", firstName, test.FirstName)
}

for k, v := range m {
if test.Map[k] != v {
t.Fatalf("expected: %s, but got: %s", m, test.Map)
}
}

if test.Email == "" {
t.Error("expected filled but got empty")
}
Expand All @@ -874,9 +911,6 @@ func TestItThrowsAnErrorWhenKeepIsUsedOnIncomparableType(t *testing.T) {
type TypeStructWithStruct struct {
Struct struct{} `faker:"first_name_male,keep"`
}
type TypeStructWithMap struct {
Map map[string]string `faker:"first_name_male,keep"`
}
type TypeStructWithSlice struct {
Slice []string `faker:"first_name_male,keep"`
}
Expand All @@ -885,11 +919,10 @@ func TestItThrowsAnErrorWhenKeepIsUsedOnIncomparableType(t *testing.T) {
}

withStruct := TypeStructWithStruct{}
withMap := TypeStructWithMap{}
withSlice := TypeStructWithSlice{}
withArray := TypeStructWithArray{}

for _, item := range []interface{}{withArray,withStruct,withMap,withSlice} {
for _, item := range []interface{}{withArray, withStruct, withSlice} {
err := FakeData(&item)
if err == nil {
t.Errorf("expected error, but got nil")
Expand Down

0 comments on commit f4822c5

Please sign in to comment.