Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Luoxin committed May 20, 2020
1 parent f7a278c commit 7cd6f58
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 48 deletions.
90 changes: 47 additions & 43 deletions faker.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ func (f *Faker) Choice(itemList interface{}) interface{} {
}

if ref.Len() == 0 {
return itemList
ref = reflect.MakeSlice(ref.Type(), 1, 1)
return ref.Index(0).Interface()
}

return ref.Index(f.Generator.Intn(ref.Len())).Interface()
Expand Down Expand Up @@ -203,6 +204,51 @@ func (f *Faker) ShuffleString(s string) string {
return strings.Join(dest, "")
}

func (f *Faker) Bool() bool {
return f.IntBetween(0, 100) > 50
}

// 创建新的
func (f *Faker) InitGenerator() {
f.SetSeed(time.Now().UnixNano())
}

func (f *Faker) SetSeed(seed int64) {
f.Generator = rand.New(rand.NewSource(seed))
}

func (f *Faker) SetLanguage(i18n I18nLanguage) {
f.Language = i18n
}

func New() *Faker {
f := &Faker{
Generator: rand.New(rand.NewSource(time.Now().UnixNano())),
Language: I18nLanguageEnUs,
}
f.InitProviderMap()
return f
}

// 格式化数据
func (f *Faker) Format(fmt string, args map[string]interface{}) (out string) {
var msg bytes.Buffer

tmpl, err := template.New("").Parse(fmt)
if err != nil {
return fmt
}
err = tmpl.Execute(&msg, args)
if err != nil {
return fmt
}

out = msg.String()
out = f.Bothify(out)
out = f.Asciify(out)
return out
}

func (f *Faker) Numerify(in string) (out string) {
for _, c := range strings.Split(in, "") {
if c == "#" {
Expand Down Expand Up @@ -244,45 +290,3 @@ func (f *Faker) Asciify(in string) (out string) {

return
}

func (f *Faker) Bool() bool {
return f.IntBetween(0, 100) > 50
}

// 创建新的
func (f *Faker) InitGenerator() {
f.SetSeed(time.Now().UnixNano())
}

func (f *Faker) SetSeed(seed int64) {
f.Generator = rand.New(rand.NewSource(seed))
}

func (f *Faker) SetLanguage(i18n I18nLanguage) {
f.Language = i18n
}

func New() *Faker {
f := &Faker{
Generator: rand.New(rand.NewSource(time.Now().UnixNano())),
Language: I18nLanguageEnUs,
}
f.InitProviderMap()
return f
}

// 格式化数据
func (f *Faker) Format(fmt string, args map[string]interface{}) string {
var msg bytes.Buffer

tmpl, err := template.New("").Parse(fmt)
if err != nil {
return fmt
}
err = tmpl.Execute(&msg, args)
if err != nil {
return fmt
}

return msg.String()
}
2 changes: 1 addition & 1 deletion person.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (f *Faker) PersonNameFemale() (name string) {

nameFormatTemplate := p.Person.NameFormatTemplate
if nameFormatTemplate == "" {
nameFormatTemplate = "{{FirstName}}{{LastName}}"
nameFormatTemplate = "{{.FirstName}}{{.LastName}}"
}

return f.Format(nameFormatTemplate, map[string]interface{}{
Expand Down
24 changes: 24 additions & 0 deletions phone_number.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package faker

func (f *Faker) PhoneNumber() (phoneNumber string) {
check := func(provider *Provider) bool {
return provider.PhoneNumber == nil || len(provider.PhoneNumber.PhonePrefixes) == 0
}
p := f.GetProviderWithCheck(check)
if p.PhoneNumber == nil {
return
}

formatTemplate := p.PhoneNumber.PhoneFormatTemplate
if formatTemplate == "" {
formatTemplate = "###-###-###"
}

args := map[string]interface{}{}

if p.PhoneNumber != nil {
args["PhonePrefix"] = f.RandomStringElement(p.PhoneNumber.PhonePrefixes)
}

return f.Format(formatTemplate, args)
}
24 changes: 21 additions & 3 deletions providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,17 @@ type InternetProvider struct {
FreeEmailDomains, Tlds []string
}

type PhoneNumberProvider struct {
PhonePrefixes []string
PhoneFormatTemplate string
}

type Provider struct {
Person *PersonProvider
Address *AddressProvider
Internet *InternetProvider
Person *PersonProvider
Address *AddressProvider
Internet *InternetProvider
PhoneNumber *PhoneNumberProvider

Language I18nLanguage
}

Expand All @@ -39,6 +46,9 @@ func (f *Faker) InitProviderMap() {
Person: &PersonProvider{
NameFormatTemplate: "{{.FirstName}}{{.LastName}}",
},
PhoneNumber: &PhoneNumberProvider{
PhoneFormatTemplate: "###-###-###",
},
},
I18nLanguageEnUs: {
Person: &PersonProvider{
Expand Down Expand Up @@ -1271,6 +1281,14 @@ func (f *Faker) InitProviderMap() {
},
NameFormatTemplate: "{{.LastName}}{{.FirstName}}",
},
PhoneNumber: &PhoneNumberProvider{
PhonePrefixes: []string{
"134", "135", "136", "137", "138", "139", "147", "150,", "151", "152",
"157", "158", "159", "182", "187", "188,", "130", "131", "132", "145",
"155", "156", "185", "186,", "145", "133", "153", "180", "181", "189",
},
PhoneFormatTemplate: "{{.PhonePrefix}}########",
},
},
}

Expand Down
39 changes: 38 additions & 1 deletion utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package faker

import "reflect"
import (
"io"
"reflect"
)

func UniqueSlice(a interface{}) interface{} {
aReflect := reflect.ValueOf(a)
Expand All @@ -26,3 +29,37 @@ func UniqueSlice(a interface{}) interface{} {
func UniqueSliceStr(a []string) []string {
return UniqueSlice(a).([]string)
}

func MakeReflectNew(ref reflect.Value) interface{} {
switch ref.Kind() {
case reflect.String:
return ""
case reflect.Bool:
return false
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return 0
case reflect.Float32, reflect.Float64:
return 0.0
case reflect.Map:
return reflect.MakeMap(ref.Type())
case reflect.Slice:
return reflect.MakeSlice(ref.Type(), 0, 0)
case reflect.Chan:
return reflect.MakeChan(ref.Type(), 0)
case reflect.Func:
return reflect.MakeFunc(ref.Type(), func([]reflect.Value) []reflect.Value {
return []reflect.Value{reflect.ValueOf(io.EOF)}
})
default:
//Uintptr
//Complex64
//Complex128
//Array
//Interface
//Ptr
//Struct
//UnsafePointer
return nil
}
}

0 comments on commit 7cd6f58

Please sign in to comment.