Skip to content

Commit 23b0a62

Browse files
author
Andrea Spacca
authored
Do not expose GenState (#110)
* make GenState explicitely owned by the generator * adapt tests * make state private
1 parent b8e1442 commit 23b0a62

File tree

7 files changed

+84
-93
lines changed

7 files changed

+84
-93
lines changed

internal/corpus/generator.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,6 @@ func (gc GeneratorCorpus) eventsPayloadFromFields(template []byte, fields Fields
129129
return err
130130
}
131131

132-
state := genlib.NewGenState()
133-
134132
var buf *bytes.Buffer
135133
if len(template) == 0 {
136134
buf = bytes.NewBuffer(createPayload)
@@ -144,7 +142,7 @@ func (gc GeneratorCorpus) eventsPayloadFromFields(template []byte, fields Fields
144142

145143
for {
146144
buf.Truncate(len(createPayload))
147-
err := evgen.Emit(state, buf)
145+
err := evgen.Emit(buf)
148146
if err == nil {
149147
buf.WriteByte('\n')
150148

pkg/genlib/generator_interface.go

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,17 @@ var (
5959
)
6060

6161
// This is the emit function for the custom template engine where we stream content directly to the output buffer and no need a return value
62-
type emitFNotReturn func(state *GenState, buf *bytes.Buffer) error
62+
type emitFNotReturn func(state *genState, buf *bytes.Buffer) error
6363

6464
// EmitF Typedef of the internal emit function
65-
type EmitF func(state *GenState) any
65+
type EmitF func(state *genState) any
6666

6767
type Generator interface {
68-
Emit(state *GenState, buf *bytes.Buffer) error
68+
Emit(buf *bytes.Buffer) error
6969
Close() error
7070
}
7171

72-
type GenState struct {
72+
type genState struct {
7373
// event counter
7474
counter uint64
7575
// total events
@@ -84,8 +84,8 @@ type GenState struct {
8484
pool sync.Pool
8585
}
8686

87-
func NewGenState() *GenState {
88-
return &GenState{
87+
func newGenState() *genState {
88+
return &genState{
8989
prevCache: make(map[string]any),
9090
prevCacheForDup: make(map[string]map[any]struct{}),
9191
prevCacheCardinality: make(map[string][]any, 0),
@@ -370,7 +370,7 @@ func randGeoPointWithReturn() string {
370370

371371
func bindConstantKeyword(field Field, fieldMap map[string]any) error {
372372
var emitFNotReturn emitFNotReturn
373-
emitFNotReturn = func(state *GenState, buf *bytes.Buffer) error {
373+
emitFNotReturn = func(state *genState, buf *bytes.Buffer) error {
374374
value, ok := state.prevCache[field.Name].(string)
375375
if !ok {
376376
// randomdata.Adjective() + randomdata.Noun() -> 364 * 527 (~190k) different values
@@ -388,7 +388,7 @@ func bindConstantKeyword(field Field, fieldMap map[string]any) error {
388388
func bindKeyword(fieldCfg ConfigField, field Field, fieldMap map[string]any) error {
389389
if len(fieldCfg.Enum) > 0 {
390390
var emitFNotReturn emitFNotReturn
391-
emitFNotReturn = func(state *GenState, buf *bytes.Buffer) error {
391+
emitFNotReturn = func(state *genState, buf *bytes.Buffer) error {
392392
idx := customRand.Intn(len(fieldCfg.Enum))
393393
buf.WriteString(fieldCfg.Enum[idx])
394394
return nil
@@ -413,7 +413,7 @@ func bindKeyword(fieldCfg ConfigField, field Field, fieldMap map[string]any) err
413413
return bindJoinRand(field, totWords, joiner, fieldMap)
414414
} else {
415415
var emitFNotReturn emitFNotReturn
416-
emitFNotReturn = func(state *GenState, buf *bytes.Buffer) error {
416+
emitFNotReturn = func(state *genState, buf *bytes.Buffer) error {
417417
// randomdata.Adjective() + randomdata.Noun() -> 364 * 527 (~190k) different values
418418
buf.WriteString(randomdata.Adjective() + randomdata.Noun())
419419
return nil
@@ -426,7 +426,7 @@ func bindKeyword(fieldCfg ConfigField, field Field, fieldMap map[string]any) err
426426

427427
func bindJoinRand(field Field, N int, joiner string, fieldMap map[string]any) error {
428428
var emitFNotReturn emitFNotReturn
429-
emitFNotReturn = func(state *GenState, buf *bytes.Buffer) error {
429+
emitFNotReturn = func(state *genState, buf *bytes.Buffer) error {
430430
for i := 0; i < N-1; i++ {
431431
buf.WriteString(randomdata.Noun())
432432
buf.WriteString(joiner)
@@ -449,7 +449,7 @@ func bindStatic(field Field, v any, fieldMap map[string]any) error {
449449
}
450450

451451
var emitFNotReturn emitFNotReturn
452-
emitFNotReturn = func(state *GenState, buf *bytes.Buffer) error {
452+
emitFNotReturn = func(state *genState, buf *bytes.Buffer) error {
453453
buf.Write(vstr)
454454
return nil
455455
}
@@ -460,7 +460,7 @@ func bindStatic(field Field, v any, fieldMap map[string]any) error {
460460

461461
func bindBool(field Field, fieldMap map[string]any) error {
462462
var emitFNotReturn emitFNotReturn
463-
emitFNotReturn = func(state *GenState, buf *bytes.Buffer) error {
463+
emitFNotReturn = func(state *genState, buf *bytes.Buffer) error {
464464
switch customRand.Int() % 2 {
465465
case 0:
466466
buf.WriteString("false")
@@ -476,7 +476,7 @@ func bindBool(field Field, fieldMap map[string]any) error {
476476

477477
func bindGeoPoint(field Field, fieldMap map[string]any) error {
478478
var emitFNotReturn emitFNotReturn
479-
emitFNotReturn = func(state *GenState, buf *bytes.Buffer) error {
479+
emitFNotReturn = func(state *genState, buf *bytes.Buffer) error {
480480
return randGeoPoint(buf)
481481
}
482482

@@ -486,7 +486,7 @@ func bindGeoPoint(field Field, fieldMap map[string]any) error {
486486

487487
func bindWordN(field Field, n int, fieldMap map[string]any) error {
488488
var emitFNotReturn emitFNotReturn
489-
emitFNotReturn = func(state *GenState, buf *bytes.Buffer) error {
489+
emitFNotReturn = func(state *genState, buf *bytes.Buffer) error {
490490
genNounsN(customRand.Intn(n), buf)
491491
return nil
492492
}
@@ -497,7 +497,7 @@ func bindWordN(field Field, n int, fieldMap map[string]any) error {
497497

498498
func bindNearTime(fieldCfg ConfigField, field Field, fieldMap map[string]any) error {
499499
var emitFNotReturn emitFNotReturn
500-
emitFNotReturn = func(state *GenState, buf *bytes.Buffer) error {
500+
emitFNotReturn = func(state *genState, buf *bytes.Buffer) error {
501501
var offset time.Duration
502502
if fieldCfg.Period > 0 && state.totEvents > 0 {
503503
offset = time.Duration((fieldCfg.Period.Nanoseconds() / int64(state.totEvents)) * int64(state.counter))
@@ -516,7 +516,7 @@ func bindNearTime(fieldCfg ConfigField, field Field, fieldMap map[string]any) er
516516

517517
func bindIP(field Field, fieldMap map[string]any) error {
518518
var emitFNotReturn emitFNotReturn
519-
emitFNotReturn = func(state *GenState, buf *bytes.Buffer) error {
519+
emitFNotReturn = func(state *genState, buf *bytes.Buffer) error {
520520
i0 := customRand.Intn(255)
521521
i1 := customRand.Intn(255)
522522
i2 := customRand.Intn(255)
@@ -544,7 +544,7 @@ func bindLong(fieldCfg ConfigField, field Field, fieldMap map[string]any) error
544544

545545
if fieldCfg.Fuzziness <= 0 {
546546
var emitFNotReturn emitFNotReturn
547-
emitFNotReturn = func(state *GenState, buf *bytes.Buffer) error {
547+
emitFNotReturn = func(state *genState, buf *bytes.Buffer) error {
548548
v := make([]byte, 0, 32)
549549
v = strconv.AppendInt(v, dummyFunc(), 10)
550550
buf.Write(v)
@@ -560,7 +560,7 @@ func bindLong(fieldCfg ConfigField, field Field, fieldMap map[string]any) error
560560
max, _ := fieldCfg.Range.MaxAsFloat64()
561561

562562
var emitFNotReturn emitFNotReturn
563-
emitFNotReturn = func(state *GenState, buf *bytes.Buffer) error {
563+
emitFNotReturn = func(state *genState, buf *bytes.Buffer) error {
564564
var dummyInt int64
565565
if previousDummyInt, ok := state.prevCache[field.Name].(int64); ok {
566566
if previousDummyInt == 0 {
@@ -594,7 +594,7 @@ func bindDouble(fieldCfg ConfigField, field Field, fieldMap map[string]any) erro
594594

595595
if fieldCfg.Fuzziness <= 0 {
596596
var emitFNotReturn emitFNotReturn
597-
emitFNotReturn = func(state *GenState, buf *bytes.Buffer) error {
597+
emitFNotReturn = func(state *genState, buf *bytes.Buffer) error {
598598
dummyFloat := dummyFunc()
599599
_, err := fmt.Fprintf(buf, "%f", dummyFloat)
600600
return err
@@ -608,7 +608,7 @@ func bindDouble(fieldCfg ConfigField, field Field, fieldMap map[string]any) erro
608608
max, _ := fieldCfg.Range.MaxAsFloat64()
609609

610610
var emitFNotReturn emitFNotReturn
611-
emitFNotReturn = func(state *GenState, buf *bytes.Buffer) error {
611+
emitFNotReturn = func(state *genState, buf *bytes.Buffer) error {
612612
var dummyFloat float64
613613
if previousDummyFloat, ok := state.prevCache[field.Name].(float64); ok {
614614
dummyFloat = fuzzyFloat(previousDummyFloat, fieldCfg.Fuzziness, min, max)
@@ -646,7 +646,7 @@ func bindCardinality(cfg Config, field Field, fieldMap map[string]any) error {
646646
}
647647

648648
var emitFNotReturn emitFNotReturn
649-
emitFNotReturn = func(state *GenState, buf *bytes.Buffer) error {
649+
emitFNotReturn = func(state *genState, buf *bytes.Buffer) error {
650650
// Have we rolled over once? If not, generate a value and cache it.
651651
if len(state.prevCacheCardinality[field.Name]) < cardinality {
652652

@@ -689,7 +689,7 @@ func bindCardinality(cfg Config, field Field, fieldMap map[string]any) error {
689689
}
690690

691691
func makeDynamicStub(boundF any) emitFNotReturn {
692-
return func(state *GenState, buf *bytes.Buffer) error {
692+
return func(state *genState, buf *bytes.Buffer) error {
693693
v := state.pool.Get()
694694
tmp := v.(*bytes.Buffer)
695695
tmp.Reset()
@@ -713,14 +713,14 @@ func makeDynamicStub(boundF any) emitFNotReturn {
713713
}
714714

715715
func makeDynamicStubWithReturn(boundF any) EmitF {
716-
return func(state *GenState) any {
716+
return func(state *genState) any {
717717
return boundF.(EmitF)(state)
718718
}
719719
}
720720

721721
func bindConstantKeywordWithReturn(field Field, fieldMap map[string]any) error {
722722
var emitF EmitF
723-
emitF = func(state *GenState) any {
723+
emitF = func(state *genState) any {
724724
value, ok := state.prevCache[field.Name].(string)
725725
if !ok {
726726
// randomdata.Adjective() + randomdata.Noun() -> 364 * 527 (~190k) different values
@@ -737,7 +737,7 @@ func bindConstantKeywordWithReturn(field Field, fieldMap map[string]any) error {
737737
func bindKeywordWithReturn(fieldCfg ConfigField, field Field, fieldMap map[string]any) error {
738738
if len(fieldCfg.Enum) > 0 {
739739
var emitF EmitF
740-
emitF = func(state *GenState) any {
740+
emitF = func(state *genState) any {
741741
idx := customRand.Intn(len(fieldCfg.Enum))
742742
return fieldCfg.Enum[idx]
743743
}
@@ -761,7 +761,7 @@ func bindKeywordWithReturn(fieldCfg ConfigField, field Field, fieldMap map[strin
761761
return bindJoinRandWithReturn(field, totWords, joiner, fieldMap)
762762
} else {
763763
var emitF EmitF
764-
emitF = func(state *GenState) any {
764+
emitF = func(state *genState) any {
765765
// randomdata.Adjective() + randomdata.Noun() -> 364 * 527 (~190k) different values
766766
return randomdata.Adjective() + randomdata.Noun()
767767
}
@@ -773,7 +773,7 @@ func bindKeywordWithReturn(fieldCfg ConfigField, field Field, fieldMap map[strin
773773

774774
func bindJoinRandWithReturn(field Field, N int, joiner string, fieldMap map[string]any) error {
775775
var emitF EmitF
776-
emitF = func(state *GenState) any {
776+
emitF = func(state *genState) any {
777777
value := ""
778778
for i := 0; i < N-1; i++ {
779779
value += randomdata.Noun() + joiner
@@ -792,7 +792,7 @@ func bindJoinRandWithReturn(field Field, N int, joiner string, fieldMap map[stri
792792

793793
func bindStaticWithReturn(field Field, v any, fieldMap map[string]any) error {
794794
var emitF EmitF
795-
emitF = func(state *GenState) any {
795+
emitF = func(state *genState) any {
796796
return v
797797
}
798798

@@ -802,7 +802,7 @@ func bindStaticWithReturn(field Field, v any, fieldMap map[string]any) error {
802802

803803
func bindBoolWithReturn(field Field, fieldMap map[string]any) error {
804804
var emitF EmitF
805-
emitF = func(state *GenState) any {
805+
emitF = func(state *genState) any {
806806
switch customRand.Int() % 2 {
807807
case 0:
808808
return false
@@ -817,7 +817,7 @@ func bindBoolWithReturn(field Field, fieldMap map[string]any) error {
817817

818818
func bindGeoPointWithReturn(field Field, fieldMap map[string]any) error {
819819
var emitF EmitF
820-
emitF = func(state *GenState) any {
820+
emitF = func(state *genState) any {
821821
return randGeoPointWithReturn()
822822
}
823823

@@ -828,7 +828,7 @@ func bindGeoPointWithReturn(field Field, fieldMap map[string]any) error {
828828

829829
func bindWordNWithReturn(field Field, n int, fieldMap map[string]any) error {
830830
var emitF EmitF
831-
emitF = func(state *GenState) any {
831+
emitF = func(state *genState) any {
832832
return genNounsNWithReturn(customRand.Intn(n))
833833
}
834834
fieldMap[field.Name] = emitF
@@ -837,7 +837,7 @@ func bindWordNWithReturn(field Field, n int, fieldMap map[string]any) error {
837837

838838
func bindNearTimeWithReturn(fieldCfg ConfigField, field Field, fieldMap map[string]any) error {
839839
var emitF EmitF
840-
emitF = func(state *GenState) any {
840+
emitF = func(state *genState) any {
841841
var offset time.Duration
842842
if fieldCfg.Period > 0 {
843843
offset = time.Duration((fieldCfg.Period.Nanoseconds() / int64(state.totEvents)) * int64(state.counter))
@@ -855,7 +855,7 @@ func bindNearTimeWithReturn(fieldCfg ConfigField, field Field, fieldMap map[stri
855855

856856
func bindIPWithReturn(field Field, fieldMap map[string]any) error {
857857
var emitF EmitF
858-
emitF = func(state *GenState) any {
858+
emitF = func(state *genState) any {
859859
i0 := customRand.Intn(255)
860860
i1 := customRand.Intn(255)
861861
i2 := customRand.Intn(255)
@@ -873,7 +873,7 @@ func bindLongWithReturn(fieldCfg ConfigField, field Field, fieldMap map[string]a
873873

874874
if fieldCfg.Fuzziness <= 0 {
875875
var emitF EmitF
876-
emitF = func(state *GenState) any {
876+
emitF = func(state *genState) any {
877877
return dummyFunc()
878878
}
879879

@@ -885,7 +885,7 @@ func bindLongWithReturn(fieldCfg ConfigField, field Field, fieldMap map[string]a
885885
max, _ := fieldCfg.Range.MaxAsFloat64()
886886

887887
var emitF EmitF
888-
emitF = func(state *GenState) any {
888+
emitF = func(state *genState) any {
889889
var dummyInt int64
890890
if previousDummyInt, ok := state.prevCache[field.Name].(int64); ok {
891891
if previousDummyInt == 0 {
@@ -908,7 +908,7 @@ func bindDoubleWithReturn(fieldCfg ConfigField, field Field, fieldMap map[string
908908

909909
if fieldCfg.Fuzziness <= 0 {
910910
var emitF EmitF
911-
emitF = func(state *GenState) any {
911+
emitF = func(state *genState) any {
912912
return dummyFunc()
913913
}
914914

@@ -921,7 +921,7 @@ func bindDoubleWithReturn(fieldCfg ConfigField, field Field, fieldMap map[string
921921
max, _ := fieldCfg.Range.MaxAsFloat64()
922922

923923
var emitF EmitF
924-
emitF = func(state *GenState) any {
924+
emitF = func(state *genState) any {
925925
var dummyFloat float64
926926
if previousDummyFloat, ok := state.prevCache[field.Name].(float64); ok {
927927
dummyFloat = fuzzyFloat(previousDummyFloat, fieldCfg.Fuzziness, min, max)
@@ -954,7 +954,7 @@ func bindCardinalityWithReturn(cfg Config, field Field, fieldMap map[string]any)
954954
// We will wrap the function we just generated
955955
boundFWithReturn := fieldMap[field.Name].(EmitF)
956956
var emitF EmitF
957-
emitF = func(state *GenState) any {
957+
emitF = func(state *genState) any {
958958
var value any
959959
// Have we rolled over once? If not, generate a value and cache it.
960960
if len(state.prevCacheCardinality[field.Name]) < cardinality {

0 commit comments

Comments
 (0)