cheekybits / genny

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

Lowercase replace in strings? #57

Open nsemikov opened 5 years ago

nsemikov commented 5 years ago

template

package xmpl

import (
    "github.com/jmoiron/sqlx"
)

type Types []Type

func GetTypes(db *sqlx.DB) (Types, error) {
    result := Types{}
    err := db.Select(&result, "SELECT * FROM table;")
    return result, err
}

generated with

//go:generate genny -in=$GOFILE -out=gen-$GOFILE gen "Type=Operation table=operation"

will replace table by Operation, but not operation:

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

package operation

import "github.com/jmoiron/sqlx"

type Operations []Operation

func GetOperations(db *sqlx.DB) (Operations, error) {
    result := Operations{}
    err := db.Select(&result, "SELECT * FROM Operation;")
    return result, err
}
zhl11b commented 4 years ago

this is a bug,I hava same situation.

gitmonster commented 4 years ago

same here. has anyone a solution?

zhiqiangxu commented 4 years ago

@stdatiks @zhl11b @gitmonster https://github.com/zhiqiangxu/gg can do such jobs with more accurate control:

cat source.go:

package xmpl

import (
    "github.com/jmoiron/sqlx"
)

type Type interface{}

type Types []Type

const Table = "table"

func GetTypes(db *sqlx.DB) (Types, error) {
    result := Types{}
    err := db.Select(&result, "SELECT * FROM " + Table + ";")
    return result, err
}

Then gg -i source.go -t Type=Operation -c Table='"operation"' -d GetTypes=GetOperations will output:

package xmpl

import (
    "github.com/jmoiron/sqlx"
)

type Types []Operation

const Table = "operation"

func GetOperations(db *sqlx.DB) (Types, error) {
    result := Types{}
    err := db.Select(&result, "SELECT * FROM "+Table+";")
    return result, err
}
nsemikov commented 4 years ago

@zhiqiangxu no, thanks

rmccrystal commented 3 years ago

Running into the same issue. I tried something like this:

//go:generate genny -in=$GOFILE -out=gen-$GOFILE -tag=generate gen "Model=Student,Event,Location model=student,event,location"

but it made functions for every combination.

rmccrystal commented 3 years ago

One workaround I can think of is having a generate tag for each type, for instance:

//go:generate genny -in=$GOFILE -out=student-gen.go -tag=generate gen "Model=Student model=student"
//go:generate genny -in=$GOFILE -out=event-gen.go -tag=generate gen "Model=Event model=event"
//go:generate genny -in=$GOFILE -out=location-gen.go -tag=generate gen "Model=Location model=location"

but something cleaner might be nice. Considering this project hasn't been updated in a while and go2 is coming soon, this is probably never going to be fixed :/