Skip to content

Commit

Permalink
refactor: minor code refacotr
Browse files Browse the repository at this point in the history
  • Loading branch information
ratanphayade committed Jul 10, 2020
1 parent 9589010 commit 2fcb624
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 54 deletions.
35 changes: 34 additions & 1 deletion generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package main

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

Expand All @@ -12,7 +14,38 @@ func init() {

type GeneratorFunc func(n int) string

var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
const (
GeneratorString = "string"
GeneratorInt = "int"
)

var (
generator = map[string]GeneratorFunc{
GeneratorString: generateString,
GeneratorInt: generateInt,
}

letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
)

func generate(tokens []string) string {
if len(tokens) != 2 {
log.Println("error: invalid number of arguments in generator")
return ""
}

if gen, ok := generator[tokens[0]]; ok {
val, err := strconv.Atoi(tokens[1])
if err != nil {
log.Println("error: ", err)
}

return gen(val)
}

log.Println("error: specified generator not found")
return ""
}

func generateString(n int) string {
b := make([]rune, n)
Expand Down
53 changes: 2 additions & 51 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,13 @@ package main
import (
"log"
"regexp"
"strconv"
"strings"
)

const (
ResolverGenerator = "generate"
ResolverCopy = "copy"
ResolverCustom = "custom"

GeneratorString = "string"
GeneratorInt = "int"

CustomConstant = "constant"
)

var (
generator = map[string]GeneratorFunc{
GeneratorString: generateString,
GeneratorInt: generateInt,
}

call = map[string]callerFunc{
CustomConstant: callConcat,
}
ResolverCall = "call"
)

type Response struct {
Expand Down Expand Up @@ -75,7 +58,7 @@ func resolve(key string, tokens []string, data collector) string {
case ResolverCopy:
return copyFrom(tokens, data)

case ResolverCustom:
case ResolverCall:
return customCall(data, tokens)
}

Expand All @@ -90,35 +73,3 @@ func copyFrom(tokens []string, data collector) string {

return data.get(tokens[0], tokens[1])
}

func generate(tokens []string) string {
if len(tokens) != 2 {
log.Println("error: invalid number of arguments in generator")
return ""
}

if gen, ok := generator[tokens[0]]; ok {
val, err := strconv.Atoi(tokens[1])
if err != nil {
log.Println("error: ", err)
}

return gen(val)
}

log.Println("error: specified generator not found")
return ""
}

func customCall(data collector, tokens []string) string {
if len(tokens) < 1 {
log.Println("error: invalid number of arguments in call")
return ""
}

if caller, ok := call[tokens[0]]; ok {
return caller(data, tokens[1:]...)
}

return ""
}
2 changes: 1 addition & 1 deletion test/app/create_users.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{
"response": {
"label": "create",
"body": "{\"id\":\"{{ generate string 14 }}\",\"firstname\":\"{{ copy body firstname }}\",\"lastname\":\"{{ custom constant }}\",\"last_seen\":\"{{ copy body option.status }}\"}",
"body": "{\"id\":\"{{ generate string 14 }}\",\"firstname\":\"{{ copy body firstname }}\",\"lastname\":\"{{ call custom }}\",\"last_seen\":\"{{ copy body option.status }}\"}",
"latency": 0,
"status_code": 201,
"headers": {
Expand Down
2 changes: 1 addition & 1 deletion test/app/get_user.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"label": "search",
"body": "{\"id\":2,\"firstname\":\"John\",\"lastname\":\"Snow\",\"last_seen\":\"The Iron Throne\"}",
"latency": 0,
"status_code": 201,
"status_code": 200,
"headers": {
"Content-Type": "application/json"
}
Expand Down

0 comments on commit 2fcb624

Please sign in to comment.