forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/brianvoe/gofakeit/v6/product.go

416 lines
8.3 KiB
Go
Raw Normal View History

2024-04-26 19:30:35 +00:00
package gofakeit
import (
"fmt"
"math/rand"
"strings"
)
type ProductInfo struct {
Name string `json:"name" xml:"name"`
Description string `json:"description" xml:"description"`
Categories []string `json:"categories" xml:"categories"`
Price float64 `json:"price" xml:"price"`
Features []string `json:"features" xml:"features"`
Color string `json:"color" xml:"color"`
Material string `json:"material" xml:"material"`
UPC string `json:"upc" xml:"upc"`
2024-04-26 19:30:35 +00:00
}
// Product will generate a random set of product information
2024-04-26 19:30:35 +00:00
func Product() *ProductInfo { return product(globalFaker.Rand) }
// Product will generate a random set of product information
2024-04-26 19:30:35 +00:00
func (f *Faker) Product() *ProductInfo { return product(f.Rand) }
func product(r *rand.Rand) *ProductInfo {
2024-04-26 19:30:35 +00:00
// Categories
2024-04-26 19:30:35 +00:00
categories := []string{}
2024-04-26 19:30:35 +00:00
weightedCategory, _ := weighted(r, []any{1, 2, 3, 4}, []float32{1, 4, 3, 4})
for i := 0; i < weightedCategory.(int); i++ {
2024-04-26 19:30:35 +00:00
categories = append(categories, productCategory(r))
2024-04-26 19:30:35 +00:00
}
// Features
2024-04-26 19:30:35 +00:00
features := []string{}
2024-04-26 19:30:35 +00:00
for i := 0; i < number(r, 1, 5); i++ {
2024-04-26 19:30:35 +00:00
features = append(features, productFeature(r))
2024-04-26 19:30:35 +00:00
}
product := &ProductInfo{
Name: productName(r),
2024-04-26 19:30:35 +00:00
Description: productDescription(r),
Categories: categories,
Price: price(r, 3.00, 100.00),
UPC: productUPC(r),
Features: features,
Color: safeColor(r),
Material: productMaterial(r),
2024-04-26 19:30:35 +00:00
}
return product
2024-04-26 19:30:35 +00:00
}
// ProductName will generate a random product name
2024-04-26 19:30:35 +00:00
func ProductName() string { return productName(globalFaker.Rand) }
// ProductName will generate a random product name
2024-04-26 19:30:35 +00:00
func (f *Faker) ProductName() string { return productName(f.Rand) }
func productName(r *rand.Rand) string {
2024-04-26 19:30:35 +00:00
name := getRandValue(r, []string{"product", "name"})
2024-04-26 19:30:35 +00:00
switch number(r, 0, 9) {
2024-04-26 19:30:35 +00:00
case 1:
2024-04-26 19:30:35 +00:00
// Name + Adjective + Feature
2024-04-26 19:30:35 +00:00
return title(fmt.Sprintf("%s %s %s", name, getRandValue(r, []string{"product", "adjective"}), productFeature(r)))
2024-04-26 19:30:35 +00:00
case 2:
2024-04-26 19:30:35 +00:00
// Adjective + Material + Name
2024-04-26 19:30:35 +00:00
return title(fmt.Sprintf("%s %s %s", getRandValue(r, []string{"product", "adjective"}), productMaterial(r), name))
2024-04-26 19:30:35 +00:00
case 3:
2024-04-26 19:30:35 +00:00
// Color + Name + Suffix
2024-04-26 19:30:35 +00:00
return title(fmt.Sprintf("%s %s %s", safeColor(r), name, getRandValue(r, []string{"product", "suffix"})))
2024-04-26 19:30:35 +00:00
case 4:
2024-04-26 19:30:35 +00:00
// Feature + Name + Adjective
2024-04-26 19:30:35 +00:00
return title(fmt.Sprintf("%s %s %s", productFeature(r), name, getRandValue(r, []string{"product", "adjective"})))
2024-04-26 19:30:35 +00:00
case 5:
2024-04-26 19:30:35 +00:00
// Material + Color + Name
2024-04-26 19:30:35 +00:00
return title(fmt.Sprintf("%s %s %s", productMaterial(r), safeColor(r), name))
2024-04-26 19:30:35 +00:00
case 6:
2024-04-26 19:30:35 +00:00
// Name + Suffix + Material
2024-04-26 19:30:35 +00:00
return title(fmt.Sprintf("%s %s %s", name, getRandValue(r, []string{"product", "suffix"}), productMaterial(r)))
2024-04-26 19:30:35 +00:00
case 7:
2024-04-26 19:30:35 +00:00
// Adjective + Feature + Name
2024-04-26 19:30:35 +00:00
return title(fmt.Sprintf("%s %s %s", getRandValue(r, []string{"product", "adjective"}), productFeature(r), name))
2024-04-26 19:30:35 +00:00
case 8:
2024-04-26 19:30:35 +00:00
// Color + Material + Name
2024-04-26 19:30:35 +00:00
return title(fmt.Sprintf("%s %s %s", safeColor(r), productMaterial(r), name))
2024-04-26 19:30:35 +00:00
case 9:
2024-04-26 19:30:35 +00:00
// Suffix + Adjective + Name
2024-04-26 19:30:35 +00:00
return title(fmt.Sprintf("%s %s %s", getRandValue(r, []string{"product", "suffix"}), getRandValue(r, []string{"product", "adjective"}), name))
2024-04-26 19:30:35 +00:00
}
// case: 0 - Adjective + Name + Suffix
2024-04-26 19:30:35 +00:00
return title(fmt.Sprintf("%s %s %s", getRandValue(r, []string{"product", "adjective"}), name, getRandValue(r, []string{"product", "suffix"})))
2024-04-26 19:30:35 +00:00
}
// ProductDescription will generate a random product description
2024-04-26 19:30:35 +00:00
func ProductDescription() string { return productDescription(globalFaker.Rand) }
// ProductDescription will generate a random product description
2024-04-26 19:30:35 +00:00
func (f *Faker) ProductDescription() string { return productDescription(f.Rand) }
func productDescription(r *rand.Rand) string {
2024-04-26 19:30:35 +00:00
desc := []string{}
2024-04-26 19:30:35 +00:00
for i := 0; i < number(r, 1, 3); i++ {
2024-04-26 19:30:35 +00:00
desc = append(desc, sentence(r, number(r, 5, 15)))
2024-04-26 19:30:35 +00:00
}
return strings.Join(desc, " ")
2024-04-26 19:30:35 +00:00
}
// ProductCategory will generate a random product category
2024-04-26 19:30:35 +00:00
func ProductCategory() string { return productCategory(globalFaker.Rand) }
// ProductCategory will generate a random product category
2024-04-26 19:30:35 +00:00
func (f *Faker) ProductCategory() string { return productCategory(f.Rand) }
func productCategory(r *rand.Rand) string {
2024-04-26 19:30:35 +00:00
return getRandValue(r, []string{"product", "category"})
2024-04-26 19:30:35 +00:00
}
// ProductFeature will generate a random product feature
2024-04-26 19:30:35 +00:00
func ProductFeature() string { return productFeature(globalFaker.Rand) }
// ProductFeature will generate a random product feature
2024-04-26 19:30:35 +00:00
func (f *Faker) ProductFeature() string { return productFeature(f.Rand) }
func productFeature(r *rand.Rand) string {
2024-04-26 19:30:35 +00:00
return getRandValue(r, []string{"product", "feature"})
2024-04-26 19:30:35 +00:00
}
// ProductMaterial will generate a random product material
2024-04-26 19:30:35 +00:00
func ProductMaterial() string { return productMaterial(globalFaker.Rand) }
// ProductMaterial will generate a random product material
2024-04-26 19:30:35 +00:00
func (f *Faker) ProductMaterial() string { return productMaterial(f.Rand) }
func productMaterial(r *rand.Rand) string {
2024-04-26 19:30:35 +00:00
return getRandValue(r, []string{"product", "material"})
2024-04-26 19:30:35 +00:00
}
// ProductUPC will generate a random product UPC
2024-04-26 19:30:35 +00:00
func ProductUPC() string { return productUPC(globalFaker.Rand) }
// ProductUPC will generate a random product UPC
2024-04-26 19:30:35 +00:00
func (f *Faker) ProductUPC() string { return productUPC(f.Rand) }
func productUPC(r *rand.Rand) string {
2024-04-26 19:30:35 +00:00
// The first digit of a UPC is a fixed digit (usually 0)
2024-04-26 19:30:35 +00:00
upc := "0"
// Generate the remaining 11 digits randomly
2024-04-26 19:30:35 +00:00
for i := 1; i < 12; i++ {
2024-04-26 19:30:35 +00:00
digit := number(r, 0, 9)
2024-04-26 19:30:35 +00:00
upc += fmt.Sprintf("%d", digit)
2024-04-26 19:30:35 +00:00
}
return upc
2024-04-26 19:30:35 +00:00
}
func addProductLookup() {
2024-04-26 19:30:35 +00:00
AddFuncLookup("product", Info{
Display: "Product",
Category: "product",
2024-04-26 19:30:35 +00:00
Description: "An item created for sale or use",
2024-04-26 19:30:35 +00:00
Example: `{
2024-04-26 19:30:35 +00:00
"name": "olive copper monitor",
2024-04-26 19:30:35 +00:00
"description": "Backwards caused quarterly without week it hungry thing someone him regularly. Whomever this revolt hence from his timing as quantity us these yours.",
2024-04-26 19:30:35 +00:00
"categories": [
2024-04-26 19:30:35 +00:00
"clothing",
2024-04-26 19:30:35 +00:00
"tools and hardware"
2024-04-26 19:30:35 +00:00
],
2024-04-26 19:30:35 +00:00
"price": 7.61,
2024-04-26 19:30:35 +00:00
"features": [
2024-04-26 19:30:35 +00:00
"ultra-lightweight"
2024-04-26 19:30:35 +00:00
],
2024-04-26 19:30:35 +00:00
"color": "navy",
2024-04-26 19:30:35 +00:00
"material": "brass",
2024-04-26 19:30:35 +00:00
"upc": "012780949980"
2024-04-26 19:30:35 +00:00
}`,
Output: "map[string]any",
2024-04-26 19:30:35 +00:00
ContentType: "application/json",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return product(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("productname", Info{
Display: "Product Name",
Category: "product",
2024-04-26 19:30:35 +00:00
Description: "Distinctive title or label assigned to a product for identification and marketing",
Example: "olive copper monitor",
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return productName(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("productdescription", Info{
Display: "Product Description",
Category: "product",
2024-04-26 19:30:35 +00:00
Description: "Explanation detailing the features and characteristics of a product",
Example: "Backwards caused quarterly without week it hungry thing someone him regularly. Whomever this revolt hence from his timing as quantity us these yours.",
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return productDescription(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("productcategory", Info{
Display: "Product Category",
Category: "product",
2024-04-26 19:30:35 +00:00
Description: "Classification grouping similar products based on shared characteristics or functions",
Example: "clothing",
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return productCategory(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("productfeature", Info{
Display: "Product Feature",
Category: "product",
2024-04-26 19:30:35 +00:00
Description: "Specific characteristic of a product that distinguishes it from others products",
Example: "ultra-lightweight",
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return productFeature(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("productmaterial", Info{
Display: "Product Material",
Category: "product",
2024-04-26 19:30:35 +00:00
Description: "The substance from which a product is made, influencing its appearance, durability, and properties",
Example: "brass",
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return productMaterial(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("productupc", Info{
Display: "Product UPC",
Category: "product",
2024-04-26 19:30:35 +00:00
Description: "Standardized barcode used for product identification and tracking in retail and commerce",
Example: "012780949980",
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return productUPC(r), nil
2024-04-26 19:30:35 +00:00
},
})
2024-04-26 19:30:35 +00:00
}