vcgen

package module
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: MIT Imports: 5 Imported by: 0

README

Voucher Code Generator

Generate unique, random, and hard to guess coupon / voucher codes. Use cases: promo codes, loyalty coupons, gift vouchers, in-app purchases, referral links

Installation

Just use go get.

go get -u github.com/AmirSoleimani/VoucherCodeGenerator@v2.0.0
Tested in the following Golang releases

Release v1.0.1 was tested from Go1.13.x to Go1.18.x.

How to use

import (
	...
	vcgen "github.com/AmirSoleimani/VoucherCodeGenerator"
)

func main() {
	wg := sync.WaitGroup{}
	wg.Add(3)

	// normal
	go func(wg *sync.WaitGroup) {
		defer wg.Done()

		vc, _ := vcgen.NewWithOptions(
			vcgen.SetCount(10),
			vcgen.SetPattern("###-###-###"),
			vcgen.SetCharset("0123456789"),
		)
		result, err := vc.Run()
		if err != nil {
			fmt.Println(err)
		}

		fmt.Println(result)
	}(&wg)

	// with prefix
	go func(wg *sync.WaitGroup) {
		defer wg.Done()

		vc, _ := vcgen.NewWithOptions(
			vcgen.SetCount(10),
			vcgen.SetPattern("######"),
			vcgen.SetPrefix("WELC-"),
		)
		result, err := vc.Run()
		if err != nil {
			fmt.Println(err)
		}

		fmt.Println(result)
	}(&wg)

	// with prefix + suffix
	go func(wg *sync.WaitGroup) {
		defer wg.Done()

		vc, _ := vcgen.NewWithOptions(
			vcgen.SetCount(10),
			vcgen.SetPattern("######"),
			vcgen.SetPrefix("WELC-"),
			vcgen.SetSuffix("-B"),
		)
		result, err := vc.Run()
		if err != nil {
			fmt.Println(err)
		}

		fmt.Println(result)
	}(&wg)

	wg.Wait()

}
Options
SetLength(length uint16)
SetCount(count uint16)
SetCharset(charset string)
SetPrefix(prefix string)
SetSuffix(suffix string)
SetPattern(pattern string)
Prefix and Suffix

You can optionally surround each generated code with a prefix and/or suffix.

Pattern

Codes may follow a specified pattern. Use hash (#) as a placeholder for random characters.

Infeasible configs

There exist some configs that are not feasible. For example it's not possible to generate 1000 codes if you want your codes to be 2 characters long and consisting only of numbers. Voucher code generator detects such cases and throws an error "Not possible to generate requested number of codes.".

Config reference
attribute default value description
length 6 Number of characters in a generated code (excluding prefix and suffix)
count 1 Number of codes generated.
charset alphanumeric Characters that can appear in the code.
prefix "" A text appended before the code.
suffix "" A text appended after the code.
pattern "######" A pattern for codes where hashes (#) will be replaced with random characters.
License

Code released under the MIT license.

Documentation

Index

Constants

View Source
const (
	// Charset types
	CharsetNumbers      = "0123456789"
	CharsetAlphabetic   = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
	CharsetAlphanumeric = CharsetNumbers + CharsetAlphabetic
)

Variables

View Source
var (
	ErrNotFeasible       = errors.New("Not feasible to generate requested number of codes")
	ErrPatternIsNotMatch = errors.New("Pattern is not match with the length value")
)
View Source
var (
	ErrInvalidCount   = errors.New("invalid count, It should be greater than 0")
	ErrInvalidCharset = errors.New("invalid charset, charset length should be greater than 0")
	ErrInvalidPattern = errors.New("invalid pattern, pattern cannot be empty")
)

Functions

This section is empty.

Types

type Generator

type Generator struct {
	// Length of the code to generate
	Length uint16 `json:"length"`

	// Count of the codes
	// How many codes to generate
	Count uint16 `json:"count"`

	// Charset to use
	// `CharsetNumbers`, `CharsetAlphabetic`, and `CharsetAlphanumeric`
	// are already defined and you can use them.
	Charset string `json:"charset"`

	// Prefix of the code
	Prefix string `json:"prefix"`

	// Suffix of the code
	Suffix string `json:"suffix"`

	// Pattern of the code
	// # is the placeholder for the charset
	Pattern string `json:"pattern"`
}

func Default

func Default() *Generator

Creates a new generator with default values

func NewWithOptions

func NewWithOptions(opts ...Option) (*Generator, error)

Creates a new generator with options

func (*Generator) Run

func (g *Generator) Run() ([]string, error)

Generates a list of codes

type Option

type Option func(*Generator) error

func SetCharset

func SetCharset(charset string) Option

SetCharset sets the charset of the code

func SetCount

func SetCount(count uint16) Option

SetCount sets the count of the code

func SetLength

func SetLength(length uint16) Option

SetLength sets the length of the code

func SetPattern

func SetPattern(pattern string) Option

SetPattern sets the pattern of the code

func SetPrefix

func SetPrefix(prefix string) Option

SetPrefix sets the prefix of the code

func SetSuffix

func SetSuffix(suffix string) Option

SetSuffix sets the suffix of the code

Jump to

Keyboard shortcuts

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