generator

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2025 License: Apache-2.0 Imports: 28 Imported by: 0

README

Generator

This directory contains multiple custom generators.

Format tags

Name Return Type
preposition_simple string
verb_action string
buzz_word string
country string
hacker_adjective string
last_name string
movie_genre string
book_title string
connective string
dessert string
bird string
middle_name string
movie_name string
url string
username string
ach_routing string
adverb_manner string
hacker_noun string
product_upc string
pronoun_indefinite string
adverb_degree string
longitude float64
street_suffix string
vowel string
beer_hop string
cusip string
noun_determiner string
phrase_preposition string
preposition string
car_maker string
http_status_code int
hackering_verb string
hour int
interjection string
language string
adjective_descriptive string
dog string
first_name string
minecraft_weather string
name string
safari_user_agent string
uuid string
verb string
bool bool
connective_listing string
digit string
adjective_quantitative string
adverb string
error_http_client error
gamertag string
minecraft_villager_job string
song_genre string
cat string
month_string string
noun_concrete string
street_prefix string
uint_32 uint32
bitcoin_private_key string
minecraft_wood string
preposition_double string
user_agent string
emoji_alias string
mac_address string
noun_proper string
input_name string
name_suffix string
pronoun_reflective string
verb_linking string
adjective string
adjective_interrogative string
book_genre string
domain_suffix string
error_database error
error_http_server error
hacker_verb string
isin string
name_prefix string
minecraft_animal string
street_name string
time_zone string
hex_color string
bs string
ach_account string
firefox_user_agent string
hacker_abbreviation string
pronoun_object string
year int
city string
minecraft_mob_neutral string
noun_collective_animal string
credit_card_exp string
language_bcp string
adjective_demonstrative string
breakfast string
day int
phrase_adverb string
time_zone_full string
job_title string
phone string
street_number string
uint_8 uint8
gender string
noun_collective_people string
slogan string
language_abbreviation string
song_artist string
street string
uint_16 uint16
emoji_description string
minecraft_ore string
sentence_simple string
adverb_place string
farm_animal string
http_method string
latitude float64
app_name string
file_mime_type string
lorem_ipsum_word string
lunch string
minecraft_armor_part string
noun string
phrase string
letter string
company string
emoji_tag string
minecraft_mob_boss string
minecraft_mob_passive string
phrase_noun string
beer_ibu string
drink string
http_version string
int_32 int32
state_abr string
connective_casual string
emoji string
error_runtime error
int_64 int64
minecraft_armor_tier string
product_material string
pronoun_relative string
ssn string
uint uint
uint_64 uint64
adverb_frequency_indefinite string
car_transmission_type string
month int
quote string
minecraft_dye string
programming_language string
week_day string
company_suffix string
error_grpc error
snack string
preposition_compound string
minecraft_mob_hostile string
past_date Time
phone_formatted string
pronoun_personal string
app_author string
celebrity_business string
noun_uncountable string
pronoun_interrogative string
verb_helping string
hacker_phrase string
product_category string
product_use_case string
file_extension string
hobby string
pet_name string
word string
beer_blg string
product_benefit string
connective_examplify string
date Time
error_http error
flip_a_coin string
product_name string
verb_transitive string
beer_yeast string
celebrity_sport string
credit_card_cvv string
error_validation error
int int
minecraft_food string
time_zone_region string
dinner string
noun_countable string
product_suffix string
safe_color string
comment string
connective_time string
minecraft_biome string
zip string
adjective_indefinite string
bitcoin_address string
currency_short string
emoji_category string
minute int
product_dimension string
connective_comparative string
i_pv_6_address string
product_description string
vegetable string
adjective_proper string
country_abr string
noun_common string
beer_alcohol string
animal string
car_fuel_type string
animal_type string
car_model string
color string
currency_long string
error error
song_name string
chrome_user_agent string
nano_second int
adverb_time_definite string
adverb_time_indefinite string
domain_name string
school string
second int
time_zone_abv string
minecraft_weapon string
noun_abstract string
opera_user_agent string
time_zone_offset float32
error_object error
future_date Time
http_status_code_simple int
hipster_word string
minecraft_tool string
pronoun_demonstrative string
pronoun_possessive string
verb_intransitive string
float_32 float32
fruit string
int_16 int16
minecraft_villager_level string
noun_collective_thing string
product_feature string
pronoun string
celebrity_actor string
adjective_possessive string
blurb string
float_64 float64
i_pv_4_address string
state string
connective_complaint string
phrase_verb string
question string
adverb_frequency_definite string
credit_card_type string
beer_name string
beer_style string
car_type string
job_descriptor string
minecraft_villager_station string
app_version string
beer_malt string
book_author string
email string
int_8 int8
job_level string

Thanks

Inspire by dg and gofakeit.

Documentation

Index

Constants

View Source
const DefaultRefLimit = 1000

Variables

View Source
var (
	GlobalGenRule = GenRule{
		"null_frequency": GLOBAL_NULL_FREQUENCY,
	}
	GLOBAL_NULL_FREQUENCY = 0.0 // Default null frequency is 0%
	MAX_DECIMAL_INT_LEN   = len(strconv.FormatInt(math.MaxInt64, 10))

	TypeAlias = map[string]string{
		"INTEGER":    "INT",
		"TEXT":       "STRING",
		"BOOL":       "BOOLEAN",
		"DECIMALV2":  "DECIMAL",
		"DECIMALV3":  "DECIMAL",
		"DATEV1":     "DATE",
		"DATEV2":     "DATE",
		"DATETIMEV1": "DATETIME",
		"DATETIMEV2": "DATETIME",
		"TIMESTAMP":  "DATETIME",
	}

	DefaultTypeGenRules = lo.MapValues(map[string]GenRule{
		"ARRAY": {
			"length": GenRule{
				"min": 1,
				"max": 3,
			},
		},
		"MAP": {
			"length": GenRule{
				"min": 1,
				"max": 3,
			},
		},
		"JSON": {
			"structure": `STRUCT<col1:SMALLINT, col2:SMALLINT>`,
		},
		"JSONB": {
			"structure": `STRUCT<col1:SMALLINT, col2:SMALLINT>`,
		},
		"VARIANT": {
			"structure": `STRUCT<col1:SMALLINT, col2:SMALLINT>`,
		},
		"BITMAP": {
			"length": 5,
			"min":    0,
			"max":    math.MaxInt32,
		},
		"TEXT": {
			"length": GenRule{
				"min": 1,
				"max": 10,
			},
		},
		"STRING": {
			"length": GenRule{
				"min": 1,
				"max": 10,
			},
		},
		"VARCHAR": {
			"length": GenRule{
				"min": 1,
				"max": 10,
			},
		},
		"TINYINT": {
			"min": math.MinInt8,
			"max": math.MaxInt8,
		},
		"SMALLINT": {
			"min": math.MinInt16,
			"max": math.MaxInt16,
		},
		"INT": {
			"min": math.MinInt32,
			"max": math.MaxInt32,
		},
		"BIGINT": {
			"min": math.MinInt32,
			"max": math.MaxInt32,
		},
		"LARGEINT": {
			"min": math.MinInt32,
			"max": math.MaxInt32,
		},
		"FLOAT": {
			"min": math.MinInt16,
			"max": math.MaxInt16,
		},
		"DOUBLE": {
			"min": math.MinInt32,
			"max": math.MaxInt32,
		},
		"DECIMAL": {
			"min": math.MinInt32,
			"max": math.MaxInt32,
		},
		"DATE": {
			"min": time.Now().AddDate(-10, 0, 0),
			"max": time.Now(),
		},
		"DATETIME": {
			"min": time.Now().AddDate(-10, 0, 0),
			"max": time.Now(),
		},
	}, func(v GenRule, _ string) any { return v })
)
View Source
var CustomGenConstructors map[string]CustomGenConstructor
View Source
var FormatTags map[string]reflect.Value

Functions

func Cast

func Cast[R CastType](v any) (r R, err error)

func Cast2

func Cast2[R CastType](v1, v2 any) (r1, r2 R, err error)

func CastMinMax

func CastMinMax[R int8 | int16 | int | int32 | int64 | float32 | float64 | time.Time](min_, max_ any, baseType, colpath string, errmsg ...string) (R, R)

func CloneGenRules added in v1.1.3

func CloneGenRules(src any) any

func GetCustomTableGenRule

func GetCustomTableGenRule(table string) (rows int, colrules map[string]GenRule)

func GetTableRefGen

func GetTableRefGen(table string) map[string]*RefGen

func MergeGenRules added in v1.1.3

func MergeGenRules(dst, src GenRule, overwrite bool)

func MustJSONMarshal

func MustJSONMarshal(v any) []byte

func MustYAMLUmarshal

func MustYAMLUmarshal(s string) map[string]any

func Setup

func Setup(genconf string) error

func SetupFormatTags

func SetupFormatTags()

func SetupGenRules added in v1.1.3

func SetupGenRules(configFile string) error

Types

type ArrayGen

type ArrayGen struct {
	Element        Gen
	LenMin, LenMax int
}

func (*ArrayGen) Gen

func (g *ArrayGen) Gen() any

func (*ArrayGen) SetElementGen

func (g *ArrayGen) SetElementGen(elem Gen)

type CastType

type CastType interface {
	int8 | int16 | int | int32 | int64 | float32 | float64 | string | time.Time
}

type CustomGenConstructor

type CustomGenConstructor = func(v *TypeVisitor, dataType parser.IDataTypeContext, r GenRule) (Gen, error)

type EnumGen

type EnumGen struct {
	Enum    []any     `yaml:"enum,omitempty"`
	Weights []float32 `yaml:"weights,omitempty"`
	// contains filtered or unexported fields
}

func (*EnumGen) Gen

func (g *EnumGen) Gen() any

type FormatGen

type FormatGen struct {
	Format string
	// contains filtered or unexported fields
}

func (*FormatGen) Gen

func (g *FormatGen) Gen() any

type Gen

type Gen interface {
	Gen() any
}

func NewEnumGenerator

func NewEnumGenerator(visitor *TypeVisitor, dataType parser.IDataTypeContext, r GenRule) (Gen, error)

func NewFormatGenerator

func NewFormatGenerator(format string, inner Gen) (Gen, error)

func NewFuncGen

func NewFuncGen[T any](f func() T) Gen

func NewGolangGenerator

func NewGolangGenerator(_ *TypeVisitor, _ parser.IDataTypeContext, r GenRule) (Gen, error)

func NewIncGenerator

func NewIncGenerator(_ *TypeVisitor, _ parser.IDataTypeContext, r GenRule) (Gen, error)

func NewIntGen

func NewIntGen[T int8 | int16 | int | int32](minVal, maxVal T) Gen

func NewPartsGenerator added in v1.1.3

func NewPartsGenerator(visitor *TypeVisitor, dataType parser.IDataTypeContext, r GenRule) (Gen, error)

func NewRefGenerator

func NewRefGenerator(v *TypeVisitor, _ parser.IDataTypeContext, r GenRule) (Gen, error)

func NewTypeGenerator

func NewTypeGenerator(v *TypeVisitor, _ parser.IDataTypeContext, r GenRule) (Gen, error)

type GenRule

type GenRule = map[string]any

type GolangGen

type GolangGen struct {
	Code string
	// contains filtered or unexported fields
}

func (*GolangGen) Gen

func (g *GolangGen) Gen() any

type IncGen

type IncGen struct {
	Start int64 `yaml:"start,omitempty"`
	Step  int64 `yaml:"step,omitempty"`
}

func (*IncGen) Gen

func (g *IncGen) Gen() any

type MapGen

type MapGen struct {
	Key, Value     Gen
	LenMin, LenMax int
}

func (*MapGen) Gen

func (g *MapGen) Gen() any

func (*MapGen) SetKeyGen

func (g *MapGen) SetKeyGen(k Gen)

func (*MapGen) SetValueGen

func (g *MapGen) SetValueGen(v Gen)

type PartsGen added in v1.1.3

type PartsGen struct {
	Parts []any `yaml:"parts,omitempty"`
}

func (*PartsGen) Gen added in v1.1.3

func (g *PartsGen) Gen() any

type RefGen

type RefGen struct {
	Table  string
	Column string
	Limit  int
	// contains filtered or unexported fields
}

func (*RefGen) AddRefVals

func (g *RefGen) AddRefVals(vals ...any)

func (*RefGen) Clone

func (g *RefGen) Clone() *RefGen

func (*RefGen) Gen

func (g *RefGen) Gen() any

func (*RefGen) TableColumn

func (g *RefGen) TableColumn() string

type StructFieldGen

type StructFieldGen struct {
	Name  string
	Value Gen
}

func (*StructFieldGen) Gen

func (g *StructFieldGen) Gen() any

type StructGen

type StructGen struct {
	Fields []*StructFieldGen
}

func (*StructGen) AddChild

func (g *StructGen) AddChild(name string, child Gen)

func (*StructGen) Gen

func (g *StructGen) Gen() any

type TypeGen

type TypeGen struct {
	GenRule GenRule
	// contains filtered or unexported fields
}

func (*TypeGen) Gen

func (g *TypeGen) Gen() any

type TypeVisitor

type TypeVisitor struct {
	Colpath string  // the path of the column, e.g. "db.table.col"
	GenRule GenRule // rules of generator

	// the tables that ref generator point to
	TableRefs *[]string
}

func NewTypeVisitor

func NewTypeVisitor(colpath string, genRule GenRule) *TypeVisitor

func (*TypeVisitor) ChildGenRule

func (v *TypeVisitor) ChildGenRule(name string) GenRule

func (*TypeVisitor) GetBaseType

func (v *TypeVisitor) GetBaseType(type_ parser.IDataTypeContext) (t string)

func (*TypeVisitor) GetChildGen

func (v *TypeVisitor) GetChildGen(name string, childType parser.IDataTypeContext, childGenRule ...GenRule) Gen

func (*TypeVisitor) GetGen added in v1.1.3

func (v *TypeVisitor) GetGen(type_ parser.IDataTypeContext) Gen

func (*TypeVisitor) GetLength

func (v *TypeVisitor) GetLength() (minVal, maxVal int)

func (*TypeVisitor) GetMinMax

func (v *TypeVisitor) GetMinMax() (any, any)

func (*TypeVisitor) GetNullFrequency

func (v *TypeVisitor) GetNullFrequency() float32

func (*TypeVisitor) GetRule

func (v *TypeVisitor) GetRule(name string, defaultValue ...any) any

func (*TypeVisitor) HasGenRule

func (v *TypeVisitor) HasGenRule() bool

func (*TypeVisitor) MergeDefaultRule

func (v *TypeVisitor) MergeDefaultRule(baseType string) *TypeVisitor

Jump to

Keyboard shortcuts

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