Skip to content

Commit a33344d

Browse files
authored
extend enum support (#144)
1 parent 1676ff9 commit a33344d

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

pkg/genlib/generator_interface.go

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@ import (
99
"encoding/json"
1010
"errors"
1111
"fmt"
12-
"github.com/Pallinder/go-randomdata"
13-
"github.com/elastic/elastic-integration-corpus-generator-tool/pkg/genlib/config"
14-
"github.com/elastic/elastic-integration-corpus-generator-tool/pkg/genlib/fields"
1512
"math"
1613
"regexp"
1714
"strconv"
1815
"strings"
1916
"sync"
2017
"testing"
2118
"time"
19+
20+
"github.com/Pallinder/go-randomdata"
21+
"github.com/elastic/elastic-integration-corpus-generator-tool/pkg/genlib/config"
22+
"github.com/elastic/elastic-integration-corpus-generator-tool/pkg/genlib/fields"
2223
)
2324

2425
var timeNowToBind time.Time
@@ -970,6 +971,23 @@ func bindLongWithReturn(fieldCfg ConfigField, field Field, fieldMap map[string]a
970971
return err
971972
}
972973

974+
if len(fieldCfg.Enum) > 0 {
975+
var emitF emitF
976+
idx := customRand.Intn(len(fieldCfg.Enum))
977+
f, err := strconv.ParseInt(fieldCfg.Enum[idx], 10, 64)
978+
if err != nil {
979+
return fmt.Errorf("field %s enum value is not a long: %w", fieldCfg.Name, err)
980+
}
981+
982+
emitF = func(state *genState) any {
983+
return f
984+
}
985+
986+
fieldMap[field.Name] = emitF
987+
988+
return nil
989+
}
990+
973991
if fieldCfg.Counter {
974992
var emitF emitF
975993

@@ -1072,6 +1090,23 @@ func bindDoubleWithReturn(fieldCfg ConfigField, field Field, fieldMap map[string
10721090
return err
10731091
}
10741092

1093+
if len(fieldCfg.Enum) > 0 {
1094+
var emitF emitF
1095+
idx := customRand.Intn(len(fieldCfg.Enum))
1096+
f, err := strconv.ParseFloat(fieldCfg.Enum[idx], 64)
1097+
if err != nil {
1098+
return fmt.Errorf("field %s enum value is not a double: %w", fieldCfg.Name, err)
1099+
}
1100+
1101+
emitF = func(state *genState) any {
1102+
return f
1103+
}
1104+
1105+
fieldMap[field.Name] = emitF
1106+
1107+
return nil
1108+
}
1109+
10751110
if fieldCfg.Counter {
10761111
var emitF emitF
10771112

0 commit comments

Comments
 (0)