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

Commit

Permalink
fix: resolve pointer on tagged field (#47)
Browse files Browse the repository at this point in the history
* resolve pointer on tagged field
  • Loading branch information
bxcodec authored Jan 22, 2019
1 parent ad75524 commit bfe096f
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 24 deletions.
5 changes: 0 additions & 5 deletions address.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,8 @@ func (i Address) Latitude(v reflect.Value) (interface{}, error) {
kind := v.Kind()
val := i.latitute()
if kind == reflect.Float32 {
v.Set(reflect.ValueOf(val))
return float32(val), nil
}
v.Set(reflect.ValueOf(float64(val)))

return float64(val), nil
}

Expand All @@ -58,9 +55,7 @@ func (i Address) Longitude(v reflect.Value) (interface{}, error) {
kind := v.Kind()
val := i.longitude()
if kind == reflect.Float32 {
v.Set(reflect.ValueOf(val))
return float32(val), nil
}
v.Set(reflect.ValueOf(float64(val)))
return float64(val), nil
}
2 changes: 1 addition & 1 deletion datetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ func (d DateTime) UnixTime(v reflect.Value) (interface{}, error) {
} else {
val = 0
}
v.SetInt(val)

return val, nil
}

Expand Down
14 changes: 0 additions & 14 deletions datetime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package faker

import (
"fmt"
"log"
"reflect"
"testing"
"time"
Expand All @@ -27,19 +26,6 @@ func TestUnixTimeValueValid(t *testing.T) {
t.Error("UnixTime should return time <= now")
}
}
func TestUnixTimeValueNotValid(t *testing.T) {
d := GetDateTimer()
var ref = struct {
some int
}{
some: 1212,
}
d.UnixTime(reflect.ValueOf(&ref.some).Elem())
log.Println(ref.some)
if ref.some != 0 {
t.Errorf("UnixTime should return 0, get : %v", ref.some)
}
}

func TestDate(t *testing.T) {
d := GetDateTimer()
Expand Down
72 changes: 68 additions & 4 deletions faker.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,50 @@ const (
SKIP = "-"
)

var defaultTag = map[string]string{
Email: Email,
MacAddress: MacAddress,
DomainName: DomainName,
URL: URL,
UserName: UserName,
IPV4: IPV4,
IPV6: IPV6,
PASSWORD: PASSWORD,
CreditCardType: CreditCardType,
CreditCardNumber: CreditCardNumber,
LATITUDE: LATITUDE,
LONGITUDE: LONGITUDE,
PhoneNumber: PhoneNumber,
TollFreeNumber: TollFreeNumber,
E164PhoneNumber: E164PhoneNumber,
TitleMale: TitleMale,
TitleFemale: TitleFemale,
FirstName: FirstName,
FirstNameMale: FirstNameMale,
FirstNameFemale: FirstNameFemale,
LastName: LastName,
NAME: NAME,
UnixTime: UnixTime,
DATE: DATE,
TIME: Time,
MonthName: MonthName,
YEAR: Year,
DayOfWeek: DayOfWeek,
DayOfMonthTag: DayOfMonth,
TIMESTAMP: TIMESTAMP,
CENTURY: CENTURY,
TIMEZONE: TIMEZONE,
TimePeriodTag: TimePeriod,
WORD: WORD,
SENTENCE: SENTENCE,
PARAGRAPH: PARAGRAPH,
Currency: Currency,
Amount: Amount,
AmountWithCurrency: AmountWithCurrency,
ID: ID,
HyphenatedID: HyphenatedID,
}

// TaggedFunction ...
type TaggedFunction func(v reflect.Value) (interface{}, error)

Expand Down Expand Up @@ -291,8 +335,32 @@ func setDataWithTag(v reflect.Value, tag string) error {
return errors.New(ErrValueNotPtr)
}

if _, exist := mapperTag[tag]; !exist {
return errors.New(ErrTagNotSupported)
}

v = reflect.Indirect(v)
switch v.Kind() {
case reflect.Ptr:
if _, def := defaultTag[tag]; !def {
res, err := mapperTag[tag](v)
if err != nil {
return err
}
v.Set(reflect.ValueOf(res))
return nil
}

t := v.Type()
newv := reflect.New(t.Elem())
res, err := mapperTag[tag](newv.Elem())
if err != nil {
return err
}
rval := reflect.ValueOf(res)
newv.Elem().Set(rval)
v.Set(newv)
return nil
case reflect.Float32, reflect.Float64:
return userDefinedFloat(v, tag)
case reflect.String:
Expand All @@ -301,15 +369,11 @@ func setDataWithTag(v reflect.Value, tag string) error {
case reflect.Int, reflect.Int32, reflect.Int64, reflect.Int8, reflect.Int16:
return userDefinedInt(v, tag)
default:
if _, exist := mapperTag[tag]; !exist {
return errors.New(ErrTagNotSupported)
}
res, err := mapperTag[tag](v)
if err != nil {
return err
}
v.Set(reflect.ValueOf(res))

}
return nil
}
Expand Down
44 changes: 44 additions & 0 deletions faker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,3 +547,47 @@ func TestTagAlreadyExists(t *testing.T) {
t.Error("Expected ErrTagAlreadyExists Error, But Got: ", err)
}
}

func TestTagWithPointer(t *testing.T) {

type TestStruct struct {
FirstName *string `json:"first_name,omitempty" faker:"first_name_male"`
Email *string `json:"email,omitempty" faker:"email"`
Latitude *float64 `faker:"lat"`
Latitude32 *float32 `faker:"lat"`
UnixTime *int64 `faker:"unix_time"`
School *School `faker:"school"`
}
// With custom provider
AddProvider("school", func(v reflect.Value) (interface{}, error) {
return &School{Location: "Jakarta"}, nil
})

var sample TestStruct
err := FakeData(&sample)
if err != nil {
t.Error("Expected Not Error, But Got: ", err)
}

//Assert
if sample.FirstName == nil || *sample.FirstName == "" {
t.Error("Expected filled but got emtpy")
}
if sample.Email == nil || *sample.Email == "" {
t.Error("Expected filled but got emtpy")
}
if sample.Latitude == nil || *sample.Latitude == 0 {
t.Error("Expected filled but got emtpy")
}
if sample.Latitude32 == nil || *sample.Latitude32 == 0 {
t.Error("Expected filled but got emtpy")
}

if sample.UnixTime == nil || *sample.UnixTime == 0 {
t.Error("Expected filled but got emtpy")
}

if sample.School == nil || sample.School.Location == "" {
t.Error("Expected filled but got emtpy")
}
}

0 comments on commit bfe096f

Please sign in to comment.