Skip to content

Commit

Permalink
Moving GeneratedContent to the intepreter and renaming to GenerationO…
Browse files Browse the repository at this point in the history
…utput

* generator.Generate now returns generator.GeneratedEntities
  • Loading branch information
ibnc committed Jul 27, 2017
1 parent 23afa9e commit cdae510
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 143 deletions.
73 changes: 0 additions & 73 deletions generator/generated_content.go

This file was deleted.

29 changes: 0 additions & 29 deletions generator/generated_content_test.go

This file was deleted.

16 changes: 16 additions & 0 deletions generator/generated_entities.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package generator

type GeneratedEntities []GeneratedFields

type GeneratedFields map[string]interface{}

func NewGeneratedEntities(count int64) GeneratedEntities {
return make([]GeneratedFields, count)
}

func (ge GeneratedEntities) Concat(newEntities GeneratedEntities) GeneratedEntities {
for _, entity := range newEntities {
ge = append(ge, entity)
}
return ge
}
7 changes: 2 additions & 5 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,7 @@ func (g *Generator) WithField(fieldName, fieldType string, fieldOpts interface{}
return nil
}

func (g *Generator) Generate(count int64) GeneratedContent {
result := NewGeneratedContent()
func (g *Generator) Generate(count int64) GeneratedEntities {
entities := NewGeneratedEntities(count)
for i := int64(0); i < count; i++ {
entity := GeneratedFields{}
Expand All @@ -131,7 +130,5 @@ func (g *Generator) Generate(count int64) GeneratedContent {
}
entities[i] = entity
}
result[g.Name] = entities

return result
return entities
}
12 changes: 6 additions & 6 deletions generator/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,19 @@ func TestExtendGenerator(t *testing.T) {
m.WithStaticField("species", "h00man")
m.WithStaticField("name", "kyle")

data := NewGeneratedContent()
data := GeneratedEntities{}

data = g.Generate(1)

base := data["thing"][0]
base := data[0]

AssertEqual(t, "human", base["species"])
AssertEqual(t, 10, len(base["name"].(string)))
Assert(t, isBetween(base["age"].(float64), 2, 4), "base entity failed to generate the correct age")

data = m.Generate(1)

extended := data["thang"][0]
extended := data[0]
AssertEqual(t, "h00man", extended["species"])
AssertEqual(t, "kyle", extended["name"].(string))
Assert(t, isBetween(extended["age"].(float64), 2, 4), "extended entity failed to generate the correct age")
Expand Down Expand Up @@ -178,7 +178,7 @@ func TestFieldOptsMatchesFieldType(t *testing.T) {
}

func TestGenerateProducesGeneratedContent(t *testing.T) {
data := NewGeneratedContent()
data := GeneratedEntities{}
logger := GetLogger(t)
g := NewGenerator("thing", logger)
timeMin, _ := time.Parse("2006-01-02", "1945-01-01")
Expand All @@ -203,7 +203,7 @@ func TestGenerateProducesGeneratedContent(t *testing.T) {

data = g.Generate(3)

AssertEqual(t, 3, len(data["thing"]))
AssertEqual(t, 3, len(data))

var testFields = []struct {
fieldName string
Expand All @@ -228,7 +228,7 @@ func TestGenerateProducesGeneratedContent(t *testing.T) {
{"q", uuid.NewV4()},
}

entity := data["thing"][0]
entity := data[0]
for _, field := range testFields {
actual := reflect.TypeOf(entity[field.fieldName])
expected := reflect.TypeOf(field.fieldType)
Expand Down
23 changes: 0 additions & 23 deletions generator/util.go

This file was deleted.

14 changes: 7 additions & 7 deletions interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,22 @@ func debug(format string, tokens ...interface{}) {
}

type Interpreter struct {
entities map[string]*generator.Generator // TODO: should probably be a more generic symbol table or possibly the parent scope
generatedContent generator.GeneratedContent
entities map[string]*generator.Generator // TODO: should probably be a more generic symbol table or possibly the parent scope
output GenerationOutput
}

func New() *Interpreter {
return &Interpreter{
entities: make(map[string]*generator.Generator),
generatedContent: generator.NewGeneratedContent(),
entities: make(map[string]*generator.Generator),
output: GenerationOutput{},
}
}

func (i *Interpreter) WriteGeneratedContent(dest string, filePerEntity bool) error {
if filePerEntity {
return i.generatedContent.WriteFilePerKey()
return i.output.writeFilePerKey()
} else {
return i.generatedContent.WriteContentToFile(dest)
return i.output.writeToFile(dest)
}
}

Expand Down Expand Up @@ -291,6 +291,6 @@ func (i *Interpreter) GenerateFromNode(generationNode dsl.Node) error {
return generationNode.Err("Must generate at least 1 `%s` entity", entityGenerator.Name)
}

i.generatedContent.Append(entityGenerator.Generate(count))
i.output.addAndAppend(entityGenerator.Name, entityGenerator.Generate(count))
return nil
}
77 changes: 77 additions & 0 deletions interpreter/output.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package interpreter

import (
"bufio"
"encoding/json"
"fmt"
g "github.com/ThoughtWorksStudios/datagen/generator"
"io"
"os"
)

type GenerationOutput map[string]g.GeneratedEntities

func (output GenerationOutput) addAndAppend(entityName string, entities g.GeneratedEntities) {
if _, ok := output[entityName]; ok {
output[entityName] = output[entityName].Concat(entities)
} else {
output[entityName] = entities
}
}

func (output GenerationOutput) writeFilePerKey() error {
for k, v := range output {
out, err := createWriterFor(fmt.Sprintf("%s.json", k))
if err != nil {
return err
}
d := GenerationOutput{}
d[k] = v
if err = d.write(out); err != nil {
return err
}
}
return nil
}

func (output GenerationOutput) writeToFile(dest string) error {
out, err := createWriterFor(dest)
if err != nil {
return err
}

return output.write(out)
}

func (output GenerationOutput) write(out io.Writer) error {
if closeable, doClose := isClosable(out); doClose {
defer closeable.Close()
}

writer := bufio.NewWriter(out)
encoder := json.NewEncoder(writer)
encoder.SetIndent("", "\t")

if err := encoder.Encode(output); err != nil {
return err
}

return writer.Flush()
}

func isClosable(v interface{}) (io.Closer, bool) {
closeable, doClose := v.(io.Closer)
return closeable, doClose
}

func createWriterFor(filename string) (io.Writer, error) {
var f interface{}
var err error
f, err = os.Create(filename)
if err != nil {
return nil, err
}
out, _ := f.(io.Writer)

return out, nil
}
30 changes: 30 additions & 0 deletions interpreter/output_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package interpreter

import (
g "github.com/ThoughtWorksStudios/datagen/generator"
. "github.com/ThoughtWorksStudios/datagen/test_helpers"
"reflect"
"testing"
)

func TestAppendingToGenerationOutput(t *testing.T) {
actual := GenerationOutput{}

beast := g.GeneratedEntities{g.GeneratedFields{"of the beast": 666}}

expected := GenerationOutput{"sign": beast}
actual.addAndAppend("sign", beast)

Assert(t, reflect.DeepEqual(expected, actual), "expected \n%v\n to be equal to \n%v\n but wasn't", expected, actual)

rick := g.GeneratedFields{
"of Rick": "wubba lubba dub dub!!!!",
}

expected = GenerationOutput{
"sign": append(beast, rick),
}

actual.addAndAppend("sign", g.GeneratedEntities{rick})
Assert(t, reflect.DeepEqual(expected, actual), "expected \n%v\n to be equal to \n%v\n but wasn't", expected, actual)
}

0 comments on commit cdae510

Please sign in to comment.