Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

feat: Support for Japanese hiragana and katakana #145

Merged
merged 5 commits into from Dec 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions example_with_tags_lang_test.go
Expand Up @@ -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{}
Expand All @@ -25,6 +26,7 @@ func Example_withTagsLang() {
StringENG:VVcaPS
StringCHI: 随机字符串
StringRUS:ваЩфз
StringJPN:びゃほぱヒてふ
}
*/
}
6 changes: 5 additions & 1 deletion faker.go
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
18 changes: 17 additions & 1 deletion faker_test.go
Expand Up @@ -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"}
Expand Down Expand Up @@ -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"`
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down