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

Why Find{{ .Name }} is not generated when there is no columns except primary key? #75

Open
cloverrose opened this issue Jul 29, 2021 · 1 comment

Comments

@cloverrose
Copy link

yo/templates/type.go.tpl

Lines 157 to 196 in 20fce06

// Find{{ .Name }} gets a {{ .Name }} by primary key
func Find{{ .Name }}(ctx context.Context, db YORODB{{ gocustomparamlist .PrimaryKeyFields true true }}) (*{{ .Name }}, error) {
key := spanner.Key{ {{ gocustomparamlist .PrimaryKeyFields false false }} }
row, err := db.ReadRow(ctx, "{{ $table }}", key, {{ .Name }}Columns())
if err != nil {
return nil, newError("Find{{ .Name }}", "{{ $table }}", err)
}
decoder := new{{ .Name }}_Decoder({{ .Name}}Columns())
{{ $short }}, err := decoder(row)
if err != nil {
return nil, newErrorWithCode(codes.Internal, "Find{{ .Name }}", "{{ $table }}", err)
}
return {{ $short }}, nil
}
// Read{{ .Name }} retrieves multiples rows from {{ .Name }} by KeySet as a slice.
func Read{{ .Name }}(ctx context.Context, db YORODB, keys spanner.KeySet) ([]*{{ .Name }}, error) {
var res []*{{ .Name }}
decoder := new{{ .Name }}_Decoder({{ .Name}}Columns())
rows := db.Read(ctx, "{{ $table }}", keys, {{ .Name }}Columns())
err := rows.Do(func(row *spanner.Row) error {
{{ $short }}, err := decoder(row)
if err != nil {
return err
}
res = append(res, {{ $short }})
return nil
})
if err != nil {
return nil, newErrorWithCode(codes.Internal, "Read{{ .Name }}", "{{ $table }}", err)
}
return res, nil
}
{{ end }}

Hi, this is just a question. I want to know the background of it.
I imagine I want to use Find{{ .Name }} method to know the data already exists or not.

@kazegusuri
Copy link
Collaborator

Sorry I overlooked this issue. I confirmed FindXXX method is not genreated if the table is consists of primary keys only.
I think Yo should generate the methods for those tables too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants