Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Template feature so documents can be generated using the built in functions #266

Merged
merged 23 commits into from
Nov 19, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions .vscode/settings.json

This file was deleted.

13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ FileExtension() string
FileMimeType() string
```


### Person

```go
Expand Down Expand Up @@ -768,3 +769,15 @@ ErrorRuntime() error
```go
school() string
```

### Template

Generate custom documents using golang's template engine.

```go
Template(template string, lines int) ([]byte, error)
TemplateDocument(sections int) (string, error)
TemplateMarkdown(sections int) (string, error)
TemplateEmail(sections int) (string, error)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TemplateEmailText - lets change it to TemplateEmailText for now in the event we do an html version in the future.

```

3 changes: 2 additions & 1 deletion data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package data
// Data consists of the main set of fake information
var Data = map[string]map[string][]string{
"person": Person,
"template": Template,
"address": Address,
"company": Company,
"job": Job,
Expand All @@ -28,7 +29,7 @@ var Data = map[string]map[string][]string{
"celebrity": Celebrity,
"error": Error,
"html": Html,
"book": Books,
"book": Books,
"movie": Movies,
"school": School,
}
Expand Down
98 changes: 98 additions & 0 deletions data/template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package data

// Email Template
const email_template1 = `{{RandomString (ListS 'Hi' 'Hello' 'Dear' 'Good morning' 'Good afternoon' 'Good evening' )}}, {{$saved_to:=Person}}{{$saved_to.FirstName}}\n
{{$res:=CreateListResult 1 7}}{{range $y := IntRange 1 .Lines}}{{$res = ListResult ($res) 1 7 true}}{{if eq $res.Value 1}}{{Paragraph (Number 1 3) (Number 3 5) (Number 10 20) '\n\n'}}
{{end}}{{if eq $res.Value 2}}{{Question}}\n{{end}}{{if eq $res.Value 3}}{{Quote}}\n{{end}}{{if eq $res.Value 4}}{{HipsterParagraph (Number 1 3) (Number 3 5) (Number 10 20) '\n\n'}}
{{end}}{{if eq $res.Value 5}}{{RandomString (ListS (conc 'Have you seen' (MovieName) 'it is' (AdjectiveDescriptive)) (conc 'Have you seen the' (MovieName) '?') (conc 'Do you want to watch' (MovieName) (AdverbTimeDefinite) '?') )}}
{{end}}{{if eq $res.Value 6}}{{conc (PronounPersonal) (VerbHelping) (VerbIntransitive) (AdverbTimeDefinite) (PronounDemonstrative) (AdverbFrequencyIndefinite) 'Happens' (AdverbTimeDefinite)}}\n{{end}}{{if eq $res.Value 7}}{{HipsterSentence (Number 10 20)}}{{end}}{{end}}
{{$saved_from:=Person}}{{RandomString (ListS 'Regards' 'Sincerely' 'Best wishes' 'Cheers' 'Take care' 'Best' 'Thank you' 'I appreciate your help' 'I appreciate your feedback' 'I appreciate your input')}}\n{{$saved_from.FirstName}}{{if Bool}} {{$saved_from.LastName}}{{end}}\n{{if Bool}}{{if Bool}}\nCompany: {{$saved_from.Job.Company}}\nJob Role: {{$saved_from.Job.Title}}\n{{end}}
{{if Bool}}Address: {{$saved_from.Address.Address}}\nCity: {{$saved_from.Address.City}}\nState: {{$saved_from.Address.State}}\nZip: {{$saved_from.Address.Zip}}{{end}}Phone: {{$saved_from.Contact.Phone}}\nEmail: {{$saved_from.Contact.Email}}{{end}}`

// Markdown Template
const markdown_body1 = `{{$res:=CreateListResult 1 7}}
{{range $y := IntRange 1 .Lines}}{{$res = ListResult ($res) 1 7 true}}{{if eq $res.Value 1}}# Paragraph

{{Paragraph (Number 1 5) (Number 1 5) (Number 1 30) '\n\n'}}

---
{{end}}{{if eq $res.Value 2}}# Block Quote

{{Paragraph (Number 1 5) (Number 1 5) (Number 1 30) '\n\n'}}

---
{{end}}{{if eq $res.Value 3}}## Details

{{Paragraph (Number 1 5) (Number 1 5) (Number 1 30) '\n\n'}}

<details>
<summary>{{SentenceSimple}} </summary>

'''
go install {{URL}}
'''

</details>

---
{{end}}{{if eq $res.Value 4}}## Url

Golang you will need to install

{{range $y := IntRange 1 (Number 1 10)}}[{{$saved_url:=URL}}{{$saved_url}}]({{$saved_url}})
{{end}}
---
{{end}}{{if eq $res.Value 5}}## LISTS

{{Paragraph (Number 1 5) (Number 1 5) (Number 1 30) '\n\n'}}.

{{range $y := IntRange 1 (Number 1 10)}}1. {{PhraseVerb}}
{{end}}
---
{{end}}{{if eq $res.Value 6}}## Images

{{Paragraph (Number 1 5) (Number 1 5) (Number 10 30) '\n\n'}}.

{{range $y := IntRange 1 (Number 1 2)}}<image src="data:image/png;base64,{{Base64Enc (ImagePng 50 50)}}"" width="200" height="200" alt/><em>{{AdjectiveProper}}</em>
{{end}}
---
{{end}}{{if eq $res.Value 7}}## Code Examples {{$data_type:=RandomString (ListS 'Int' 'Float32' 'Float64' 'Uint32' 'Int63' )}}

'''
package main

import (
"fmt"
"math/rand"
"time"
)

// GenerateRandom{{$data_type}} generates a random {{lc ($data_type)}} between min and max (inclusive).
//
// Parameters:
// min ({{lc ($data_type)}}): The minimum value of the random {{lc ($data_type)}}.
// max ({{lc ($data_type)}}): The maximum value of the random {{lc ($data_type)}}.
//
// Returns:
// int: A random {{lc ($data_type)}} between min and max (inclusive).
func GenerateRandom{{$data_type}}(min, max {{lc ($data_type)}}) {{lc ($data_type)}} {
rand.Seed(time.Now().UnixNano())
return min + rand.{{$data_type}}()*(max-min)
}

func main() {
random{{$data_type}} := GenerateRandom{{$data_type}} (1, 10)
fmt.Printf("Random {{$data_type}}: %d'|n'", random{{$data_type}})
}

'''

---{{end}}
{{end}}`

// template contains golang templates
var Template = map[string][]string{
"email": {email_template1},
"markdown": {markdown_body1},
"document": {email_template1, markdown_body1},
}
14 changes: 14 additions & 0 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package gofakeit

import (
crand "crypto/rand"
b64 "encoding/base64"
"encoding/binary"
"fmt"
"math"
"math/rand"
"reflect"
Expand Down Expand Up @@ -373,3 +375,15 @@ func addSplitValsToMapParams(splitVals []string, info *Info, mapParams *MapParam
}
return mapParams
}

// function to base64 encode a string used to embed images in html
func base64EncString(value interface{}) (string, error) {
switch v := value.(type) {
case []byte:
return b64.StdEncoding.EncodeToString(v), nil
case string:
return b64.StdEncoding.EncodeToString([]byte(v)), nil
default:
return "", fmt.Errorf("value must be a string or []byte")
}
}
1 change: 1 addition & 0 deletions lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ func initLookup() {
addFileJSONLookup()
addFileXMLLookup()
addFileCSVLookup()
addTemplateLookup()
addEmojiLookup()
addImageLookup()
addNumberLookup()
Expand Down
Loading