Repository: bxcodec/faker
Pull request #41 (2018-11-21 14:09:42)

fix: fake data generation for custom scalar type pointers
There was a panic while faker tries to generate data for custom scalar type pointers

codecov[bot]: # [Codecov](https://codecov.io/gh/bxcodec/faker/pull/41?src=pr&el=h1) Report
> Merging [#41](https://codecov.io/gh/bxcodec/faker/pull/41?src=pr&el=desc) into [master](https://codecov.io/gh/bxcodec/faker/commit/bf89c38ff1742f295e15df0bd5e8b3e81da95855?src=pr&el=desc) will **not change** coverage.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/bxcodec/faker/pull/41/graphs/tree.svg?width=650&token=cLPGB7MHVc&height=150&src=pr)](https://codecov.io/gh/bxcodec/faker/pull/41?src=pr&el=tree)

```diff
@@           Coverage Diff           @@
##           master      #41   +/-   ##
=======================================
  Coverage   98.12%   98.12%           
=======================================
  Files          10       10           
  Lines         426      426           
=======================================
  Hits          418      418           
  Misses          4        4           
  Partials        4        4
```


| [Impacted Files](https://codecov.io/gh/bxcodec/faker/pull/41?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [faker.go](https://codecov.io/gh/bxcodec/faker/pull/41/diff?src=pr&el=tree#diff-ZmFrZXIuZ28=) | `95.45% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/bxcodec/faker/pull/41?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/bxcodec/faker/pull/41?src=pr&el=footer). Last update [bf89c38...3d82e11](https://codecov.io/gh/bxcodec/faker/pull/41?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/faker.go b/faker.go
index 7efb40c..6438b5e 100644
--- a/faker.go
+++ b/faker.go
@@ -113,7 +113,7 @@ var mapperTag = map[string]interface{}{
 // 		ErrUnsupportedKind: Error on passing unsupported kind
 // 		ErrValueNotPtr: Error when value is not pointer
 // 		ErrTagNotSupported: Error when tag is not supported
-//		ErrTagAlreadyExists: Error when tag exists and call AddProvider
+// 		ErrTagAlreadyExists: Error when tag exists and call AddProvider
 // 		ErrMoreArguments: Error on passing more arguments
 // 		ErrNotSupportedPointer: Error when passing unsupported pointer
 var (
@@ -151,7 +151,7 @@ func FakeData(a interface{}) error {
 		return err
 	}
 
-	rval.Elem().Set(finalValue)
+	rval.Elem().Set(finalValue.Convert(reflectType.Elem()))
 	return nil
 }
 
@@ -171,13 +171,12 @@ func getValue(t reflect.Type) (reflect.Value, error) {
 
 	switch k {
 	case reflect.Ptr:
-
 		v := reflect.New(t.Elem())
 		val, err := getValue(t.Elem())
 		if err != nil {
 			return reflect.Value{}, err
 		}
-		v.Elem().Set(val)
+		v.Elem().Set(val.Convert(t.Elem()))
 		return v, nil
 	case reflect.Struct:
 


Test patch:
diff --git a/faker_test.go b/faker_test.go
index e7db415..34de8cd 100644
--- a/faker_test.go
+++ b/faker_test.go
@@ -388,6 +388,32 @@ func TestUnexportedFieldStruct(t *testing.T) {
 	fmt.Printf(" A value: %+v , SampleStruct Value: %+v  ", a, a)
 }
 
+func TestPointerToCustomScalar(t *testing.T) {
+	// This test is to ensure that the faker won't panic if trying to fake data on struct that has field
+	a := new(CustomInt)
+	err := FakeData(a)
+
+	if err != nil {
+		t.Error("Expected Not Error, But Got: ", err)
+	}
+	fmt.Printf(" A value: %+v , Custom scalar Value: %+v  ", a, a)
+}
+
+type PointerCustomIntStruct struct {
+	V *CustomInt
+}
+
+func TestPointerToCustomIntStruct(t *testing.T) {
+	// This test is to ensure that the faker won't panic if trying to fake data on struct that has field
+	a := new(PointerCustomIntStruct)
+	err := FakeData(a)
+
+	if err != nil {
+		t.Error("Expected Not Error, But Got: ", err)
+	}
+	fmt.Printf(" A value: %+v , PointerCustomIntStruct scalar Value: %+v  ", a, a)
+}
+
 func TestSkipField(t *testing.T) {
 	// This test is to ensure that the faker won't fill field with tag skip