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

Commit

Permalink
fix: Allow AddProvider to still work with some custom types (#102)
Browse files Browse the repository at this point in the history
* adding log statements

update module path

add more log statements

add more clear logs

add more log statements in critical places

remove panic causing line

remove panic causing line 2

add check for custom tag handler, and logging

add check for user defined provider before calling built in func

put back module & imports the way they were

add test and logic for checking for custom tag before defaulting to userDefinedArray func

update readme and add example

update example to not be random and therefore pass the test command

add comment

* add additional assertion for better test coverage

Co-authored-by: Iman Tumorang <iman.tumorang@gmail.com>
  • Loading branch information
andrew-werdna and bxcodec authored Jun 4, 2020
1 parent 0e4c0b6 commit 627121e
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ Unfortunately this library has some limitation
* It does not support the `interface{}` data type. How could we generate anything without knowing its data type?
* It does not support the `map[interface{}]interface{}`, `map[any_type]interface{}` & `map[interface{}]any_type` data types. Once again, we cannot generate values for an unknown data type.
* Custom types are not fully supported. However some custom types are already supported: we are still investigating how to do this the correct way. For now, if you use `faker`, it's safer not to use any custom types in order to avoid panics.
* Some extra custom types can be supported IF AND ONLY IF extended with [AddProvider()](https://github.com/bxcodec/faker/blob/9169c33ae9926e5b8f8732909790ee20b10b736a/faker.go#L320) please see [example](example_custom_faker_test.go#L46)

## Contribution

Expand Down
13 changes: 12 additions & 1 deletion example_custom_faker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ type Gondoruwo struct {
Locatadata int
}

// custom type that aliases over slice of byte
type CustomUUID []byte

// Sample ...
type Sample struct {
ID int64 `faker:"customIdFaker"`
Gondoruwo Gondoruwo `faker:"gondoruwo"`
Danger string `faker:"danger"`
UUID CustomUUID `faker:"customUUID"`
}

// CustomGenerator ...
Expand All @@ -36,6 +40,13 @@ func CustomGenerator() {
}
return obj, nil
})

_ = faker.AddProvider("customUUID", func(v reflect.Value) (interface{}, error) {
s := []byte {
0,8,7,2,3,
}
return s, nil
})
}

// You can also add your own generator function to your own defined tags.
Expand All @@ -45,5 +56,5 @@ func Example_customFaker() {
_ = faker.FakeData(&sample)
fmt.Printf("%+v", sample)
// Output:
// {ID:43 Gondoruwo:{Name:Power Locatadata:324} Danger:danger-ranger}
// {ID:43 Gondoruwo:{Name:Power Locatadata:324} Danger:danger-ranger UUID:[0 8 7 2 3]}
}
15 changes: 15 additions & 0 deletions faker.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,21 @@ func setDataWithTag(v reflect.Value, tag string) error {
reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64:
return userDefinedNumber(v, tag)
case reflect.Slice, reflect.Array:
/**
* check for added Provider tag first before
* defaulting to userDefinedArray()
* this way the user at least has the
* option of a custom tag working
*/
_, tagExists := mapperTag[tag]
if tagExists {
res, err := mapperTag[tag](v)
if err != nil {
return err
}
v.Set(reflect.ValueOf(res))
return nil
}
return userDefinedArray(v, tag)
case reflect.Map:
return userDefinedMap(v, tag)
Expand Down
29 changes: 29 additions & 0 deletions faker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,11 @@ type School struct {
Location string
}

type CustomTypeOverSlice []byte
type CustomThatUsesSlice struct {
UUID CustomTypeOverSlice `faker:"custom-type-over-slice"`
}

func TestExtend(t *testing.T) {
// This test is to ensure that faker can be extended new providers

Expand Down Expand Up @@ -961,6 +966,30 @@ func TestExtend(t *testing.T) {
}
})

/**
* Before updates, this test would fail
*/
t.Run("test-with-custom-slice-type", func(t *testing.T) {
a := CustomThatUsesSlice{}
err := AddProvider("custom-type-over-slice", func(v reflect.Value) (interface{}, error) {
return []byte{0,1,2,3,4}, nil
})

if err != nil {
t.Error("Expected Not Error, But Got: ", err)
}

err = FakeData(&a)

if err != nil {
t.Error("Expected Not Error, But Got: ", err)
}

if reflect.DeepEqual(a.UUID, []byte{0,1,2,3,4}) {
t.Error("UUID should equal test value")
}
})

}

func TestTagAlreadyExists(t *testing.T) {
Expand Down

0 comments on commit 627121e

Please sign in to comment.