From fb1212864a890c5e285700913ef3f05c86d78c0a Mon Sep 17 00:00:00 2001 From: Enrico Date: Mon, 22 Jun 2020 16:49:18 +0200 Subject: [PATCH 1/4] 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. --- faker.go | 13 +++++++++++++ faker_test.go | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/faker.go b/faker.go index 0080cfe..2228db8 100644 --- a/faker.go +++ b/faker.go @@ -224,6 +224,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 ( @@ -232,6 +233,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." @@ -388,6 +390,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 { diff --git a/faker_test.go b/faker_test.go index ecefa25..130782d 100644 --- a/faker_test.go +++ b/faker_test.go @@ -1090,6 +1090,25 @@ func TestTagAlreadyExists(t *testing.T) { } } +func TestRemoveProvider(t *testing.T) { + AddProvider("test", func(v reflect.Value) (interface{}, error) { + return "test", nil + }) + + err := RemoveProvider("test") + 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 { From 518ff022d0c61169faed97094206d32084042d34 Mon Sep 17 00:00:00 2001 From: Enrico Date: Mon, 22 Jun 2020 16:54:18 +0200 Subject: [PATCH 2/4] Check AddProvider return value --- faker_test.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/faker_test.go b/faker_test.go index 130782d..5228e45 100644 --- a/faker_test.go +++ b/faker_test.go @@ -1091,11 +1091,14 @@ func TestTagAlreadyExists(t *testing.T) { } func TestRemoveProvider(t *testing.T) { - AddProvider("test", func(v reflect.Value) (interface{}, error) { + 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("test") + err = RemoveProvider("new_test_tag") if err != nil { t.Error("Expected Not Error, But Got: ", err) } From 2902c7de30a8f1ca9f3a5d1702480a4c90337b4f Mon Sep 17 00:00:00 2001 From: Enrico Date: Wed, 24 Jun 2020 16:04:08 +0200 Subject: [PATCH 3/4] Update go.mod --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 6901f72..5b87935 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module github.com/bxcodec/faker/v3 +module github.com/pioz/faker/v3 go 1.12 From ffafd893b313a76ae63a9b20f6cd1270c88e266e Mon Sep 17 00:00:00 2001 From: Enrico Date: Wed, 24 Jun 2020 17:00:31 +0200 Subject: [PATCH 4/4] Revert go.mod --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 5b87935..6901f72 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module github.com/pioz/faker/v3 +module github.com/bxcodec/faker/v3 go 1.12