Skip to content

Commit

Permalink
Make schemagen compilable and add test for it
Browse files Browse the repository at this point in the history
  • Loading branch information
dkropachev committed Jun 26, 2024
1 parent 7072863 commit 2f653fe
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 3 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ GOTEST := go test -cpu $(GOTEST_CPU) -count=1 -cover -race -tags all

.PHONY: test
test:
@go build cmd/schemagen/testdata/models.go # Make sure the generated code compiles
@$(GOTEST) .
@$(GOTEST) ./qb
@$(GOTEST) ./table
Expand Down
1 change: 1 addition & 0 deletions cmd/schemagen/keyspace.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var (
{{- $type_name := .Name | camelize}}
{{- $field_types := .FieldTypes}}
type {{$type_name}}UserType struct {
gocqlx.UDT
{{- range $index, $element := .FieldNames}}
{{. | camelize}} {{(index $field_types $index) | mapScyllaToGoType}}
{{- end}}
Expand Down
8 changes: 6 additions & 2 deletions cmd/schemagen/schemagen.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ func renderTemplate(md *gocql.KeyspaceMetadata) ([]byte, error) {
}

imports := make([]string, 0)
if len(md.Types) != 0 {
imports = append(imports, "github.com/scylladb/gocqlx/v2")
}

for _, t := range md.Tables {
// Ensure ordered columns are sorted alphabetically
sort.Strings(t.OrderedColumns)
Expand All @@ -120,8 +124,8 @@ func renderTemplate(md *gocql.KeyspaceMetadata) ([]byte, error) {
if c.Type == "decimal" && !existsInSlice(imports, "gopkg.in/inf.v0") {
imports = append(imports, "gopkg.in/inf.v0")
}
if c.Type == "duration" && !existsInSlice(imports, "github.com/gocql/gocql") {
imports = append(imports, "github.com/gocql/gocql")
if c.Type == "duration" && !existsInSlice(imports, "github.com/gocql/gocql/v2") {
imports = append(imports, "github.com/gocql/gocql/v2")
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/schemagen/schemagen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestSchemagen(t *testing.T) {

b := runSchemagen(t, "foobar")

const goldenFile = "testdata/models.go.txt"
const goldenFile = "testdata/models.go"
if *flagUpdate {
if err := ioutil.WriteFile(goldenFile, b, os.ModePerm); err != nil {
t.Fatal(err)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2f653fe

Please sign in to comment.