Repository: bxcodec/faker
Pull request #115 (2020-06-22 14:50:42)

feat: Add RemoveProvider public method
This method removes existing customization added with AddProvider.
This can be useful if you need to reset your customization and add
a new one with the same name.

codecov[bot]: # [Codecov](https://codecov.io/gh/bxcodec/faker/pull/115?src=pr&el=h1) Report
> Merging [#115](https://codecov.io/gh/bxcodec/faker/pull/115?src=pr&el=desc) into [master](https://codecov.io/gh/bxcodec/faker/commit/02fc1b24f66d9227a11c9bc375adb50d8cdbfd28&el=desc) will **increase** coverage by `0.02%`.
> The diff coverage is `100.00%`.

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

```diff
@@            Coverage Diff             @@
##           master     #115      +/-   ##
==========================================
+ Coverage   93.87%   93.89%   +0.02%     
==========================================
  Files          11       11              
  Lines        1289     1294       +5     
==========================================
+ Hits         1210     1215       +5     
  Misses         43       43              
  Partials       36       36              
```


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

------

[Continue to review full report at Codecov](https://codecov.io/gh/bxcodec/faker/pull/115?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/115?src=pr&el=footer). Last update [02fc1b2...518ff02](https://codecov.io/gh/bxcodec/faker/pull/115?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 3bdbd44..d10ccd5 100644
--- a/faker.go
+++ b/faker.go
@@ -227,6 +227,7 @@ var mapperTag = map[string]TaggedFunction{
 // 		ErrValueNotPtr: Error when value is not pointer
 // 		ErrTagNotSupported: Error when tag is not supported
 // 		ErrTagAlreadyExists: Error when tag exists and call AddProvider
+// 		ErrTagDoesNotExist: Error when tag does not exist and call RemoveProvider
 // 		ErrMoreArguments: Error on passing more arguments
 // 		ErrNotSupportedPointer: Error when passing unsupported pointer
 var (
@@ -235,6 +236,7 @@ var (
 	ErrValueNotPtr         = "Not a pointer value"
 	ErrTagNotSupported     = "Tag unsupported: %s"
 	ErrTagAlreadyExists    = "Tag exists"
+	ErrTagDoesNotExist     = "Tag does not exist"
 	ErrMoreArguments       = "Passed more arguments than is possible : (%d)"
 	ErrNotSupportedPointer = "Use sample:=new(%s)\n faker.FakeData(sample) instead"
 	ErrSmallerThanZero     = "Size:%d is smaller than zero."
@@ -391,6 +393,17 @@ func AddProvider(tag string, provider TaggedFunction) error {
 	return nil
 }
 
+// RemoveProvider removes existing customization added with AddProvider
+func RemoveProvider(tag string) error {
+	if _, ok := mapperTag[tag]; !ok {
+		return errors.New(ErrTagDoesNotExist)
+	}
+
+	delete(mapperTag, tag)
+
+	return nil
+}
+
 func getValue(a interface{}) (reflect.Value, error) {
 	t := reflect.TypeOf(a)
 	if t == nil {


Test patch:
diff --git a/faker_test.go b/faker_test.go
index 8e297e6..e8dce13 100644
--- a/faker_test.go
+++ b/faker_test.go
@@ -1092,6 +1092,28 @@ func TestTagAlreadyExists(t *testing.T) {
 	}
 }
 
+func TestRemoveProvider(t *testing.T) {
+	err := AddProvider("new_test_tag", func(v reflect.Value) (interface{}, error) {
+		return "test", nil
+	})
+	if err != nil {
+		t.Error("Expected Not Error, But Got: ", err)
+	}
+
+	err = RemoveProvider("new_test_tag")
+	if err != nil {
+		t.Error("Expected Not Error, But Got: ", err)
+	}
+}
+
+func TestTagDoesNotExist(t *testing.T) {
+	err := RemoveProvider("not_existing_test_tag")
+
+	if err == nil || err.Error() != ErrTagDoesNotExist {
+		t.Error("Expected ErrTagDoesNotExist Error,  But Got: ", err)
+	}
+}
+
 func TestTagWithPointer(t *testing.T) {
 
 	type TestStruct struct {