2024-04-26 19:30:35 +00:00
package gofakeit
import (
"bytes"
"fmt"
"math/rand"
"reflect"
2024-06-14 08:41:36 +00:00
"strconv"
2024-04-26 19:30:35 +00:00
"strings"
"text/template"
2024-06-14 08:41:36 +00:00
"time"
2024-04-26 19:30:35 +00:00
)
// TemplateOptions defines values needed for template document generation
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
type TemplateOptions struct {
Funcs template . FuncMap ` fake:"-" `
2024-07-24 23:45:04 +00:00
Data any ` json:"data" xml:"data" fake:"-" `
2024-04-26 19:30:35 +00:00
}
// Template generates an document based on the the supplied template
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
func Template ( template string , co * TemplateOptions ) ( string , error ) {
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
if co == nil {
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
co = & TemplateOptions { }
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
globalFaker . Struct ( co )
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
}
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
return templateFunc ( template , templateFuncMap ( globalFaker . Rand , & co . Funcs ) , co )
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
}
// Template generates an document based on the the supplied template
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
func ( f * Faker ) Template ( template string , co * TemplateOptions ) ( string , error ) {
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
if co == nil {
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
co = & TemplateOptions { }
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
f . Struct ( co )
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
}
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
return templateFunc ( template , templateFuncMap ( f . Rand , & co . Funcs ) , co )
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
}
// MarkdownOptions defines values needed for markdown document generation
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
type MarkdownOptions struct {
}
// Template for Markdown
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
const templateMarkdown = `
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
{ { $ repo := Gamertag } }
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
{ { $ language := RandomString ( SliceString "go" "python" "javascript" ) } }
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
{ { $ username := Gamertag } }
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
{ { $ weightedSlice := SliceAny "github.com" "gitlab.com" "bitbucket.org" } }
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
{ { $ weightedWeights := SliceF32 5 1 1 } }
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
{ { $ domain := Weighted $ weightedSlice $ weightedWeights } }
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
{ { $ action := RandomString ( SliceString "process" "run" "execute" "perform" "handle" ) } }
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
{ { $ usage := RandomString ( SliceString "whimsical story" "quirky message" "playful alert" "funny request" "lighthearted command" ) } }
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
{ { $ result := RandomString ( SliceString "success" "error" "unknown" "completed" "failed" "finished" "in progress" "terminated" ) } }
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
# { { $ repo } }
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
* Author : { { FirstName } } { { LastName } } *
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
{ { Paragraph 2 5 7 "\n\n" } }
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
# # Table of Contents
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
- [ Installation ] ( # installation )
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
- [ Usage ] ( # usage )
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
- [ License ] ( # license )
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
# # Installation
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
{ { if eq $ language "go" } } ''' go
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
go get { { $ domain } } / { { $ username } } / { { $ repo } }
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
''' { { else if eq $ language "python" } } ''' bash
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
pip install { { $ repo } }
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
''' { { else if eq $ language "javascript" } } ''' js
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
npm install { { $ repo } }
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
''' { { end } }
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
# # Usage
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
{ { if eq $ language "go" } } ''' go
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
result := { { $ repo } } . { { $ action } } ( "{{ToLower $usage}}" )
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
fmt . Println ( "{{ToLower $repo}} result:" , "{{ToLower $result}}" )
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
''' { { else if eq $ language "python" } } ''' python
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
result = { { ToLower $ repo } } . { { $ action } } ( "{{ToLower $usage}}" )
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
print ( "{{ToLower $repo}} result:" , "{{ToLower $result}}" )
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
''' { { else if eq $ language "javascript" } } ''' javascript
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
const result = { { ToLower $ repo } } . { { $ action } } ( "{{ToLower $usage}}" ) ;
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
console . log ( "{{ToLower $repo}} result:" , "{{ToLower $result}}" ) ;
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
''' { { end } }
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
# # License
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
{ { RandomString ( SliceString "MIT" "Apache 2.0" "GPL-3.0" "BSD-3-Clause" "ISC" ) } }
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
`
// Markdown will return a single random Markdown template document
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
func Markdown ( co * MarkdownOptions ) ( string , error ) {
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
if co == nil {
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
co = & MarkdownOptions { }
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
globalFaker . Struct ( co )
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
}
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
return templateFunc ( templateMarkdown , templateFuncMap ( globalFaker . Rand , nil ) , co )
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
}
// Markdown will return a single random Markdown template document
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
func ( f * Faker ) Markdown ( co * MarkdownOptions ) ( string , error ) {
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
if co == nil {
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
co = & MarkdownOptions { }
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
f . Struct ( co )
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
}
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
return templateFunc ( templateMarkdown , templateFuncMap ( f . Rand , nil ) , co )
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
}
// EmailOptions defines values needed for email document generation
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
type EmailOptions struct {
}
// Template for email text
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
const templateEmail = `
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
Subject : { { RandomString ( SliceString "Greetings" "Hello" "Hi" ) } } from { { FirstName } } !
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
Dear { { LastName } } ,
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
{ { RandomString ( SliceString "Greetings!" "Hello there!" "Hi, how are you?" ) } } { { RandomString ( SliceString "How's everything going?" "I hope your day is going well." "Sending positive vibes your way." ) } }
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
{ { RandomString ( SliceString "I trust this email finds you well." "I hope you're doing great." "Hoping this message reaches you in good spirits." ) } } { { RandomString ( SliceString "Wishing you a fantastic day!" "May your week be filled with joy." "Sending good vibes your way." ) } }
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
{ { Paragraph 3 5 10 "\n\n" } }
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
{ { RandomString ( SliceString "I would appreciate your thoughts on" "I'm eager to hear your feedback on" "I'm curious to know what you think about" ) } } it . If you have a moment , please feel free to check out the project on { { RandomString ( SliceString "GitHub" "GitLab" "Bitbucket" ) } }
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
{ { RandomString ( SliceString "Your insights would be invaluable." "I'm eager to hear what you think." "Feel free to share your opinions with me." ) } } { { RandomString ( SliceString "Looking forward to your feedback!" "Your perspective is highly valued." "Your thoughts matter to me." ) } }
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
{ { RandomString ( SliceString "Thank you for your consideration!" "I appreciate your attention to this matter." "Your support means a lot to me." ) } } { { RandomString ( SliceString "Wishing you a wonderful day!" "Thanks in advance for your time." "Your feedback is greatly appreciated." ) } }
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
{ { RandomString ( SliceString "Warm regards" "Best wishes" "Kind regards" "Sincerely" "With gratitude" ) } }
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
{ { FirstName } } { { LastName } }
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
{ { Email } }
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
{ { PhoneFormatted } }
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
`
// EmailText will return a single random text email template document
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
func EmailText ( co * EmailOptions ) ( string , error ) {
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
if co == nil {
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
co = & EmailOptions { }
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
globalFaker . Struct ( co )
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
}
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
return templateFunc ( templateEmail , templateFuncMap ( globalFaker . Rand , nil ) , co )
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
}
// EmailText will return a single random text email template document
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
func ( f * Faker ) EmailText ( co * EmailOptions ) ( string , error ) {
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
if co == nil {
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
co = & EmailOptions { }
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
f . Struct ( co )
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
}
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
return templateFunc ( templateEmail , templateFuncMap ( f . Rand , nil ) , co )
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
}
// functions that wont work with template engine
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
var templateExclusion = [ ] string {
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
"RandomMapKey" ,
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
"SQL" ,
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
"Template" ,
}
// Build the template.FuncMap for the template engine
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
func templateFuncMap ( r * rand . Rand , fm * template . FuncMap ) * template . FuncMap {
// create a new function map
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
funcMap := template . FuncMap { }
// build the function map from a faker using their rand
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
f := & Faker { Rand : r }
v := reflect . ValueOf ( f )
// loop through the methods
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
for i := 0 ; i < v . NumMethod ( ) ; i ++ {
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
// check if the method is in the exclusion list
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
if stringInSlice ( v . Type ( ) . Method ( i ) . Name , templateExclusion ) {
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
continue
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
}
// Check if method has return values
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
// If not don't add to function map
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
if v . Type ( ) . Method ( i ) . Type . NumOut ( ) == 0 {
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
continue
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
}
// add the method to the function map
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
funcMap [ v . Type ( ) . Method ( i ) . Name ] = v . Method ( i ) . Interface ( )
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
}
// make string upper case
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
funcMap [ "ToUpper" ] = strings . ToUpper
// make string lower case
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
funcMap [ "ToLower" ] = strings . ToLower
// make string title case
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
funcMap [ "IntRange" ] = func ( start , end int ) [ ] int {
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
n := end - start + 1
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
result := make ( [ ] int , n )
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
for i := 0 ; i < n ; i ++ {
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
result [ i ] = start + i
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
}
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
return result
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
}
// enable passing any type to return a string
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
funcMap [ "ToInt" ] = func ( args any ) int {
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
switch v := args . ( type ) {
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
case string :
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
i , err := strconv . Atoi ( v )
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
if err != nil {
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
return 0
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
}
return i
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
case float64 :
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
return int ( v )
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
case float32 :
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
return int ( v )
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
case int :
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
return v
// Anything else return 0
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
default :
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
return 0
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
}
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
}
// enable passing any type to return a float64
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
funcMap [ "ToFloat" ] = func ( args any ) float64 {
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
switch v := args . ( type ) {
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
case string :
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
i , err := strconv . ParseFloat ( v , 64 )
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
if err != nil {
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
return 0
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
}
return i
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
case float64 :
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
return v
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
case float32 :
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
return float64 ( v )
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
case int :
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
return float64 ( v )
// Anything else return 0
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
default :
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
return 0
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
}
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
}
// ensable passing any type to return a string
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
funcMap [ "ToString" ] = func ( args any ) string {
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
switch v := args . ( type ) {
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
case string :
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
return v
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
case float64 :
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
return strconv . FormatFloat ( v , 'f' , - 1 , 64 )
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
case float32 :
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
return strconv . FormatFloat ( float64 ( v ) , 'f' , - 1 , 32 )
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
case int :
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
return strconv . Itoa ( v )
// Anything else return empty string
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
default :
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
return ""
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
}
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
}
// function to convert string to date time
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
funcMap [ "ToDate" ] = func ( dateString string ) time . Time {
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
date , err := time . Parse ( "2006-01-02" , dateString )
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
if err != nil {
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
return time . Now ( )
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
}
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
return date
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
}
// enable passing slice of interface to functions
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
funcMap [ "SliceAny" ] = func ( args ... any ) [ ] any {
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
return args
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
}
// enable passing slice of string to functions
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
funcMap [ "SliceString" ] = func ( args ... string ) [ ] string {
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
return args
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
}
// enable passing slice of uint to functions
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
funcMap [ "SliceUInt" ] = func ( args ... uint ) [ ] uint {
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
return args
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
}
// enable passing slice of int to functions
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
funcMap [ "SliceInt" ] = func ( args ... int ) [ ] int {
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
return args
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
}
// enable passing slice of int to functions
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
funcMap [ "SliceF32" ] = func ( args ... float32 ) [ ] float32 {
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
return args
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
}
// Add passed in function map to the function map
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
if fm != nil {
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
for k , v := range * fm {
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
funcMap [ k ] = v
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
}
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
}
return & funcMap
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
}
// function to build the function map for the template engine from the global faker
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
func templateFunc ( temp string , funcs * template . FuncMap , data any ) ( string , error ) {
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
if temp == "" {
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
return "" , fmt . Errorf ( "template parameter is empty" )
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
}
// Create a new template and parse
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
template_gen , err := template . New ( "CodeRun" ) . Funcs ( * funcs ) . Parse ( temp )
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
if err != nil {
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
return "" , err
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
}
b := & bytes . Buffer { }
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
err = template_gen . Execute ( b , data )
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
if err != nil {
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
return "" , err
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
}
// Return the result
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
return strings . ReplaceAll ( b . String ( ) , "\\n" , "\n" ) , nil
}
// addTemplateLookup will add the template functions to the global lookup
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
func addTemplateLookup ( ) {
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
AddFuncLookup ( "template" , Info {
2024-07-24 23:45:04 +00:00
Display : "Template" ,
Category : "template" ,
2024-04-26 19:30:35 +00:00
Description : "Generates document from template" ,
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
Example : ` { { Firstname } } { { Lastname } }
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
// output
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
Markus Moen ` ,
2024-07-24 23:45:04 +00:00
Output : "string" ,
2024-04-26 19:30:35 +00:00
ContentType : "text/plain" ,
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
Params : [ ] Param {
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
{ Field : "template" , Display : "Template" , Type : "string" , Description : "Golang template to generate the document from" } ,
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
{ Field : "data" , Display : "Custom Data" , Type : "string" , Default : "" , Optional : true , Description : "Custom data to pass to the template" } ,
} ,
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
Generate : func ( r * rand . Rand , m * MapParams , info * Info ) ( any , error ) {
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
tpl , err := info . GetString ( m , "template" )
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
if err != nil {
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
return nil , err
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
}
data , err := info . GetAny ( m , "data" )
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
if err != nil {
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
return nil , err
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
}
templateOut , err := templateFunc ( tpl , templateFuncMap ( r , nil ) , & TemplateOptions { Data : data } )
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
if err != nil {
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
return nil , err
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
}
return templateOut , nil
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
} ,
} )
AddFuncLookup ( "markdown" , Info {
2024-07-24 23:45:04 +00:00
Display : "Random markdown document" ,
Category : "template" ,
2024-04-26 19:30:35 +00:00
Description : "Lightweight markup language used for formatting plain text" ,
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
Example : ` # PurpleSheep5
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
* Author : Amie Feil *
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
Quarterly without week it hungry thing someone . Him regularly today whomever this revolt hence . From his timing as quantity us these . Yours live these frantic not may another . How this ours his them those whose .
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
Them batch its Iraqi most that few . Abroad cheese this whereas next how there . Gorgeous genetics time choir fiction therefore yourselves . Am those infrequently heap software quarterly rather . Punctuation yellow where several his orchard to .
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
# # Table of Contents
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
- [ Installation ] ( # installation )
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
- [ Usage ] ( # usage )
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
- [ License ] ( # license )
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
# # Installation
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
''' bash
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
pip install PurpleSheep5
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
'''
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
# # Usage
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
''' python
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
result = purplesheep5 . process ( "funny request" )
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
print ( "purplesheep5 result:" , "in progress" )
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
'''
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
# # License
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
MIT ` ,
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
Output : "string" ,
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
Generate : func ( r * rand . Rand , m * MapParams , info * Info ) ( any , error ) {
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
template_result , err := templateFunc ( templateMarkdown , templateFuncMap ( r , nil ) , & MarkdownOptions { } )
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
return string ( template_result ) , err
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
} ,
} )
AddFuncLookup ( "email_text" , Info {
2024-07-24 23:45:04 +00:00
Display : "Random text email Document" ,
Category : "template" ,
2024-04-26 19:30:35 +00:00
Description : "Written content of an email message, including the sender's message to the recipient" ,
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
Example : ` Subject : Greetings from Marcel !
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
Dear Pagac ,
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
Hello there ! Sending positive vibes your way .
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
I hope you ' re doing great . May your week be filled with joy .
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
Virtually woman where team late quarterly without week it hungry . Thing someone him regularly today whomever this revolt hence from . His timing as quantity us these yours live these frantic . Not may another how this ours his them those whose . Them batch its Iraqi most that few abroad cheese this .
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
Whereas next how there gorgeous genetics time choir fiction therefore . Yourselves am those infrequently heap software quarterly rather punctuation yellow . Where several his orchard to frequently hence victorious boxers each . Does auspicious yourselves first soup tomorrow this that must conclude . Anyway some yearly who cough laugh himself both yet rarely .
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
Me dolphin intensely block would leap plane us first then . Down them eager would hundred super throughout animal yet themselves . Been group flock shake part purchase up usually it her . None it hers boat what their there Turkmen moreover one . Lebanese to brace these shower in it everybody should whatever .
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
I ' m curious to know what you think about it . If you have a moment , please feel free to check out the project on Bitbucket
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
I ' m eager to hear what you think . Looking forward to your feedback !
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
Thank you for your consideration ! Thanks in advance for your time .
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
Kind regards
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
Milford Johnston
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
jamelhaag @ king . org
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
( 507 ) 0 96 - 3058 ` ,
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
Output : "string" ,
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
Generate : func ( r * rand . Rand , m * MapParams , info * Info ) ( any , error ) {
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
template_result , err := templateFunc ( templateEmail , templateFuncMap ( r , nil ) , & EmailOptions { } )
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
return string ( template_result ) , err
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
} ,
} )
2024-07-24 23:45:04 +00:00
2024-04-26 19:30:35 +00:00
}