Skip to content

Commit

Permalink
Merge pull request #273 from Mrpye/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
brianvoe authored Nov 26, 2023
2 parents 31a5a84 + 2fdb121 commit d0cd060
Show file tree
Hide file tree
Showing 10 changed files with 628 additions and 195 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
test:
strategy:
matrix:
go: [1.18, 1.19, 1.20, 1.21]
go: [1.18, 1.19, 1.20.0, 1.21]
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}

Expand Down
55 changes: 33 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -793,12 +793,15 @@ To help with formatting and using Gofakeit function there are some template help

```go
- Replace(s string, old string, new string) string // Replace a old string with new string
- Concat(sep string,args ...string) string // concatenate strings together using a separator
- Upper(s string) string // make string upper case
- Lower(s string) string // make string lower case
- Concat(sep string,args ...string) string // Concatenate strings together using a separator
- Upper(s string) string // Make string upper case
- Lower(s string) string // Make string lower case
- String(s interface{}) //Convert to string
- DateS(s string) time.Time //Convert string to date

```

</details>
</details>

<details>
<summary>2. Slice Functions</summary>
Expand All @@ -818,13 +821,30 @@ To help with formatting and using Gofakeit function there are some template help
The following Gofakeit function are not available to use in templates

```go
// Not available to use in templates
- Template(co *TemplateOptions) ([]byte, error)
- RandomMapKey(mapI any) any
- ShuffleAnySlice(v any)
- ShuffleInts(a []int)
- ShuffleStrings(a []string)
- Struct(v any)
- Slice(v any)

// Can only use with null options
- Svg(options *SVGOptions) string
- XML(options *XMLOptions) string
- JSON(options *JSONOptions) string
- CSV(options *CSVOptions) string
- Markdown(co *MarkdownOptions) (string, error)
- EmailText(co *EmailOptions) (string, error)
- FixedWidth(co *FixedWidthOptions) ([]byte, error)

//Example
// {{Svg nil}}
// {{FixedWidth nil}}
// {{String (XML nil)}}
// {{String (JSON nil)}}
// {{String (CSV nil)}}
```

</details>
Expand Down Expand Up @@ -869,36 +889,27 @@ fmt.Println(string(value))
```go
import "github.com/brianvoe/gofakeit/v6"

Seed(11)
value, err := gofakeit.FixedWidth(&FixedWidthOptions{
RowCount: 3,
Fields: []Field{
{Name: "name", //Column title
Function: "{{FirstName}} {{LastName}}", // Template function call
Params: MapParams{
"spacing": {"15"},// Specify the column width
"align": {"<"}}},// Align content left
{Name: "last_name",
Function: "lastname", // faker function call
Params: MapParams{
"spacing": {"-1"},}},// auto the column width
{Name: "Money", Function: "{{Number 1 100}}",
Params: MapParams{
"spacing": {"10"},
"align": {">"},}},// Align content right
{Name: "Name", Function: "{firstname} {lastname}"},
{Name: "Email", Function: "email"},
{Name: "Password", Function: "password", Params: MapParams{"special": {"false"}}},
{Name: "Age", Function: "{number:1,100}"},
},
})

if err != nil {
fmt.Println(err)
}

fmt.Println(string(value))

// Output:
// name last_name Money
// Markus Moen Daniel 40
// Anibal Kozey Moen 16
// Sylvan Mraz Pagac 62
// Name Email Password Age
// Markus Moen sylvanmraz@murphy.net 6VlvH6qqXc7g 13
// Alayna Wuckert santinostanton@carroll.biz g7sLrS0gEwLO 46
// Lura Lockman zacherykuhic@feil.name S8gV7Z64KlHG 12

```
</details>
Expand Down
2 changes: 1 addition & 1 deletion data/word.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ var Word = map[string][]string{

// Connectives
"connective_time": {"after a while", "afterwards", "at once", "at this moment", "at this point", "before that", "finally", "first", "here", "in the end", "lastly", "later on", "meanwhile", "next", "next time", "now", "on another occasion", "previously", "since", "soon", "straightaway", "then", "until then", "when", "whenever", "while"},
"connective_comparitive": {"additionally", "also", "as well", "even", "furthermore", "in addition", "indeed", "let alone", "moreover", "not only", "alternatively", "anyway", "but", "by contrast", "differs from", "elsewhere", "even so", "however", "in contrast", "in fact", "in other respects", "in spite of this", "in that respect", "instead", "nevertheless", "on the contrary", "on the other hand", "rather", "though", "whereas", "yet", "after all", "anyway", "besides", "moreover"},
"connective_comparative": {"additionally", "also", "as well", "even", "furthermore", "in addition", "indeed", "let alone", "moreover", "not only", "alternatively", "anyway", "but", "by contrast", "differs from", "elsewhere", "even so", "however", "in contrast", "in fact", "in other respects", "in spite of this", "in that respect", "instead", "nevertheless", "on the contrary", "on the other hand", "rather", "though", "whereas", "yet", "after all", "anyway", "besides", "moreover"},
"connective_complaint": {"besides", "e.g.", "for example", "for instance", "i.e.", "in other words", "in that", "that is to say"},
"connective_listing": {"firstly", "secondly", "first of all", "finally", "lastly", "for one thing", "for another", "in the first place", "to begin with", "next", "in summation", "to conclude"},
"connective_casual": {"accordingly", "all the same", "an effect of", "an outcome of", "an upshot of", "as a consequence of", "as a result of", "because", "caused by", "consequently", "despite this", "even though", "hence", "however", "in that case", "moreover", "nevertheless", "otherwise", "so", "so as", "stemmed from", "still", "then", "therefore", "though", "under the circumstances", "yet"},
Expand Down
11 changes: 7 additions & 4 deletions generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,24 +515,27 @@ func addGenerateLookup() {
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
co := FixedWidthOptions{}

rowcount, err := info.GetInt(m, "rowcount")
if err == nil {
co.RowCount = rowcount
rowCount, err := info.GetInt(m, "rowcount")
if err != nil {
return nil, err
}

co.RowCount = rowCount

fields, _ := info.GetStringArray(m, "fields")

// Check to make sure fields has length
if len(fields) > 0 {
co.Fields = make([]Field, len(fields))

for i, f := range fields {
// Unmarshal fields string into fields array
err = json.Unmarshal([]byte(f), &co.Fields[i])
if err != nil {
return nil, err
}
}
} else {
return nil, errors.New("missing fields")
}

out, err := fixeWidthFunc(r, &co)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/brianvoe/gofakeit/v6

go 1.18
go 1.21
30 changes: 27 additions & 3 deletions lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,12 +291,36 @@ func (i *Info) GetAny(m *MapParams, field string) (any, error) {
}

var anyValue any

// Try to convert to int
valueInt, err := strconv.ParseInt(value[0], 10, 64)
if err == nil {
return int(valueInt), nil
}

// Try to convert to float
valueFloat, err := strconv.ParseFloat(value[0], 64)
if err == nil {
return valueFloat, nil
}

// Try to convert to boolean
valueBool, err := strconv.ParseBool(value[0])
if err == nil {
return valueBool, nil
}

err = json.Unmarshal([]byte(value[0]), &anyValue)
if err != nil {
return nil, fmt.Errorf("%s field could not parse to any", field)
if err == nil {
return valueBool, nil
}

return anyValue, nil
return value[0], nil
/*if err != nil {
return nil, fmt.Errorf("%s field could not parse to any", field)
}*/

//return anyValue, nil
}

// GetMap will retrieve map[string]interface{} field from data
Expand Down
4 changes: 4 additions & 0 deletions lookup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ func TestLookupChecking(t *testing.T) {
mapData[p.Field] = []string{`{"name":"first_name","function":"firstname"}`}
case "interface":
mapData[p.Field] = []string{Letter()}
case "any":
mapData[p.Field] = []string{Letter()}
default:
t.Fatalf("Looking for %s but switch case doesnt have it", p.Type)
}
Expand Down Expand Up @@ -348,6 +350,8 @@ func TestLookupCalls(t *testing.T) {
mapData.Add(p.Field, fmt.Sprintf("%v", Float32()))
case "[]Field":
mapData.Add(p.Field, `{"name":"first_name","function":"firstname"}`)
case "any":
mapData[p.Field] = []string{Letter()}
default:
t.Fatalf("Looking for %s but switch case doesnt have it", p.Type)
}
Expand Down
Loading

0 comments on commit d0cd060

Please sign in to comment.