2024-04-26 19:30:35 +00:00
|
|
|
package gofakeit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"math/rand"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/brianvoe/gofakeit/v6/data"
|
|
|
|
)
|
|
|
|
|
|
|
|
// InputName will return a random input field name
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
func InputName() string {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
return inputName(globalFaker.Rand)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// InputName will return a random input field name
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
func (f *Faker) InputName() string {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
return inputName(f.Rand)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func inputName(r *rand.Rand) string {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
return getRandValue(r, []string{"html", "input_name"})
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type SVGOptions struct {
|
|
|
|
Height int
|
2024-07-24 23:45:04 +00:00
|
|
|
|
|
|
|
Width int
|
|
|
|
|
|
|
|
Type string
|
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
Colors []string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate a random svg generator
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
func Svg(options *SVGOptions) string { return svg(globalFaker.Rand, options) }
|
|
|
|
|
|
|
|
// Generate a random svg generator
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
func (f *Faker) Svg(options *SVGOptions) string { return svg(f.Rand, options) }
|
|
|
|
|
|
|
|
func svg(r *rand.Rand, options *SVGOptions) string {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
// If options is nil, set it to empty struct
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
if options == nil {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
options = &SVGOptions{}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If options height and weight is not set, set it to random number between 100 and 500
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
if options.Width == 0 {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
options.Width = number(r, 100, 500)
|
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
|
|
|
widthStr := strconv.Itoa(options.Width)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
if options.Height == 0 {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
options.Height = number(r, 100, 500)
|
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
|
|
|
heightStr := strconv.Itoa(options.Height)
|
|
|
|
|
|
|
|
// Check if type is set, if not set to random type
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
if options.Type == "" {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
options.Type = randomString(r, data.GetSubData("html", "svg"))
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the colors are not set, set it to a set of nice colors
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
if len(options.Colors) == 0 {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
options.Colors = niceColors(r)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start svg string
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
svgStr := `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ` + widthStr + ` ` + heightStr + `" width="` + widthStr + `" height="` + heightStr + `">`
|
|
|
|
|
|
|
|
// Add a rect for the background
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
svgStr += `<rect x="0" y="0" width="100%" height="100%" fill="` + randomString(r, options.Colors) + `" />`
|
|
|
|
|
|
|
|
// Add a random number of shapes
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
for i := 0; i < number(r, 10, 20); i++ {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
// Add a random shape
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
switch options.Type {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
case "rect":
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
svgStr += `<rect x="` + strconv.Itoa(number(r, 0, options.Width)) + `" y="` + strconv.Itoa(number(r, 0, options.Height)) + `" width="` + strconv.Itoa(number(r, 0, options.Width)) + `" height="` + strconv.Itoa(number(r, 0, options.Height)) + `" fill="` + randomString(r, options.Colors) + `" />`
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
case "circle":
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
svgStr += `<circle cx="` + strconv.Itoa(number(r, 0, options.Width)) + `" cy="` + strconv.Itoa(number(r, 0, options.Height)) + `" r="` + strconv.Itoa(number(r, 0, options.Width)) + `" fill="` + randomString(r, options.Colors) + `" />`
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
case "ellipse":
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
svgStr += `<ellipse cx="` + strconv.Itoa(number(r, 0, options.Width)) + `" cy="` + strconv.Itoa(number(r, 0, options.Height)) + `" rx="` + strconv.Itoa(number(r, 0, options.Width)) + `" ry="` + strconv.Itoa(number(r, 0, options.Height)) + `" fill="` + randomString(r, options.Colors) + `" />`
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
case "line":
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
svgStr += `<line x1="` + strconv.Itoa(number(r, 0, options.Width)) + `" y1="` + strconv.Itoa(number(r, 0, options.Height)) + `" x2="` + strconv.Itoa(number(r, 0, options.Width)) + `" y2="` + strconv.Itoa(number(r, 0, options.Height)) + `" stroke="` + randomString(r, options.Colors) + `" />`
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
case "polyline":
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
svgStr += `<polyline points="` + strconv.Itoa(number(r, 0, options.Width)) + `,` + strconv.Itoa(number(r, 0, options.Height)) + ` ` + strconv.Itoa(number(r, 0, options.Width)) + `,` + strconv.Itoa(number(r, 0, options.Height)) + ` ` + strconv.Itoa(number(r, 0, options.Width)) + `,` + strconv.Itoa(number(r, 0, options.Height)) + `" fill="` + randomString(r, options.Colors) + `" />`
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
case "polygon":
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
svgStr += `<polygon points="` + strconv.Itoa(number(r, 0, options.Width)) + `,` + strconv.Itoa(number(r, 0, options.Height)) + ` ` + strconv.Itoa(number(r, 0, options.Width)) + `,` + strconv.Itoa(number(r, 0, options.Height)) + ` ` + strconv.Itoa(number(r, 0, options.Width)) + `,` + strconv.Itoa(number(r, 0, options.Height)) + `" fill="` + randomString(r, options.Colors) + `" />`
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// End svg string
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
svgStr += `</svg>`
|
|
|
|
|
|
|
|
return svgStr
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func addHtmlLookup() {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
AddFuncLookup("inputname", Info{
|
2024-07-24 23:45:04 +00:00
|
|
|
|
|
|
|
Display: "Input Name",
|
|
|
|
|
|
|
|
Category: "html",
|
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
Description: "Attribute used to define the name of an input element in web forms",
|
2024-07-24 23:45:04 +00:00
|
|
|
|
|
|
|
Example: "first_name",
|
|
|
|
|
|
|
|
Output: "string",
|
|
|
|
|
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
|
|
|
return inputName(r), nil
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
AddFuncLookup("svg", Info{
|
2024-07-24 23:45:04 +00:00
|
|
|
|
|
|
|
Display: "Image SVG",
|
|
|
|
|
|
|
|
Category: "html",
|
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
Description: "Scalable Vector Graphics used to display vector images in web content",
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
Example: `<svg width="369" height="289">
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
<rect fill="#4f2958" />
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
<polygon points="382,87 418,212 415,110" fill="#fffbb7" />
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
</svg>`,
|
2024-07-24 23:45:04 +00:00
|
|
|
|
|
|
|
Output: "string",
|
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
ContentType: "image/svg+xml",
|
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: "width", Display: "Width", Type: "int", Default: "500", Description: "Width in px"},
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
{Field: "height", Display: "Height", Type: "int", Default: "500", Description: "Height in px"},
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
{Field: "type", Display: "Type", Type: "string", Optional: true, Options: data.GetSubData("html", "svg"), Description: "Sub child element type"},
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
{Field: "colors", Display: "Colors", Type: "[]string", Optional: true, Description: "Hex or RGB array of colors to use"},
|
|
|
|
},
|
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
|
|
|
// Setup new options
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
options := SVGOptions{}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
options.Width, err = info.GetInt(m, "width")
|
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
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
if options.Width < 10 || options.Width >= 1000 {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
return nil, errors.New("invalid image width, must be greater than 10, less than 1000")
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
options.Height, err = info.GetInt(m, "height")
|
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
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
if options.Height < 10 || options.Height >= 1000 {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
return nil, errors.New("invalid image height, must be greater than 10, less than 1000")
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
options.Type, err = info.GetString(m, "type")
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
svgData := data.GetSubData("html", "svg")
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// If type is empty, set with random type
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
if options.Type == "" {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
options.Type = randomString(r, svgData)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If not in date html svg type array, return error
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
if !stringInSlice(options.Type, svgData) {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
return nil, errors.New("invalid svg type, must be one of " + strings.Join(svgData, ","))
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get colors
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
options.Colors, err = info.GetStringArray(m, "colors")
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// If colors is empty, set with random colors
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
if len(options.Colors) == 0 {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
options.Colors = niceColors(r)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-04-26 19:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return svg(r, &options), nil
|
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
|
|
|
}
|