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 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: 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 (i.e. hex numbers, etc). 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
113 changes: 103 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,7 @@ 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."
)

// Compiled regexp
Expand Down Expand Up @@ -865,17 +867,108 @@ 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.Float64:
{
toRet, err := extractFloat64FromTagArgs(args)
if err != nil {
return nil, err
}
return toRet.(float64), nil
}
case reflect.Float32:
{
toRet, err := extractFloat32FromTagArgs(args)
if err != nil {
return nil, err
}
return toRet.(float32), nil
}
case reflect.Int64:
{
toRet, err := extractInt64FromTagArgs(args)
if err != nil {
return nil, err
}
return toRet.(int64), nil
}
case reflect.Int32:
{
toRet, err := extractInt32FromTagArgs(args)
if err != nil {
return nil, err
}
return toRet.(int32), nil
}
case reflect.Int16:
{
toRet, err := extractInt16FromTagArgs(args)
if err != nil {
return nil, err
}
return toRet.(int16), nil
}
case reflect.Int8:
{
toRet, err := extractInt8FromTagArgs(args)
if err != nil {
return nil, err
}
return toRet.(int8), nil
}
case reflect.Int:
{
toRet, err := extractIntFromTagArgs(args)
if err != nil {
return nil, err
}
return toRet.(int), nil
}
case reflect.Uint64:
{
toRet, err := extractUint64FromTagArgs(args)
if err != nil {
return nil, err
}
return toRet.(uint64), nil
}
case reflect.Uint32:
{
toRet, err := extractUint32FromTagArgs(args)
if err != nil {
return nil, err
}
return toRet.(uint32), nil
}
case reflect.Uint16:
{
toRet, err := extractUint16FromTagArgs(args)
if err != nil {
return nil, err
}
return toRet.(uint16), nil
}
case reflect.Uint8:
{
toRet, err := extractUint8FromTagArgs(args)
if err != nil {
return nil, err
}
return toRet.(uint8), nil
}
case reflect.Uint:
{
toRet, err := extractUintFromTagArgs(args)
if err != nil {
return nil, err
}
return toRet.(uint), 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