generator

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2025 License: MIT Imports: 3 Imported by: 0

README

Fakestack Generator Library

A standalone Go library for generating realistic fake data. Can be used independently without database connections.

Installation

go get github.com/0xdps/fake-stack/core/pkg/generator

Usage

import "github.com/0xdps/fake-stack/core/pkg/generator"

// Create a generator
gen := generator.New()

// Define fields
fields := []generator.Field{
    {Name: "id", Generator: "uuid"},
    {Name: "name", Generator: "name"},
    {Name: "email", Generator: "email"},
    {Name: "age", Generator: "random_int", Args: map[string]interface{}{"min": 18.0, "max": 65.0}},
}

// Generate records
records, err := gen.GenerateRecords(fields, 100)

Available Generators

Personal
  • name, first_name, last_name, username, email, password, gender
Address
  • address, street_address, city, state, country, postcode, zip_code
  • latitude, longitude
Company
  • company, job, catch_phrase
Internet
  • url, domain_name, ipv4, ipv6, mac_address
Dates
  • date, date_time, past_date, future_date
Text
  • text, sentence, paragraph, word
Numbers
  • random_int (with min/max args), random_digit, random_number
Other
  • phone_number, phone, boolean, uuid
Complex Objects
  • person - Returns object with first_name, last_name, email, gender, address, full_name
  • user - Returns person object + username, password
  • person.{property} - Access specific property (e.g., "person.first_name")
  • user.{property} - Access specific property (e.g., "user.username")
Custom
  • random_from - Pick random item from array: Args: []interface{}{"red", "blue", "green"}
  • unique_item - Unique item from array

Example: API Usage

Perfect for building mock APIs like apimock.codes:

func GetUsers(c *gin.Context) {
    gen := generator.New()
    
    fields := []generator.Field{
        {Name: "id", Generator: "uuid"},
        {Name: "username", Generator: "username"},
        {Name: "email", Generator: "email"},
        {Name: "first_name", Generator: "first_name"},
        {Name: "last_name", Generator: "last_name"},
    }
    
    users, _ := gen.GenerateRecords(fields, 100)
    c.JSON(200, users)
}

Testing

go test ./pkg/generator -v

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Field

type Field struct {
	Name      string
	Generator string
	Args      interface{}
}

Field represents a field configuration for generation

type Generator

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

Generator handles fake data generation

func New

func New() *Generator

New creates a new Generator

func NewWithSeed

func NewWithSeed(seed uint64) *Generator

NewWithSeed creates a new Generator with a specific seed

func (*Generator) ClearUniques

func (g *Generator) ClearUniques()

ClearUniques clears the unique value cache

func (*Generator) Generate

func (g *Generator) Generate(field Field, commons map[string]interface{}) (interface{}, error)

Generate generates a value based on the generator type

func (*Generator) GenerateRecords

func (g *Generator) GenerateRecords(fields []Field, count int) ([]map[string]interface{}, error)

GenerateRecords generates multiple records based on fields configuration

Jump to

Keyboard shortcuts

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