rof

package module
v0.0.0-...-bdacdd5 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2019 License: Apache-2.0 Imports: 5 Imported by: 0

README

Build Status Coverage Status

Object Factory Go

Object Factory Go populates Go struct with random values. This greatly saves time to create randomized objects in unit testing. It carries the same philosophy as Reflection Object Factory in Java.

Usage

Create Random Primitives

Random Go primitives can be created with simpily two lines of code:

    package main
    
    import rof "github.com/leakingtapan/rof-go"
    
    func main() {
        // create random int
        // pointer is required for the varible
        var value int
        rof.Create(&value)
        
        // create random string
        // pointer is required for the variable
        var str string
        rof.Create(&str)
    }

Create Random Composite Object

Fields of composite object will be populated recursively.

    package main
    
    import rof "github.com/leakingtapan/rof-go"
    
    type Singer struct {
        FirstName string
        LastName string
        Songs []struct {
           Title string 
        } 
    }
    
    func main() {
        var s Singer
        rof.Create(&s)
    }

Supported Built-in Types
  • Primitives Types:
    • bool
    • int, int8, int16, int32, int64
    • uint, uint8, uint16, uint32, uint64
    • float32, float64
    • complex64, complex128
    • string
  • Composite Types:
Customize function providers

Each built-in types are creates using pre-built function similar to following signature:

    func intFunc() int {
        return 128
    }

A custom function can be passed in using rof.SetFunc API:

    rof.SetFunc(func() string {
        return "my own string"
    })
    
    var str string
    rof.Create(&str)
    fmt.Println(str) // "my own string"

Then this function will be used in following variable creation.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Create

func Create(v interface{}) error

Top level method

func SetFunc

func SetFunc(f interface{})

SetFunc sets the default function if it already exists Or a new one will be added

Types

type InvalidInputError

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

func (*InvalidInputError) Error

func (e *InvalidInputError) Error() string

type ObjectFactory

type ObjectFactory interface {
	Create(v interface{}) error
}

Top level interface

import (
    . "github.com/leakingtapan/rof-go"
)

func main() {
    var v int
    err := Create(&v)
}

type UnknownTypeError

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

func (*UnknownTypeError) Error

func (e *UnknownTypeError) Error() string

Jump to

Keyboard shortcuts

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