generate.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package gofakeit
  2. import (
  3. "strings"
  4. )
  5. // Generate fake information from given string. String should contain {category.subcategory}
  6. //
  7. // Ex: {person.first} - random firstname
  8. //
  9. // Ex: {person.first}###{person.last}@{person.last}.{internet.domain_suffix} - billy834smith@smith.com
  10. //
  11. // Ex: ### - 481 - random numbers
  12. //
  13. // Ex: ??? - fda - random letters
  14. //
  15. // For a complete list possible categories use the Categories() function.
  16. func Generate(dataVal string) string {
  17. // Identify items between brackets: {person.first}
  18. for strings.Count(dataVal, "{") > 0 && strings.Count(dataVal, "}") > 0 {
  19. catValue := ""
  20. startIndex := strings.Index(dataVal, "{")
  21. endIndex := strings.Index(dataVal, "}")
  22. replace := dataVal[(startIndex + 1):endIndex]
  23. categories := strings.Split(replace, ".")
  24. if len(categories) >= 2 && dataCheck([]string{categories[0], categories[1]}) {
  25. catValue = getRandValue([]string{categories[0], categories[1]})
  26. }
  27. dataVal = strings.Replace(dataVal, "{"+replace+"}", catValue, 1)
  28. }
  29. // Replace # with numbers
  30. dataVal = replaceWithNumbers(dataVal)
  31. // Replace ? with letters
  32. dataVal = replaceWithLetters(dataVal)
  33. return dataVal
  34. }