From 236aa9b25f95118e8a519599beb4d9a1e857d89e Mon Sep 17 00:00:00 2001 From: NicoGGG Date: Wed, 2 Dec 2020 16:30:10 +0100 Subject: [PATCH] rework file structure of value interface --- generator/date_value.go | 28 ++++++++++ generator/float_value.go | 21 ++++++++ generator/id_value.go | 27 ++++++++++ generator/int_value.go | 21 ++++++++ generator/string_value.go | 26 ++++++++++ generator/value.go | 105 -------------------------------------- 6 files changed, 123 insertions(+), 105 deletions(-) create mode 100644 generator/date_value.go create mode 100644 generator/float_value.go create mode 100644 generator/id_value.go create mode 100644 generator/int_value.go create mode 100644 generator/string_value.go diff --git a/generator/date_value.go b/generator/date_value.go new file mode 100644 index 0000000..459b26c --- /dev/null +++ b/generator/date_value.go @@ -0,0 +1,28 @@ +package generator + +import ( + "log" + "time" +) + +type dateValue struct { + currentValue time.Time + step time.Duration +} + +func (v *dateValue) generateValue() { + v.currentValue = v.currentValue.Add(v.step) +} + +func (v *dateValue) init(i string) { + var err error + v.currentValue, err = time.Parse("2006-01-02 15:04:05", i) + if err != nil { + log.Fatalln(err) + } + v.step = 1000000000 // 1B nanoseconds = 1s +} + +func (v dateValue) getCurrentValue() string { + return v.currentValue.String() +} diff --git a/generator/float_value.go b/generator/float_value.go new file mode 100644 index 0000000..0003ba2 --- /dev/null +++ b/generator/float_value.go @@ -0,0 +1,21 @@ +package generator + +import "strconv" + +type floatValue struct { + currentValue float64 + step float64 +} + +func (v *floatValue) generateValue() { + v.currentValue += v.step +} + +func (v *floatValue) init(i string) { + v.currentValue, _ = strconv.ParseFloat(i, 64) + v.step = 0.1 +} + +func (v floatValue) getCurrentValue() string { + return strconv.FormatFloat(v.currentValue, 'f', 2, 64) +} diff --git a/generator/id_value.go b/generator/id_value.go new file mode 100644 index 0000000..8e74b73 --- /dev/null +++ b/generator/id_value.go @@ -0,0 +1,27 @@ +package generator + +import ( + "strconv" + + "github.com/brianvoe/gofakeit/v4" +) + +type idIntValue struct { + currentValue uint64 +} + +/* + * All new types need all the methods in interface value in value.go + */ + +func (v *idIntValue) generateValue() { + v.currentValue = gofakeit.Uint64() +} + +func (v *idIntValue) init(i string) { + v.currentValue = gofakeit.Uint64() +} + +func (v idIntValue) getCurrentValue() string { + return strconv.FormatUint(v.currentValue, 10) +} diff --git a/generator/int_value.go b/generator/int_value.go new file mode 100644 index 0000000..fe842be --- /dev/null +++ b/generator/int_value.go @@ -0,0 +1,21 @@ +package generator + +import "strconv" + +type intValue struct { + currentValue int64 + step int64 +} + +func (v *intValue) generateValue() { + v.currentValue += v.step +} + +func (v *intValue) init(i string) { + v.currentValue, _ = strconv.ParseInt(i, 10, 64) + v.step = 1 +} + +func (v intValue) getCurrentValue() string { + return strconv.FormatInt(v.currentValue, 10) +} diff --git a/generator/string_value.go b/generator/string_value.go new file mode 100644 index 0000000..9baee10 --- /dev/null +++ b/generator/string_value.go @@ -0,0 +1,26 @@ +package generator + +import "strconv" + +type stringValue struct { + prefix string + currentValue string + step int64 + currentStep int64 +} + +func (v *stringValue) generateValue() { + v.currentStep = v.currentStep + v.step + v.currentValue = v.prefix + "_" + strconv.FormatInt(v.currentStep, 10) +} + +func (v *stringValue) init(i string) { + v.prefix = i + v.currentStep = 1 + v.currentValue = v.prefix + "_" + strconv.FormatInt(v.currentStep, 10) + v.step = 1 +} + +func (v stringValue) getCurrentValue() string { + return v.currentValue +} diff --git a/generator/value.go b/generator/value.go index ed2cdc5..65ed630 100644 --- a/generator/value.go +++ b/generator/value.go @@ -1,112 +1,7 @@ package generator -import ( - "log" - "strconv" - "time" - - "github.com/brianvoe/gofakeit/v4" -) - -type stringValue struct { - prefix string - currentValue string - step int64 - currentStep int64 -} - -type intValue struct { - currentValue int64 - step int64 -} - -type floatValue struct { - currentValue float64 - step float64 -} - -type dateValue struct { - currentValue time.Time - step time.Duration -} - -type idIntValue struct { - currentValue uint64 -} - type value interface { getCurrentValue() string generateValue() init(i string) } - -func (v *idIntValue) generateValue() { - v.currentValue = gofakeit.Uint64() -} - -func (v *intValue) generateValue() { - v.currentValue += v.step -} - -func (v *floatValue) generateValue() { - v.currentValue += v.step -} - -func (v *stringValue) generateValue() { - v.currentStep = v.currentStep + v.step - v.currentValue = v.prefix + "_" + strconv.FormatInt(v.currentStep, 10) -} - -func (v *dateValue) generateValue() { - v.currentValue = v.currentValue.Add(v.step) -} - -func (v *idIntValue) init(i string) { - v.currentValue = gofakeit.Uint64() -} - -func (v *intValue) init(i string) { - v.currentValue, _ = strconv.ParseInt(i, 10, 64) - v.step = 1 -} - -func (v *floatValue) init(i string) { - v.currentValue, _ = strconv.ParseFloat(i, 64) - v.step = 0.1 -} - -func (v *stringValue) init(i string) { - v.prefix = i - v.currentStep = 1 - v.currentValue = v.prefix + "_" + strconv.FormatInt(v.currentStep, 10) - v.step = 1 -} - -func (v *dateValue) init(i string) { - var err error - v.currentValue, err = time.Parse("2006-01-02 15:04:05", i) - if err != nil { - log.Fatalln(err) - } - v.step = 1000000 // 1M nanoseconds = 1ms -} - -func (v idIntValue) getCurrentValue() string { - return strconv.FormatUint(v.currentValue, 10) -} - -func (v intValue) getCurrentValue() string { - return strconv.FormatInt(v.currentValue, 10) -} - -func (v floatValue) getCurrentValue() string { - return strconv.FormatFloat(v.currentValue, 'f', 2, 64) -} - -func (v stringValue) getCurrentValue() string { - return v.currentValue -} - -func (v dateValue) getCurrentValue() string { - return v.currentValue.String() -}