Open nati opened 7 years ago
I think it's a bit difficult to implement the same syntax as Java :), but we can use the golang style to nest the interface.
In your example, you can use existed interface defined in other packages
(The following code will work on genny df3d48a)
package genny
import (
"bytes"
"fmt"
)
type Something interface {
fmt.Stringer
}
type SomethingQueue struct {
items []Something
}
...
func (q *SomethingQueue) String() string {
var buf bytes.Buffer
for _, item := range q.items {
buf.WriteString(item.String())
}
return buf.String()
}
It's also easy to extend other interfaces, like json.Marshaler
. We can also nest with generic.Type
to indicate the interface is generic
type Something interface {
generic.Type
json.Marshaler
fmt.Stringer
}
We can do this because genney
will replace the string identifier of the file, regardless of its actual type. In this way, we can extend more complex types while being type-safe.
I have summarized several common usages used in my toy project.
Type Object
extends to struct (has fields)
type Object struct {
generic.Type
name string
age int
}
// Now you can use it in function without compile errors func doSomeThing(o *Object) (string, int) { ... return o.name, o.age }
2. Type `Object` extends to interface (has method)
```go
type Object interface {
generic.Type // can be removed
run() Object
stop() Object
}
// use it as normal interface
func operate(o Object) {
o.run().stop()
}
// file1: demo.go
type mockPackage struct {
// package function
Log(...interface{}) error
// package exported variable
ColorRed int
ColorGreen int
// Not find a way to mock exported Type
}
var PackageName mockPackage
// file2: demo_template.go // Use in another file func normalFunc() { PackageName.Log("Hello", "Good") color := PackageName.ColorGreen ... }
// use command // geeny -in=demo_template.go -out=gen_demo.go gen "PackageName=mypkg"
Hope to be helpful :)
Hi. Thank you for your providing a neat package!
I would like to extend generic type like this Java example so that I can call method in the type.
and I find I could do similar stuff with Genny. but it is a bit hacky way.
How about support this use case officially, and add "generic.ignore" for ignoring line?