Giter Site home page Giter Site logo

fako's Introduction

Fako

Circle CI Godoc Go Report Card

Fako is a library intended to fake Golang structs with fake but coherent data, Fako maps struct field tags and generates fake data accordingly.

We find it useful when writing specs to generate fake database data, hope you too.

Example

This is an example of how Fako works.

import(
  "fmt"
  "github.com/wawandco/fako"
)

type User struct {
    Name     string `fako:"full_name"`
  	Username string `fako:"user_name"`
  	Email    string `fako:"email_address"`//Notice the fako:"email_address" tag
  	Phone    string `fako:"phone"`
  	Password string `fako:"simple_password"`
  	Address  string `fako:"street_address"`
}

func main(){
  var user User
  fako.Fill(&user)

  fmt.Println(&user.Email)
  // This prints something like [email protected]
  // or another valid email

  var userWithOnlyEmail User
  fako.FillOnly(&userWithOnlyEmail, "Email")
  //This will fill all only the email

  var userWithoutEmail User
  fako.FillExcept(&userWithoutEmail, "Email")
  //This will fill all the fields except the email

}

Fako provides 3 built in functions Fill, FillOnly, and FillExcept, please go to godoc for details.

Fako support most of the fields on the fake library, below you can see a list of the field types you can use.

  • brand
  • character
  • characters
  • city
  • color
  • company
  • continent
  • country
  • credit_card_type
  • currency
  • currency_code
  • digits
  • domain_name
  • domain_zone
  • email_address
  • email_body
  • email_subject
  • female_first_name
  • female_full_name
  • female_full_name_with_prefix
  • female_full_name_with_suffix
  • female_last_name
  • female_patronymic
  • first_name
  • full_name
  • full_name_with_prefix
  • full_name_with_suffix
  • gender
  • gender_abbrev
  • hex_color
  • hex_color_short
  • ip_v4
  • industry
  • job_title
  • language
  • last_name
  • latitude_direction
  • longitude_direction
  • male_first_name
  • male_full_name
  • male_full_name_with_prefix
  • male_full_name_with_suffix
  • male_last_name
  • male_patronymic
  • model
  • month
  • month_short
  • paragraph
  • paragraphs
  • patronymic
  • phone
  • product
  • product_name
  • sentence
  • sentences
  • simple_password
  • state
  • state_abbrev
  • street
  • street_address
  • title
  • top_level_domain
  • user_name
  • week_day
  • week_day_short
  • word
  • words
  • zip

Custom Generators

Fako provides a function called Register to add custom data generators in case you need something that our provided generators cannot cover.

To add a custom generator simply call the Register function as in the following example:

import(
  "fmt"
  "github.com/wawandco/fako"
)

type User struct {
    Name     string `fako:"full_name"`
    Username string `fako:"user_name"`
    Email    string `fako:"email_address"`//Notice the fako:"email_address" tag
    Phone    string `fako:"phone"`
    Password string `fako:"simple_password"`
    Address  string `fako:"street_address"`

    AValue   string `fako:"a_gen"`
}

func main(){
  fako.Register("a_gen", func() string {
    return "My Value"
  })

  var user User
  fako.Fill(&user)
  fmt.Println(user.AValue) //should print My Value
}

When using custom generators please keep the following in mind:

  1. Call Register function before calling Fill and its brothers.
  2. Custom generators override base generators, if you pick the same name as one of the existing generators, we will override the existing generator with yours.

Fuzzing

Sometimes you just want to generate random data inside a struct, for those cases you wont want to fill fako types (yes, we understand that part). Fako provides you a Fuzz function you can use to fuzz your structs with random data that simply matches the struct's field types.

You can use it as in the following example:

import "fako"

type Instance struct {
   Name string
   Number int
}

func main(){
  instance := Instance{}
  fako.Fuzz(&instance) // This fills your instance variable
}

Note, Fuzz function works for the following types string, bool, int, int32, int64, float32, float64. other types like Array or Chan are out of our scope.

Credits

As you may have noticed this is based on fake library, which does all the work to generate data.

Copyright

Fako is Copyright © 2008-2015 Wawandco SAS. It is free software, and may be redistributed under the terms specified in the LICENSE file.

fako's People

Contributors

paganotoni avatar verto avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fako's Issues

failed for aliases

Example:
type DateTime string
Runtime error:
panic serving 172.20.0.1:33258: reflect.Set: value of type string is not assignable to type DateTime

Nested Faking

Unsure if we need to add this, if you're reading this and thing it worth doing it, please let me know.
Appreciate your feedback!, i will add details on the implementation soon.

Support for numeric types

When faking data, sometimes we will need to generate numeric values like:

  • invoice amount
  • number of items
  • received calls
  • failed password attempts

Currently Fako doesn't support generating numeric values for us, my idea is to allow users to define a range for particular fields like:

type Invoice struct {
  Amount  float64  `fako:"max:200.0;min:0.0"`
  DaysToWait  int64     `fako:"max:200;min:0"`
}

This way when funning fako.FIll(&invoice) it should fill invoice.Amount with a value between 0.0 and 200.0 (float).

Allow simple Fuzzing functionallity

Fako should provide a function that returns a Struct instance simply populated with data that matches each struct type, no matter if its simply Noise.

Something like:

type Instance struct {
   Name string
   Number int
}

instance := Instance{}
fako.Fuzz(&instance)

Should fill Name and Number with no sense (but valid for the type) data.(p.e. AsDeasd0938SD!! and 11234)

Fako should allow developers to add his own data-generators and used these on the structs.

I'm thinking on this the following way:

  • Fako needs to provide the users a function to register generators
package fako

//Function to register a data generator
func Register(identifier string, function func()string){
  ...
}

Which could be used like:

  fako.Register("a_gen", func() string {
     return  "A"
  })

And referenced on the Structs like:

type User struct {
   ...
   a_value string `fako:"a_gen"`
   ...
}

Note: fako.Register should be called before fako.Filland its brothers get called.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.