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

Custom Get most recent failing #327

Open
sherodtaylor opened this issue Sep 24, 2021 · 2 comments
Open

Custom Get most recent failing #327

sherodtaylor opened this issue Sep 24, 2021 · 2 comments

Comments

@sherodtaylor
Copy link

I'm trying to follow the readme to create custom functions but it's failing with the readme example.

error: unable to parse template schema/typedef.xo.go.tpl: template: schema/typedef.xo.go.tpl:214: undefined variable "$type"
make: *** [generate-db] Error 1

templates/schema/typedef.xo.go.tpl

...
{{- end -}}
{{- end }}
// GetMostRecent{{ $type.Name }} returns n most recent rows from '{{ $table }}',
// ordered by "created_at" in descending order.
func GetMostRecent{{ $type.Name }}(ctx context.Context, db DB, n int) ([]*{{ $type.Name }}, error) {
    const sqlstr = `SELECT ` +
        `{{ $type.Fields "created_at" "modified_at" }}` +
        `FROM {{ $table }} ` +
        `ORDER BY created_at DESC LIMIT $1`

    rows, err := db.QueryContext(ctx, sqlstr, n)
    if err != nil {
        return nil, logerror(err)
    }
    defer rows.Close()

    // load results
    var res []*{{ $type.Name }}
    for rows.Next() {
        {{ $short }} := {{ $type.Name }}{
        {{- if $type.PrimaryKey }}
            _exists: true,
        {{ end -}}
        }
        // scan
        if err := rows.Scan({{ fieldnames $type.Fields (print "&" $short) }}); err != nil {
            return nil, logerror(err)
        }
        res = append(res, &{{ $short }})
    }
    return res, nil
}
@jnflint
Copy link

jnflint commented Oct 13, 2021

Having the same problem.
$table and fieldnames may also need referencing somehow.
It looks like a good tool, but needs a working example of retrieving multiple rows to be usable for my case.

@HaswinVidanage
Copy link

any update on this? the example on readme still fails with this error

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

3 participants