-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
232 additions
and
37 deletions.
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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,55 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"log" | ||
"os" | ||
"path" | ||
"strings" | ||
|
||
"github.com/inklabs/rangedb/pkg/commandgenerator" | ||
"github.com/inklabs/rangedb/pkg/structparser" | ||
) | ||
|
||
func main() { | ||
pkg := flag.String("package", os.Getenv("GOPACKAGE"), "package") | ||
id := flag.String("id", "", "id") | ||
aggregateType := flag.String("aggregateType", "", "aggregate name") | ||
inFilePath := flag.String("inFile", os.Getenv("GOFILE"), "input filename containing commands") | ||
outFilePath := flag.String("outFile", "", "output file name") | ||
flag.Parse() | ||
|
||
if *outFilePath == "" { | ||
if *outFilePath == "" { | ||
fileName := strings.TrimSuffix(*inFilePath, path.Ext(*inFilePath)) | ||
*outFilePath = fmt.Sprintf("%s_gen.go", fileName) | ||
} | ||
} | ||
|
||
commandsFile, err := os.Open(*inFilePath) | ||
if err != nil { | ||
log.Fatalf("unable to open (%s): %v", *inFilePath, err) | ||
} | ||
defer func() { | ||
_ = commandsFile.Close() | ||
}() | ||
|
||
commands, err := structparser.GetStructNames(commandsFile) | ||
if err != nil { | ||
log.Fatalf("unable to extract commands: %v", err) | ||
} | ||
|
||
outFile, err := os.Create(*outFilePath) | ||
if err != nil { | ||
log.Fatalf("unable to create aggreate file: %v", err) | ||
} | ||
defer func() { | ||
_ = outFile.Close() | ||
}() | ||
|
||
err = commandgenerator.Write(outFile, commands, *pkg, *id, *aggregateType) | ||
if err != nil { | ||
log.Fatalf("unable to write to aggregates file: %v", err) | ||
} | ||
} |
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 |
---|---|---|
|
@@ -156,5 +156,4 @@ func (a *thing) raise(events ...rangedb.Event) { | |
` | ||
assert.Equal(t, expectedOut, out.String()) | ||
}) | ||
|
||
} |
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,45 @@ | ||
package commandgenerator | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"text/template" | ||
"time" | ||
) | ||
|
||
var NowFunc = time.Now | ||
|
||
func Write(out io.Writer, commands []string, pkg, id, aggregateType string) error { | ||
if len(commands) < 1 { | ||
return fmt.Errorf("no commands found") | ||
} | ||
|
||
return fileTemplate.Execute(out, templateData{ | ||
Timestamp: NowFunc(), | ||
Commands: commands, | ||
AggregateType: aggregateType, | ||
ID: id, | ||
Package: pkg, | ||
}) | ||
} | ||
|
||
type templateData struct { | ||
Timestamp time.Time | ||
Commands []string | ||
AggregateType string | ||
ID string | ||
Package string | ||
} | ||
|
||
var fileTemplate = template.Must(template.New("").Parse(`// Code generated by go generate; DO NOT EDIT. | ||
// This file was generated at | ||
// {{ .Timestamp }} | ||
package {{ $.Package }} | ||
{{- range .Commands }} | ||
func (c {{ . }}) AggregateID() string { return c.{{ $.ID }} } | ||
func (c {{ . }}) AggregateType() string { return "{{ $.AggregateType }}" } | ||
func (c {{ . }}) CommandType() string { return "{{ . }}" } | ||
{{- end }} | ||
`)) |
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,88 @@ | ||
package commandgenerator_test | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/inklabs/rangedb/pkg/commandgenerator" | ||
) | ||
|
||
func TestWrite(t *testing.T) { | ||
const ( | ||
pkg = "mypkg" | ||
id = "ID" | ||
aggregateType = "thing" | ||
) | ||
commandgenerator.NowFunc = func() time.Time { | ||
return time.Unix(1611080667, 25600800).UTC() | ||
} | ||
|
||
t.Run("no commands found", func(t *testing.T) { | ||
// Given | ||
var commands []string | ||
var out bytes.Buffer | ||
|
||
// When | ||
err := commandgenerator.Write(&out, commands, pkg, id, aggregateType) | ||
|
||
// Then | ||
require.EqualError(t, err, "no commands found") | ||
}) | ||
|
||
t.Run("single command", func(t *testing.T) { | ||
// Given | ||
commands := []string{ | ||
"DoThing", | ||
} | ||
var out bytes.Buffer | ||
|
||
// When | ||
err := commandgenerator.Write(&out, commands, pkg, id, aggregateType) | ||
|
||
// Then | ||
require.NoError(t, err) | ||
expectedOut := `// Code generated by go generate; DO NOT EDIT. | ||
// This file was generated at | ||
// 2021-01-19 18:24:27.0256008 +0000 UTC | ||
package mypkg | ||
func (c DoThing) AggregateID() string { return c.ID } | ||
func (c DoThing) AggregateType() string { return "thing" } | ||
func (c DoThing) CommandType() string { return "DoThing" } | ||
` | ||
assert.Equal(t, expectedOut, out.String()) | ||
}) | ||
|
||
t.Run("two commands", func(t *testing.T) { | ||
// Given | ||
commands := []string{ | ||
"DoThing", | ||
"DoAnother", | ||
} | ||
var out bytes.Buffer | ||
|
||
// When | ||
err := commandgenerator.Write(&out, commands, pkg, id, aggregateType) | ||
|
||
// Then | ||
require.NoError(t, err) | ||
expectedOut := `// Code generated by go generate; DO NOT EDIT. | ||
// This file was generated at | ||
// 2021-01-19 18:24:27.0256008 +0000 UTC | ||
package mypkg | ||
func (c DoThing) AggregateID() string { return c.ID } | ||
func (c DoThing) AggregateType() string { return "thing" } | ||
func (c DoThing) CommandType() string { return "DoThing" } | ||
func (c DoAnother) AggregateID() string { return c.ID } | ||
func (c DoAnother) AggregateType() string { return "thing" } | ||
func (c DoAnother) CommandType() string { return "DoAnother" } | ||
` | ||
assert.Equal(t, expectedOut, out.String()) | ||
}) | ||
} |