Repository: bxcodec/faker
Pull request #145 (2021-07-20 11:19:54)

Support for Japanese hiragana and katakana
It corresponds to Japanese hiragana and katakana.
Kanji is not supported due to its wide range.


Patch:
diff --git a/faker.go b/faker.go
index 447f600..33b8556 100644
--- a/faker.go
+++ b/faker.go
@@ -60,6 +60,8 @@ var (
 	LangCHI = langRuneBoundary{19968, 40869, nil}
 	// LangRUS is for russian language
 	LangRUS = langRuneBoundary{1025, 1105, nil}
+	// LangJPN is for japanese Hiragana Katakana language
+	LangJPN = langRuneBoundary{12353, 12534, []rune{12436, 12437, 12438, 12439, 12440, 12441, 12442, 12443, 12444, 12445, 12446, 12447, 12448}}
 )
 
 // Supported tags
@@ -306,7 +308,7 @@ func SetRandomStringLength(size int) error {
 	return nil
 }
 
-// SetStringLang sets language of random string generation (LangENG, LangCHI, LangRUS)
+// SetStringLang sets language of random string generation (LangENG, LangCHI, LangRUS, LangJPN)
 func SetStringLang(l langRuneBoundary) {
 	lang = l
 }
@@ -902,6 +904,8 @@ func extractLangFromTag(tag string) (*langRuneBoundary, error) {
 		return &LangRUS, nil
 	case "chi":
 		return &LangCHI, nil
+	case "jpn":
+		return &LangJPN, nil
 	default:
 		return &LangENG, nil
 	}


Test patch:
diff --git a/example_with_tags_lang_test.go b/example_with_tags_lang_test.go
index b87792e..6e4c4e4 100644
--- a/example_with_tags_lang_test.go
+++ b/example_with_tags_lang_test.go
@@ -13,6 +13,7 @@ func Example_withTagsLang() {
 		StringENG string `faker:"lang=eng"`
 		StringCHI string `faker:"lang=chi"`
 		StringRUS string `faker:"lang=rus"`
+		StringJPN string `faker:"lang=jpn"`
 	}
 
 	a := SomeStruct{}
@@ -25,6 +26,7 @@ func Example_withTagsLang() {
 			   StringENG:VVcaPS
 			   StringCHI: 随机字符串
 			   StringRUS:ваЩфз
+			   StringJPN:びゃほぱヒてふ
 		   }
 	*/
 }
diff --git a/faker_test.go b/faker_test.go
index 4c47701..e50d4ed 100644
--- a/faker_test.go
+++ b/faker_test.go
@@ -18,10 +18,11 @@ const (
 	someStructWithLenAndLangENG = 5
 	someStructWithLenAndLangCHI = 10
 	someStructWithLenAndLangRUS = 15
+	someStructWithLenAndLangJPN = 20
 )
 
 var (
-	langCorrectTagsMap = map[string]langRuneBoundary{"lang=eng": LangENG, "lang=chi": LangCHI, "lang=rus": LangRUS}
+	langCorrectTagsMap = map[string]langRuneBoundary{"lang=eng": LangENG, "lang=chi": LangCHI, "lang=rus": LangRUS, "lang=jpn": LangJPN}
 	langUncorrectTags  = [3]string{"lang=", "lang", "lng=eng"}
 
 	lenCorrectTags   = [3]string{"len=4", "len=5", "len=10"}
@@ -110,6 +111,7 @@ type SomeStructWithLang struct {
 	ValueENG string `faker:"lang=eng"`
 	ValueCHI string `faker:"lang=chi"`
 	ValueRUS string `faker:"lang=rus"`
+	ValueJPN string `faker:"lang=jpn"`
 
 	ValueWithUndefinedLang string `faker:"lang=und"`
 }
@@ -118,6 +120,7 @@ type SomeStructWithLenAndLang struct {
 	ValueENG string `faker:"len=5, lang=eng"`
 	ValueCHI string ` faker:"len=10, lang=chi"`
 	ValueRUS string ` faker:"len=15, lang=rus"`
+	ValueJPN string ` faker:"len=20, lang=jpn"`
 }
 
 func (s SomeStruct) String() string {
@@ -588,6 +591,10 @@ func TestLang(t *testing.T) {
 	if err != nil {
 		t.Error(err.Error())
 	}
+	err = isStringLangCorrect(someStruct.ValueJPN, LangJPN)
+	if err != nil {
+		t.Error(err.Error())
+	}
 
 	err = isStringLangCorrect(someStruct.ValueWithUndefinedLang, LangENG)
 	if err != nil {
@@ -721,6 +728,15 @@ func TestLangWithLen(t *testing.T) {
 	if rusLen != someStructWithLenAndLangRUS {
 		t.Errorf("Got %d, but expected to be %d as a string len", rusLen, someStructWithLenAndLangRUS)
 	}
+
+	err = isStringLangCorrect(someStruct.ValueJPN, LangJPN)
+	if err != nil {
+		t.Error(err.Error())
+	}
+	jpnLen := utfLen(someStruct.ValueJPN)
+	if jpnLen != someStructWithLenAndLangJPN {
+		t.Errorf("Got %d, but expected to be %d as a string len", jpnLen, someStructWithLenAndLangJPN)
+	}
 }
 
 func isStringLangCorrect(value string, lang langRuneBoundary) error {