cloudspannerecosystem / yo

yo is a command-line tool to generate Go code for Google Cloud Spanner.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

cloverrose opened this issue · comments

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.

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.