cheekybits / genny

Elegant generics for Go
MIT License
1.71k stars 167 forks source link

genny can not support slice type #33

Closed fastfading closed 5 years ago

fastfading commented 7 years ago

//go:generate genny -pkg chr -in=../gen/grow.go -out=chargrow.go gen "SliceType=float32,float64,[]int"

package gen

import ( "github.com/cheekybits/genny/generic" )

func GrowSliceType(sli []SliceType, n int) { dif := n - cap(sli) if dif > 0 { sli = append((sli)[:cap(sli)], make([]SliceType, dif)...) } else { sli = (*sli)[:n] } }

error Failed to goimports the generated code: ../gen/grow.go:36:10: expected '(', found '[' char.go:1: running "genny": exit status 4

matryer commented 7 years ago

I think the generated code is invalid - does the code compile before genny is run?

pdrum commented 5 years ago

The problem seems to be with the compile error. Therefore closing the issue.

libmonsoon-dev commented 5 years ago

I have a similar problem.

from.go:


import "github.com/cheekybits/genny/generic"

type T generic.Type

func sliceFromChanT(ch <-chan T) []T {
    items := make([]T, len(ch))

    for item := range ch {
        items = append(items, item)
    }

    return items
}

cat from.go | genny gen "T=[]error": Failed to goimports the generated code: stdout:11:18: expected '(', found '[' (and 1 more errors)

go env

GOARCH="amd64"
GOBIN=""
GOCACHE="/home/user/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/user/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/lib/go-1.12"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go-1.12/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build417713142=/tmp/go-build -gno-record-gcc-switches"
libmonsoon-dev commented 5 years ago

It looks like a problem in the function name generation. If you remove the generic from the function name (T), everything works as expected.

cat from.go | genny gen "T=[]error"

// This file was automatically generated by genny.
// Any changes will be lost if this file is regenerated.
// see https://github.com/cheekybits/genny

package slice

func sliceFromChan(ch <-chan []error) [][]error {
        items := make([][]error, len(ch))

        for item := range ch {
                items = append(items, item)
        }

        return items
}