Skip to content

Commit

Permalink
feat: generate comments for repo methods (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcbinz authored May 2, 2024
1 parent d5ebeca commit b374fa3
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 27 deletions.
91 changes: 64 additions & 27 deletions core/codegen/base.builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,11 @@ func (b *build) buildBaseFile(node *field.NodeTable) error {
jen.Id("Relate").Call().Op("*").Qual(b.subPkg(def.PkgRelate), node.NameGo()),
)

f.Line()
f.Func().
Params(jen.Id("c").Op("*").Id("ClientImpl")).
f.Line().
Add(comment(`
` + node.NameGo() + `Repo returns a new repository instance for the ` + node.NameGo() + ` model.
`)).
Func().Params(jen.Id("c").Op("*").Id("ClientImpl")).
Id(node.NameGo() + "Repo").Params().Id(node.NameGo() + "Repo").
Block(
jen.Return(
Expand Down Expand Up @@ -358,9 +360,11 @@ func (b *build) buildBaseFile(node *field.NodeTable) error {
),
)

f.Line()
f.Func().
Params(jen.Id("r").Op("*").Id(node.NameGoLower())).
f.Line().
Add(comment(`
Query returns a new query builder for the `+node.NameGo()+` model.
`)).
Func().Params(jen.Id("r").Op("*").Id(node.NameGoLower())).
Id("Query").Params().
Qual(pkgQuery, "Builder").
Types(
Expand All @@ -374,9 +378,12 @@ func (b *build) buildBaseFile(node *field.NodeTable) error {
)),
)

f.Line()
f.Func().
Params(jen.Id("r").Op("*").Id(node.NameGoLower())).
f.Line().
Add(comment(`
Create creates a new record for the `+node.NameGo()+` model.
The ID will be generated automatically as a ULID.
`)).
Func().Params(jen.Id("r").Op("*").Id(node.NameGoLower())).
Id("Create").
Params(
jen.Id("ctx").Qual("context", "Context"),
Expand All @@ -402,9 +409,11 @@ func (b *build) buildBaseFile(node *field.NodeTable) error {
),
)

f.Line()
f.Func().
Params(jen.Id("r").Op("*").Id(node.NameGoLower())).
f.Line().
Add(comment(`
CreateWithID creates a new record for the `+node.NameGo()+` model with the given id.
`)).
Func().Params(jen.Id("r").Op("*").Id(node.NameGoLower())).
Id("CreateWithID").
Params(
jen.Id("ctx").Qual("context", "Context"),
Expand Down Expand Up @@ -432,9 +441,12 @@ func (b *build) buildBaseFile(node *field.NodeTable) error {
),
)

f.Line()
f.Func().
Params(jen.Id("r").Op("*").Id(node.NameGoLower())).
f.Line().
Add(comment(`
Read returns the record for the given id, if it exists.
The returned bool indicates whether the record was found or not.
`)).
Func().Params(jen.Id("r").Op("*").Id(node.NameGoLower())).
Id("Read").
Params(
jen.Id("ctx").Qual("context", "Context"),
Expand All @@ -450,9 +462,11 @@ func (b *build) buildBaseFile(node *field.NodeTable) error {
),
)

f.Line()
f.Func().
Params(jen.Id("r").Op("*").Id(node.NameGoLower())).
f.Line().
Add(comment(`
Update updates the record for the given model.
`)).
Func().Params(jen.Id("r").Op("*").Id(node.NameGoLower())).
Id("Update").
Params(
jen.Id("ctx").Qual("context", "Context"),
Expand All @@ -479,9 +493,11 @@ func (b *build) buildBaseFile(node *field.NodeTable) error {
),
)

f.Line()
f.Func().
Params(jen.Id("r").Op("*").Id(node.NameGoLower())).
f.Line().
Add(comment(`
Delete deletes the record for the given model.
`)).
Func().Params(jen.Id("r").Op("*").Id(node.NameGoLower())).
Id("Delete").
Params(
jen.Id("ctx").Qual("context", "Context"),
Expand All @@ -503,9 +519,11 @@ func (b *build) buildBaseFile(node *field.NodeTable) error {
),
)

f.Line()
f.Func().
Params(jen.Id("r").Op("*").Id(node.NameGoLower())).
f.Line().
Add(comment(`
Refresh refreshes the given model with the remote data.
`)).
Func().Params(jen.Id("r").Op("*").Id(node.NameGoLower())).
Id("Refresh").
Params(
jen.Id("ctx").Qual("context", "Context"),
Expand All @@ -532,9 +550,11 @@ func (b *build) buildBaseFile(node *field.NodeTable) error {
),
)

f.Line()
f.Func().
Params(jen.Id("r").Op("*").Id(node.NameGoLower())).
f.Line().
Add(comment(`
Relate returns a new relate instance for the `+node.NameGo()+` model.
`)).
Func().Params(jen.Id("r").Op("*").Id(node.NameGoLower())).
Id("Relate").Params().
Op("*").Qual(b.subPkg(def.PkgRelate), node.NameGo()).
Block(
Expand Down Expand Up @@ -593,3 +613,20 @@ func (b *build) basePkgName() string {
func (b *build) subPkg(pkg string) string {
return path.Join(b.basePkg(), pkg)
}

//
// -- HELPER
//

func comment(text string) jen.Code {
var code jen.Statement

text = strings.TrimSpace(text)
lines := strings.Split(text, "\n")

for _, line := range lines {
code.Comment(line).Line()
}

return &code
}
11 changes: 11 additions & 0 deletions tests/basic/gen/som/node.all_field_types.go

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

11 changes: 11 additions & 0 deletions tests/basic/gen/som/node.fields_like_db_response.go

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

11 changes: 11 additions & 0 deletions tests/basic/gen/som/node.group.go

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

Loading

0 comments on commit b374fa3

Please sign in to comment.