vburenin / ifacemaker

Generate interfaces from structure methods.
Apache License 2.0
320 stars 43 forks source link

Fix result generation for map, slice, pointer when targeting other package #66

Open mikeschinkel opened 8 months ago

mikeschinkel commented 8 months ago

Using the following command line where persister contains files generated by sqlc, the latest version in master generates invalid code for results that are references, e.g. map, slice, and pointers.

This PR aims to fix that — which appear to be that the original fix for #12 was incomplete, and this PR assumes PR #65 has been committed.

ifacemaker -f ./persister/*.go -s Queries -i DataStoreQueries -p app -o ./app/query.iface.go

Here is the test code I wrote against:

package persister
type Category struct {}
func (q *Queries) Ex1() (map[int]*Category, error) {
    return nil, nil
}
func (q *Queries) Ex2() (map[int]Category, error) {
    return nil, nil
}
func (q *Queries) Ex3() ([]*Category, error) {
    return nil, nil
}
func (q *Queries) Ex4() ([]Category, error) {
    return nil, nil
}
func (q *Queries) Ex5() (*Category, error) {
    return nil, nil
}

Without the PR it generates this (and some newlines I removed), which fails to compile in Go:

// Code generated by ifacemaker; DO NOT EDIT.
package app
import (
    _ "embed"
    _ "github.com/mattn/go-sqlite3"
)
// DataStoreQueries ...
type DataStoreQueries interface {
    Ex1() (map[int]*Category, error)
    Ex2() (map[int]Category, error)
    Ex3() ([]*Category, error)
    Ex4() ([]Category, error)
    Ex5() (*Category, error)
}

With the PR, it generates this, also minus newlines, which does compile in Go:

// Code generated by ifacemaker; DO NOT EDIT.
package app
import (
    _ "embed"
    _ "github.com/mattn/go-sqlite3"
    "github.com/mikeschinkel/gerardus/persister"
)
// DataStoreQueries ...
type DataStoreQueries interface {
    Ex1() (map[int]*persister.Category, error)
    Ex2() (map[int]persister.Category, error)
    Ex3() ([]*persister.Category, error)
    Ex4() ([]persister.Category, error)
    Ex5() (*persister.Category, error)
}