forked from brianvoe/gofakeit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
html.go
43 lines (32 loc) · 1.16 KB
/
html.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package gofakeit
import (
"errors"
"fmt"
"math/rand"
)
type HTMLOptions struct {
Nested bool `json:"nested" xml:"nested"`
Fields []Field `json:"fields" xml:"fields"` // The fields to be generated
}
func HTML(ho *HTMLOptions) (string, error) { return htmlFunc(globalFaker.Rand, ho) }
func (f *Faker) HTML(ho *HTMLOptions) (string, error) { return htmlFunc(f.Rand, ho) }
func htmlFunc(r *rand.Rand, ho *HTMLOptions) (string, error) {
if ho.Fields == nil { // Fields not provided, generate a stub.
htmlElement := getRandValue(r, []string{"html", "element"})
return fmt.Sprintf("<%s></%s>", htmlElement, htmlElement), nil
}
if len(ho.Fields) != 1 { // TOOD: Can we have multiple fields?
return "", errors.New("must provide only one field")
}
funcInfo := GetFuncLookup(ho.Fields[0].Function)
if funcInfo == nil {
return "", errors.New("invalid function, " + ho.Fields[0].Function + " does not exist")
}
// Generate the value
val, err := funcInfo.Generate(r, &ho.Fields[0].Params, funcInfo)
if err != nil {
return "", err
}
htmlElement := getRandValue(r, []string{"html", "element"})
return fmt.Sprintf("<%s>%s</%s>", htmlElement, val, htmlElement), nil
}