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

oneof float support #106

Merged
merged 16 commits into from
Jun 11, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ Unfortunately this library has some limitation
* It does not support the `map[interface{}]interface{}`, `map[any_type]interface{}` & `map[interface{}]any_type` data types. Once again, we cannot generate values for an unknown data type.
* Custom types are not fully supported. However some custom types are already supported: we are still investigating how to do this the correct way. For now, if you use `faker`, it's safer not to use any custom types in order to avoid panics.
* Some extra custom types can be supported IF AND ONLY IF extended with [AddProvider()](https://github.com/bxcodec/faker/blob/9169c33ae9926e5b8f8732909790ee20b10b736a/faker.go#L320) please see [example](example_custom_faker_test.go#L46)
* The `oneof` tag currently only supports `string` & `int`. Further support is coming soon. See [example](example_with_tags_test.go#L53) for usage.
* The `oneof` tag currently only supports `string`, the `int` types, and both `float32` & `float64`. Further support is coming soon. See [example](example_with_tags_test.go#L53) for usage.

## Contribution

Expand Down
24 changes: 23 additions & 1 deletion example_with_tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ type SomeStructWithTags struct {
Skip string `faker:"-"`
PaymentMethod string `faker:"oneof: cc, paypal, check, money order"` // oneof will randomly pick one of the comma-separated values supplied in the tag
AccountID int `faker:"oneof: 15, 27, 61"` // use commas to separate the values for now. Future support for other separator characters may be added
Price32 float32 `faker:"oneof: 4.95, 9.99, 31997.97"`
Price64 float64 `faker:"oneof: 47463.9463525, 993747.95662529, 11131997.978767990"`
NumS64 int64 `faker:"oneof: 1, 2"`
NumS32 int32 `faker:"oneof: -3, 4"`
NumS16 int16 `faker:"oneof: -5, 6"`
NumS8 int8 `faker:"oneof: 7, -8"`
NumU64 uint64 `faker:"oneof: 9, 10"`
NumU32 uint32 `faker:"oneof: 11, 12"`
NumU16 uint16 `faker:"oneof: 13, 14"`
NumU8 uint8 `faker:"oneof: 15, 16"`
NumU uint `faker:"oneof: 17, 18"`
}

func Example_withTags() {
Expand Down Expand Up @@ -107,7 +118,18 @@ func Example_withTags() {
UUIDHypenated: 8f8e4463-9560-4a38-9b0c-ef24481e4e27,
UUID: 90ea6479fd0e4940af741f0a87596b73,
PaymentMethod: paypal,
AccountID: 61
AccountID: 61,
Price32: 4.95,
Price64: 993747.95662529
NumS64: 1
NumS32: -3
NumS16: 5
NumS8: -8
NumU64: 9
NumU32: 11
NumU16: 13
NumU8: 15
NumU: 17
Skip:
}
*/
Expand Down
112 changes: 102 additions & 10 deletions faker.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ const (
comma = ","
colon = ":"
ONEOF = "oneof"
// period = "."
//period = "."
hyphen = "-"
)

var defaultTag = map[string]string{
Expand Down Expand Up @@ -239,6 +240,8 @@ var (
ErrUnsupportedTagArguments = "Tag arguments are not compatible with field type."
ErrDuplicateSeparator = "Duplicate separator for tag arguments."
ErrNotEnoughTagArguments = "Not enough arguments for tag."
ErrUnsupportedNumberType = "Unsupported Number type."
ErrMixedSignedWithUnsigned = "Passed signed int to unsigned type."
)

// Compiled regexp
Expand Down Expand Up @@ -865,17 +868,106 @@ func extractNumberFromTag(tag string, t reflect.Type) (interface{}, error) {
if len(args) < 2 {
return nil, fmt.Errorf(ErrNotEnoughTagArguments)
}
var numberValues []int
for _, i := range args {
k := strings.TrimSpace(i)
j, err := strconv.Atoi(k)
if err != nil {
return nil, fmt.Errorf(ErrUnsupportedTagArguments)
switch t.Kind() {
case reflect.Float32:
{
bytes := 32
var floatValues []float32
for _, i := range args {
k := strings.TrimSpace(i)
j, err := strconv.ParseFloat(k, bytes)
if err != nil {
return nil, fmt.Errorf(ErrUnsupportedTagArguments)
}
floatValues = append(floatValues, float32(j))
}
toRet := floatValues[rand.Intn(len(floatValues))]
return toRet, nil
}
case reflect.Float64:
{
bytes := 64
var floatValues []float64
for _, i := range args {
k := strings.TrimSpace(i)
j, err := strconv.ParseFloat(k, bytes)
if err != nil {
return nil, fmt.Errorf(ErrUnsupportedTagArguments)
}
floatValues = append(floatValues, j)
}
toRet := floatValues[rand.Intn(len(floatValues))]
return toRet, nil
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
{
var numberValues []int
for _, i := range args {
k := strings.TrimSpace(i)
switch t.Kind() {
case reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8, reflect.Uint:
{
if strings.Contains(k, hyphen) {
return nil, fmt.Errorf(ErrMixedSignedWithUnsigned)
}
}
}
j, err := strconv.Atoi(k)
if err != nil {
return nil, fmt.Errorf(ErrUnsupportedTagArguments)
}
numberValues = append(numberValues, j)
}
toRet := numberValues[rand.Intn(len(numberValues))]
switch t.Kind() {
case reflect.Int64:
{
return int64(toRet), nil
}
case reflect.Int32:
{
return int32(toRet), nil
}
case reflect.Int16:
{
return int16(toRet), nil
}
case reflect.Int8:
{
return int8(toRet), nil
}
case reflect.Uint64:
{
return uint64(toRet), nil
}
case reflect.Uint32:
{
return uint32(toRet), nil
}
case reflect.Uint16:
{
return uint16(toRet), nil
}
case reflect.Uint8:
{
return uint8(toRet), nil
}
case reflect.Uint:
{
return uint(toRet), nil
}
default:
{
return toRet, nil
}
}
}
default:
{
return nil, fmt.Errorf(ErrUnsupportedNumberType)
}
numberValues = append(numberValues, j)
}
toRet := numberValues[rand.Intn(len(numberValues))]
return toRet, nil
}

// handling boundary tags
Expand Down
Loading