pseudo

package
v0.0.0-...-202847b Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2023 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Overview

package pseudo is the fake data generator, heavily inspired by forgery and ffaker Ruby gems.

CoreStore: This package has been refactored to avoid package global variables and global settings which are an anti-pattern. In a multi store/language environment a package global language limits everything. Also the global PRNG got eliminate and reduces a mutex bottle neck. This package can also handle max_len values for generated data and supports embedded structs which implements sql.Scanner interface.

Most data and methods are ported from forgery/ffaker Ruby gems.

Currently english and russian languages are available.

For the list of available methods please look at https://godoc.org/github.com/icrowley/fake.

Fake embeds samples data files unless you call UseExternalData(true) in order to be able to work without external files dependencies when compiled, so, if you add new data files or make changes to existing ones don't forget to regenerate data.go file using github.com/mjibson/esc tool and esc -o data.go -pkg fake data command (or you can just use go generate command if you are using Go 1.4 or later).

Index

Constants

View Source
const (

	// Skip indicates a struct tag, that the field should be skipped.
	Skip                     = "-"
	DefaultMaxRecursionLevel = 10
)

Supported tags

Variables

This section is empty.

Functions

func Asset

func Asset(name string) ([]byte, error)

Asset loads and returns the asset for the given name. It returns an error if the asset could not be found or could not be loaded.

func AssetDir

func AssetDir(name string) ([]string, error)

AssetDir returns the file names below a certain directory embedded in the file by go-bindata. For example if you run go-bindata on data/... and data contains the following hierarchy:

data/
  foo.txt
  img/
    a.png
    b.png

then AssetDir("data") would return []string{"foo.txt", "img"} AssetDir("data/img") would return []string{"a.png", "b.png"} AssetDir("foo.txt") and AssetDir("notexist") would return an error AssetDir("") will return []string{"data"}.

func AssetInfo

func AssetInfo(name string) (os.FileInfo, error)

AssetInfo loads and returns the asset info for the given name. It returns an error if the asset could not be found or could not be loaded.

func AssetNames

func AssetNames() []string

AssetNames returns the names of the assets. nolint: deadcode

func MustAsset

func MustAsset(name string) []byte

MustAsset is like Asset but panics when Asset would return an error. It simplifies safe initialization of global variables. nolint: deadcode

func RestoreAsset

func RestoreAsset(dir, name string) error

RestoreAsset restores an asset under the given directory

func RestoreAssets

func RestoreAssets(dir, name string) error

RestoreAssets restores an asset under the given directory recursively

func WithTagFakeFunc

func WithTagFakeFunc(tag string, provider FakeFunc) optionFn

WithTagFakeFunc extends faker with a new tag (or field name) to generate fake data with a specified custom algorithm. It will overwrite a previous set function.

func WithTagFakeFuncAlias

func WithTagFakeFuncAlias(aliasTag ...string) optionFn

WithTagFakeFuncAlias sets a new alias. E.g. when WithTagFakeFunc("http",...) adds a function to generate HTTP links, then calling WithTagFakeFuncAlias("https","http","ftps","ftp") would say that https is an alias of the http function and ftps is an alias of ftp.

Types

type FakeFunc

type FakeFunc func(maxLen int) interface{}

FakeFunc generates new specific fake values. The returned interface{} type can only contain primitive types and time.Time.

type Faker

type Faker interface {
	Fake(fieldName string) (hasFakeDataApplied bool, err error)
}

Faker allows a type to implement a custom fake data generation. The argument fieldName contains the name of the current field for which random/fake data should be generated. The return argument hasFakeDataApplied can be set to true, if fake data gets generated for the current field. Setting hasFakeDataApplied to false, the fake data should be generated by this package.

type Options

type Options struct {
	Lang            string
	UseExternalData bool
	EnFallback      bool
	TimeLocation    *time.Location // defaults to UTC
	// RespectValidField if enabled allows to observe the `Valid bool` field of
	// a struct. Like sql.NullString, sqlNullInt64 or all null.* types. If Valid
	// is false, the other fields will be reset to their repective default
	// values. Reason: All fields of a struct are getting applied with fake
	// data, if Valid is false and the field gets written to the e.g. DB and
	// later compared.
	RespectValidField bool
	// DisabledFieldNameUse if enabled avoids the usage of the field name
	// instead of the struct tag to find out which kind of random function is
	// needed.
	DisabledFieldNameUse bool
	// MaxLenStringLimit defines the upper bound of the maximal length for a
	// string. If not set, defaults to 512.
	MaxLenStringLimit uint64
	// MaxFloatDecimals limits the float generation to this amount of decimals.
	// Useful for MySQL/MariaDB float column type.
	MaxFloatDecimals int
	// MaxRecursionLevel default see DefaultMaxRecursionLevel
	MaxRecursionLevel int
}

Options applied for the Service type.

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service provides a service to generate fake data.

func MustNewService

func MustNewService(seed uint64, o *Options, opts ...optionFn) *Service

MustNewService creates a new Service but panics on error.

func NewService

func NewService(seed uint64, o *Options, opts ...optionFn) (*Service, error)

NewService creates a new fake service.

func (*Service) Address

func (s *Service) Address() string

Address returns an american style address

func (*Service) Brand

func (s *Service) Brand() string

Brand generates brand name

func (*Service) Character

func (s *Service) Character() string

Character generates random character in the given language

func (*Service) Characters

func (s *Service) Characters() string

Characters generates from 1 to 5 characters in the given language

func (*Service) CharactersN

func (s *Service) CharactersN(n int) string

CharactersN generates n random characters in the given language

func (*Service) City

func (s *Service) City() string

City generates random city

func (*Service) Clock

func (s *Service) Clock() string

Clock formats DateTime to return e.g. 15:04, the clock time.

func (*Service) Color

func (s *Service) Color() string

Color generates color name

func (*Service) Company

func (s *Service) Company() string

Company generates company name

func (*Service) CompanyLegal

func (s *Service) CompanyLegal() string

Company generates company name

func (*Service) Continent

func (s *Service) Continent() string

Continent generates random continent

func (*Service) Country

func (s *Service) Country() string

Country generates random country

func (*Service) CountryISO2

func (s *Service) CountryISO2() string

CountryISO2 generates a random country iso2 code

func (*Service) CreditCardNum

func (s *Service) CreditCardNum(vendor string) string

CreditCardNum generated credit card number according to the card number rules

func (*Service) CreditCardType

func (s *Service) CreditCardType() string

CreditCardType returns one of the following credit values: VISA, MasterCard, American Express and Discover

func (*Service) Currency

func (s *Service) Currency() string

Currency generates currency name.

func (*Service) CurrencyCode

func (s *Service) CurrencyCode() string

CurrencyCode generates currency code.

func (*Service) Date

func (s *Service) Date() string

Date formats DateTime using example BaseDate const

func (*Service) Day

func (s *Service) Day() int

Day generates day of the month

func (*Service) Digits

func (s *Service) Digits() string

Digits returns from 1 to 5 digits as a string

func (*Service) DigitsN

func (s *Service) DigitsN(n int) string

DigitsN returns n digits as a string

func (*Service) Dob18

func (s *Service) Dob18() time.Time

Dob18 returns a date of birth in the format 2006-01-02 00:00:00 with a minimum age of 18 years.

func (*Service) DomainName

func (s *Service) DomainName() string

DomainName generates random domain name

func (*Service) DomainZone

func (s *Service) DomainZone() string

DomainZone generates random domain zone

func (*Service) EmailAddress

func (s *Service) EmailAddress() string

EmailAddress generates email address

func (*Service) EmailBody

func (s *Service) EmailBody() string

EmailBody generates random email body

func (*Service) EmailSubject

func (s *Service) EmailSubject(maxLen int) string

EmailSubject generates random email subject

func (*Service) FakeData

func (s *Service) FakeData(ptr 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. Unsupported types are getting ignored.

func (*Service) FemaleFirstName

func (s *Service) FemaleFirstName() string

FemaleFirstName generates female first name

func (*Service) FemaleFullName

func (s *Service) FemaleFullName() string

FemaleFullName generates female full name it can occasionally include prefix or suffix

func (*Service) FemaleFullNameWithPrefix

func (s *Service) FemaleFullNameWithPrefix() string

FemaleFullNameWithPrefix generates prefixed female full name if prefixes for the given language are available

func (*Service) FemaleFullNameWithSuffix

func (s *Service) FemaleFullNameWithSuffix() string

FemaleFullNameWithSuffix generates suffixed female full name if suffixes for the given language are available

func (*Service) FemaleLastName

func (s *Service) FemaleLastName() string

FemaleLastName generates female last name

func (*Service) FemalePatronymic

func (s *Service) FemalePatronymic() string

FemalePatronymic generates female patronymic

func (*Service) FirstName

func (s *Service) FirstName() string

FirstName generates first name

func (*Service) Float64

func (s *Service) Float64() float64

func (*Service) FullName

func (s *Service) FullName() string

FullName generates full name it can occasionally include prefix or suffix

func (*Service) FullNameWithPrefix

func (s *Service) FullNameWithPrefix() string

FullNameWithPrefix generates prefixed full name if prefixes for the given language are available

func (*Service) FullNameWithSuffix

func (s *Service) FullNameWithSuffix() string

FullNameWithSuffix generates suffixed full name if suffixes for the given language are available

func (*Service) Gender

func (s *Service) Gender() string

Gender generates random gender

func (*Service) GenderAbbrev

func (s *Service) GenderAbbrev() string

GenderAbbrev returns first downcased letter of the random gender

func (*Service) GetLangs

func (s *Service) GetLangs() []string

GetLangs returns a slice of available languages

func (*Service) HexColor

func (s *Service) HexColor() string

HexColor generates hex color name

func (*Service) HexColorShort

func (s *Service) HexColorShort() string

HexColorShort generates short hex color name

func (*Service) ID

func (s *Service) ID() uint64

ID returns a never seen before unique ID.

func (*Service) IPv4

func (s *Service) IPv4() string

IPv4 generates IPv4 address

func (*Service) IPv6

func (s *Service) IPv6() string

IPv6 generates IPv6 address

func (*Service) Industry

func (s *Service) Industry() string

Industry generates industry name

func (*Service) Intn

func (s *Service) Intn(n int) int

Intn returns a non-negative pseudo-random int.

func (*Service) JobTitle

func (s *Service) JobTitle() string

JobTitle generates job title

func (*Service) Language

func (s *Service) Language() string

Language generates random human language

func (*Service) LastName

func (s *Service) LastName() string

LastName generates last name

func (*Service) Latitude

func (s *Service) Latitude() float64

Latitude generates latitude (from -90.0 to 90.0)

func (*Service) LatitudeDegrees

func (s *Service) LatitudeDegrees() int

LatitudeDegrees generates latitude degrees (from -90 to 90)

func (*Service) LatitudeDirection

func (s *Service) LatitudeDirection() string

LatitudeDirection generates latitude direction (N(orth) o S(outh))

func (*Service) LatitudeMinutes

func (s *Service) LatitudeMinutes() int

LatitudeMinutes generates latitude minutes (from 0 to 60)

func (*Service) LatitudeSeconds

func (s *Service) LatitudeSeconds() int

LatitudeSeconds generates latitude seconds (from 0 to 60)

func (*Service) Longitude

func (s *Service) Longitude() float64

Longitude generates longitude (from -180 to 180)

func (*Service) LongitudeDegrees

func (s *Service) LongitudeDegrees() int

LongitudeDegrees generates longitude degrees (from -180 to 180)

func (*Service) LongitudeDirection

func (s *Service) LongitudeDirection() string

LongitudeDirection generates (W(est) or E(ast))

func (*Service) LongitudeMinutes

func (s *Service) LongitudeMinutes() int

LongitudeMinutes generates (from 0 to 60)

func (*Service) LongitudeSeconds

func (s *Service) LongitudeSeconds() int

LongitudeSeconds generates (from 0 to 60)

func (*Service) MacAddress

func (s *Service) MacAddress() string

MacAddress generates random MacAddress

func (*Service) MaleFirstName

func (s *Service) MaleFirstName() string

MaleFirstName generates male first name

func (*Service) MaleFullName

func (s *Service) MaleFullName() string

MaleFullName generates male full name it can occasionally include prefix or suffix

func (*Service) MaleFullNameWithPrefix

func (s *Service) MaleFullNameWithPrefix() string

MaleFullNameWithPrefix generates prefixed male full name if prefixes for the given language are available

func (*Service) MaleFullNameWithSuffix

func (s *Service) MaleFullNameWithSuffix() string

MaleFullNameWithSuffix generates suffixed male full name if suffixes for the given language are available

func (*Service) MaleLastName

func (s *Service) MaleLastName() string

MaleLastName generates male last name

func (*Service) MalePatronymic

func (s *Service) MalePatronymic() string

MalePatronymic generates male patronymic

func (*Service) Model

func (s *Service) Model() string

Model generates model name that consists of letters and digits, optionally with a hyphen between them

func (*Service) Month

func (s *Service) Month() string

Month generates month name

func (*Service) MonthNum

func (s *Service) MonthNum() int

MonthNum generates month number (from 1 to 12)

func (*Service) MonthShort

func (s *Service) MonthShort() string

MonthShort generates abbreviated month name

func (*Service) Paragraph

func (s *Service) Paragraph(maxLen int) string

Paragraph generates paragraph

func (*Service) Paragraphs

func (s *Service) Paragraphs(maxLen int) string

Paragraphs generates from 1 to 5 paragraphs

func (*Service) ParagraphsN

func (s *Service) ParagraphsN(n int, maxLen int) string

ParagraphsN generates n paragraphs

func (*Service) Password

func (s *Service) Password(atLeast, atMost int, allowUpper, allowNumeric, allowSpecial bool) string

Password generates password with the length from atLeast to atMOst charachers, allow* parameters specify whether corresponding symbols can be used

func (*Service) Patronymic

func (s *Service) Patronymic() string

Patronymic generates patronymic

func (*Service) Phone

func (s *Service) Phone() string

Phone generates random phone number using one of the formats format specified in phone_format file

func (*Service) Prefix

func (s *Service) Prefix() string

Prefix returns a random prefix for either a female or male, sometimes an empty prefix.

func (*Service) Price

func (s *Service) Price() float64

Amount returns a random floating price amount with a random precision of [1,2] up to (10**8 - 1)

func (*Service) PriceWithCurrency

func (s *Service) PriceWithCurrency() string

PriceWithCurrency combines both price and currency together

func (*Service) Product

func (s *Service) Product() string

Product generates product title as brand + product name

func (*Service) ProductName

func (s *Service) ProductName() string

ProductName generates product name

func (*Service) RandomUnixTime

func (s *Service) RandomUnixTime() int64

RandomUnixTime returns a random unix time stamp between now and the last 6 years.

func (*Service) Sentence

func (s *Service) Sentence(maxLen int) string

Sentence generates random sentence. If maxLen > 0, it will be considered as the total length of one sentence. The sentence gets cut after maxLen bytes.

func (*Service) Sentences

func (s *Service) Sentences(maxLen int) string

Sentences generates from 1 to 5 random sentences

func (*Service) SentencesN

func (s *Service) SentencesN(count int, maxLen int) string

SentencesN generates n random sentences

func (*Service) SetLang

func (s *Service) SetLang(newLang string) error

SetLang sets the language in which the data should be generated returns error if passed language is not available

func (*Service) SimplePassword

func (s *Service) SimplePassword() string

SimplePassword is a wrapper around Password, it generates password with the length from 6 to 12 symbols, with upper characters and numeric symbols allowed

func (*Service) State

func (s *Service) State() string

State generates random state

func (*Service) StateAbbrev

func (s *Service) StateAbbrev() string

StateAbbrev generates random state abbreviation

func (*Service) Street

func (s *Service) Street() string

Street generates random street name

func (*Service) StreetAddress

func (s *Service) StreetAddress() string

StreetAddress generates random street name along with building number

func (*Service) Suffix

func (s *Service) Suffix() string

Prefix returns a random prefix for either a female or male, sometimes an empty prefix.

func (*Service) Time

func (s *Service) Time() time.Time

Time returns a random time.

func (*Service) TimeStamp

func (s *Service) TimeStamp() string

TimeStamp returns a time in the format 2006-01-02 15:04:05

func (*Service) TimeZone

func (s *Service) TimeZone() string

TimeStamp returns a time in the format 2006-01-02 15:04:05

func (*Service) Title

func (s *Service) Title() string

Title generates from 2 to 5 titleized words

func (*Service) TopLevelDomain

func (s *Service) TopLevelDomain() string

TopLevelDomain generates random top level domain

func (*Service) ULID

func (s *Service) ULID() ulid.ULID

ULID returns an ULID which is a 16 byte Universally Unique Lexicographically Sortable Identifier.

func (*Service) URL

func (s *Service) URL() string

URL generates random URL standardised in urlFormats const

func (*Service) UUID

func (s *Service) UUID() []byte

UUID returns a UUID v4.

func (*Service) UUIDString

func (s *Service) UUIDString() string

UUID returns a UUID v4 as string.

func (*Service) UserName

func (s *Service) UserName() string

UserName generates user name in one of the following forms first name + last name, letter + last names or concatenation of from 1 to 3 lowercased words

func (*Service) WeekDay

func (s *Service) WeekDay() string

WeekDay generates name ot the week day

func (*Service) WeekDayShort

func (s *Service) WeekDayShort() string

WeekDayShort generates abbreviated name of the week day

func (*Service) WeekdayNum

func (s *Service) WeekdayNum() int

WeekdayNum generates number of the day of the week

func (*Service) Word

func (s *Service) Word(maxLen int) string

Word generates random word

func (*Service) Words

func (s *Service) Words(maxLen int) string

Words generates from 1 to 5 random words

func (*Service) WordsN

func (s *Service) WordsN(n int, maxLen int) string

WordsN generates n random words

func (*Service) Year

func (s *Service) Year(from, to int) int

Year generates year using the given boundaries

func (*Service) Zip

func (s *Service) Zip() string

Zip generates random zip code using one of the formats specifies in zip_format file

Jump to

Keyboard shortcuts

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