Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve ability to add model specific methods #31

Merged
merged 6 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ gofmt:
gofmt -l -s -w .

gogenertate:
go generate ./... && goimports -w .
go generate ./...

build-web-assets:
esbuild web/projector.js web/projector.css web/slide/*.css web/slide/*.js --outdir=static/ --external:*.woff --bundle --minify --sourcemap --target=chrome58,firefox57,safari11,edge16
Expand Down
11 changes: 11 additions & 0 deletions cmd/modelgen/imports.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package models

import (
"encoding/json"
"fmt"
"slices"
"strconv"
"strings"

"github.com/rs/zerolog/log"
)
20 changes: 15 additions & 5 deletions cmd/modelgen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,19 @@ import (

//go:generate sh -c "go run main.go"

//go:embed imports.go.tmpl
var importsTmpl []byte

//go:embed model_struct.go.tmpl
var modelStructTmpl string

func main() {
if err := run(os.Stdout); err != nil {
if err := run(); err != nil {
log.Fatalf("Error: %v", err)
}
}

func run(w io.Writer) error {
func run() error {
if err := genStrutcts(); err != nil {
return fmt.Errorf("generate field methods: %w", err)
}
Expand All @@ -58,6 +61,11 @@ func genStrutcts() error {
return fmt.Errorf("parsing template: %w", err)
}

allModels := importsTmpl
sort.Slice(collections, func(i, j int) bool {
return collections[i].CollectionName < collections[j].CollectionName
})

for _, coll := range collections {
buf := new(bytes.Buffer)

Expand All @@ -70,9 +78,11 @@ func genStrutcts() error {
return fmt.Errorf("formating code: %w", err)
}

if err := os.WriteFile("../../pkg/models/"+coll.CollectionName+".go", formated, 0o644); err != nil {
return fmt.Errorf("writing output: %w", err)
}
allModels = append(allModels, formated...)
}

if err := os.WriteFile("../../pkg/models/gen_models.go", allModels, 0o644); err != nil {
return fmt.Errorf("writing output: %w", err)
}

return nil
Expand Down
11 changes: 0 additions & 11 deletions cmd/modelgen/model_struct.go.tmpl
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
package models

import (
"encoding/json"
"fmt"
"slices"
"strconv"
"strings"

"github.com/rs/zerolog/log"
)

type {{.GoName}} struct { {{range .Fields}}
{{.GoName}} {{.ValueType}} `json:"{{.FieldName}}"`{{end}}
Expand Down
1 change: 0 additions & 1 deletion pkg/datastore/subscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ func (q *query[T, PT]) SubscribeOne(model PT) (*subscription[<-chan []string], e
}

for field, subQuery := range q.subquerys {
println(field)
update, err := q.recursiveUpdateSubqueries(model.GetRelatedModelsAccessor(), field, subQuery, update)
updatedAny = updatedAny || update
if err != nil {
Expand Down
117 changes: 0 additions & 117 deletions pkg/models/action_worker.go

This file was deleted.

Loading
Loading