faker

package module
v1.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 11, 2017 License: MIT Imports: 7 Imported by: 0

README

Docs

faker

Struct Data Fake Generator

Faker will generate you a fake data based on your Struct.

Build Status codecov Go Report Card License GoDoc

Index

Support

You can file an Issue. See documentation in Godoc

Getting Started

Download
go get -u github.com/bxcodec/faker

Example

With Tag

Supported tag :

  • Email
  • IP Address (IPV4 IPV6 )
  • Credit Card Type (VISA, MASTERCARD , AMERICAN EXPRESS ,DISCOVER)
  • Credit Card Number
  • Latitude and Longitude

package main

import (
	"fmt"
	"github.com/bxcodec/faker"
)

type SomeStruct struct {
 Latitude         float32 `faker:"lat"`
 Long             float32 `faker:"long"`
 CreditCardType   string  `faker:"cc_type"`
 CreditCardNumber string  `faker:"cc_number"`
 Email            string  `faker:"email"`
 IPV4             string  `faker:"ipv4"`
 IPV6             string  `faker:"ipv6"`

}

func main() {

  a= SomeStruct{}
  err:= faker.FakeData(&a)
  if err!= nil {
    fmt.Println(err)
  }
  fmt.Printf("%+v", a)
	//Will Print  :
	// {Latitude:31.456718 Long:159.9867 CreditCardType:Discover CreditCardNumber:6011091892846730 Email:MeHOZEM@Zxhmd.com IPV4:76.24.239.144 IPV6:e8e9:4a26:4c2b:20d3:541e:98b:c5fd:aa26}PASS

}

Without Tag

package main

import (
	"fmt"
	"github.com/bxcodec/faker"
)

type SomeStruct struct {
	Int      int
	Int8     int8
	Int16    int16
	Int32    int32
	Int64    int64
	String   string
	Bool     bool
	SString  []string
	SInt     []int
	SInt8    []int8
	SInt16   []int16
	SInt32   []int32
	SInt64   []int64
	SFloat32 []float32
	SFloat64 []float64
	SBool    []bool
	Struct   AStruct
}
type AStruct struct {
	Number        int64
	Height        int64
	AnotherStruct BStruct
}

type BStruct struct {
	Image string
}

func main() {

  a= SomeStruct{}
  err:= faker.FakeData(&a)
  if err!= nil {
    fmt.Println(err)
  }
  fmt.Printf("%+v", a)
}

Output :

{Int:7088293148785081331 Int8:7 Int16:14 Int32:1777976883 Int64:2467854463682814928 String:XMhCTmwvVqEUryIKnpWrQmBdb Bool:true SString:[iiCGZ GESVVaP] SInt:[2391903971675293806 5270400206229440165 7315288441301820955] SInt8:[124 104 84] SInt16:[-9403 -23327 -19174] SInt32:[1714966339 1617248797 1233525792] SInt64:[6505581000035730776 989945489581839946 7467254172609770414] SFloat32:[0.6761954 0.13427323 0.35608092] SFloat64:[0.49714054026277343 0.29188223737765046 0.7800285978504301] SBool:[true true true] Struct:{Number:8662858647992239649 Height:2466984558238338402 AnotherStruct:{Image:kNIwoxPiVcOqQxBUyyAuDAKom}}}

Example to use Faker

Bench To Generate Fake Data

Without Tag
 BenchmarkFakerDataNOTTagged-4             500000              2871 ns/op             488 B/op         20 allocs/op

Using Tag
 BenchmarkFakerDataTagged-4                300000              4041 ns/op             383 B/op         26 allocs/op
MUST KNOW

The Struct Field must PUBLIC.
Support Only For :

  • int int8 int16 int32 int64
  • []int []int8 []int16 []int32 []int64
  • bool []bool
  • string []string
  • float32 float64 []float32 []float64
  • Nested Struct Field for Non POINTER
  • time.Time []time.Time

Documentation

Index

Constants

View Source
const (
	Email              = "email"
	IPV4               = "ipv4"
	IPV6               = "ipv6"
	LATITUDE           = "lat"
	LONGITUDE          = "long"
	CREDIT_CARD_NUMBER = "cc_number"
	CREDIT_CARD_TYPE   = "cc_type"
)

Variables

This section is empty.

Functions

func CreditCardNum added in v1.2.1

func CreditCardNum(ccType string) string

CreditCardNum generated credit card number according to the card number rules

func CreditCardType added in v1.2.1

func CreditCardType() string

func FakeData

func FakeData(a interface{}) error

FakeData is the main function. Will generate a fake data based on your struct. You can use this for automation testing, or anything that need automated data. You don't need to Create your own data for your testing.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL