-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moving GeneratedContent to the intepreter and renaming to GenerationO…
…utput * generator.Generate now returns generator.GeneratedEntities
- Loading branch information
Showing
9 changed files
with
138 additions
and
143 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |