Skip to content

Commit

Permalink
rework file structure of value interface
Browse files Browse the repository at this point in the history
  • Loading branch information
NicoGGG committed Dec 2, 2020
1 parent 3e3a332 commit 236aa9b
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 105 deletions.
28 changes: 28 additions & 0 deletions generator/date_value.go
Original file line number Diff line number Diff line change
@@ -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()
}
21 changes: 21 additions & 0 deletions generator/float_value.go
Original file line number Diff line number Diff line change
@@ -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)
}
27 changes: 27 additions & 0 deletions generator/id_value.go
Original file line number Diff line number Diff line change
@@ -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)
}
21 changes: 21 additions & 0 deletions generator/int_value.go
Original file line number Diff line number Diff line change
@@ -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)
}
26 changes: 26 additions & 0 deletions generator/string_value.go
Original file line number Diff line number Diff line change
@@ -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
}
105 changes: 0 additions & 105 deletions generator/value.go
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit 236aa9b

Please sign in to comment.