Repository: bxcodec/faker
Pull request #32 (2018-08-22 17:40:58)

feat: Add skipping field tag
Hello. Thank you for good library. But I don't see ability to skip some fields for faking data. This PR should be fix it.



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

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

```diff
@@            Coverage Diff             @@
##           master      #32      +/-   ##
==========================================
+ Coverage   97.84%   97.86%   +0.01%     
==========================================
  Files           8        8              
  Lines         372      375       +3     
==========================================
+ Hits          364      367       +3     
  Misses          4        4              
  Partials        4        4
```


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

------

[Continue to review full report at Codecov](https://codecov.io/gh/bxcodec/faker/pull/32?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/32?src=pr&el=footer). Last update [25eb97b...d379b4a](https://codecov.io/gh/bxcodec/faker/pull/32?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 7794068..834f120 100644
--- a/faker.go
+++ b/faker.go
@@ -55,6 +55,7 @@ const (
 	WORD               = "word"
 	SENTENCE           = "sentence"
 	SENTENCES          = "sentences"
+	SKIP               = "-"
 )
 
 var mapperTag = map[string]interface{}{
@@ -168,14 +169,18 @@ func getValue(t reflect.Type) (reflect.Value, error) {
 					continue // to avoid panic to set on unexported field in struct
 				}
 				tag := t.Field(i).Tag.Get(tagName)
-				if tag == "" {
+
+				switch tag {
+				case "":
 					val, err := getValue(v.Field(i).Type())
 					if err != nil {
 						return reflect.Value{}, err
 					}
 					val = val.Convert(v.Field(i).Type())
 					v.Field(i).Set(val)
-				} else {
+				case SKIP:
+					continue
+				default:
 					err := setDataWithTag(v.Field(i).Addr(), tag)
 					if err != nil {
 						return reflect.Value{}, err


Test patch:
diff --git a/faker_test.go b/faker_test.go
index 3c9c028..42a636b 100644
--- a/faker_test.go
+++ b/faker_test.go
@@ -370,3 +370,23 @@ func TestUnexportedFieldStruct(t *testing.T) {
 	}
 	fmt.Printf(" A value: %+v , SampleStruct Value: %+v  ", a, a)
 }
+
+func TestSkipField(t *testing.T) {
+	// This test is to ensure that the faker won't fill field with tag skip
+
+	a := struct {
+		ID              int
+		ShouldBeSkipped int `faker:"-"`
+	}{}
+
+	err := FakeData(&a)
+
+	if err != nil {
+		t.Error("Expected Not Error, But Got: ", err)
+	}
+
+	if a.ShouldBeSkipped != 0 {
+		t.Error("Expected that field will be skipped")
+	}
+
+}