Skip to content

Commit

Permalink
support anyof (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
Clivern committed Nov 14, 2020
1 parent 1b4c6a9 commit 9327a2b
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM golang:1.15.5

ARG RHINO_VERSION=1.4.0
ARG RHINO_VERSION=1.5.0

ENV GO111MODULE=on

Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<p align="center">
<img alt="Rhino Logo" src="https://raw.githubusercontent.com/clivern/Rhino/master/assets/img/gopher.png?v=1.4.0" width="150" />
<img alt="Rhino Logo" src="https://raw.githubusercontent.com/clivern/Rhino/master/assets/img/gopher.png?v=1.5.0" width="150" />
<h3 align="center">Rhino</h3>
<p align="center">HTTP Mocking & Debugging Service</p>
<p align="center">
<a href="https://github.com/Clivern/Rhino/actions"><img src="https://github.com/Clivern/Rhino/workflows/Build/badge.svg"></a>
<a href="https://github.com/Clivern/Rhino/actions"><img src="https://github.com/Clivern/Rhino/workflows/Release/badge.svg"></a>
<a href="https://github.com/Clivern/Rhino/releases"><img src="https://img.shields.io/badge/Version-1.4.0-red.svg"></a>
<a href="https://goreportcard.com/report/github.com/Clivern/Rhino"><img src="https://goreportcard.com/badge/github.com/clivern/Rhino?v=1.4.0"></a>
<a href="https://github.com/Clivern/Rhino/releases"><img src="https://img.shields.io/badge/Version-1.5.0-red.svg"></a>
<a href="https://goreportcard.com/report/github.com/Clivern/Rhino"><img src="https://goreportcard.com/badge/github.com/clivern/Rhino?v=1.5.0"></a>
<a href="https://hub.docker.com/r/clivern/rhino"><img src="https://img.shields.io/badge/Docker-Latest-green"></a>
<a href="https://github.com/Clivern/Rhino/blob/master/LICENSE"><img src="https://img.shields.io/badge/LICENSE-MIT-orange.svg"></a>
</p>
Expand Down Expand Up @@ -117,6 +117,7 @@ $ curl http://127.0.0.1:8080/_health
You can use fake data flags inside response body and rhino will auto generate them. Here is the full list of supported types:

```bash
AnyOf: @fake(:anyof[A||B||C||D])
Latitude: @fake(:lat)
Longitude: @fake(:long)
CreditCardNumber: @fake(:cc_number)
Expand Down
2 changes: 1 addition & 1 deletion config.dist.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"headers": [
{"key": "Content-Type", "value": "application/json"}
],
"body": "{\"Latitude\":\"@fake(:lat)\",\"Longitude\":\"@fake(:long)\",\"CreditCardNumber\":\"@fake(:cc_number)\",\"CreditCardType\":\"@fake(:cc_type)\",\"Email\":\"@fake(:email)\",\"DomainName\":\"@fake(:domain_name)\",\"IPV4\":\"@fake(:ipv4)\",\"IPV6\":\"@fake(:ipv6)\",\"Password\":\"@fake(:password)\",\"PhoneNumber\":\"@fake(:phone_number)\",\"MacAddress\":\"@fake(:mac_address)\",\"URL\":\"@fake(:url)\",\"UserName\":\"@fake(:username)\",\"TollFreeNumber\":\"@fake(:toll_free_number)\",\"E164PhoneNumber\":\"@fake(:e_164_phone_number)\",\"TitleMale\":\"@fake(:title_male)\",\"TitleFemale\":\"@fake(:title_female)\",\"FirstName\":\"@fake(:first_name)\",\"FirstNameMale\":\"@fake(:first_name_male)\",\"FirstNameFemale\":\"@fake(:first_name_female)\",\"LastName\":\"@fake(:last_name)\",\"Name\":\"@fake(:name)\",\"UnixTime\":\"@fake(:unix_time)\",\"Date\":\"@fake(:date)\",\"Time\":\"@fake(:time)\",\"MonthName\":\"@fake(:month_name)\",\"Year\":\"@fake(:year)\",\"DayOfWeek\":\"@fake(:day_of_week)\",\"DayOfMonth\":\"@fake(:day_of_month)\",\"Timestamp\":\"@fake(:timestamp)\",\"Century\":\"@fake(:century)\",\"TimeZone\":\"@fake(:timezone)\",\"TimePeriod\":\"@fake(:time_period)\",\"Word\":\"@fake(:word)\",\"Sentence\":\"@fake(:sentence)\",\"Paragraph\":\"@fake(:paragraph)\",\"Currency\":\"@fake(:currency)\",\"Amount\":\"@fake(:amount)\",\"AmountWithCurrency\":\"@fake(:amount_with_currency)\",\"UUIDHypenated\":\"@fake(:uuid_hyphenated)\",\"UUID\":\"@fake(:uuid_digit)\"}"
"body": "{\"AnyOf\":\"@fake(:anyof[A||B||C||D])\",\"Latitude\":\"@fake(:lat)\",\"Longitude\":\"@fake(:long)\",\"CreditCardNumber\":\"@fake(:cc_number)\",\"CreditCardType\":\"@fake(:cc_type)\",\"Email\":\"@fake(:email)\",\"DomainName\":\"@fake(:domain_name)\",\"IPV4\":\"@fake(:ipv4)\",\"IPV6\":\"@fake(:ipv6)\",\"Password\":\"@fake(:password)\",\"PhoneNumber\":\"@fake(:phone_number)\",\"MacAddress\":\"@fake(:mac_address)\",\"URL\":\"@fake(:url)\",\"UserName\":\"@fake(:username)\",\"TollFreeNumber\":\"@fake(:toll_free_number)\",\"E164PhoneNumber\":\"@fake(:e_164_phone_number)\",\"TitleMale\":\"@fake(:title_male)\",\"TitleFemale\":\"@fake(:title_female)\",\"FirstName\":\"@fake(:first_name)\",\"FirstNameMale\":\"@fake(:first_name_male)\",\"FirstNameFemale\":\"@fake(:first_name_female)\",\"LastName\":\"@fake(:last_name)\",\"Name\":\"@fake(:name)\",\"UnixTime\":\"@fake(:unix_time)\",\"Date\":\"@fake(:date)\",\"Time\":\"@fake(:time)\",\"MonthName\":\"@fake(:month_name)\",\"Year\":\"@fake(:year)\",\"DayOfWeek\":\"@fake(:day_of_week)\",\"DayOfMonth\":\"@fake(:day_of_month)\",\"Timestamp\":\"@fake(:timestamp)\",\"Century\":\"@fake(:century)\",\"TimeZone\":\"@fake(:timezone)\",\"TimePeriod\":\"@fake(:time_period)\",\"Word\":\"@fake(:word)\",\"Sentence\":\"@fake(:sentence)\",\"Paragraph\":\"@fake(:paragraph)\",\"Currency\":\"@fake(:currency)\",\"Amount\":\"@fake(:amount)\",\"AmountWithCurrency\":\"@fake(:amount_with_currency)\",\"UUIDHypenated\":\"@fake(:uuid_hyphenated)\",\"UUID\":\"@fake(:uuid_digit)\"}"
},
"chaos": {
"latency": "0s",
Expand Down
8 changes: 7 additions & 1 deletion core/module/faker.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package module

import (
"fmt"
"math/rand"
"regexp"
"strings"

Expand Down Expand Up @@ -68,8 +69,13 @@ func (f *Faker) Transform(data string) (string, error) {
}

for i := 0; i < len(types); i++ {
if strings.HasPrefix(types[i], "@fake(:anyof[") {
item := strings.TrimPrefix(types[i], "@fake(:anyof[")
item = strings.TrimSuffix(item, "])")
items := strings.Split(item, "||")

if types[i] == "@fake(:lat)" {
data = strings.Replace(data, types[i], items[rand.Intn(len(items))], -1)
} else if types[i] == "@fake(:lat)" {
data = strings.Replace(data, types[i], fmt.Sprintf("%f", f.Latitude), -1)
} else if types[i] == "@fake(:long)" {
data = strings.Replace(data, types[i], fmt.Sprintf("%f", f.Longitude), -1)
Expand Down
2 changes: 1 addition & 1 deletion deployment/docker-compose/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: '3'
services:
rhino:
image: 'clivern/rhino:release-1.4.0'
image: 'clivern/rhino:release-1.5.0'
ports:
- "8080:8080"
command: '/app/rhino serve -c /app/configs/config.prod.json'
Expand Down

0 comments on commit 9327a2b

Please sign in to comment.