Skip to content

Commit

Permalink
fields - simplified and replaced with name fields
Browse files Browse the repository at this point in the history
  • Loading branch information
brianvoe committed Nov 26, 2023
1 parent 6306bc7 commit 0cff5c0
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 8 deletions.
2 changes: 1 addition & 1 deletion csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
type CSVOptions struct {
Delimiter string `json:"delimiter" xml:"delimiter" fake:"{randomstring:[,,tab]}"`
RowCount int `json:"row_count" xml:"row_count" fake:"{number:1,10}"`
Fields []Field `json:"fields" xml:"fields" fake:"{internal_exampleFields}"`
Fields []Field `json:"fields" xml:"fields" fake:"{fields}"`
}

// CSV generates an object or an array of objects in json format
Expand Down
2 changes: 1 addition & 1 deletion generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func generate(r *rand.Rand, dataVal string) string {
// FixedWidthOptions defines values needed for csv generation
type FixedWidthOptions struct {
RowCount int `json:"row_count" xml:"row_count" fake:"{number:1,10}"`
Fields []Field `json:"fields" xml:"fields" fake:"{internal_exampleFields}"`
Fields []Field `json:"fields" xml:"fields" fake:"{fields}"`
}

// FixedWidth generates an table of random data in fixed width format
Expand Down
2 changes: 1 addition & 1 deletion json.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
type JSONOptions struct {
Type string `json:"type" xml:"type" fake:"{randomstring:[array,object]}"` // array or object
RowCount int `json:"row_count" xml:"row_count" fake:"{number:1,10}"`
Fields []Field `json:"fields" xml:"fields" fake:"{internal_exampleFields}"`
Fields []Field `json:"fields" xml:"fields" fake:"{fields}"`
Indent bool `json:"indent" xml:"indent"`
}

Expand Down
35 changes: 31 additions & 4 deletions lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,21 @@ func initLookup() {
addWordVerbLookup()
}

// internalFuncLookups is the internal map array with mapping to all available data
var internalFuncLookups map[string]Info = map[string]Info{
"fields": {
Description: "Example fields for generating csv, json, xml, etc",
Output: "gofakeit.Field",
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
function, _ := GetRandomSimpleFunc(r)
return Field{
Name: function,
Function: function,
}, nil
},
},
}

// NewMapParams will create a new MapParams
func NewMapParams() *MapParams {
return &MapParams{}
Expand Down Expand Up @@ -174,11 +189,14 @@ func (m *MapParamsValue) UnmarshalJSON(data []byte) error {
return nil
}

func GetRandomFunc(r *rand.Rand) (string, Info) {
func GetRandomSimpleFunc(r *rand.Rand) (string, Info) {
// Loop through all the functions and add them to a slice
var keys []string
for k := range FuncLookups {
keys = append(keys, k)
for k, info := range FuncLookups {
// Only grab simple functions
if info.Params == nil {
keys = append(keys, k)
}
}

// Randomly grab a function from the slice
Expand Down Expand Up @@ -206,7 +224,16 @@ func AddFuncLookup(functionName string, info Info) {

// GetFuncLookup will lookup
func GetFuncLookup(functionName string) *Info {
info, ok := FuncLookups[functionName]
var info Info
var ok bool

// Check internal functions first
info, ok = internalFuncLookups[functionName]
if ok {
return &info
}

info, ok = FuncLookups[functionName]
if ok {
return &info
}
Expand Down
2 changes: 1 addition & 1 deletion xml.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type XMLOptions struct {
RootElement string `json:"root_element" xml:"root_element"`
RecordElement string `json:"record_element" xml:"record_element"`
RowCount int `json:"row_count" xml:"row_count" fake:"{number:1,10}"`
Fields []Field `json:"fields" xml:"fields" fake:"{internal_exampleFields}"`
Fields []Field `json:"fields" xml:"fields" fake:"{fields}"`
Indent bool `json:"indent" xml:"indent"`
}

Expand Down

0 comments on commit 0cff5c0

Please sign in to comment.