Skip to content

Commit 87651be

Browse files
committed
refactor: seperate faker functions
Signed-off-by: Dwi Siswanto <git@dw1.io>
1 parent c18fe9a commit 87651be

2 files changed

Lines changed: 368 additions & 350 deletions

File tree

dsl.go

Lines changed: 78 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ var (
8383
var PrintDebugCallback func(args ...interface{}) error
8484

8585
var functions []dslFunction
86-
var fakerFunctions []string
86+
var fakerFunctions []dslFunction
8787

8888
func AddFunction(function dslFunction) error {
8989
for _, f := range functions {
@@ -95,11 +95,13 @@ func AddFunction(function dslFunction) error {
9595
return nil
9696
}
9797

98-
func AddFakerFunction(function dslFunction) error {
99-
if err := AddFunction(function); err != nil {
100-
return err
98+
func addFakerFunction(function dslFunction) error {
99+
for _, f := range functions {
100+
if function.Name == f.Name {
101+
return fmt.Errorf("%w: %q", errDuplicateFunc, f.Name)
102+
}
101103
}
102-
fakerFunctions = append(fakerFunctions, function.Name)
104+
fakerFunctions = append(fakerFunctions, function)
103105
return nil
104106
}
105107

@@ -1318,8 +1320,51 @@ func init() {
13181320
},
13191321
))
13201322

1321-
// Add faker functions
1323+
DefaultHelperFunctions = HelperFunctions()
1324+
FunctionNames = GetFunctionNames(DefaultHelperFunctions)
1325+
}
1326+
1327+
// Helper function to generate function signatures for faker functions
1328+
func getFakerSignature(info gofakeit.Info) string {
1329+
var params []string
1330+
for _, p := range info.Params {
1331+
params = append(params, fmt.Sprintf("%s %s", p.Field, p.Type))
1332+
}
1333+
return fmt.Sprintf("(%s) %s", strings.Join(params, ", "), info.Output)
1334+
}
1335+
1336+
func NewWithSingleSignature(name, signature string, cacheable bool, logic govaluate.ExpressionFunction) dslFunction {
1337+
return NewWithMultipleSignatures(name, []string{signature}, cacheable, logic)
1338+
}
1339+
1340+
func NewWithMultipleSignatures(name string, signatures []string, cacheable bool, expr govaluate.ExpressionFunction) dslFunction {
1341+
function := dslFunction{
1342+
Name: name,
1343+
Signatures: signatures,
1344+
ExpressionFunction: expr,
1345+
IsCacheable: cacheable,
1346+
}
1347+
1348+
return function
1349+
}
1350+
1351+
func NewWithPositionalArgs(name string, numberOfArgs int, cacheable bool, expr govaluate.ExpressionFunction) dslFunction {
1352+
function := dslFunction{
1353+
Name: name,
1354+
NumberOfArgs: numberOfArgs,
1355+
ExpressionFunction: expr,
1356+
IsCacheable: cacheable,
1357+
}
1358+
return function
1359+
}
1360+
1361+
// FakerFunctions returns the faker functions
1362+
//
1363+
// Note: It does not support backwards compatibility for function names
1364+
func FakerFunctions() map[string]govaluate.ExpressionFunction {
1365+
funcs := make(map[string]govaluate.ExpressionFunction)
13221366
slug.CustomSub = map[string]string{" ": "_"}
1367+
13231368
for _, fInfo := range gofakeit.FuncLookups {
13241369
// NOTE(dwisiswant0): Skipping function because it not callable or the
13251370
// output is not printable.
@@ -1366,58 +1411,25 @@ func init() {
13661411

13671412
value, err := fInfo.Generate(faker, params, &fInfo)
13681413
if err != nil {
1369-
return "", err
1414+
return "", fmt.Errorf("faker error: %w", err)
13701415
}
13711416

13721417
return value, nil
13731418
}
13741419
}
13751420

13761421
// Register the function with the DSL
1377-
err := AddFakerFunction(NewWithSingleSignature(
1378-
funcName, getFakerSignature(fInfo), false, fakerFunc(fInfo),
1422+
f := fakerFunc(fInfo)
1423+
err := addFakerFunction(NewWithSingleSignature(
1424+
funcName, getFakerSignature(fInfo), false, f,
13791425
))
13801426
if err != nil && !errors.Is(err, errDuplicateFunc) {
13811427
panic(fmt.Errorf("%w (faker)", err))
13821428
}
1429+
funcs[funcName] = f
13831430
}
13841431

1385-
DefaultHelperFunctions = HelperFunctions()
1386-
FunctionNames = GetFunctionNames(DefaultHelperFunctions)
1387-
}
1388-
1389-
// Helper function to generate function signatures for faker functions
1390-
func getFakerSignature(info gofakeit.Info) string {
1391-
var params []string
1392-
for _, p := range info.Params {
1393-
params = append(params, fmt.Sprintf("%s %s", p.Field, p.Type))
1394-
}
1395-
return fmt.Sprintf("(%s) %s", strings.Join(params, ", "), info.Output)
1396-
}
1397-
1398-
func NewWithSingleSignature(name, signature string, cacheable bool, logic govaluate.ExpressionFunction) dslFunction {
1399-
return NewWithMultipleSignatures(name, []string{signature}, cacheable, logic)
1400-
}
1401-
1402-
func NewWithMultipleSignatures(name string, signatures []string, cacheable bool, expr govaluate.ExpressionFunction) dslFunction {
1403-
function := dslFunction{
1404-
Name: name,
1405-
Signatures: signatures,
1406-
ExpressionFunction: expr,
1407-
IsCacheable: cacheable,
1408-
}
1409-
1410-
return function
1411-
}
1412-
1413-
func NewWithPositionalArgs(name string, numberOfArgs int, cacheable bool, expr govaluate.ExpressionFunction) dslFunction {
1414-
function := dslFunction{
1415-
Name: name,
1416-
NumberOfArgs: numberOfArgs,
1417-
ExpressionFunction: expr,
1418-
IsCacheable: cacheable,
1419-
}
1420-
return function
1432+
return funcs
14211433
}
14221434

14231435
// HelperFunctions returns the dsl helper functions
@@ -1444,23 +1456,37 @@ func GetFunctionNames(heperFunctions map[string]govaluate.ExpressionFunction) []
14441456
return maputils.GetKeys(heperFunctions)
14451457
}
14461458

1459+
// GetPrintableDslFunctionSignatures returns the function signatures for the
1460+
// default DSL functions
14471461
func GetPrintableDslFunctionSignatures(noColor bool) string {
14481462
if noColor {
1449-
return aggregate(getDslFunctionSignatures())
1463+
return aggregate(getDslFunctionSignatures(functions))
14501464
}
1451-
return aggregate(colorizeDslFunctionSignatures())
1465+
return aggregate(colorizeDslFunctionSignatures(functions))
14521466
}
14531467

1454-
func getDslFunctionSignatures() []string {
1468+
// GetPrintableFakerDslFunctionSignatures returns the function signatures for
1469+
// the faker functions.
1470+
//
1471+
// Note: [FakerFunctions] must be called first to populate the functions
1472+
// map with the faker functions.
1473+
func GetPrintableFakerDslFunctionSignatures(noColor bool) string {
1474+
if noColor {
1475+
return aggregate(getDslFunctionSignatures(fakerFunctions))
1476+
}
1477+
return aggregate(colorizeDslFunctionSignatures(fakerFunctions))
1478+
}
1479+
1480+
func getDslFunctionSignatures(funcs []dslFunction) []string {
14551481
var result []string
1456-
for _, function := range functions {
1457-
result = append(result, function.GetSignatures()...)
1482+
for _, f := range funcs {
1483+
result = append(result, f.GetSignatures()...)
14581484
}
14591485
return result
14601486
}
14611487

1462-
func colorizeDslFunctionSignatures() []string {
1463-
signatures := getDslFunctionSignatures()
1488+
func colorizeDslFunctionSignatures(funcs []dslFunction) []string {
1489+
signatures := getDslFunctionSignatures(funcs)
14641490

14651491
colorToOrange := func(value string) string {
14661492
return aurora.Index(208, value).String()

0 commit comments

Comments
 (0)