Skip to content

Commit 2452c99

Browse files
committed
feat(cli): add price and float placeholder
1 parent 8179b96 commit 2452c99

3 files changed

Lines changed: 78 additions & 16 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ test-template.json
33
test
44
*.test
55
test.go
6-
tabby
6+
.env

cmd/post.go

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,26 @@ var postCmd = &cobra.Command{
3030
Long: `Make a POST request to the specified URL with optional data and headers.
3131
3232
Supports random data generation using templates with placeholders:
33-
{{name}} - Random full name
34-
{{firstname}} - Random first name
35-
{{lastname}} - Random last name
36-
{{email}} - Random email address
37-
{{phone}} - Random phone number
38-
{{number}} - Random number (1-100)
39-
{{number:1:50}} - Random number with range
40-
{{text}} - Random paragraph
41-
{{sentence}} - Random sentence
42-
{{uuid}} - Random UUID
43-
{{bool}} - Random boolean
44-
{{date}} - Random date`,
33+
{{name}} - Random full name
34+
{{firstname}} - Random first name
35+
{{lastname}} - Random last name
36+
{{email}} - Random email address
37+
{{phone}} - Random phone number
38+
{{number}} - Random number (1-100)
39+
{{number:1:50}} - Random number with range
40+
{{float}} - Random float (0-100, 2 decimals)
41+
{{float:1:50:3}} - Random float with range and decimal places
42+
{{price}} - Random price ending in .99 (1-100)
43+
{{price:5:50}} - Random price with range
44+
{{text}} - Random paragraph
45+
{{sentence}} - Random sentence
46+
{{uuid}} - Random UUID
47+
{{bool}} - Random boolean
48+
{{date}} - Random date
49+
50+
Note: For numeric JSON fields, don't wrap placeholders in quotes:
51+
✓ "price": {{price}}
52+
✗ "price": "{{price}}"`,
4553
Args: cobra.ExactArgs(1),
4654
Run: func(cmd *cobra.Command, args []string) {
4755
url := args[0]
@@ -108,9 +116,7 @@ Supports random data generation using templates with placeholders:
108116
os.Exit(1)
109117
}
110118

111-
if postContentType != "" {
112-
req.Header.Set("Content-Type", postContentType)
113-
}
119+
req.Header.Set("Content-Type", postContentType)
114120

115121
for _, header := range postHeaders {
116122
parts := strings.SplitN(header, ":", 2)

internal/generator/generator.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ func (g *Generator) generateValue(placeholderType string, params []string) (stri
9292
return g.randomPhone(), nil
9393
case "number", "int":
9494
return g.randomNumber(params), nil
95+
case "float", "decimal":
96+
return g.randomFloat(params), nil
97+
case "price":
98+
return g.randomPrice(params), nil
9599
case "text", "paragraph":
96100
return g.randomParagraph(), nil
97101
case "sentence":
@@ -156,6 +160,58 @@ func (g *Generator) randomNumber(params []string) string {
156160
return strconv.Itoa(g.secureRandomInt(max-min) + min)
157161
}
158162

163+
func (g *Generator) randomFloat(params []string) string {
164+
min, max := 0.0, 100.0
165+
decimals := 2
166+
167+
if len(params) >= 1 {
168+
if val, err := strconv.ParseFloat(params[0], 64); err == nil {
169+
min = val
170+
}
171+
}
172+
if len(params) >= 2 {
173+
if val, err := strconv.ParseFloat(params[1], 64); err == nil {
174+
max = val
175+
}
176+
}
177+
if len(params) >= 3 {
178+
if val, err := strconv.Atoi(params[2]); err == nil {
179+
decimals = val
180+
}
181+
}
182+
183+
if max <= min {
184+
max = min + 1
185+
}
186+
187+
randFraction := float64(g.secureRandomInt(10000)) / 10000.0
188+
value := min + randFraction*(max-min)
189+
190+
return strconv.FormatFloat(value, 'f', decimals, 64)
191+
}
192+
193+
func (g *Generator) randomPrice(params []string) string {
194+
min, max := 1.0, 100.0
195+
196+
if len(params) >= 1 {
197+
if val, err := strconv.ParseFloat(params[0], 64); err == nil {
198+
min = val
199+
}
200+
}
201+
if len(params) >= 2 {
202+
if val, err := strconv.ParseFloat(params[1], 64); err == nil {
203+
max = val
204+
}
205+
}
206+
207+
if max <= min {
208+
max = min + 1
209+
}
210+
211+
wholeNumber := g.secureRandomInt(int(max-min)) + int(min)
212+
return fmt.Sprintf("%d.99", wholeNumber)
213+
}
214+
159215
func (g *Generator) randomParagraph() string {
160216
wordCount := g.secureRandomInt(20) + 30
161217
var result []string

0 commit comments

Comments
 (0)